-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gamestate.py
executable file
·34 lines (30 loc) · 1.17 KB
/
Gamestate.py
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
import struct
from Player import Player
class Gamestate:
def __init__( self ):
self.lasttick = 0
self.players = []
self.local_player = 0
def add_player( self, number ):
self.players.append( Player( number, None ))
def del_player( self, number ):
for player in self.players:
if number == player.number:
self.players.remove( player )
def handle_worldstate( self, packet ):
n = Player.packet_size()
packet = packet[1:]
if len( packet ) % n != 0: return
for player in self.players: player.valid = False
for key, value in enumerate( \
[packet[i:i + n] for i in range(0, len( packet ), n )] ):
handled = False
for player in self.players:
handled = player.handle_packet( value ) or handled
if not handled:
number = struct.unpack( Player.packet_format, value )[0]
self.add_player( number )
for player in self.players: player.handle_packet( value )
for player in self.players:
if player.valid == False:
self.del_player( player.number )