File: World.py

package info (click to toggle)
fofix-dfsg 3.121-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,016 kB
  • ctags: 4,131
  • sloc: python: 44,002; makefile: 99; perl: 42; sh: 39
file content (263 lines) | stat: -rwxr-xr-x 9,135 bytes parent folder | download | duplicates (4)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#####################################################################
# -*- coding: iso-8859-1 -*-                                        #
#                                                                   #
# Frets on Fire                                                     #
# Copyright (C) 2006 Sami Ky�stil�                                  #
#                                                                   #
# This program is free software; you can redistribute it and/or     #
# modify it under the terms of the GNU General Public License       #
# as published by the Free Software Foundation; either version 2    #
# of the License, or (at your option) any later version.            #
#                                                                   #
# This program is distributed in the hope that it will be useful,   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of    #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     #
# GNU General Public License for more details.                      #
#                                                                   #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software       #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        #
# MA  02110-1301, USA.                                              #
#####################################################################

from Player import Player
from Session import MessageHandler, Message
import Network
import SceneFactory
import Config
from Scene import SuppressScene

STARTUP_SCENE = "SongChoosingScene"

# Messages from client to server
class CreatePlayer(Message): pass
class StartGame(Message): pass
class CreateScene(Message): pass
class DeleteScene(Message): pass
class GameFinished(Message): pass

# Messages from server to client
class PlayerJoined(Message): pass
class PlayerLeft(Message): pass
class GameStarted(Message): pass
class SceneCreated(Message): pass
class SceneDeleted(Message): pass
class EnterScene(Message): pass
class LeaveScene(Message): pass

# Internal messages
class SceneEntered(Message): pass
class SceneLeft(Message): pass

class World(MessageHandler):
  # TODO: abstract away the network details somehow
  def __init__(self, engine, broker):
    self.objects = Network.ObjectCollection()
    self.engine = engine
    self.broker = broker
    self.players = []
    self.scenes = []

  def handlePlayerJoined(self, sender, id, owner, name):
    player = Player(owner, name, len(self.players))
    self.players.append(player)
    self.objects[id] = player

  def handlePlayerLeft(self, sender, id):
    player = self.objects[id]
    self.players.remove(player)
    del self.objects[id]
    self.finishGameIfNeeded()

  def finishGameIfNeeded(self):
    if not self.players and not self.scenes:
      self.broker.signalMessage(0, GameFinished())

  def handleSceneDeleted(self, sender, id):
    try:
      scene = self.objects[id]
      self.broker.removeMessageHandler(scene)
      self.engine.removeTask(scene)
      if scene in self.scenes:
        self.scenes.remove(scene)
      del self.objects[id]
    except KeyError:
      pass
    self.finishGameIfNeeded()

  def handleSceneEntered(self, sender, sceneId, playerId):
    scene  = self.objects[sceneId]
    player = self.objects[playerId]
    scene.addPlayer(player)
    
  def handleSceneLeft(self, sender, sceneId, playerId):
    try:
      scene  = self.objects[sceneId]
      player = self.objects[playerId]
      scene.removePlayer(player)
    except KeyError:
      pass
    
class WorldServer(World):
  def __init__(self, engine, server):
    World.__init__(self, engine, server.broker)
    self.server = server

  def handleCreatePlayer(self, sender, name):
    id = self.objects.generateId()
    self.server.broadcastMessage(PlayerJoined(id = id, owner = sender, name = name))
    return id

  def handleCreateScene(self, sender, name, args):
    id = self.objects.generateId()
    self.server.broadcastMessage(SceneCreated(id = id, owner = sender, name = name, args = args))
    return id
  
  def handleDeleteScene(self, sender, id):
    try:
      scene = self.objects[id]
      self.deleteScene(scene)
    except KeyError:
      pass
  
  def deletePlayer(self, player):
    id = self.objects.id(player)
    self.server.broadcastMessage(PlayerLeft(id = id))

  def deleteScene(self, scene):
    id = self.objects.id(scene)
    self.server.broadcastMessage(SceneDeleted(id = id))

  def createScene(self, name, **args):
    id = self.objects.generateId()
    self.server.broadcastMessage(SceneCreated(id = id, owner = None, name = name, args = args))
    return id

  def enterScene(self, scene, player):
    sceneId  = self.objects.id(scene)
    playerId = self.objects.id(player)
    self.server.broadcastMessage(SceneEntered(sceneId = sceneId, playerId = playerId))

  def leaveScene(self, scene, player):
    sceneId  = self.objects.id(scene)
    playerId = self.objects.id(player)
    self.server.broadcastMessage(SceneLeft(sceneId = sceneId, playerId = playerId))

  def handleSessionOpened(self, session):
    # TODO: make this more automatic
    for player in self.players:
      id = self.objects.id(player)
      session.sendMessage(PlayerJoined(id = id, owner = player.owner, name = player.name))
    for scene in self.scenes:
      id = self.objects.id(scene)
      session.sendMessage(SceneCreated(id = id, owner = scene.owner, name = scene.__name__, args = scene.args))

  def handleSessionClosed(self, session):
    # TODO: make this more automatic
    for player in self.players:
      if player.owner == session.id:
        self.deletePlayer(player)
    for scene in self.scenes:
      if scene.owner == session.id:
        self.deleteScene(scene)

  def handleStartGame(self, sender, args):
    self.server.broadcastMessage(GameStarted())
    id = self.createScene(STARTUP_SCENE, **args)
    if id:
      for player in self.players:
        playerId = self.objects.id(player)
        self.server.broadcastMessage(EnterScene(sceneId = id, playerId = playerId))

  def handleEnterScene(self, sender, sceneId, playerId):
    scene  = self.objects[sceneId]
    player = self.objects[playerId]
    self.enterScene(scene, player)

  def handleLeaveScene(self, sender, sceneId, playerId):
    scene  = self.objects[sceneId]
    player = self.objects[playerId]
    self.leaveScene(scene, player)

  def handleSceneCreated(self, sender, id, owner, name, args):
    try:
      scene = SceneFactory.create(engine = self.engine, name = name, owner = owner, server = self.server, **args)
    except SuppressScene:
      return  #stump
    self.broker.addMessageHandler(scene)
    self.engine.addTask(scene)
    self.scenes.append(scene)
    self.objects[id] = scene

  def handleSceneDeleted(self, sender, id):
    try:
      scene = self.objects[id]
      for player in scene.players:
        self.leaveScene(scene, player)
    except KeyError:
      pass
    World.handleSceneDeleted(self, sender, id)
    
  def finishGameIfNeeded(self):
    if not self.players and not self.scenes:
      self.server.broadcastMessage(GameFinished())
    World.finishGameIfNeeded(self)
    
class WorldClient(World):
  def __init__(self, engine, session):
    World.__init__(self, engine, session.broker)
    self.session = session

  def finishGame(self):
    for scene in self.scenes:
      self.deleteScene(scene)
    for player in self.players:
      self.deletePlayer(player)

  def createPlayer(self, name):
    self.session.sendMessage(CreatePlayer(name = name))

  def deletePlayer(self, player):
    id = self.objects.id(player)
    self.session.sendMessage(PlayerLeft(id = id))

  def createScene(self, name, **args):
    self.session.sendMessage(CreateScene(name = name, args = args))

  def enterScene(self, player, scene):
    sceneId  = self.objects.id(scene)
    playerId = self.objects.id(player)
    self.session.sendMessage(EnterScene(sceneId = sceneId, playerId = playerId))

  def leaveScene(self, player, scene):
    sceneId  = self.objects.id(scene)
    playerId = objects.id(player)
    self.session.sendMessage(LeaveScene(sceneId = sceneId, playerId = playerId))

  def deleteScene(self, scene):
    id = self.objects.id(scene)
    self.session.sendMessage(DeleteScene(id = id))

  def startGame(self, **args):
    self.session.sendMessage(StartGame(args = args))

  def getLocalPlayer(self):
    for player in self.players:
      if player.owner == self.session.id:
        return player
      
  def getMultiplayers(self):
    return self.players[1:len(self.players)]
        
  def handleSceneCreated(self, sender, id, owner, name, args):
    try:
      scene = SceneFactory.create(engine = self.engine, name = name, owner = owner, session = self.session, **args)
    except SuppressScene:
      return  #stump
    self.broker.addMessageHandler(scene)
    self.scenes.append(scene)
    self.objects[id] = scene
    if owner == self.session.id:
      for player in self.players:
        if player.owner == self.session.id:
          self.enterScene(player, scene)