My Project
player.hpp
Go to the documentation of this file.
1#ifndef _PLAYER_H
2#define _PLAYER_H
3#include "game.hpp"
4#include "wordle-de.pb.h"
5#include <botan/argon2.h>
6#include <botan/system_rng.h>
7
8class Player {
9 friend GameState;
10 std::string username;
11 std::string hashedPw;
12
14
15public:
16 Player() {}
17 Player(const std::string &name, const std::string &pw)
18 : state(), username(name) {
19 Botan::System_RNG rng;
20
21 hashedPw = Botan::argon2_generate_pwhash(pw.c_str(), sizeof(pw.c_str()),
22 rng, 1, 4000, 1);
23 }
24
25 bool authenticate(const std::string &pw) {
26 return Botan::argon2_check_pwhash(pw.c_str(), pw.size(), hashedPw);
27 }
28 std::array<GameState::matchType, WORD_LENGTH>
29 guess(const std::string &guess) {
30 return state.guess(guess);
31 }
32 std::string getUsername() { return username; }
34 void setWord(const std::string &w) { state.setWord(w); }
35
36 void fromData(const std::string &data) {
37 wordle_de::Player protoPlayer;
38 protoPlayer.ParseFromString(data);
39 username = protoPlayer.username();
40 hashedPw = protoPlayer.hashedpw();
41 state = protoPlayer.state();
42 }
43
44 std::string toData() {
45 wordle_de::Player protoPlayer;
46 protoPlayer.set_username(username);
47 protoPlayer.set_hashedpw(hashedPw);
48 auto localState = new wordle_de::GameState;
49 localState->set_word(state.getWord());
50 localState->set_tries(state.getTries());
51 for (auto const &g : state.getGuesses()) {
52 auto add = localState->add_guesses();
53 add->set_guess(g);
54 }
55 protoPlayer.set_allocated_state(localState);
56 return protoPlayer.SerializeAsString();
57 }
58};
59
60#endif
Definition: game.hpp:12
std::string getWord()
Definition: game.hpp:30
std::vector< std::string > getGuesses()
Definition: game.hpp:32
uint8_t getTries()
Definition: game.hpp:31
guessResultT guess(const std::string &)
Definition: game.cpp:19
void setWord(const std::string &)
Definition: game.cpp:16
Definition: player.hpp:8
std::string toData()
Definition: player.hpp:44
std::array< GameState::matchType, WORD_LENGTH > guess(const std::string &guess)
Definition: player.hpp:29
std::string getUsername()
Definition: player.hpp:32
Player(const std::string &name, const std::string &pw)
Definition: player.hpp:17
std::string hashedPw
Definition: player.hpp:11
GameState getGameState()
Definition: player.hpp:33
void fromData(const std::string &data)
Definition: player.hpp:36
friend GameState
Definition: player.hpp:9
bool authenticate(const std::string &pw)
Definition: player.hpp:25
void setWord(const std::string &w)
Definition: player.hpp:34
GameState state
Definition: player.hpp:13
std::string username
Definition: player.hpp:10
Player()
Definition: player.hpp:16