summaryrefslogtreecommitdiffhomepage
path: root/Stars45/NetLobby.h
blob: 028e931e4b1b96413e2bd3cb13163349012e7e29 (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
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
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Base Class for Multiplayer Game Lobby classes
*/

#ifndef NetLobby_h
#define NetLobby_h

#include "Types.h"
#include "NetLink.h"
#include "List.h"

#define NET_CAMPAIGN_SHIFT    12
#define NET_MISSION_MASK      0xfff
#define NET_DISCONNECT_TIME   30

// +-------------------------------------------------------------------+

class Campaign;
class Mission;
class MissionElement;
class MissionInfo;
class NetCampaignInfo;
class NetChatEntry;
class NetUser;
class NetUnitEntry;
class NetLobbyParam;
class ModInfo;

// +--------------------------------------------------------------------+

class NetLobby
{
public:
    static const char* TYPENAME() { return "NetLobby"; }

    NetLobby(bool temporary = false);
    virtual ~NetLobby();

    virtual bool            IsClient()     const { return false; }
    virtual bool            IsServer()     const { return false; }
    virtual bool            IsActive()     const { return active; }

    virtual void            ExecFrame();
    virtual void            Recv();
    virtual void            Send();
    virtual int             GetLastError() const { return 0; }

    virtual NetUser*        FindUserByAddr(const NetAddr& addr);
    virtual NetUser*        FindUserByName(const char* name);
    virtual NetUser*        FindUserByNetID(DWORD id);

    virtual void            BanUser(NetUser* user);
    virtual void            AddUser(NetUser* user);
    virtual void            DelUser(NetUser* user);
    virtual bool            SetUserHost(NetUser* user, bool host);
    virtual int             NumUsers();
    virtual NetUser*        GetHost();
    virtual bool            HasHost();
    virtual bool            IsHost()       const { return false; }
    virtual List<NetUser>&  GetUsers();

    virtual List<ModInfo>&  GetServerMods();

    virtual NetUser*        GetLocalUser();
    virtual void            SetLocalUser(NetUser* user);

    virtual int             GetStatus()    const { return status; }
    virtual void            SetStatus(int s)     { status = s;    }

    virtual void            AddChat(NetUser* user, const char* msg, bool route=true);
    virtual List<NetChatEntry>&
    GetChat();
    virtual void            ClearChat();
    virtual void            SaveChat()           { }
    virtual DWORD           GetStartTime() const { return start_time; }

    virtual List<NetCampaignInfo>&
    GetCampaigns();

    virtual void            AddUnitMap(MissionElement* elem, int index=0);
    virtual List<NetUnitEntry>&
    GetUnitMap();
    virtual void            ClearUnitMap();
    virtual void            MapUnit(int n, const char* user, bool lock=false);
    virtual void            UnmapUnit(const char* user);
    virtual bool            IsMapped(const char* user);

    virtual Mission*        GetSelectedMission()         { return mission;          }
    virtual DWORD           GetSelectedMissionID() const { return selected_mission; }
    virtual void            SelectMission(DWORD id);

    virtual const Text&     GetMachineInfo()     { return machine_info;  }
    virtual WORD            GetGamePort()        { return 0;             }

    // actions:
    virtual bool            Ping();
    virtual void            GameStart()          { }
    virtual void            GameStop()           { }
    virtual DWORD           GetLag();

    // instance management:
    static NetLobby*        GetInstance();
    static bool             IsNetLobbyClient();
    static bool             IsNetLobbyServer();

protected:
    virtual void            DoPing(NetPeer* peer, Text msg)           { }
    virtual void            DoServerInfo(NetPeer* peer, Text msg)     { }
    virtual void            DoServerMods(NetPeer* peer, Text msg)     { }
    virtual void            DoLogin(NetPeer* peer, Text msg)          { }
    virtual void            DoLogout(NetPeer* peer, Text msg)         { }
    virtual void            DoAuthUser(NetPeer* peer, Text msg)       { }
    virtual void            DoUserAuth(NetPeer* peer, Text msg)       { }
    virtual void            DoChat(NetPeer* peer, Text msg)           { }
    virtual void            DoUserList(NetPeer* peer, Text msg)       { }
    virtual void            DoBanUser(NetPeer* peer, Text msg)        { }
    virtual void            DoMissionList(NetPeer* peer, Text msg)    { }
    virtual void            DoMissionSelect(NetPeer* peer, Text msg)  { }
    virtual void            DoMissionData(NetPeer* peer, Text msg)    { }
    virtual void            DoUnitList(NetPeer* peer, Text msg)       { }
    virtual void            DoMapUnit(NetPeer* peer, Text msg)        { }
    virtual void            DoGameStart(NetPeer* peer, Text msg)      { }
    virtual void            DoGameStop(NetPeer* peer, Text msg)       { }
    virtual void            DoExit(NetPeer* peer, Text msg)           { }

    virtual void            ParseMsg(Text msg, List<NetLobbyParam>& params);

    NetLink*                link;
    NetUser*                local_user;
    List<NetUser>           users;
    List<NetChatEntry>      chat_log;
    List<NetCampaignInfo>   campaigns;
    List<NetUnitEntry>      unit_map;
    Text                    machine_info;
    List<ModInfo>           server_mods;

    bool                    active;
    DWORD                   last_send_time;
    DWORD                   start_time;
    DWORD                   selected_mission;
    Mission*                mission;
    int                     status;
};

// +-------------------------------------------------------------------+

class NetLobbyParam
{
public:
    static const char* TYPENAME() { return "NetLobbyParam"; }

    NetLobbyParam(const char* n, const char* v) : name(n), value(v) { }

    int operator == (const NetLobbyParam& p) const { return name == p.name; }

    Text  name;
    Text  value;
};

// +-------------------------------------------------------------------+

class NetUnitEntry
{
public:
    static const char* TYPENAME() { return "NetUnitEntry"; }

    NetUnitEntry(MissionElement* elem, int index=0);
    NetUnitEntry(const char* elem, const char* design, int iff, int index=0);
    ~NetUnitEntry();

    int operator == (const NetUnitEntry& e) const { return (elem == e.elem) && (index == e.index); }

    Text              GetDescription()        const;
    const Text&       GetUserName()           const { return user;    }
    const Text&       GetElemName()           const { return elem;    }
    const Text&       GetDesign()             const { return design;  }
    int               GetIFF()                const { return iff;     }
    int               GetIndex()              const { return index;   }
    int               GetLives()              const { return lives;   }
    int               GetIntegrity()          const { return hull;    }
    int               GetMissionRole()        const { return role;    }
    bool              GetLocked()             const { return lock;    }

    void              SetUserName(const char* u)    { user = u;       }
    void              SetElemName(const char* e)    { elem = e;       }
    void              SetDesign(const char* d)      { design = d;     }
    void              SetIFF(int i)                 { iff = i;        }
    void              SetIndex(int i)               { index = i;      }
    void              SetLives(int i)               { lives = i;      }
    void              SetIntegrity(int i)           { hull = i;       }
    void              SetMissionRole(int i)         { role = i;       }
    void              SetLock(bool l)               { lock = l;       }

private:
    Text  user;
    Text  elem;
    Text  design;
    int   iff;
    int   index;
    int   lives;
    int   hull;
    int   role;
    bool  lock;
};

// +--------------------------------------------------------------------+

class NetServerInfo
{
public:
    static const char* TYPENAME() { return "NetServerInfo"; }

    enum STATUS { OFFLINE, LOBBY, BRIEFING, ACTIVE, DEBRIEFING, PERSISTENT };

    NetServerInfo();

    Text        name;
    Text        hostname;
    Text        password;
    Text        type;
    NetAddr     addr;
    WORD        port;
    WORD        gameport;
    bool        save;

    Text        version;
    Text        machine_info;
    int         nplayers;
    int         hosted;
    int         status;

    DWORD       ping_time;
};

// +--------------------------------------------------------------------+

class NetCampaignInfo
{
public:
    static const char* TYPENAME() { return "NetCampaignInfo"; }

    NetCampaignInfo() : id(0) { }
    ~NetCampaignInfo()        { }

    int               id;
    Text              name;

    List<MissionInfo> missions;
};

// +--------------------------------------------------------------------+

enum NET_LOBBY_MESSAGES {
    NET_LOBBY_PING        = 0x10,
    NET_LOBBY_SERVER_INFO,
    NET_LOBBY_SERVER_MODS,
    NET_LOBBY_LOGIN,
    NET_LOBBY_LOGOUT,
    NET_LOBBY_CHAT,
    NET_LOBBY_USER_LIST,
    NET_LOBBY_BAN_USER,
    NET_LOBBY_MISSION_LIST,
    NET_LOBBY_MISSION_SELECT,
    NET_LOBBY_MISSION_DATA,
    NET_LOBBY_UNIT_LIST,
    NET_LOBBY_MAP_UNIT,

    NET_LOBBY_AUTH_USER,
    NET_LOBBY_USER_AUTH,

    NET_LOBBY_GAME_START,
    NET_LOBBY_GAME_STOP,
    NET_LOBBY_EXIT
};

// +--------------------------------------------------------------------+

#endif  // NetLobby_h