Commits

Chris Beelby committed 307a204

Added more card definitions. Remove OpenGame class. Modified UpFront class to allow building up.

  • Participants
  • Parent commits 1082ce7

Comments (0)

Files changed (5)

File upfront/game.py

+from persistent import Persistent
 
-class PersonalityCard(object):
+ALL = ['germany', 'russia', 'america']
+R = 'red'
+B = 'black'
+G = 'germany'
+RU = 'russia'
+A = 'america'
+
+class ActionCard(Persistent):
+    def __init__(self, id, name, actions, randomNumber, randomPositionNumbers):
+        self.id = id
+        self.name = name
+        self.actions = actions
+        self.randomNumber = randomNumber
+        self.randomPositionNumbers = randomPositionNumbers
+
+    def __repr__(self):
+        return 'ActionCard(%s, %s, %s, %s)' % (repr(self.id), repr(self.actions),
+            repr(self.randomNumber), repr(self.randomPositionNumbers))
+
+class ActionCardAction(Persistent):
+    def __init__(self, actionType, nations, **kwargs):
+        for nation in nations:
+            if not nation in ALL:
+                raise ValueError('Invalid nation: %s' % (repr(nation)))
+        self.actionType = actionType
+        self.nations = nations
+        self.kwargs = kwargs
+        for key in kwargs:
+            setattr(self, key, kwargs[key])
+
+    def __repr__(self):
+        return 'ActionCardAction(%s, %s, %s)' % (repr(self.actionType), repr(self.nations),
+            repr(self.kwargs))
+
+class PersonalityCard(Persistent):
     def __init__(self, id, nation, name, weapon, morale, panic, rank=None):
         self.id = id
         self.name = name
         return 'PersonalityCard(%s, %s, %s, %s, %s, %s, %s)' % (self.id, repr(self.name),
             repr(self.nation), self.weapon, self.morale, self.panic, repr(self.rank))
 
-class UpFront(object):
+class UpFront(Persistent):
     def __init__(self, player1, player1nation, player2=None, player2nation=None):
         """
-        Create basic setup of an Up Front game. player1 and player2 are the names
-        of the players for reference.
+        Create basic setup of an Up Front game. player1 and player2 are the actual
+        User() objects which are also under root['users'].
 
         player1nation and player2nation should be one of: 'germany', 'america', or 'russia'.
-        An exception is raised if the two values are not different or if one of the two is not
-        america.
+        An exception is raised if you try to start a game and the two values are not different
+        or if one of the two is not america.
         """
 
         self.state = 'setup'
         if not nation1 == 'america' and not nation2 == 'america':
             raise ValueError('At least one player must be American')
 
-    def getCards(nation):
+    def getPersonalityCards(nation):
         if nation == 'germany':
             pistol = {'type': 'Machine Pistol', 'name': 'MP 38',
                 'firepower': {0:0, 1:0, 2:0, 3:1, 4:3, 5:5}}
 
             return russians
 
+    def getActionCards():
+        conceal1 = ActionCardAction('Conceal', ALL, conceal=-1)
+        conceal1r = ActionCardAction('Conceal', [RU], conceal=-1)
+        cowerag = ActionCardAction('Cower', [A, G])
+        rally2ag = ActionCardAction('Rally', [A, G], rally=2)
+        rally1 = ActionCardAction('Rally', ALL, rally=1)
+        fire5p11 = ActionCardAction('Fire', ALL, firepower=11, firestrength=5)
+        fire3p4 = ActionCardAction('Fire', ALL, firepower=4, firestrength=3)
+        move = ActionCardAction('Move', ALL)
+
+        cards = list()
+
+        rpn = [(1, R), (1, B), (1, B), (3, B), (5, B), (1, B), (6, B), (7, B), (1, B), (5, B)]
+        cards.append(ActionCard(55, 'Concealed', [conceal1], (2, B), rpn))
+
+        rpn = [(1, R), (2, R), (3, R), (4, B), (5, R), (6, B), (4, B), (4, B), (6, B), (0, B)]
+        cards.append(ActionCard(60, 'Concealed', [cowerag, conceal1r], (2, R), rpn))
+
+        rpn = [(1, B), (1, R), (3, B), (3, B), (1, B), (3, B), (2, B), (3, B), (6, B), (1, B)]
+        cards.append(ActionCard(51, 'Rally', [conceal1r, rally2ag], (1, B), rpn))
+
+        rpn = [(1, B), (2, B), (2, B), (4, R), (2, B), (2, B), (4, B), (8, R), (5, B), (2, B)]
+        cards.append(ActionCard(32, 'Rally', [rally1], (6, B), rpn))
+
+        rpn = [(1, B), (1, R), (2, R), (1, B), (1, B), (5, R), (3, B), (5, B), (2, B), (1, B)]
+        cards.append(ActionCard(101, 'Fire', [fire5p11], (0, R), rpn))
+
+        rpn = [(1, R), (2, B), (2, B), (2, B), (1, B), (2, B), (5, R), (2, R), (8, B), (6, B)]
+        cards.append(ActionCard(26, 'Movement', [move], (5, R), rpn))
+
+        rpn = [(1, B)]*10
+        cards.append(ActionCard(1, 'Movement', [move], (6, B), rpn))
+
+        rpn = [(1, R), (1, R), (2, B), (3, B), (5, B), (5, R), (1, B), (3, B), (2, B), (5, B)]
+        cards.append(ActionCard(155, 'Fire', [fire3p4], (4, R), rpn))
+
+        return list(cards)
+
+

File upfront/models.py

 from persistent.mapping import PersistentMapping
 from persistent.list import PersistentList
 from BTrees.IOBTree import IOBTree
-
 from pyramid.security import Allow, Everyone, Deny, Authenticated
+from upfront.game import UpFront
 
 class Root(PersistentMapping):
     __parent__ = __name__ = None
     def __repr__(self):
         return 'Root()'
 
-class OpenGame(object):
-    def __init__(self, player1, player1nation):
-        self.player1 = player1
-        self.player1nation = player1nation
-        self.__acl__ = [(Allow, player1.name.lower(), 'delete-game')]
-
 class Games(PersistentMapping):
     __name__ = 'games'
     __acl__ = [(Allow, 'admin', 'delete-game'), (Allow, Authenticated, 'create-game')]
         except ValueError:
             raise KeyError
 
-    def addOpenGame(self, player1, player1nation):
-        game = OpenGame(player1, player1nation)
+    def createGame(self, player1, player1nation):
+        game = UpFront(player1, player1nation)
+        game.__acl__ = [(Allow, player1.name.lower(), 'delete-game')]
         while True:
             try:
                 id = self.games.maxKey() + 1
         self.games = IOBTree()
 
     def __repr__(self):
-        return 'User(%s, %s)' % (repr(self.name), repr(self.password))
+        return 'User(%s)' % (repr(self.name))
 
 def appmaker(zodb_root):
     if not 'app_root' in zodb_root:

File upfront/templates/opengames.mako

 <%inherit file="base.mako"/>
-
+% if games:
 <table>
 <tr>
     <th>Opponent</th>
 %>
 % endfor
 </table>
+% else:
+<i>There are no open games at this time</i>
+% endif

File upfront/templates/users/games.mako

 <h1>My Games</h1>
 
 <h2>Open Games</h2>
-
+% if opengames:
 <table>
 <tr>
     <th>Playing As</th>
 %>
 % endfor
 </table>
-
+% else:
+<i>You do not have any open games</i>
+% endif
 <h2>Games In Progress</h2>
+% if games:
+...
+% else:
+<i>You are not currently in any active games</i>
+% endif
 
 <h2>Complete Games</h2>
+% if finished:
+...
+% else:
+<i>You have not finished any games</i>
+% endif

File upfront/views.py

 from pyramid.security import Allow, Everyone, Deny
 from pyramid.security import remember
 from pyramid.url import resource_url
+from pyramid.response import Response
+
 from upfront.game import UpFront
+from upfront.models import Root, Users, User, Games
 
-from upfront.models import Root, Users, User, Games, OpenGame
+@view_config(context=Root, name='image')
+def image_test(context, request):
+    resp = Response(content_type='image/jpeg')
+    resp.app_iter = open('/home/chris/env/upfront/upfront/static/images/headerphoto.jpg', 'rb')
+    return resp
 
 @view_config(context=User, renderer='upfront:templates/users/games.mako', name='games')
 def my_games(context, request):
     games = dict()
     finished = dict()
     for id, game in context.games.iteritems():
-        if isinstance(game, OpenGame):
+        if game.state == 'setup':
             opengames[id] = game
         elif game.state == 'finished':
             finished[id] = game
             games[id] = game
     return dict(opengames=opengames, games=games, finished=finished)
 
-@view_config(context=OpenGame, renderer='upfront:templates/game.mako', name='join')
+@view_config(context=UpFront, renderer='upfront:templates/game.mako', name='join')
 def join_game(context, request):
-    game = UpFront(context.player1, context.player1nation, request.user, request.subpath[0])
+    game = context
+    game.player2 = request.user
+    game.player2nation = request.subpath[0]
+
     id = int(context.__name__)
     games = context.__parent__
+
     del games.openGames[id]
-    games.games[id] = game
-    context.player1.games[id] = game
     request.user.games[id] = game 
     return dict(game=game) 
 
-@view_config(context=OpenGame, name='delete', permission='delete-game')
 @view_config(context=UpFront, name='delete', permission='delete-game')
 def delete_game(context, request):
     id = int(context.__name__)
-    if isinstance(context, OpenGame):
-        del context.__parent__.openGames[id]
+    games = context.__parent__
+
+    if games.openGames.has_key(id):
+        del games.openGames[id]
 
     del context.player1.games[id]
-    del context.__parent__.games[id]        
-    return HTTPFound(location=resource_url(context.__parent__, request, 'open'))
+    del games.games[id]        
 
+    if context.player2:
+        del context.player2.games[id]
+
+    return HTTPFound(location=resource_url(games, request, 'open'))
 
 @view_config(context=Games, renderer='upfront:templates/opengames.mako', name='open')
 def open_games(context, request):
 @view_config(context=Games, renderer='upfront:templates/newgame.mako', permission='create-game', name='new')
 def new_game(context, request):
     if 'form.submitted' in request.params:
-        context.addOpenGame(request.user, request.params['nation'])
+        context.createGame(request.user, request.params['nation'])
         return HTTPFound(location = resource_url(context, request, 'open'))
 
     return dict(url=resource_url(context, request, 'new'))