Commits

Chris Beelby  committed 6411337

Working on adding actual game functionality

  • Participants
  • Parent commits c2837cc

Comments (0)

Files changed (5)

File upfront/models.py

     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')]
 
     def __init__(self):
+        self.games = IOBTree()
         self.openGames = IOBTree()
 
     def __repr__(self):
         return 'Games()'
 
+    def __getitem__(self, value):
+        try:
+            value = int(value)
+
+            if value in self.games:
+                game = self.games[value]
+                game.__name__ = '%s' % (value)
+                game.__parent__ = self
+                return game
+        except ValueError:
+            raise KeyError
+
     def addOpenGame(self, player1, player1nation):
         game = OpenGame(player1, player1nation)
-        try:
-            id = self.openGames.maxKey() + 1
-        except ValueError:
-            id = 1
-        self.openGames[id] = game
-        
+        while True:
+            try:
+                id = self.games.maxKey() + 1
+            except ValueError:
+                id = 1
+            if self.games.insert(id, game):
+                player1.games[id] = game 
+                self.openGames[id] = game
+                break
+        return id
 
 class Users(PersistentMapping):
     __name__ = 'users'
         self.name = name
         self.password = password
         self.__name__ = name.lower()
+        self.games = IOBTree()
 
     def __repr__(self):
         return 'User(%s, %s)' % (repr(self.name), repr(self.password))

File upfront/templates/base.mako

         <ul class="sidemenu">
         <li><a href="${request.application_url + '/games/new'}" class="top">New Game</a></li> 
         <li><a href="${request.application_url + '/users/' + request.user.name.lower() + '/games'}">My Games</a></li>
+        <li><a href="${request.application_url + '/games/open'}">Open Games</a></li>
         <li><a href="${request.application_url + '/users/' + request.user.name.lower()}">Edit Profile</a></li>
         </ul>
         </div>

File upfront/templates/opengames.mako

+<%inherit file="base.mako"/>
+
+<table>
+<tr>
+    <th>Opponent</th>
+    <th>Playing As</th>
+    <th colspan="2">Join As</th>
+</tr>
+<% i = 'a' %>
+% for id, game in games.iteritems():
+<tr class="row-${i}">
+    <td>${game.player1.name}</td>
+    <td>${game.player1nation.capitalize()}</td>
+    % if game.player1 == request.user:
+    <td colspan=2>N/A</td>
+    % elif game.player1nation == 'america':
+    <td><a href="/games/${id}/join/germany">Germany</a></td>
+    <td><a href="/games/${id}/join/russia">Russia</a></td>
+    % else:
+    <td colspan=2><a href="/games/${id}/join/america">America</a></td>
+    % endif
+</tr>
+<%
+if i == 'a':
+    i = 'b'
+else:
+    i = 'a'
+%>
+% endfor
+</table>

File upfront/templates/users/games.mako

+<%inherit file="../base.mako"/>
+
+<h1>My Games</h1>
+
+<h2>Open Games</h2>
+
+<table>
+<tr>
+    <th>Playing As</th>
+    <th>&nbsp;</th>
+</tr>
+<% i = 'a' %>
+% for id, game in opengames.iteritems():
+<tr class="row-${i}">
+    <td>${game.player1nation.capitalize()}</td>
+    <td><a href="/games/${id}/delete">Delete</a></td>
+</tr>
+<%
+if i == 'a':
+    i = 'b'
+else:
+    i = 'a'
+%>
+% endfor
+</table>
+
+<h2>Games In Progress</h2>
+
+<h2>Complete Games</h2>

File upfront/views.py

 from pyramid.url import resource_url
 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=User, renderer='upfront:templates/users/games.mako', name='games')
+def my_games(context, request):
+    opengames = dict()
+    games = dict()
+    finished = dict()
+    for id, game in context.games.iteritems():
+        if isinstance(game, OpenGame):
+            opengames[id] = game
+        elif game.state == 'finished':
+            finished[id] = game
+        else:
+            games[id] = game
+    return dict(opengames=opengames, games=games, finished=finished)
+
+@view_config(context=OpenGame, renderer='upfront:templates/game.mako', name='join')
+def join_game(context, request):
+    return dict(nation=request.subpath[0]) 
+
+@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]
+
+    del context.player1.games[id]
+    del context.__parent__.games[id]        
+    return HTTPFound(location=resource_url(context.__parent__, request, 'open'))
+
+
+@view_config(context=Games, renderer='upfront:templates/opengames.mako', name='open')
+def open_games(context, request):
+    games = request.root['games'].openGames
+    return dict(games=games)
 
 @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'])
-        return HTTPFound(location = resource_url(context, request, ''))
+        return HTTPFound(location = resource_url(context, request, 'open'))
 
-    return dict(url = resource_url(context, request, 'new'))
+    return dict(url=resource_url(context, request, 'new'))
 
 @view_config(context=Root, renderer='upfront:templates/index.mako')
 @view_config(context=HTTPNotFound, renderer='upfront:templates/index.mako')
 
     if isinstance(context, HTTPNotFound):
         message = 'AWOL - The page you requested is missing'
-    return {'project':'upfront', 'section': 'home', 'message': message}
+    return dict(section='home', message=message)
 
 @view_config(context=User, renderer='upfront:templates/users/user.mako', permission='view-user-details')
 def user_view(context, request):
-    return {'user': context} 
+    return dict(user=context)
 
 @view_config(context=User, renderer='upfront:templates/users/delete.mako', name='delete',
     permission='delete-user')
 
 @view_config(context=Users, renderer='upfront:templates/users/list.mako', permission='view-user-details')
 def user_list(context, request):
-    return {'users': context}
+    return dict(users=context)
 
 @view_config(context=Users, renderer='upfront:templates/users/create.mako', name='create')
 def create_user(context, request):