Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
AuthSystem.cpp
Go to the documentation of this file.
1
9#include <fstream>
10
11namespace rtp::server {
12
14 // Public API
16
18 : _network(network), _registry(registry) {};
19
20 void AuthSystem::update(float dt)
21 {
22 (void)dt;
23 };
24
25 std::tuple<bool, std::string, uint8_t> AuthSystem::handleLoginRequest(uint32_t sessionId, const net::Packet& packet)
26 {
27 std::ifstream inFile("logins.txt");
28
29 std::string username;
30 std::string password;
31
32 net::LoginPayload payload;
33 net::Packet tempPacket = packet;
34 tempPacket >> payload;
35 username = std::string(payload.username);
36 password = std::string(payload.password);
37 uint8_t weaponKind = payload.weaponKind;
38
39 log::info("Login attempt: session={} username='{}' password='{}'",
40 sessionId, username, password);
41
42 if (!inFile) {
43 log::error("Failed to open logins.txt for reading");
44 sendLoginResponse(sessionId, false, username);
45 return {false, username, weaponKind};
46 }
47
48 std::string line;
49 std::string record = username + ":" + password;
50
51 while (std::getline(inFile, line)) {
52 if (line == record) {
53 log::info("Login successful for username '{}'", username);
54 sendLoginResponse(sessionId, true, username);
55 return {true, username, weaponKind};
56 }
57 }
58 log::warning("Login failed for username '{}'", username);
59 sendLoginResponse(sessionId, false, username);
60 return {false, username, weaponKind};
61 }
62
63 std::pair<bool, std::string> AuthSystem::handleRegisterRequest(uint32_t sessionId, const net::Packet& packet)
64 {
65 std::ofstream outFile("logins.txt", std::ios::app);
66
67 std::string username;
68 std::string password;
69
71 net::Packet tempPacket = packet;
72 tempPacket >> payload;
73 username = std::string(payload.username);
74 password = std::string(payload.password);
75
76 log::info("Registration attempt: username='{}' password='{}'",
77 username, password);
78
79 if (!outFile) {
80 log::error("Failed to open logins.txt for writing");
81 sendRegisterResponse(sessionId, false, username);
82 return {false, username};
83 }
84
85 if (username.find(':') != std::string::npos || password.find(':') != std::string::npos) {
86 log::warning("Registration failed: username or password contains invalid character ':'");
87 sendRegisterResponse(sessionId, false, username);
88 return {false, username};
89 }
90
91 std::ifstream inFile("logins.txt");
92 std::string line;
93 while (std::getline(inFile, line)) {
94 size_t delimPos = line.find(':');
95 if (delimPos != std::string::npos) {
96 std::string existingUsername = line.substr(0, delimPos);
97 if (existingUsername == username) {
98 log::warning("Registration failed: username '{}' already exists", username);
99 sendRegisterResponse(sessionId, false, username);
100 return {false, username};
101 }
102 }
103 }
104
105 outFile << username << ":" << password << "\n";
106 log::info("Registration successful for username '{}'", username);
107 sendRegisterResponse(sessionId, true, username);
108 return {true, username};
109 }
110
111 void AuthSystem::sendLoginResponse(uint32_t sessionId, bool success, const std::string& username)
112 {
115 payload.success = success;
116 std::strncpy(payload.username, username.c_str(), sizeof(payload.username) - 1);
117 payload.username[sizeof(payload.username) - 1] = '\0';
118 responsePacket << payload;
119 _network.sendPacket(sessionId, responsePacket, net::NetworkMode::TCP);
120 }
121
122 void AuthSystem::sendRegisterResponse(uint32_t sessionId, bool success, const std::string& username)
123 {
126 payload.success = success;
127 std::strncpy(payload.username, username.c_str(), sizeof(payload.username) - 1);
128 payload.username[sizeof(payload.username) - 1] = '\0';
129 responsePacket << payload;
130 _network.sendPacket(sessionId, responsePacket, net::NetworkMode::TCP);
131 }
132
133} // namespace rtp::server
Network packet with header and serializable body.
Definition Packet.hpp:471
ServerNetwork & _network
Reference to the server network manager.
void sendRegisterResponse(uint32_t sessionId, bool success, const std::string &username)
Send a registration response to a client.
void sendLoginResponse(uint32_t sessionId, bool success, const std::string &username)
Send a login response to a client.
std::pair< bool, std::string > handleRegisterRequest(uint32_t sessionId, const net::Packet &packet)
Handle a registration request from a client.
void update(float dt) override
Update system logic for one frame.
std::tuple< bool, std::string, uint8_t > handleLoginRequest(uint32_t sessionId, const net::Packet &packet)
Handle a login request from a client.
AuthSystem(ServerNetwork &network, ecs::Registry &registry)
Constructor for AuthSystem.
Implementation ASIO du serveur réseau (TCP + UDP)
void sendPacket(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
@ RegisterResponse
Incorrect password notification.
@ LoginResponse
Successful connection notification.
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
char password[32]
Player password.
Definition Packet.hpp:195
uint8_t weaponKind
Selected weapon kind.
Definition Packet.hpp:196
char username[32]
Player username.
Definition Packet.hpp:194
Login response data sent by server for LoginResponse Server OpCode.
Definition Packet.hpp:216
uint8_t success
Login success flag.
Definition Packet.hpp:217
Player registration data sended by client Client OpCode.
Definition Packet.hpp:205
char password[32]
Player password.
Definition Packet.hpp:207
char username[32]
Player username.
Definition Packet.hpp:206
Registration response data sent by server for RegisterResponse Server OpCode.
Definition Packet.hpp:227
uint8_t success
Registration success flag.
Definition Packet.hpp:228