mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 16:12:14 +11:00
✨ Saving/Loading
This commit is contained in:
parent
92a48c5f0c
commit
8ebe23fa15
4 changed files with 60 additions and 11 deletions
|
|
@ -3,16 +3,19 @@ project(main)
|
|||
set (CMAKE_CXX_STANDARD 17)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-g")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||
|
||||
find_package( Threads )
|
||||
add_executable(${PROJECT_NAME} main.cpp)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC third_party)
|
||||
|
||||
# add_subdirectory(third_party/drogon)
|
||||
# target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
||||
add_subdirectory(third_party/drogon)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} )
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <fstream>
|
||||
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
||||
#include "user.hpp"
|
||||
|
||||
|
|
@ -80,4 +81,43 @@ public:
|
|||
|
||||
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;
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <json/json.h>
|
||||
#include <string>
|
||||
|
||||
struct User
|
||||
|
|
@ -20,4 +21,12 @@ struct User
|
|||
* @param init_pass initial password
|
||||
*/
|
||||
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 <chrono>
|
||||
#include <thread>
|
||||
#include "bank.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
Bank.admin_pass = "root";
|
||||
std::cout << Bank.AddUser("Twix", "pass123") << '\n';
|
||||
std::cout << Bank.GetBal("Twixy") << '\n';
|
||||
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';
|
||||
|
||||
Bank.Load();
|
||||
Bank.SendFunds("0", "1", 50, "root");
|
||||
Bank.Save();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue