-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.h
More file actions
153 lines (125 loc) · 4.16 KB
/
game.h
File metadata and controls
153 lines (125 loc) · 4.16 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Game server for Alien Front Online.
Copyright (C) 2025 Flyinghead
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <dcserver/shared_this.hpp>
#include <dcserver/asio.hpp>
#include <array>
#include <vector>
#include <memory>
#include <string>
class GameAcceptor;
class Server;
class Player;
class Game : public SharedThis<Game>
{
public:
enum SlotType : uint8_t {
Open = 0,
Open_CPU = 1,
Filled = 2,
CPU = 3,
Balanced = 4, // Found in game code but unknown usage
Filled2 = 5, // Same
Closed = 255
};
enum GameType : uint8_t {
None = 0,
Watch = 1, // Found in game code but unknown usage
Competition = 2, // Same
DeathMatch = 3,
TeamFortress = 4,
CaptureTheFlag = 5,
};
void start();
uint16_t getIpPort() const { return port; }
const std::string& getName() const { return name; }
void setName(const std::string& name) { this->name = name; }
GameType getType() const { return type; }
void setType(GameType type) { this->type = type; }
unsigned getMaps() const { return maps; }
void setMaps(unsigned maps) { this->maps = maps; }
SlotType getSlotType(int slot) const { return slots[slot].type; }
void setSlots(const std::array<SlotType, 8>& slots);
std::shared_ptr<Player> getPlayer(int slot) const { return slots[slot].player; }
std::string getHttpDesc(bool attributes) const;
int assignSlot(std::shared_ptr<Player> player, bool alien);
void sendPlayerList() const;
void tcpSendToAll(const uint8_t *data, size_t len, const std::shared_ptr<Player>& except = nullptr) const;
void disconnect(std::shared_ptr<Player> player);
void addSpectator(std::shared_ptr<Player> player);
void removeSpectator(std::shared_ptr<Player> player);
private:
Game(Server& server, asio::io_context& io_context, const std::string& serverIp, uint16_t port);
void udpRead();
void onPing(const std::error_code& ec);
void udpSendToAll(const uint8_t *data, size_t len, const std::shared_ptr<Player>& except = nullptr);
std::array<uint8_t, 0x82> getPlayerList() const;
void onInitialTimeout(const std::error_code& ec);
Server& server;
asio::io_context& io_context;
std::string serverIp;
std::string name;
uint16_t port;
GameType type {};
unsigned maps = 0;
struct PlayerSlot
{
SlotType type = Closed;
SlotType openType = Closed; // to restore Open or Open/CPU when player leaves
std::shared_ptr<Player> player;
asio::chrono::time_point<asio::chrono::steady_clock> lastUdpReceive;
};
std::array<PlayerSlot, 8> slots;
std::shared_ptr<GameAcceptor> gameAcceptor;
std::vector<std::shared_ptr<Player>> spectators;
// UDP socket stuff
asio::ip::udp::socket socket;
std::array<uint8_t, 1510> recvbuf;
asio::ip::udp::endpoint source; // source endpoint when receiving UDP packets
asio::steady_timer pingTimer;
uint16_t pingSeq = 0;
friend super;
};
class GameConnection;
class GameAcceptor : public SharedThis<GameAcceptor>
{
public:
void start();
void stop() {
std::error_code ec;
acceptor.close(ec);
}
private:
GameAcceptor(asio::io_context& io_context, std::shared_ptr<Game> game)
: io_context(io_context),
acceptor(asio::ip::tcp::acceptor(io_context,
asio::ip::tcp::endpoint(asio::ip::tcp::v4(), game->getIpPort()))),
game(game)
{
asio::socket_base::reuse_address option(true);
acceptor.set_option(option);
}
void handleAccept(std::shared_ptr<GameConnection> newConnection, const std::error_code& error);
asio::io_context& io_context;
asio::ip::tcp::acceptor acceptor;
std::shared_ptr<Game> game;
friend super;
};
class Server
{
public:
virtual ~Server() = default;
virtual void deleteGame(Game::Ptr game) = 0;
};