Skip to content

Commit

Permalink
feat: support LeviLamina 0.5.1
Browse files Browse the repository at this point in the history
refactor: rename export function
  • Loading branch information
ShrBox committed Jan 20, 2024
1 parent 8073e67 commit a9488c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
30 changes: 15 additions & 15 deletions src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool initDB() {
return true;
}

long long LLMoneyGet(std::string xuid) {
long long LLMoney_Get(std::string xuid) {
try {
SQLite::Statement get{*db, "select Money from money where XUID=?"};
get.bindNoCopy(1, xuid);
Expand Down Expand Up @@ -88,7 +88,7 @@ long long LLMoneyGet(std::string xuid) {

bool isRealTrans = true;

bool LLMoneyTrans(std::string from, std::string to, long long val, std::string const& note) {
bool LLMoney_Trans(std::string from, std::string to, long long val, std::string const& note) {
bool isRealTrans = ::isRealTrans;
::isRealTrans = true;
if (isRealTrans)
Expand All @@ -99,7 +99,7 @@ bool LLMoneyTrans(std::string from, std::string to, long long val, std::string c
db->exec("begin");
SQLite::Statement set{*db, "update money set Money=? where XUID=?"};
if (from != "") {
auto fmoney = LLMoneyGet(from);
auto fmoney = LLMoney_Get(from);
if (fmoney < val) {
db->exec("rollback");
return false;
Expand All @@ -115,7 +115,7 @@ bool LLMoneyTrans(std::string from, std::string to, long long val, std::string c
}
}
if (to != "") {
auto tmoney = LLMoneyGet(to);
auto tmoney = LLMoney_Get(to);
tmoney += val;
if (tmoney < 0) {
db->exec("rollback");
Expand Down Expand Up @@ -150,27 +150,27 @@ bool LLMoneyTrans(std::string from, std::string to, long long val, std::string c
}
}

bool LLMoneyAdd(std::string xuid, long long money) {
bool LLMoney_Add(std::string xuid, long long money) {
if (!CallBeforeEvent(LLMoneyEvent::Add, "", xuid, money)) return false;

isRealTrans = false;
bool res = LLMoneyTrans("", xuid, money, "add " + std::to_string(money));
bool res = LLMoney_Trans("", xuid, money, "add " + std::to_string(money));
if (res) CallAfterEvent(LLMoneyEvent::Add, "", xuid, money);
return res;
}

bool LLMoneyReduce(std::string xuid, long long money) {
bool LLMoney_Reduce(std::string xuid, long long money) {
if (!CallBeforeEvent(LLMoneyEvent::Reduce, "", xuid, money)) return false;

isRealTrans = false;
bool res = LLMoneyTrans(xuid, "", money, "reduce " + std::to_string(money));
bool res = LLMoney_Trans(xuid, "", money, "reduce " + std::to_string(money));
if (res) CallAfterEvent(LLMoneyEvent::Reduce, "", xuid, money);
return res;
}

bool LLMoneySet(std::string xuid, long long money) {
bool LLMoney_Set(std::string xuid, long long money) {
if (!CallBeforeEvent(LLMoneyEvent::Set, "", xuid, money)) return false;
long long now = LLMoneyGet(xuid), diff;
long long now = LLMoney_Get(xuid), diff;
std::string from, to;
if (money >= now) {
from = "";
Expand All @@ -183,12 +183,12 @@ bool LLMoneySet(std::string xuid, long long money) {
}

isRealTrans = false;
bool res = LLMoneyTrans(from, to, diff, "set to " + std::to_string(money));
bool res = LLMoney_Trans(from, to, diff, "set to " + std::to_string(money));
if (res) CallAfterEvent(LLMoneyEvent::Reduce, "", xuid, money);
return res;
}

std::vector<std::pair<std::string, long long>> LLMoneyRanking(unsigned short num) {
std::vector<std::pair<std::string, long long>> LLMoney_Ranking(unsigned short num) {
try {
SQLite::Statement get{*db, "select * from money ORDER BY money DESC LIMIT ?"};
std::vector<std::pair<std::string, long long>> mapTemp;
Expand All @@ -208,7 +208,7 @@ std::vector<std::pair<std::string, long long>> LLMoneyRanking(unsigned short num
}
}

std::string LLMoneyGetHist(std::string xuid, int timediff) {
std::string LLMoney_GetHist(std::string xuid, int timediff) {
ll::service::PlayerInfo& info = ll::service::PlayerInfo::getInstance();
try {
SQLite::Statement get{
Expand Down Expand Up @@ -243,7 +243,7 @@ std::string LLMoneyGetHist(std::string xuid, int timediff) {
}
}

void LLMoneyClearHist(int difftime) {
void LLMoney_ClearHist(int difftime) {
try {
db->exec("DELETE FROM mtrans WHERE strftime('%s','now')-time>" + std::to_string(difftime));
} catch (std::exception&) {}
Expand Down Expand Up @@ -291,4 +291,4 @@ void ConvertData() {
// EXPORTAPI(LLMoneySet);
// EXPORTAPI(LLMoneyGetHist);
// EXPORTAPI(LLMoneyClearHist);
// }
// }
31 changes: 10 additions & 21 deletions src/LLMoney.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,18 @@ typedef bool (*LLMoneyCallback)(LLMoneyEvent type, std::string from, std::string
#ifdef __cplusplus
extern "C" {
#endif
LLMONEY_API long long LLMoneyGet(std::string xuid);
LLMONEY_API bool LLMoneySet(std::string xuid, long long money);
LLMONEY_API bool LLMoneyTrans(std::string from, std::string to, long long val, std::string const& note = "");
LLMONEY_API bool LLMoneyAdd(std::string xuid, long long money);
LLMONEY_API bool LLMoneyReduce(std::string xuid, long long money);
LLMONEY_API long long LLMoney_Get(std::string xuid);
LLMONEY_API bool LLMoney_Set(std::string xuid, long long money);
LLMONEY_API bool LLMoney_Trans(std::string from, std::string to, long long val, std::string const& note = "");
LLMONEY_API bool LLMoney_Add(std::string xuid, long long money);
LLMONEY_API bool LLMoney_Reduce(std::string xuid, long long money);

LLMONEY_API std::string LLMoneyGetHist(std::string xuid, int timediff = 24 * 60 * 60);
LLMONEY_API void LLMoneyClearHist(int difftime = 0);
LLMONEY_API std::string LLMoney_GetHist(std::string xuid, int timediff = 24 * 60 * 60);
LLMONEY_API void LLMoney_ClearHist(int difftime = 0);

LLMONEY_API void LLMoneyListenBeforeEvent(LLMoneyCallback callback);
LLMONEY_API void LLMoneyListenAfterEvent(LLMoneyCallback callback);
LLMONEY_API void LLMoney_ListenBeforeEvent(LLMoneyCallback callback);
LLMONEY_API void LLMoney_ListenAfterEvent(LLMoneyCallback callback);
#ifdef __cplusplus
}
#endif
LLMONEY_API std::vector<std::pair<std::string, long long>> LLMoneyRanking(unsigned short num = 5);
// Old interface
// Just for compatibility
// Do not use
namespace Money {
LLMONEY_API long long getMoney(std::string xuid);
LLMONEY_API std::string getTransHist(std::string xuid, int timediff = 24 * 60 * 60);
LLMONEY_API bool createTrans(std::string from, std::string to, long long val, std::string const& note = "");
LLMONEY_API bool setMoney(std::string xuid, long long money);
LLMONEY_API bool reduceMoney(std::string xuid, long long money);
LLMONEY_API void purgeHist(int difftime = 0);
} // namespace Money
LLMONEY_API std::vector<std::pair<std::string, long long>> LLMoney_Ranking(unsigned short num = 5);
38 changes: 19 additions & 19 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ class MoneyCommand : public Command {
}
switch (op) {
case query:
outp.addMessage("Balance: " + std::to_string(LLMoneyGet(dstxuid)), {}, CommandOutputMessageType::Success);
outp.addMessage("Balance: " + std::to_string(LLMoney_Get(dstxuid)), {}, CommandOutputMessageType::Success);
break;
case hist:
outp.addMessage(LLMoneyGetHist(dstxuid), {}, CommandOutputMessageType::Success);
outp.addMessage(LLMoney_GetHist(dstxuid), {}, CommandOutputMessageType::Success);
break;
case pay: {
if (moneynum <= 0) {
Expand All @@ -179,9 +179,9 @@ class MoneyCommand : public Command {
outp.error(tr("money.payyourself"));
return;
}
if (LLMoneyTrans(myuid, dstxuid, moneynum, "money pay")) {
if (LLMoney_Trans(myuid, dstxuid, moneynum, "money pay")) {
long long fee = (long long)(moneynum * Settings::pay_tax);
if (fee) LLMoneyTrans(dstxuid, "", fee, "money pay fee");
if (fee) LLMoney_Trans(dstxuid, "", fee, "money pay fee");
outp.success(tr("money.pay.succ"));
} else {
outp.error(tr("money.not.enough"));
Expand All @@ -193,7 +193,7 @@ class MoneyCommand : public Command {
outp.error(tr("money.no.perm"));
return;
}
if (LLMoneySet(dstxuid, moneynum)) {
if (LLMoney_Set(dstxuid, moneynum)) {
outp.success(tr("money.set.succ"));
} else {
outp.error(tr("money.invalid.arg"));
Expand All @@ -204,7 +204,7 @@ class MoneyCommand : public Command {
outp.error(tr("money.no.perm"));
return;
}
if (LLMoneyAdd(dstxuid, moneynum)) {
if (LLMoney_Add(dstxuid, moneynum)) {
outp.success(tr("money.add.succ"));
} else {
outp.error(tr("money.invalid.arg"));
Expand All @@ -215,18 +215,18 @@ class MoneyCommand : public Command {
outp.error(tr("money.no.perm"));
return;
}
if (LLMoneyReduce(dstxuid, moneynum)) {
if (LLMoney_Reduce(dstxuid, moneynum)) {
outp.success(tr("money.reduce.succ"));
} else {
outp.error(tr("money.invalid.arg"));
}
break;
case purge:
if (difftime_isSet) LLMoneyClearHist(difftime);
else LLMoneyClearHist(0);
if (difftime_isSet) LLMoney_ClearHist(difftime);
else LLMoney_ClearHist(0);
break;
case top:
std::vector<std::pair<std::string, long long>> mapTemp = LLMoneyRanking();
std::vector<std::pair<std::string, long long>> mapTemp = LLMoney_Ranking();
sort(mapTemp.begin(), mapTemp.end(), cmp);
outp.success("===== Ranking =====");
for (auto it = mapTemp.begin(); it != mapTemp.end(); it++) {
Expand Down Expand Up @@ -389,13 +389,13 @@ class MoneySCommand : public Command {
switch (op) {
case query:
outp.addMessage(
"Balance: " + std::to_string(LLMoneyGet(dstxuid.value())),
"Balance: " + std::to_string(LLMoney_Get(dstxuid.value())),
{},
CommandOutputMessageType::Success
);
break;
case hist:
outp.addMessage(LLMoneyGetHist(dstxuid.value()), {}, CommandOutputMessageType::Success);
outp.addMessage(LLMoney_GetHist(dstxuid.value()), {}, CommandOutputMessageType::Success);
break;
case pay:
if (moneynum <= 0) {
Expand All @@ -411,9 +411,9 @@ class MoneySCommand : public Command {
outp.error(tr("money.payyourself"));
return;
}
if (LLMoneyTrans(myuid, dstxuid.value(), moneynum, "money pay")) {
if (LLMoney_Trans(myuid, dstxuid.value(), moneynum, "money pay")) {
long long fee = (long long)(moneynum * Settings::pay_tax);
if (fee) LLMoneyTrans(dstxuid.value(), "", fee, "money pay fee");
if (fee) LLMoney_Trans(dstxuid.value(), "", fee, "money pay fee");
outp.success(tr("money.pay.succ"));
} else {
outp.error(tr("money.not.enough"));
Expand All @@ -426,7 +426,7 @@ class MoneySCommand : public Command {
}
bool su = 0;
for (auto i : dstxuidlist) {
if (LLMoneySet(i, moneynum)) {
if (LLMoney_Set(i, moneynum)) {
su = 1;
}
}
Expand All @@ -444,7 +444,7 @@ class MoneySCommand : public Command {
}
bool su = 0;
for (auto i : dstxuidlist) {
if (LLMoneyAdd(i, moneynum)) {
if (LLMoney_Add(i, moneynum)) {
su = 1;
}
}
Expand All @@ -462,7 +462,7 @@ class MoneySCommand : public Command {
}
bool su = 0;
for (auto i : dstxuidlist) {
if (LLMoneyReduce(i, moneynum)) {
if (LLMoney_Reduce(i, moneynum)) {
su = 1;
}
}
Expand Down Expand Up @@ -548,7 +548,7 @@ namespace legacymoney {

Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) {
logger = &mSelf.getLogger();
mSelf.getLogger().info("Loaded! Version: 0.1.3");
mSelf.getLogger().info("Loaded!");
loadCfg();
if (!initDB()) {
return;
Expand All @@ -569,4 +569,4 @@ bool Plugin::enable() {

bool Plugin::disable() { return true; }

} // namespace legacymoney
} // namespace legacymoney

0 comments on commit a9488c7

Please sign in to comment.