Package horizons :: Package network :: Package packets :: Module client
[hide private]
[frames] | no frames]

Source Code for Module horizons.network.packets.client

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import uuid 
 23   
 24  from horizons.network import NetworkException, SoftNetworkException 
 25  from horizons.network.packets import SafeUnpickler, packet 
26 27 28 -class cmd_creategame(packet):
29 clientversion = None # type: str 30 clientid = None # type: str 31 playername = None # type: str 32 playercolor = None # type: int 33 gamename = "Unnamed Game" 34 mapname = None # type: str 35 maxplayers = None # type: int 36 maphash = "" 37 password = "" 38
39 - def __init__(self, clientver, clientid, playername, playercolor, 40 gamename, mapname, maxplayers, maphash="", password=""):
41 self.clientversion = clientver 42 self.clientid = clientid 43 self.playername = playername 44 self.playercolor = playercolor 45 self.name = gamename 46 self.mapname = mapname 47 self.maxplayers = maxplayers 48 self.maphash = maphash 49 self.password = password
50 51 @staticmethod
52 - def validate(pkt, protocol):
53 if not isinstance(pkt.clientversion, str): 54 raise NetworkException("Invalid datatype: clientversion") 55 if not pkt.clientversion: 56 raise SoftNetworkException("Invalid client version") 57 58 if protocol == 0: 59 pkt.clientid = uuid.uuid4().hex 60 if not isinstance(pkt.clientid, str): 61 raise NetworkException("Invalid datatype: clientid") 62 if len(pkt.clientid) != 32: 63 raise SoftNetworkException("Invalid unique player ID") 64 65 if not isinstance(pkt.playername, str): 66 raise NetworkException("Invalid datatype: playername") 67 if not pkt.playername: 68 raise SoftNetworkException("Your player name cannot be empty") 69 70 if protocol == 0: 71 # hardcoded playercolor 72 pkt.playercolor = 1 73 else: 74 if not isinstance(pkt.playercolor, int): 75 raise NetworkException("Invalid datatype: playercolor") 76 if pkt.playercolor < 1: 77 raise SoftNetworkException("Your color is invalid") 78 79 if not isinstance(pkt.name, str): 80 raise NetworkException("Invalid datatype: name") 81 if not pkt.name: 82 pkt.name = "Unnamed Game" 83 84 if not isinstance(pkt.mapname, str): 85 raise NetworkException("Invalid datatype: mapname") 86 if not pkt.mapname: 87 raise SoftNetworkException("You can't run a game with an empty mapname") 88 89 if not isinstance(pkt.maxplayers, int): 90 raise NetworkException("Invalid datatype: maxplayers") 91 92 if protocol == 0: 93 if pkt.load is None: 94 pkt.maphash = "" 95 elif isinstance(pkt.load, str): 96 pkt.maphash = pkt.load 97 if not isinstance(pkt.maphash, str): 98 raise NetworkException("Invalid datatype: maphash") 99 100 if not isinstance(pkt.password, str): 101 raise NetworkException("Invalid datatype: password")
102 103 104 SafeUnpickler.add('client', cmd_creategame)
105 106 107 #------------------------------------------------------------------------------- 108 -class cmd_listgames(packet):
109 clientversion = 0 110 mapname = None # type: str 111 maxplayers = None # type: int 112
113 - def __init__(self, clientver, mapname=None, maxplayers=None):
114 self.clientversion = clientver 115 self.mapname = mapname 116 self.maxplayers = maxplayers
117 118 @staticmethod
119 - def validate(pkt, protocol):
120 if not isinstance(pkt.clientversion, (int, str)): 121 raise NetworkException("Invalid datatype: clientversion") 122 if pkt.mapname is not None and not isinstance(pkt.mapname, str): 123 raise NetworkException("Invalid datatype: mapname") 124 if pkt.maxplayers is not None and not isinstance(pkt.maxplayers, int): 125 raise NetworkException("Invalid datatype: maxplayers")
126 127 128 SafeUnpickler.add('client', cmd_listgames)
129 130 131 #------------------------------------------------------------------------------- 132 -class cmd_joingame(packet):
133 uuid = None # type: str 134 clientid = None # type: str 135 clientversion = None # type: str 136 playername = None # type: str 137 playercolor = None # type: int 138 password = "" 139 fetch = False 140
141 - def __init__(self, uuid, clientver, clientid, playername, playercolor, password="", fetch=False):
142 self.uuid = uuid 143 self.clientversion = clientver 144 self.clientid = clientid 145 self.playername = playername 146 self.playercolor = playercolor 147 self.password = password 148 self.fetch = fetch
149 150 @staticmethod
151 - def validate(pkt, protocol):
152 if not isinstance(pkt.uuid, str): 153 raise NetworkException("Invalid datatype: uuid") 154 if len(pkt.uuid) != 32: 155 raise SoftNetworkException("Invalid game UUID") 156 157 if not isinstance(pkt.clientversion, str): 158 raise NetworkException("Invalid datatype: clientversion") 159 if not pkt.clientversion: 160 raise SoftNetworkException("Invalid client version") 161 162 if protocol == 0: 163 pkt.clientid = uuid.uuid4().hex 164 if not isinstance(pkt.clientid, str): 165 raise NetworkException("Invalid datatype: clientid") 166 if len(pkt.clientid) != 32: 167 raise SoftNetworkException("Invalid unique player ID") 168 169 if not isinstance(pkt.playername, str): 170 raise NetworkException("Invalid datatype: playername") 171 if not pkt.playername: 172 raise SoftNetworkException("Your player name cannot be empty") 173 174 if protocol == 0: 175 # assign playercolor in packet handler 176 pkt.playercolor = None 177 else: 178 if not isinstance(pkt.playercolor, int): 179 raise NetworkException("Invalid datatype: playercolor") 180 if pkt.playercolor < 1: 181 raise SoftNetworkException("Your color is invalid") 182 183 if not isinstance(pkt.password, str): 184 raise NetworkException("Invalid datatype: password") 185 186 if not isinstance(pkt.fetch, bool): 187 raise NetworkException("Invalid datatype: fetch")
188 189 190 SafeUnpickler.add('client', cmd_joingame)
191 192 193 #------------------------------------------------------------------------------- 194 -class cmd_leavegame(packet):
195 - def __init__(self):
196 """ctor"""
197 198 199 SafeUnpickler.add('client', cmd_leavegame)
200 201 202 #------------------------------------------------------------------------------- 203 -class cmd_chatmsg(packet):
204 chatmsg = None # type: str 205
206 - def __init__(self, msg):
207 self.chatmsg = msg
208 209 @staticmethod
210 - def validate(pkt, protocol):
211 if not isinstance(pkt.chatmsg, str): 212 raise NetworkException("Invalid datatype: chatmsg") 213 if not pkt.chatmsg: 214 raise SoftNetworkException("Chat message cannot be empty")
215 216 217 SafeUnpickler.add('client', cmd_chatmsg)
218 219 220 #------------------------------------------------------------------------------- 221 -class cmd_changename(packet):
222 playername = None # type: str 223
224 - def __init__(self, playername):
226 227 @staticmethod
228 - def validate(pkt, protocol):
229 if not isinstance(pkt.playername, str): 230 raise NetworkException("Invalid datatype: playername") 231 if not pkt.playername: 232 raise SoftNetworkException("You must have a non empty name")
233 234 235 SafeUnpickler.add('client', cmd_changename)
236 237 238 #------------------------------------------------------------------------------- 239 -class cmd_changecolor(packet):
240 playercolor = None # type: int 241
242 - def __init__(self, playercolor):
244 245 @staticmethod
246 - def validate(pkt, protocol):
247 if not isinstance(pkt.playercolor, int): 248 raise NetworkException("Invalid datatype: playercolor") 249 if pkt.playercolor < 1: 250 raise SoftNetworkException("Your color is invalid")
251 252 253 SafeUnpickler.add('client', cmd_changecolor)
254 255 256 #------------------------------------------------------------------------------- 257 -class cmd_preparedgame(packet):
258 - def __init__(self):
259 """ctor"""
260 261 262 SafeUnpickler.add('client', cmd_preparedgame)
263 264 265 #------------------------------------------------------------------------------- 266 -class game_data(packet):
267 - def __init__(self, data):
268 self.data = data
269 270 # origin is 'server' as clients will send AND receive them 271 272 273 SafeUnpickler.add('server', game_data)
274 275 276 #------------------------------------------------------------------------------- 277 -class cmd_toggleready(packet):
278 - def __init__(self):
279 """ctor"""
280 281 282 SafeUnpickler.add('client', cmd_toggleready)
283 284 285 #------------------------------------------------------------------------------- 286 -class cmd_kickplayer(packet):
287 - def __init__(self, kicksid):
288 # NOTE: self.sid is used for session mgmt 289 self.kicksid = kicksid
290 291 @staticmethod
292 - def validate(pkt, protocol):
293 if not isinstance(pkt.kicksid, str): 294 raise NetworkException("Invalid datatype: player sid") 295 if len(pkt.kicksid) != 32: 296 raise SoftNetworkException("Invalid player sid")
297 298 299 SafeUnpickler.add('client', cmd_kickplayer)
300 301 302 #------------------------------------------------------------------------------- 303 -class cmd_sessionprops(packet):
304 - def __init__(self, props):
305 if 'lang' in props: 306 self.lang = props['lang']
307 308 @staticmethod
309 - def validate(pkt, protocol):
310 if hasattr(pkt, 'lang'): 311 if not isinstance(pkt.lang, str): 312 raise NetworkException("Invalid datatype: lang") 313 if not pkt.lang: 314 raise SoftNetworkException("Invalid language property")
315 316 317 SafeUnpickler.add('client', cmd_sessionprops)
318 319 320 #------------------------------------------------------------------------------- 321 #TODO 322 -class cmd_mapdata(packet):
323 - def __init__(self, data):
324 self.data = data
325 326 @staticmethod
327 - def validate(pkt, protocol):
328 if not isinstance(pkt.data, str): 329 raise NetworkException("Invalid datatype: data")
330 331 332 SafeUnpickler.add('client', cmd_mapdata) 333