From eb9b724be528235fa113e8630c9ea7fb69185e2d Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 19 May 2021 13:06:51 -0700 Subject: [PATCH] :sparkles: Logging Saved --- config.json | 7 ------- include/bank.hpp | 15 +++++---------- include/log.hpp | 12 ++++++++++++ include/user.hpp | 22 +++++++++++++++++++--- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/config.json b/config.json index fa95be9..331a931 100644 --- a/config.json +++ b/config.json @@ -4,13 +4,6 @@ "address": "0.0.0.0", "port": 80, "https": false - }, - { - "address": "0.0.0.0", - "port": 443, - "https": true, - "cert": "", - "key": "" } ] } diff --git a/include/bank.hpp b/include/bank.hpp index a386414..3a05352 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -42,7 +42,7 @@ public: return users.try_emplace_l( 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) { @@ -69,7 +69,7 @@ public: 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 if (a_name == b_name || !amount) @@ -173,15 +173,10 @@ public: if (u.password != XXH3_64bits(attempt.data(), attempt.size())) { res = 0; - return; } - - for (uint32_t i = 0; i < u.log.data.size() && u.log.data[i].amount; ++i) + else { - res[i]["to"] = u.log.data[i].to; - 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; + res = u.log.Serialize(); } })) { @@ -232,7 +227,7 @@ public: user_save.close(); 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"])); } } } diff --git a/include/log.hpp b/include/log.hpp index 86abd9a..5b30401 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -26,6 +26,18 @@ struct Log ++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; + } }; //[*][*][] diff --git a/include/user.hpp b/include/user.hpp index 97c6f22..8761f42 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -5,7 +5,7 @@ struct User { - uint_fast32_t balance = 0; + uint32_t balance = 0; uint64_t password; Log log; @@ -22,14 +22,30 @@ struct User * @param init_bal initial balance * @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(uint_fast32_t init_bal, uint64_t init_pass) : balance(init_bal), password(init_pass) {} + User(uint32_t init_bal, std::string &&init_pass) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {} + + /** + * @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 res; res["balance"] = (Json::UInt)balance; res["password"] = (Json::UInt64)password; + res["log"] = log.Serialize(); return res; } }; \ No newline at end of file