File: HumanClientFSM.h

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (428 lines) | stat: -rw-r--r-- 17,754 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#ifndef _HumanClientFSM_h_
#define _HumanClientFSM_h_

#include "../ClientFSMEvents.h"
#include "../../util/Logger.h"

#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/deferral.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>

#include <chrono>

// Human client-specific events not already defined in ClientFSMEvents.h

/** Indicates that the "Start Game" button was clicked in the MP Lobby UI, in
  * host player mode. */
struct StartMPGameClicked : boost::statechart::event<StartMPGameClicked> {};

// Indicates that the "Cancel" button was clicked in the MP Lobby UI.
struct CancelMPGameClicked : boost::statechart::event<CancelMPGameClicked> {};

// Indicates that an SP-host request was sent to the server.
struct HostSPGameRequested : boost::statechart::event<HostSPGameRequested> {};

// Indicates that a MP-host request was sent to the server.
struct HostMPGameRequested : boost::statechart::event<HostMPGameRequested> {};

// Indicates that a MP-join request was sent to the server.
struct JoinMPGameRequested : boost::statechart::event<JoinMPGameRequested> {};

// Indicates that the player's turn has been sent to the server.
struct TurnEnded : boost::statechart::event<TurnEnded> {};

// Posted to advance the turn, including when auto-advancing the first turn
struct AdvanceTurn : boost::statechart::event<AdvanceTurn> {};

/** The set of events used by the QuittingGame state. */
class Process;
struct StartQuittingGame : boost::statechart::event<StartQuittingGame> {
    StartQuittingGame(Process& server_, std::function<void()>&& after_server_shutdown_action_) :
        m_server(server_), m_after_server_shutdown_action(std::move(after_server_shutdown_action_))
    {}

    Process& m_server;
    /// An action to be completed after disconnecting from the server
    std::function<void()> m_after_server_shutdown_action;
};

struct ShutdownServer : boost::statechart::event<ShutdownServer> {};
struct WaitForDisconnect : boost::statechart::event<WaitForDisconnect> {};
struct TerminateServer : boost::statechart::event<TerminateServer> {};

/** This is a Boost.Preprocessor list of all the events above.  As new events
  * are added above, they should be added to this list as well. */
#define HUMAN_CLIENT_FSM_EVENTS                                 \
    (StartMPGameClicked)                                        \
    (CancelMPGameClicked)                                       \
    (HostSPGameRequested)                                       \
    (HostMPGameRequested)                                       \
    (JoinMPGameRequested)                                       \
    (TurnEnded)                                                 \
    (StartQuittingGame)                                         \
    (ShutdownServer)                                            \
    (WaitForDisconnect)                                         \
    (TerminateServer)

// Top-level human client states
struct IntroMenu;
struct PlayingGame;
struct WaitingForSPHostAck;
struct WaitingForMPHostAck;
struct WaitingForMPJoinAck;
struct MPLobby;
struct QuittingGame;

// Substates of PlayingGame
struct WaitingForGameStart;
struct WaitingForTurnData;
struct PlayingTurn;

class GGHumanClientApp;
class IntroScreen;
class MultiPlayerLobbyWnd;

#define CLIENT_ACCESSOR private: GGHumanClientApp& Client() { return context<HumanClientFSM>().m_client; }


/** The finite state machine that represents the human client's operation. */
struct HumanClientFSM : boost::statechart::state_machine<HumanClientFSM, IntroMenu> {
    typedef boost::statechart::state_machine<HumanClientFSM, IntroMenu> Base;

    HumanClientFSM(GGHumanClientApp &human_client);

    void unconsumed_event(const boost::statechart::event_base &event);

    GGHumanClientApp& m_client;

private:
    using Base::post_event; // for synchronous FSM, should only be called within state transitions and constructors...
};


/** The human client's initial state. */
struct IntroMenu : boost::statechart::state<IntroMenu, HumanClientFSM> {
    typedef boost::statechart::state<IntroMenu, HumanClientFSM> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<HostSPGameRequested>,
        boost::statechart::custom_reaction<HostMPGameRequested>,
        boost::statechart::custom_reaction<JoinMPGameRequested>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<EndGame>
    > reactions;

    IntroMenu(my_context ctx);
    ~IntroMenu();

    boost::statechart::result react(const HostSPGameRequested& a);
    boost::statechart::result react(const HostMPGameRequested& a);
    boost::statechart::result react(const JoinMPGameRequested& a);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const EndGame&);
    boost::statechart::result react(const Disconnection&);

    CLIENT_ACCESSOR
};


/** The human client state in which the player has requested to host a single
  * player game and is waiting for the server to acknowledge the request. */
struct WaitingForSPHostAck : boost::statechart::simple_state<WaitingForSPHostAck, HumanClientFSM> {
    typedef boost::statechart::simple_state<WaitingForSPHostAck, HumanClientFSM> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<HostSPGame>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<Error>,
        boost::statechart::custom_reaction<CheckSum>
    > reactions;

    WaitingForSPHostAck();
    ~WaitingForSPHostAck();

    boost::statechart::result react(const HostSPGame& a);
    boost::statechart::result react(const Disconnection& d);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const Error& msg);
    boost::statechart::result react(const CheckSum& msg);

    CLIENT_ACCESSOR
};


/** The human client state in which the player has requested to host a
  * multiplayer game and is waiting for the server to acknowledge the request. */
struct WaitingForMPHostAck : boost::statechart::simple_state<WaitingForMPHostAck, HumanClientFSM> {
    typedef boost::statechart::simple_state<WaitingForMPHostAck, HumanClientFSM> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<HostMPGame>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<Error>,
        boost::statechart::custom_reaction<CheckSum>
    > reactions;

    WaitingForMPHostAck();
    ~WaitingForMPHostAck();

    boost::statechart::result react(const HostMPGame& a);
    boost::statechart::result react(const Disconnection& d);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const Error& msg);
    boost::statechart::result react(const CheckSum& msg);

    CLIENT_ACCESSOR
};


/** The human client state in which the player has requested to join a
  * single-player game and is waiting for the server to acknowledge the
  * player's join. */
struct WaitingForMPJoinAck : boost::statechart::simple_state<WaitingForMPJoinAck, HumanClientFSM> {
    typedef boost::statechart::simple_state<WaitingForMPJoinAck, HumanClientFSM> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<JoinGame>,
        boost::statechart::custom_reaction<AuthRequest>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<CancelMPGameClicked>,
        boost::statechart::custom_reaction<Error>
    > reactions;

    WaitingForMPJoinAck();
    ~WaitingForMPJoinAck();

    boost::statechart::result react(const JoinGame& a);
    boost::statechart::result react(const AuthRequest& a);
    boost::statechart::result react(const Disconnection& d);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const CancelMPGameClicked& msg);
    boost::statechart::result react(const Error& msg);

    CLIENT_ACCESSOR
};


/** The human client state in which the multiplayer lobby is active. */
struct MPLobby : boost::statechart::state<MPLobby, HumanClientFSM> {
    typedef boost::statechart::state<MPLobby, HumanClientFSM> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<HostID>,
        boost::statechart::custom_reaction<LobbyUpdate>,
        boost::statechart::custom_reaction<PlayerChat>,
        boost::statechart::custom_reaction<CancelMPGameClicked>,
        boost::statechart::custom_reaction<StartMPGameClicked>,
        boost::statechart::custom_reaction<GameStart>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<Error>,
        boost::statechart::custom_reaction<CheckSum>,
        boost::statechart::custom_reaction<ChatHistory>,
        boost::statechart::custom_reaction<PlayerStatus>,
        boost::statechart::custom_reaction<SaveGameComplete>,
        boost::statechart::custom_reaction<TurnProgress>
    > reactions;

    MPLobby(my_context ctx);
    ~MPLobby();

    boost::statechart::result react(const Disconnection& d);
    boost::statechart::result react(const HostID& msg);
    boost::statechart::result react(const LobbyUpdate& msg);
    boost::statechart::result react(const PlayerChat& msg);
    boost::statechart::result react(const CancelMPGameClicked& a);
    boost::statechart::result react(const StartMPGameClicked& a);
    boost::statechart::result react(const GameStart& msg);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const Error& msg);
    boost::statechart::result react(const CheckSum& msg);
    boost::statechart::result react(const ChatHistory& msg);
    boost::statechart::result react(const PlayerStatus& msg);
    boost::statechart::result react(const SaveGameComplete& msg);
    boost::statechart::result react(const TurnProgress& msg);

    CLIENT_ACCESSOR
};


/** The human client state in which a game has been started. */
struct PlayingGame : boost::statechart::state<PlayingGame, HumanClientFSM, WaitingForGameStart> {
    typedef boost::statechart::state<PlayingGame, HumanClientFSM, WaitingForGameStart> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<HostID>,
        boost::statechart::custom_reaction<PlayerChat>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<PlayerStatus>,
        boost::statechart::custom_reaction<Diplomacy>,
        boost::statechart::custom_reaction<DiplomaticStatusUpdate>,
        boost::statechart::custom_reaction<EndGame>,
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<Error>,
        boost::statechart::custom_reaction<TurnProgress>,
        boost::statechart::custom_reaction<TurnPartialUpdate>,
        boost::statechart::custom_reaction<LobbyUpdate>,
        boost::statechart::custom_reaction<TurnTimeout>,
        boost::statechart::custom_reaction<PlayerInfoMsg>
    > reactions;

    PlayingGame(my_context ctx);
    ~PlayingGame();

    boost::statechart::result react(const HostID& msg);
    boost::statechart::result react(const PlayerChat& msg);
    boost::statechart::result react(const Disconnection& d);
    boost::statechart::result react(const PlayerStatus& msg);
    boost::statechart::result react(const Diplomacy& d);
    boost::statechart::result react(const DiplomaticStatusUpdate& u);
    boost::statechart::result react(const EndGame& msg);
    boost::statechart::result react(const StartQuittingGame& msg);
    boost::statechart::result react(const Error& msg);
    boost::statechart::result react(const TurnProgress& msg);
    boost::statechart::result react(const TurnPartialUpdate& msg);
    boost::statechart::result react(const LobbyUpdate& msg);
    boost::statechart::result react(const TurnTimeout& msg);
    boost::statechart::result react(const PlayerInfoMsg& msg);

    CLIENT_ACCESSOR
};


/** The human client state in which a game has been started but the start of
  * game message hasn't been received yet from the server. */
struct WaitingForGameStart : boost::statechart::state<WaitingForGameStart, PlayingGame> {
    typedef boost::statechart::state<WaitingForGameStart, PlayingGame> Base;

    struct GameStartDataUnpackedNotification : public boost::statechart::event<GameStartDataUnpackedNotification> {
        struct UnpackedData;
        GameStartDataUnpackedNotification() = default;
        explicit GameStartDataUnpackedNotification(std::shared_ptr<UnpackedData> unpacked_) :
            unpacked(std::move(unpacked_))
        {}
        std::shared_ptr<UnpackedData> unpacked;
    };
    struct UnpackFailedNotification : public boost::statechart::event<UnpackFailedNotification> {};

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<GameStart>,
        boost::statechart::custom_reaction<GameStartDataUnpackedNotification>,
        boost::statechart::custom_reaction<UnpackFailedNotification>
    > reactions;

    WaitingForGameStart(my_context ctx);
    ~WaitingForGameStart();

    boost::statechart::result react(const GameStart& msg);
    boost::statechart::result react(const GameStartDataUnpackedNotification& data);
    boost::statechart::result react(const UnpackFailedNotification&);

    CLIENT_ACCESSOR
};


/** The substate of PlayingGame in which a game is about to start, or the
  * player is waiting for turn resolution and a new turn. */
struct WaitingForTurnData : boost::statechart::state<WaitingForTurnData, PlayingGame> {
    typedef boost::statechart::state<WaitingForTurnData, PlayingGame> Base;

    struct TurnDataUnpackedNotification : public boost::statechart::event<TurnDataUnpackedNotification> {
        struct UnpackedData;
        explicit TurnDataUnpackedNotification(std::shared_ptr<UnpackedData> unpacked_) :
            unpacked(std::move(unpacked_))
        {}
        std::shared_ptr<UnpackedData> unpacked;
    };
    struct UnpackFailedNotification : public boost::statechart::event<UnpackFailedNotification> {};

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<SaveGameComplete>,
        boost::statechart::custom_reaction<TurnUpdate>,
        boost::statechart::custom_reaction<TurnDataUnpackedNotification>,
        boost::statechart::custom_reaction<UnpackFailedNotification>,
        boost::statechart::custom_reaction<TurnRevoked>,
        boost::statechart::custom_reaction<DispatchCombatLogs>
    > reactions;

    WaitingForTurnData(my_context ctx);
    ~WaitingForTurnData();

    boost::statechart::result react(const SaveGameComplete& d);
    boost::statechart::result react(const TurnUpdate& msg);
    boost::statechart::result react(const TurnDataUnpackedNotification& data);
    boost::statechart::result react(const UnpackFailedNotification&);
    boost::statechart::result react(const TurnRevoked& msg);
    boost::statechart::result react(const DispatchCombatLogs& msg);

    CLIENT_ACCESSOR
};


/** The substate of PlayingGame in which the player is actively playing a turn. */
struct PlayingTurn : boost::statechart::state<PlayingTurn, PlayingGame> {
    typedef boost::statechart::state<PlayingTurn, PlayingGame> Base;

    typedef boost::mpl::list<
        boost::statechart::custom_reaction<SaveGameComplete>,
        boost::statechart::custom_reaction<AdvanceTurn>,
        boost::statechart::custom_reaction<TurnUpdate>,
        boost::statechart::custom_reaction<TurnEnded>,
        boost::statechart::custom_reaction<PlayerStatus>,
        boost::statechart::custom_reaction<DispatchCombatLogs>
    > reactions;

    PlayingTurn(my_context ctx);
    ~PlayingTurn();

    boost::statechart::result react(const SaveGameComplete& d);
    boost::statechart::result react(const AdvanceTurn& d);
    boost::statechart::result react(const TurnUpdate& msg);
    boost::statechart::result react(const TurnEnded& d);
    boost::statechart::result react(const PlayerStatus& msg);
    boost::statechart::result react(const DispatchCombatLogs& msg);

    CLIENT_ACCESSOR
};


/** The state for the orderly shutdown of the server. */
struct QuittingGame : boost::statechart::state<QuittingGame, HumanClientFSM> {
    using Clock = std::chrono::steady_clock;

    using reactions = boost::mpl::list<
        boost::statechart::custom_reaction<StartQuittingGame>,
        boost::statechart::custom_reaction<ShutdownServer>,
        boost::statechart::custom_reaction<WaitForDisconnect>,
        boost::statechart::custom_reaction<Disconnection>,
        boost::statechart::custom_reaction<TerminateServer>
    >;

    QuittingGame(my_context ctx);
    ~QuittingGame();

    boost::statechart::result react(const StartQuittingGame& d);
    boost::statechart::result react(const ShutdownServer& msg);
    boost::statechart::result react(const WaitForDisconnect& msg);
    boost::statechart::result react(const Disconnection& msg);
    boost::statechart::result react(const TerminateServer& msg);

    std::chrono::steady_clock::time_point m_start_time;

    Process* m_server_process = nullptr;
    /// An action to be completed after disconnecting from the server
    std::function<void()> m_after_server_shutdown_action = std::function<void()>();

    CLIENT_ACCESSOR
};


#undef CLIENT_ACCESSOR

#endif