1
'''_Game.py____________________________________________________________________
4
Purpose: Holds the game object.
10
____________________________________________________________________________'''
21
GAME_IDS = range(1,MAX_GAMES)
23
''' This helper function returns the first available game ID. If none
24
are available it returns -1.'''
25
if (len(GAME_IDS) > 0):
31
def releaseGameID(id):
32
''' This helper function inserts a game ID in the the list of available
33
IDs. If inserting the ID would result in there being more than MAX_GAMES
35
if (len(GAME_IDS) < MAX_GAMES):
40
#_CLIENTGAME_CLASS_____________________________________________________________
42
''' The game class stores all information for active games.'''
43
def __init__(self, id=0, name='New Game', maxplayers=2, mapname='', mapfilename='', starttime=0, turnnumber=0):
44
''' Creates a new game.
45
id (Int): the unique id of this game
46
name (String): the name of this game
47
maxplayers (Int): maximum players allowed to participate
48
map (String): the file name of the map being used'''
52
self.maxPlayers = maxplayers
53
self.mapName = mapname
54
self.mapFileName= mapfilename
55
self.startTime = starttime
56
self.turnNumber = turnnumber
57
if self.mapFileName != '' and os.path.exists(Map.MAP_PATH + self.mapFileName):
58
self.mapCheck = Map.getMapMD5(self.mapFileName)
61
self.description = '%s %s %s %s %s'%(self.name,self.maxPlayers,self.mapName,self.startTime,self.turnNumber)
64
r = "<ClientGame: id=%d, name=%s, maxPlayers=%d,\n"%(self.id, self.name, self.maxPlayers)
65
r+= " startTime=%s, turnNumber=%d\n"%(self.startTime, self.turnNumber)
66
r+= " mapName=%s,\n"%self.mapName
67
r+= " mapFileName=%s\n"%self.mapFileName
69
for p in self.players:
74
#_SERVERGAME_CLASS_____________________________________________________________
76
''' The game class stores all information for active games. It also handles
77
passing messages between connected clients.'''
79
def __init__(self, name='New Game', maxplayers=2, mapname='', mapfilename=''):
80
''' Creates a new game.
81
id (Int): the unique id of this game
82
name (String): the name of this game
83
maxplayers (Int): maximum players allowed to participate
84
map (String): the file name of the map being used'''
87
print('Error getting a game ID!')
90
self.maxPlayers = maxplayers
91
self.mapName = mapname
92
self.mapFileName= mapfilename
93
self.startTime = int(time.time())
95
if self.mapFileName != '' and os.path.exists(Map.MAP_PATH + self.mapFileName):
96
self.mapCheck = Map.getMapMD5(self.mapFileName)
100
def setMap(self, mapfilename):
101
if (mapfilename != None and mapfilename != ''):
102
self.mapFile = mapfilename.replace(' ','')+'.map'
103
self.mapCheck = hashlib.md5(file('data/maps/'+self.mapFile,'rb').read()).hexdigest()
105
def addPlayer(self, player):
106
''' Adds a player to the game.
107
player (User): the user to be added to the game'''
108
self.players.append(player)
111
releaseGameID(self.id)
b'\\ No newline at end of file'