aboutsummaryrefslogtreecommitdiff
path: root/src/server/client.hh
blob: 4a2c1fde807e6eff9dbe9d3b46110192caa7ed11 (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
68
69
70
71
72
73
74
#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/chunk.hh"
#include "shared/entity/player.hh"
#include "shared/net/connection.hh"
#include "shared/world/chunk.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};

    // sequence of the client's last commands, used for prediction
    shared::tick_t sequence = 0u;

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

    bool is_in_pvs(const client& other) const noexcept;

    // Not safe to use getter functions without checking has_initalised.
    bool has_initialised() const noexcept {
        return this->player_info.has_value();
    }

    shared::player& get_player() const 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