Project Alice
Loading...
Searching...
No Matches
persistent_server_extensions.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <string>
5#include "container_types.hpp"
6#include "commands.hpp"
7#include <iostream>
8#include <chrono>
9#include <ctime>
10#include "system_state.hpp"
11#include "network.hpp"
12#include "parsers.hpp"
13#include "simple_fs.hpp"
14#include "network.hpp"
15#include "parsers.hpp"
16#include "SHA512.hpp"
17
18namespace network {
19
20static int lastchange = 0;
21
22inline void read_player_nations(sys::state& state, char const* start, char const* end) noexcept {
23 char const* cpos = start;
25
26 while(cpos < end) {
27 cpos = parsers::parse_fixed_amount_csv_values<4>(cpos, end, ';', [&](std::string_view const* values) {
28 auto first_text = parsers::remove_surrounding_whitespace(values[0]);
29 auto second_text = parsers::remove_surrounding_whitespace(values[1]);
30 auto third_text = parsers::remove_surrounding_whitespace(values[2]);
31 auto fourth_text = parsers::remove_surrounding_whitespace(values[3]);
32
33
34 if(first_text.length() > 0 && second_text.length() > 0) {
35 auto second_value = parsers::parse_int(second_text, 0, err);
36 if(second_value == 0) {
37 // dead line
38 } else {
39 auto name = sys::player_name{ }.from_string_view(first_text);
40 auto source = dcon::nation_id{ uint16_t ( second_value ) };
41 auto password_hash = sys::player_password_hash{ }.from_string_view(third_text);
42 auto password_salt = sys::player_password_salt{ }.from_string_view(fourth_text);
43
44 auto p = load_mp_player(state, name, password_hash, password_salt);
45
46 state.world.nation_set_is_player_controlled(source, true);
47 state.world.force_create_player_nation(source, p);
48
50 m.source = source;
53 auto msg = text::resolve_string_substitution(state, "chat_player_joins", sub);
54 command::chat_message(state, source, msg, dcon::nation_id{});
55
56#ifndef NDEBUG
58 state.console_log("persistent | type:read_player | nickname:" + name.to_string() + " | nation:" + std::to_string(source.index()) + "| password:" + password_hash.to_string() + " | salt: " + password_salt.to_string());
59#endif
60 }
61 }
62 });
63 }
64}
65
66inline void load_player_nations(sys::state& state) noexcept {
67 if(state.network_mode != sys::network_mode_type::host || state.host_settings.alice_persistent_server_mode != 1) {
68 return;
69 }
70
72
73 auto pl_csv_file = open_file(folder, NATIVE("players.csv"));
74 if(pl_csv_file) {
75 auto pl_content = view_contents(*pl_csv_file);
76 read_player_nations(state, pl_content.data, pl_content.data + pl_content.file_size);
77 }
78}
79
80inline void write_player_nations(sys::state& state) noexcept {
81 if(state.network_mode != sys::network_mode_type::host || state.host_settings.alice_persistent_server_mode != 1) {
82 return;
83 }
84
85 std::string res = "Nickname;NationID;PasswordHash;PasswordSalt\n";
86
87 for(auto pl : state.world.in_mp_player) {
88 auto nickname = sys::player_name{ pl.get_nickname() };
89 auto password_hash = sys::player_password_hash{ pl.get_password_hash() };
90 auto password_salt = sys::player_password_salt{ pl.get_password_salt() };
91 res += nickname.to_string() + ";" + std::to_string(pl.get_nation_from_player_nation().id.index()) + ";"
92 + password_hash.to_string() + ";" + password_salt.to_string() + "\n";
93 }
95
96 simple_fs::write_file(folder, NATIVE("players.csv"), res.data(), uint32_t(res.length()));
97}
98
99inline void every_tick_checks(sys::state& state) noexcept {
100 if(state.network_mode != sys::network_mode_type::host || state.host_settings.alice_persistent_server_mode != 1) {
101 return;
102 }
103
104 auto moment = std::chrono::system_clock::now();
105 std::time_t curtime = std::chrono::system_clock::to_time_t(moment);
106 int h = (curtime / 3600) % 24;
107 tm local_tm = *localtime(&curtime);
108
109 if(h >= state.host_settings.alice_persistent_server_pause && state.actual_game_speed != 0 && local_tm.tm_yday != lastchange) {
110 pause_game(state);
111
112 lastchange = local_tm.tm_yday;
113 }
114
115 if(h >= state.host_settings.alice_persistent_server_unpause && h < state.host_settings.alice_persistent_server_pause && state.actual_game_speed != state.ui_state.held_game_speed && local_tm.tm_yday != lastchange) {
116 unpause_game(state);
117
118 lastchange = local_tm.tm_yday;
119 }
120}
121}
122
Header file describing the function signatures and the constants of the SHA512 Algorithm.
bool pause_game(sys::state &state)
Definition: network.cpp:1246
void every_tick_checks(sys::state &state) noexcept
bool unpause_game(sys::state &state)
Definition: network.cpp:1261
dcon::mp_player_id load_mp_player(sys::state &state, sys::player_name &name, sys::player_password_hash &password_hash, sys::player_password_salt &password_salt)
Definition: network.cpp:674
void load_player_nations(sys::state &state) noexcept
void read_player_nations(sys::state &state, char const *start, char const *end) noexcept
void log_player_nations(sys::state &state)
Definition: network.cpp:527
void write_player_nations(sys::state &state) noexcept
int32_t parse_int(std::string_view content, int32_t line, error_handler &err)
Definition: parsers.cpp:226
std::string_view remove_surrounding_whitespace(std::string_view txt)
Definition: parsers.cpp:381
void write_file(directory const &dir, native_string_view file_name, char const *file_data, uint32_t file_size)
directory get_or_create_data_dumps_directory()
std::string resolve_string_substitution(sys::state &state, dcon::text_key source_text, substitution_map const &mp)
Definition: text.cpp:2137
void add_to_substitution_map(substitution_map &mp, variable_type key, substitution value)
Definition: text.cpp:1068
ankerl::unordered_dense::map< uint32_t, substitution > substitution_map
Definition: text.hpp:797
#define NATIVE(X)
uint uint32_t
player_value< _Size > from_string_view(std::string_view sv) noexcept
Holds important data about the game world, state, and other data regarding windowing,...