mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
✨ Saving/Loading
This commit is contained in:
parent
92a48c5f0c
commit
8ebe23fa15
4 changed files with 60 additions and 11 deletions
|
|
@ -9,10 +9,13 @@ endif()
|
||||||
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
|
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "-g")
|
set(CMAKE_CXX_FLAGS_DEBUG "-g")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||||
|
|
||||||
|
find_package( Threads )
|
||||||
add_executable(${PROJECT_NAME} main.cpp)
|
add_executable(${PROJECT_NAME} main.cpp)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC third_party)
|
target_include_directories(${PROJECT_NAME} PUBLIC third_party)
|
||||||
|
|
||||||
# add_subdirectory(third_party/drogon)
|
add_subdirectory(third_party/drogon)
|
||||||
# target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} )
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <fstream>
|
||||||
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
||||||
#include "user.hpp"
|
#include "user.hpp"
|
||||||
|
|
||||||
|
|
@ -80,4 +81,43 @@ public:
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||||
|
void Save() const
|
||||||
|
{
|
||||||
|
Json::StreamWriterBuilder builder;
|
||||||
|
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||||
|
|
||||||
|
std::ofstream user_save("users.json");
|
||||||
|
Json::Value temp;
|
||||||
|
for (const auto &u : users)
|
||||||
|
{
|
||||||
|
std::cout << u.first << '\n';
|
||||||
|
temp[u.first] = u.second.Serialize();
|
||||||
|
}
|
||||||
|
writer->write(temp, &user_save);
|
||||||
|
user_save.close();
|
||||||
|
}
|
||||||
|
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||||
|
void Load()
|
||||||
|
{
|
||||||
|
Json::CharReaderBuilder builder;
|
||||||
|
|
||||||
|
Json::Value temp;
|
||||||
|
std::ifstream user_save("users.json");
|
||||||
|
builder["collectComments"] = true;
|
||||||
|
JSONCPP_STRING errs;
|
||||||
|
if (!parseFromStream(builder, user_save, &temp, &errs))
|
||||||
|
{
|
||||||
|
user_save.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
user_save.close();
|
||||||
|
for (const auto &u : temp.getMemberNames())
|
||||||
|
{
|
||||||
|
users.try_emplace(u, temp[u]["balance"].asUInt(), std::forward<std::string &&>(temp[u]["password"].asString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} Bank;
|
} Bank;
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <json/json.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct User
|
struct User
|
||||||
|
|
@ -20,4 +21,12 @@ struct User
|
||||||
* @param init_pass initial password
|
* @param init_pass initial password
|
||||||
*/
|
*/
|
||||||
User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {}
|
User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {}
|
||||||
|
|
||||||
|
Json::Value Serialize() const
|
||||||
|
{
|
||||||
|
Json::Value res;
|
||||||
|
res["balance"] = balance;
|
||||||
|
res["password"] = password;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
13
main.cpp
13
main.cpp
|
|
@ -1,15 +1,12 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
#include "bank.hpp"
|
#include "bank.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Bank.admin_pass = "root";
|
Bank.Load();
|
||||||
std::cout << Bank.AddUser("Twix", "pass123") << '\n';
|
Bank.SendFunds("0", "1", 50, "root");
|
||||||
std::cout << Bank.GetBal("Twixy") << '\n';
|
Bank.Save();
|
||||||
std::cout << Bank.AdminAddUser("root", "Jollymonsam", 2500, "pass123") << '\n';
|
|
||||||
std::cout << Bank.GetBal("Jollymonsam") << '\n';
|
|
||||||
std::cout << Bank.SendFunds("Jollymonsam", "Twix", 333, "pass123") << '\n';
|
|
||||||
std::cout << Bank.GetBal("Twix") << " : " << Bank.GetBal("Jollymonsam") << '\n';
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue