1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2004, Sven2
6 * Copyright (c) 2017-2022, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18// network player management
19// manages synchronization of the player info list in network mode (lobby and runtime)
20// also handles player joins from the list
21//
22// Synchronization is centralized; i.e. clients send join request only
23// (C4PlayerJoinRequest), and the host then broadcasts list changes to all clients
24// (C4ControlPlayerInfo). PlayerInfo-controls are sent as host packets. In pause mode,
25// they will be sent end executed directly, while in running mode exchange will be done
26// via queue to ensure synchronization.
27//
28// Upon entering running mode, actual ControlQueue-PlayerJoin packets are sent by the
29// host for all list entries [/added since the run mode was last left]. In running mode,
30// the PlayerJoin-control will be sent directly after the PlayerInfo-control.
31
32#pragma once
33
34#include "C4PacketBase.h"
35
36// class predefs
37class C4Network2Players;
38
39// network player management
40class C4Network2Players
41{
42private:
43 class C4PlayerInfoList &rInfoList; // list of player infos - points to Game.PlayerInfos
44
45public:
46 C4Network2Players();
47 ~C4Network2Players() {}
48
49 void Init(); // add local players; add player file ressources - should be called with net connections initialized
50 void Clear(); // clear all player infos
51 bool JoinLocalPlayer(const char *szLocalPlayerFilename, bool fAdd); // join a local player (to game or lobby) - sends to host/and or schedules for queue
52
53public:
54 void SendUpdatedPlayers(); // send all player infos with updated flags to all clients (host only!)
55
56private:
57 void UpdateSavegameAssignments(class C4ClientPlayerInfos *pNewInfo); // resolve any savegame assignment conflicts of unjoined players; sets update-flags (host only!)
58
59 // add join-packet for given client players to control queue (host only!)
60 // if fSendInfo is true, the given info packet is sent via queue, too, and all players in it are marked as joined
61 void JoinUnjoinedPlayersInControlQueue(class C4ClientPlayerInfos *pNewPacket);
62
63public:
64 // callbacks from network system
65 void HandlePacket(char cStatus, const C4PacketBase *pPacket, class C4Network2IOConnection *pConn);
66 void OnClientPart(class C4Client *pPartClient); // called when a client disconnects - deletes all player infos and removes all players
67 void OnStatusGoReached(); // called when game starts, or continues from pause mode: send any unsent player joins
68
69 // request (client) or directly process (host) an update to existing player infos
70 // calls HandlePlayerInfoUpdRequest on host
71 void RequestPlayerInfoUpdate(const class C4ClientPlayerInfos &rRequest);
72
73 // player info packet received; handle it (CID_PlayerInfo, host and clients)
74 void HandlePlayerInfo(const class C4ClientPlayerInfos &rInfoPacket, bool localOrigin);
75
76 // player join request received; handle it (PID_PlayerInfoUpdReq, host only)
77 // adjusts the player data (colors, etc.), and creates update packets/controls
78 void HandlePlayerInfoUpdRequest(const class C4ClientPlayerInfos *pInfoPacket, bool fByHost);
79
80 // some query fns
81 C4ClientPlayerInfos *GetLocalPlayerInfoPacket() const; // get player info packet for local client (created in Init())
82 uint32_t GetClientChatColor(int idForClient, bool fLobby) const;
83};
84