aboutsummaryrefslogtreecommitdiff
path: root/src/server/client.hh
blob: 5866035c1ae62a37c4d8153abda2cef393b15980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef SERVER_CLIENT_HH_
#define SERVER_CLIENT_HH_

#include <deque>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include "server/chunk_data.hh"
#include "server/world.hh"
#include "shared/net/connection.hh"
#include "shared/player.hh"
#include "shared/world.hh"

namespace server {

// Client represents the entire state of a player.
class client {
public:
    shared::net::connection connection;
    shared::player::index_t index;

    // If this is set a disconnect packet should be set
    std::optional<std::string> disconnect_reason;

    struct player_info {
        std::string username;
        std::string password;
        shared::player player;
    };
    // If this is nullopt, we haven't got our init packet yet which contained
    // their user + pass.
    std::optional<std::shared_ptr<player_info>> player_info;

    // flag for disconnecting, the client must stay while the server is writing
    // its contents to a db
    bool disconnecting = false;

    // Chunks for which the player is associated with.
    std::unordered_set<shared::math::coords,
                       decltype(&shared::world::chunk::hash),
                       decltype(&shared::world::chunk::equal)>
        chunks{4096, shared::world::chunk::hash, shared::world::chunk::equal};

public:
    client(shared::net::connection&& con, const shared::player::index_t& index)
        : connection(std::move(con)), index(index) {}

    bool has_initialised() const noexcept {
        return this->player_info.has_value();
    }
    shared::player& get_player() noexcept {
        return (*this->player_info)->player;
    }
    const std::string& get_username() const noexcept {
        return (*this->player_info)->username;
    }
    const std::string& get_password() const noexcept {
        return (*this->player_info)->password;
    }
};

} // namespace server

#endif