示例#1
0
 def move(self, coor1, coor2, color=None):
     if self.model.ended:
         raise errors.EndGame
     game_over = None
     try:
         color = self.get_color(color)
         figure, move = self.game.move(color, coors2pos(coor1),
                                       coors2pos(coor2))
     except errors.EndGame as e:
         game_over, figure, move = e.reason, e.figure, e.move
     try:
         num = self.model.add_move(figure.symbol, move,
                                   str(self.game.board), game_over).number
     except Exception as e:
         logger.error(e)
         raise errors.BaseException
     if self.game.board._cut:
         self.model.cut += self.game.board._cut.symbol
         self.model.save()
     self.onMove()
     msg = self.get_info(invert_color(color))
     msg.update({'number': num})
     if game_over:
         self.send_ws(msg, consts.WS_LOSE, invert_color(color))
         return self.get_info()
     self.send_ws(msg, consts.WS_MOVE, invert_color(color))
     return self.get_info()
示例#2
0
 def get_info(self, color=None):
     color = self.get_color(color)
     if color == consts.WHITE:
         opponent = self.model.player_black
     else:
         opponent = self.model.player_white
     if self.model.ended:
         return {
             'board': BoardSerializer(self.game.board,
                                      consts.UNKNOWN).calc(),
             'started_at': self.model.date_created,
             'ended_at': self.model.date_end,
             'color': consts.COLORS[color],
             'opponent': opponent.username if opponent else 'anonymous',
             'winner': self.model.get_winner(),
         }
     return {
         'board': self.get_board(color),
         'time_left': self.time_left(color),
         'enemy_time_left': self.time_left(invert_color(color)),
         'started_at': self.model.date_created,
         'ended_at': self.model.date_end,
         'next_turn': consts.COLORS[self.game.current_player],
         'color': consts.COLORS[color],
         'opponent': opponent.username if opponent else 'anonymous',
     }
示例#3
0
 def load_game(cls, token):
     try:
         try:
             game_model = models.Game.get_game(token)
         except models.Game.DoesNotExist:
             data = get_cache('wait_{}'.format(token))
             if data:
                 raise errors.GameNotStartedError(*data)
             raise errors.GameNotFoundError
         game = cls(game_model.white, game_model.black)
         game.model = game_model
         game.game = engine.Game(game.model.state, game.model.next_color,
                                 game.model.cut)
         game._loaded_by = game_model._loaded_by
         if game.model.is_time_over():
             winner = game.model.winner
             loser = invert_color(winner)
             game.send_ws(game.get_info(loser), consts.WS_LOSE, loser)
             game.send_ws(game.get_info(winner), consts.WS_WIN, winner)
         if not game.model.ended:
             game.check_draw()
             game.check_castles()
     except errors.GameNotStartedError:
         raise
     except:
         raise errors.GameNotFoundError
     return game
示例#4
0
 def resign(self, color=None):
     if self.model.ended:
         raise errors.EndGame
     winner = invert_color(self.get_color(color))
     self.model.game_over(consts.END_RESIGN, winner=winner)
     self.send_ws(self.get_info(winner), consts.WS_WIN, winner)
     self.onMove()
     return self.get_info()
示例#5
0
 def draw_accept(self, color=None):
     if self.model.ended:
         raise errors.EndGame
     color = self.get_color(color)
     set_cache(self._get_draw_name(color), True)
     if self.check_draw():
         return self.get_info()
     self.send_ws('opponent offered draw', consts.WS_DRAW_REQUEST,
                  invert_color(color))
示例#6
0
 def is_time_over(self):
     if self.ended:
         return False
     if self.type_game == consts.TYPE_SLOW:
         if (datetime.now() -
                 self.date_state).total_seconds() > self.time_limit:
             self.game_over(consts.END_TIME,
                            self.date_state +
                            timedelta(seconds=self.time_limit),
                            winner=invert_color(self.next_color))
             return True
     elif self.type_game == consts.TYPE_FAST:
         moves = self.moves.select(
             Move.time_move).where(Move.color == self.next_color)
         time_spent = sum([m.time_move for m in moves])
         if time_spent + (datetime.now() - self.date_state
                          ).total_seconds() > self.time_limit:
             self.game_over(consts.END_TIME,
                            self.date_state +
                            timedelta(seconds=self.time_limit - time_spent),
                            winner=invert_color(self.next_color))
             return True
     return False
示例#7
0
 def add_move(self, figure, move, state, end_reason=None):
     with config.DB.atomic():
         color = self.next_color
         num = self.moves.select().count() + 1
         time_move = (datetime.now() - self.date_state).total_seconds()
         self.state = state
         self.next_color = invert_color(color)
         self.date_state = datetime.now()
         if end_reason:
             self.game_over(end_reason, save=False, winner=color)
         self.save()
         return Move.create(game=self,
                            number=num,
                            figure=figure,
                            move=move,
                            time_move=time_move,
                            color=color)
示例#8
0
 def move(self, color, pos1, pos2):
     if color != self.current_player:
         raise WrongTurnError
     figure = self.board.cell2Figure(*pos1)
     if not figure:
         raise NotFoundError
     if figure.color != color:
         raise WrongFigureError
     try:
         castled = isinstance(figure, King) and figure.try_to_castle(*pos2)
     except EndGame as exc:
         exc.figure = figure
         raise exc
     if not castled:
         result = figure, '{}-{}'.format(pos2coors(*pos1), pos2coors(*pos2))
         try:
             figure.move(*pos2)
         except EndGame as exc:
             exc.figure, exc.move = result
             raise exc
     else:
         result = figure, castled
     self.current_player = invert_color(self.current_player)
     return result
示例#9
0
 def test_invert_color(self):
     self.assertEqual(invert_color(WHITE), BLACK)
     self.assertEqual(invert_color(BLACK), WHITE)
示例#10
0
 def _func(a, b):
     invert_color(a)
     return 'ok'
示例#11
0
 def draw_refuse(self, color=None):
     if self.model.ended:
         raise errors.EndGame
     color = self.get_color
     delete_cache(self._get_draw_name(color))
     delete_cache(self._get_draw_name(invert_color(color)))