Пример #1
0
 def new_game( self, **kwargs ):
     name = kwargs[ 'name' ]
     game_doc = self.game_collection.find_one( { 'name' : name } )
     if game_doc:
         return { 'error' : 'A game by the name "%s" already exists.' % name }
     size = int( kwargs[ 'size' ] ) if 'size' in kwargs else 7
     go_game = GoGame( size )
     data = go_game.Serialize()
     game_doc = {
         'name' : name,
         'data' : data,
     }
     result = self.game_collection.insert_one( game_doc )
     return {}
Пример #2
0
 def take_turn( self, **kwargs ):
     name = kwargs[ 'name' ]
     game_doc = self.game_collection.find_one( { 'name' : name } )
     if not game_doc:
         return { 'error' : 'A game by the name "%s" was not found.' % name }
     color = kwargs[ 'color' ]
     if color == 'white':
         color = GoBoard.WHITE
     elif color == 'black':
         color = GoBoard.BLACK
     else:
         return { 'error' : 'Bogus color given.' }
     row = int( kwargs[ 'row' ] )
     col = int( kwargs[ 'col' ] )
     go_game = GoGame()
     go_game.Deserialize( game_doc[ 'data' ] )
     if go_game.whose_turn != color:
         return { 'error' : 'It is not yet your turn.' }
     move = None
     if row < 0 or col < 0 or go_game.CurrentBoard().GetState( ( row, col ) ) == GoBoard.EMPTY:
         try:
             go_game.PlaceStone( row, col )
         except Exception as ex:
             return { 'error' : str(ex) }
         move = { 'row' : row, 'col' : col }
         if 'respond' in kwargs and kwargs[ 'respond' ] == 'true':
             move = go_game.CalculateReasonableMove()
             go_game.PlaceStone( move[0], move[1] )
             move = { 'row' : move[0], 'col' : move[1] }
     elif go_game.CurrentBoard().GetState( ( row, col ) ) == color:
         try:
             go_game.RelinquishStone( row, col )
         except Exception as ex:
             return { 'error' : str(ex) }
     data = go_game.Serialize()
     update = { 'data' : data }
     if move:
         update[ 'most_recent_move' ] = move
     result = self.game_collection.update_one( { 'name' : name }, { '$set' : update } )
     if result.modified_count != 1:
         return { 'error' : 'Failed to update game in database.' }
     return {}