Logging Saved

This commit is contained in:
EntireTwix 2021-05-19 13:06:51 -07:00
parent 0a919a07c8
commit eb9b724be5
4 changed files with 36 additions and 20 deletions

View file

@ -4,13 +4,6 @@
"address": "0.0.0.0", "address": "0.0.0.0",
"port": 80, "port": 80,
"https": false "https": false
},
{
"address": "0.0.0.0",
"port": 443,
"https": true,
"cert": "",
"key": ""
} }
] ]
} }

View file

@ -42,7 +42,7 @@ public:
return users.try_emplace_l( return users.try_emplace_l(
name, [](User &) {}, std::move(init_pass)); name, [](User &) {}, std::move(init_pass));
} }
bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast32_t init_bal, std::string &&init_pass) bool AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass)
{ {
if (name.size() > 50) if (name.size() > 50)
{ {
@ -69,7 +69,7 @@ public:
return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); });
} }
bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast32_t amount, const std::string &attempt) bool SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt)
{ {
//cant send money to self, from self or amount is 0 //cant send money to self, from self or amount is 0
if (a_name == b_name || !amount) if (a_name == b_name || !amount)
@ -173,15 +173,10 @@ public:
if (u.password != XXH3_64bits(attempt.data(), attempt.size())) if (u.password != XXH3_64bits(attempt.data(), attempt.size()))
{ {
res = 0; res = 0;
return;
} }
else
for (uint32_t i = 0; i < u.log.data.size() && u.log.data[i].amount; ++i)
{ {
res[i]["to"] = u.log.data[i].to; res = u.log.Serialize();
res[i]["from"] = u.log.data[i].from;
res[i]["amount"] = u.log.data[i].amount;
res[i]["time"] = (Json::UInt64)u.log.data[i].time;
} }
})) }))
{ {
@ -232,7 +227,7 @@ public:
user_save.close(); user_save.close();
for (const auto &u : temp.getMemberNames()) for (const auto &u : temp.getMemberNames())
{ {
users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64())); users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64()), std::move(temp[u]["log"]));
} }
} }
} }

View file

@ -26,6 +26,18 @@ struct Log
++end; ++end;
} }
} }
Json::Value Serialize() const
{
Json::Value res;
for (uint32_t i = 0; i < data.size() && data[i].amount; ++i)
{
res[i]["to"] = data[i].to;
res[i]["from"] = data[i].from;
res[i]["amount"] = data[i].amount;
res[i]["time"] = (Json::UInt64)data[i].time;
}
return res;
}
}; };
//[*][*][] //[*][*][]

View file

@ -5,7 +5,7 @@
struct User struct User
{ {
uint_fast32_t balance = 0; uint32_t balance = 0;
uint64_t password; uint64_t password;
Log log; Log log;
@ -22,14 +22,30 @@ struct User
* @param init_bal initial balance * @param init_bal initial balance
* @param init_pass initial password * @param init_pass initial password
*/ */
User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {} User(uint32_t init_bal, std::string &&init_pass) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
User(uint_fast32_t init_bal, uint64_t init_pass) : balance(init_bal), password(init_pass) {}
/**
* @brief User Constructor for loading
*
* @param init_bal
* @param init_pass
*/
User(uint32_t init_bal, uint64_t init_pass, Json::Value&& log_j) : balance(init_bal), password(init_pass)
{
log.data.resize(log_j.size());
log.end = log_j.size();
for(uint32_t i = 0; i < log_j.size(); ++i)
{
log.data[i] = std::move(Transaction(log_j[i]["from"].asCString(), log_j[i]["to"].asCString(), log_j[i]["balance"].asUInt()));
}
}
Json::Value Serialize() const Json::Value Serialize() const
{ {
Json::Value res; Json::Value res;
res["balance"] = (Json::UInt)balance; res["balance"] = (Json::UInt)balance;
res["password"] = (Json::UInt64)password; res["password"] = (Json::UInt64)password;
res["log"] = log.Serialize();
return res; return res;
} }
}; };