Commits

Chris Beelby committed 2c3c547

Ability to join game and slight modification to UpFront object.
Viewing of a game in progress is ready, need only implement actuall view code in UpFront model

  • Participants
  • Parent commits 995a3e7

Comments (0)

Files changed (4)

         or if one of the two is not america.
         """
 
-        self.state = 'setup'
+        self.state = 'open'
         self.player1 = player1
         self.player1nation = player1nation
         self.player2 = player2
         self.player2nation = player2nation
 
-    def __validateNations(self, nation1, nation2):
+    def __validateNations(self):
         nations = ['america', 'germany', 'russia']
 
-        if not nation1 in nations:
-            raise ValueError('Invalid nation: "%s"' % (nation1))
+        if not self.player1nation in nations:
+            raise ValueError('Invalid nation: "%s"' % (self.player1nation))
 
-        if not nation2 in nations:
-            raise ValueError('Invalid nation: "%s"' % (nation2))
+        if not self.player2nation in nations:
+            raise ValueError('Invalid nation: "%s"' % (self.player2nation))
 
-        if not nation1 == 'america' and not nation2 == 'america':
+        if not self.player1nation == 'america' and not self.player2nation == 'america':
             raise ValueError('At least one player must be American')
 
-    def getPersonalityCards(nation):
+    def startGame(self):
+        self.__validateNations()
+        self.player1cards = self.getPersonalityCards(self.player1nation)
+        self.player2cards = self.getPersonalityCards(self.player2nation)
+        self.state = 'setupGroups'
+
+    def renderSetupGroups(self, renderFor):
+        if renderFor == self.player1 or renderFor == self.player2:
+            result = 'Hello %s, take your turn' % (renderFor.name)
+        elif renderFor is not None:
+            result = 'Hello %s, you may observe this game' % (renderFor.name)
+        else:
+            result = 'You are not logged in, but you can see the game state'
+
+        return result
+
+    def render(self, renderFor):
+        method = 'render' + self.state[0].upper() + self.state[1:]
+        if hasattr(self, method) and callable(getattr(self, method)):
+            f = getattr(self, method)
+            return f(renderFor)
+        else:
+            raise ValueError('%s is not a callable attribute' % method)
+
+    def getPersonalityCards(self, nation):
         if nation == 'germany':
             pistol = {'type': 'Machine Pistol', 'name': 'MP 38',
                 'firepower': {0:0, 1:0, 2:0, 3:1, 4:3, 5:5}}

upfront/models.py

 
 class Games(PersistentMapping):
     __name__ = 'games'
-    __acl__ = [(Allow, 'admin', 'delete-game'), (Allow, Authenticated, 'create-game')]
+    __acl__ = [(Allow, 'admin', 'delete-game'), (Allow, Authenticated, 'create-game'),
+        (Allow, Authenticated, 'join-game')]
 
     def __init__(self):
         self.games = IOBTree()

upfront/templates/game.mako

 <%inherit file="base.mako"/>
 
+${game.render(request.user)}
 
     data = StringIO.StringIO()
     image.save(data, 'PNG')
     resp.app_iter = data.getvalue()
+    data.close()
     return resp
 
 @view_config(context=User, renderer='upfront:templates/users/games.mako', name='games')
     games = dict()
     finished = dict()
     for id, game in context.games.iteritems():
-        if game.state == 'setup':
+        if game.state == 'open':
             opengames[id] = game
         elif game.state == 'finished':
             finished[id] = game
             games[id] = game
     return dict(opengames=opengames, games=games, finished=finished)
 
-@view_config(context=UpFront, renderer='upfront:templates/game.mako', name='join')
+@view_config(context=UpFront, renderer='upfront:templates/game.mako')
+def show_game(context, request):
+    return dict(game=context)
+
+@view_config(context=UpFront, name='join', permission='join-game')
 def join_game(context, request):
     game = context
     game.player2 = request.user
     game.player2nation = request.subpath[0]
+    game.startGame()
 
     id = int(context.__name__)
     games = context.__parent__
 
     del games.openGames[id]
     request.user.games[id] = game 
-    return dict(game=game) 
+    return HTTPFound(location=resource_url(game, request))
 
 @view_config(context=UpFront, name='delete', permission='delete-game')
 def delete_game(context, request):