~crempp/psg/devel

« back to all changes in this revision

Viewing changes to src/Game.py

  • Committer: Administrator
  • Date: 2009-12-14 04:46:06 UTC
  • Revision ID: administrator@gauss-20091214044606-deet4mtmsh0rwz52
Broken!

* Organized code into modules but did not fix it to work. In order to do this I had to remove the gui directory so I can add the GUI directory in the next commit.
* Began work on skybox extension.

Chad

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''_Game.py____________________________________________________________________
2
 
        Author:                 Chad Rempp
3
 
        Date:                   2009/05/25
4
 
        Purpose:                Holds the game object.
5
 
        Usage:                  None
6
 
        References:             None
7
 
        Restrictions:   None
8
 
        License:                TBD
9
 
        Notes:                  
10
 
____________________________________________________________________________'''
11
 
 
12
 
# Python imports
13
 
import hashlib
14
 
import os
15
 
import time
16
 
 
17
 
# PSG imports
18
 
import Map
19
 
 
20
 
MAX_GAMES  = 50
21
 
GAME_IDS = range(1,MAX_GAMES)
22
 
def getGameID():
23
 
        ''' This helper function returns the first available game ID. If none
24
 
                are available it returns -1.'''
25
 
        if (len(GAME_IDS) > 0):
26
 
                id = GAME_IDS[0]
27
 
                GAME_IDS.remove(id)
28
 
                return id
29
 
        else: return -1
30
 
        
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
34
 
                it returns -1.'''
35
 
        if (len(GAME_IDS) < MAX_GAMES):
36
 
                GAME_IDS.append(id)
37
 
                GAME_IDS.sort()
38
 
        else: return -1
39
 
 
40
 
#_CLIENTGAME_CLASS_____________________________________________________________
41
 
class ClientGame:
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'''
49
 
                self.id         = id
50
 
                self.name       = name
51
 
                self.players    = []
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)
59
 
                else:
60
 
                        self.mapCheck = ''
61
 
                self.description  = '%s %s %s %s %s'%(self.name,self.maxPlayers,self.mapName,self.startTime,self.turnNumber)
62
 
                
63
 
        def __repr__(self):
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
68
 
                r+= "  Players:\n"
69
 
                for p in self.players:
70
 
                        r+= "             %s\n"%p.name
71
 
                r+= ">"
72
 
                return r
73
 
 
74
 
#_SERVERGAME_CLASS_____________________________________________________________
75
 
class ServerGame:
76
 
        ''' The game class stores all information for active games. It also handles
77
 
                passing messages between connected clients.'''
78
 
        
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'''
85
 
                self.id         = getGameID()
86
 
                if (self.id < 0):
87
 
                        print('Error getting a game ID!')
88
 
                self.name       = name
89
 
                self.players    = []
90
 
                self.maxPlayers = maxplayers
91
 
                self.mapName    = mapname
92
 
                self.mapFileName= mapfilename
93
 
                self.startTime  = int(time.time())
94
 
                self.turnNumber = 0
95
 
                if self.mapFileName != '' and os.path.exists(Map.MAP_PATH + self.mapFileName):
96
 
                        self.mapCheck = Map.getMapMD5(self.mapFileName)
97
 
                else:
98
 
                        self.mapCheck = ''
99
 
 
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()
104
 
                        
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)
109
 
                
110
 
        def __del__(self):
111
 
                releaseGameID(self.id)
 
 
b'\\ No newline at end of file'