1 /////////////////////////////////////////
2 //
3 //             OpenLieroX
4 //
5 // code under LGPL, based on JasonBs work,
6 // enhanced by Dark Charlie and Albert Zeyer
7 //
8 //
9 /////////////////////////////////////////
10 
11 
12 #ifndef __CSERVER_NET_ENGINE_H__
13 #define __CSERVER_NET_ENGINE_H__
14 
15 #include "CWorm.h"
16 #include "CVec.h"
17 
18 class GameServer;
19 class CServerConnection;
20 
21 // This class is not finished yet (and I think it never will be),
22 // so look at it as to incorporation of all differences between OLX versions.
23 // Big part of net protocol that have not changed since 0.56b is scattered around GameServer and CWorm classes
24 
25 class CServerNetEngine
26 {
27 public:
28 	// Constructor
CServerNetEngine(GameServer * _server,CServerConnection * _client)29 	CServerNetEngine( GameServer * _server, CServerConnection * _client ):
30 		server( _server ), cl( _client )
31 		{ }
32 
~CServerNetEngine()33 	virtual ~CServerNetEngine() { }
34 
35 	// TODO: move here all net code from CServer
36 	// Do not move here ParseConnectionlessPacket(), or make it static, 'cause client is not available for connectionless packet
37 
38 	// Parsing
39 
40 	void		ParsePacket(CBytestream *bs);
41 
ParseChatCommandCompletionRequest(CBytestream * bs)42 	virtual void ParseChatCommandCompletionRequest(CBytestream *bs) { return; };
ParseAFK(CBytestream * bs)43 	virtual void ParseAFK(CBytestream *bs) { return; };
ParseReportDamage(CBytestream * bs)44 	virtual void ParseReportDamage(CBytestream *bs) { return; };
45 
46 	void		 ParseImReady(CBytestream *bs);
47 	void		 ParseUpdate(CBytestream *bs);
48 	void		 ParseDeathPacket(CBytestream *bs);
49 	void		 ParseChatText(CBytestream *bs);
50 	void		 ParseUpdateLobby(CBytestream *bs);
51 	void		 ParseDisconnect();
52 	void		 ParseGrabBonus(CBytestream *bs);
53 	void		 ParseSendFile(CBytestream *bs);
54 
55 	bool		 ParseChatCommand(const std::string& message);
56 
57 	// Sending
58 
59 	void		SendPacket(CBytestream *bs);
60 
61 	void		 SendPrepareGame();
62 	virtual void SendText(const std::string& text, int type);
SendChatCommandCompletionSolution(const std::string & startStr,const std::string & solution)63 	virtual void SendChatCommandCompletionSolution(const std::string& startStr, const std::string& solution) { return; };
SendChatCommandCompletionList(const std::string & startStr,const std::list<std::string> & solutions)64 	virtual void SendChatCommandCompletionList(const std::string& startStr, const std::list<std::string>& solutions) { return; };
SendFiles()65 	virtual int  SendFiles() { return 0; }; // Returns client ping, or 0 if no packet was sent
66 	virtual void SendReportDamage(bool flush = false) { return; }
QueueReportDamage(int victim,float damage,int offender)67 	virtual void QueueReportDamage(int victim, float damage, int offender) { return; }
68 
69 	virtual void SendWormScore(CWorm *Worm);
70 	void		 SendUpdateLobbyGame();
71 	void		 SendUpdateLobby(CServerConnection *target = NULL);
72 
73 	void SendClientReady(CServerConnection* receiver); // If Receiver != NULL we're sending to worm connected during game
74 	void SendWormsOut(const std::list<byte>& ids);
75 	void SendWormDied(CWorm *Worm);
76 	void SendWeapons();
77 	void SendSpawnWorm(CWorm *Worm, CVec pos);
78 	virtual void SendHideWorm(CWorm *worm, int forworm, bool show = false, bool immediate = false);
SendTeamScoreUpdate()79 	virtual void SendTeamScoreUpdate() {}
80 	virtual void SendWormProperties(CWorm* worm);
81 	void SendWormProperties(bool onlyIfNotDef); // for all worms
82 	static bool isWormPropertyDefault(CWorm* worm);
83 	virtual void SendSelectWeapons(CWorm* worm);
84 	virtual void SendUpdateWorm(CWorm* w);
85 
86 	int getConnectionArrayIndex();
87 
88 protected:
89 	// Attributes
90 	GameServer 	*server;
91 	CServerConnection *cl;
92 
93 	virtual void WritePrepareGame(CBytestream *bs);
94 	virtual void WriteUpdateLobbyGame(CBytestream *bs);
95 };
96 
97 class CServerNetEngineBeta3: public CServerNetEngine
98 {
99 
100 public:
CServerNetEngineBeta3(GameServer * _server,CServerConnection * _client)101 	CServerNetEngineBeta3( GameServer * _server, CServerConnection * _client ):
102 		CServerNetEngine( _server, _client )
103 		{ }
104 
105 	virtual void SendText(const std::string& text, int type);
106 	virtual void SendHideWorm(CWorm *worm, int forworm, bool show = false, bool immediate = false);
107 
108 };
109 
110 class CServerNetEngineBeta5: public CServerNetEngineBeta3
111 {
112 
113 public:
CServerNetEngineBeta5(GameServer * _server,CServerConnection * _client)114 	CServerNetEngineBeta5( GameServer * _server, CServerConnection * _client ):
115 		CServerNetEngineBeta3( _server, _client )
116 		{ }
117 
118 	virtual int SendFiles();
119 
120 };
121 
122 class CServerNetEngineBeta7: public CServerNetEngineBeta5
123 {
124 
125 public:
CServerNetEngineBeta7(GameServer * _server,CServerConnection * _client)126 	CServerNetEngineBeta7( GameServer * _server, CServerConnection * _client ):
127 		CServerNetEngineBeta5( _server, _client )
128 		{ }
129 
130 	virtual void ParseChatCommandCompletionRequest(CBytestream *bs);
131 	virtual void ParseAFK(CBytestream *bs);
132 
133 	virtual void SendChatCommandCompletionSolution(const std::string& startStr, const std::string& solution);
134 	virtual void SendChatCommandCompletionList(const std::string& startStr, const std::list<std::string>& solutions);
135 
136 protected:
137 	virtual void WritePrepareGame(CBytestream *bs);
138 	void WriteUpdateLobbyGame(CBytestream *bs);
139 };
140 
141 class CServerNetEngineBeta8: public CServerNetEngineBeta7
142 {
143 
144 public:
CServerNetEngineBeta8(GameServer * _server,CServerConnection * _client)145 	CServerNetEngineBeta8( GameServer * _server, CServerConnection * _client ):
146 		CServerNetEngineBeta7( _server, _client )
147 		{ }
148 
149 	virtual void SendText(const std::string& text, int type);
150 
151 };
152 
153 class CServerNetEngineBeta9: public CServerNetEngineBeta8
154 {
155 
156 public:
CServerNetEngineBeta9(GameServer * _server,CServerConnection * _client)157 	CServerNetEngineBeta9( GameServer * _server, CServerConnection * _client ):
158 		CServerNetEngineBeta8( _server, _client )
159 	{
160 		fLastDamageReportSent = AbsTime();
161 	}
162 
163 	virtual void ParseReportDamage(CBytestream *bs);
164 	virtual void SendReportDamage(bool flush = false);
165 	virtual void QueueReportDamage(int victim, float damage, int offender);
166 	virtual void SendWormScore(CWorm *Worm);
167 	virtual void SendHideWorm(CWorm *worm, int forworm, bool show = false, bool immediate = false);
168 	virtual void SendTeamScoreUpdate();
169 	virtual void SendWormProperties(CWorm* worm);
170 	virtual void SendSelectWeapons(CWorm* worm);
171 	virtual void SendUpdateWorm(CWorm* w);
172 
173 	static void WriteFeatureSettings(CBytestream* bs);
174 
175 protected:
176 	virtual void WritePrepareGame(CBytestream *bs);
177 	void WriteUpdateLobbyGame(CBytestream *bs);
178 
179 private:
180     AbsTime fLastDamageReportSent;
181     std::map< std::pair< int, int >, float > cDamageReport;
182 };
183 
184 #endif  //  __CSERVER_NET_ENGINE_H__
185