def create_demo_from_game(self, game_id): game = self.db.query(Game).get(game_id) if not game: raise ServiceError('game not found') room = Room(type='game') self.db.add(room) demo = Game(is_demo=True, is_ranked=False, room=room, board=game.board, komi=game.komi, stage='finished', black_display=game.black_display, white_display=game.white_display, demo_owner=self.user, demo_owner_display=self.user.display, demo_owner_rating=self.user.rating, demo_control=self.user, demo_control_display=self.user.display) self.db.add(demo) self.db.commit() return demo.id
def test_active_games(db): g1 = GameFactory(stage='playing') g2 = GameFactory(stage='playing') RoomUserFactory(room=g1.room, user=g1.black_user) RoomUserFactory(room=g1.room, user=g1.white_user) RoomUserFactory(room=g2.room, user=g2.black_user) RoomUserFactory(room=g2.room, user=g2.white_user) assert Game.active_games(db).count() == 2
def publish_status(self): if not self.user: return self.socket.publish( 'user_status', { 'id': self.user.id, 'rating': self.user.rating, 'is_online': self.user.is_online, 'wins': Game.count_wins(self.db, self.user) })
def game_from_node(node): game = Game(is_demo=True) game.board = Board(int(node.prop_one('SZ', '19'))) game.komi = float(node.prop_one('KM', 6.5)) game.result = node.prop_one('RE') game.black_display = node.prop_one('PB') game.white_display = node.prop_one('PW') game.title = node.prop_one('EV') _replay_game(game.board, node) if game.board.tree: game.board.current_node_id = 0 return game
def _create_game(self, ranked, correspondence, black, white, handicap, komi, size, timing_system, capped, maintime, overtime, overtime_count, private): board = Board(size, handicap) room = Room(type='game') ru = RoomUser(room=room, user=black) ru2 = RoomUser(room=room, user=white) game = Game(room=room, is_demo=False, is_ranked=ranked, is_correspondence=correspondence, is_private=private, board=board, stage='playing', komi=komi, black_user=black, black_display=black.display, black_rating=black.rating, white_user=white, white_display=white.display, white_rating=white.rating) start_at = datetime.utcnow() + settings.GAME_START_DELAY if timing_system == 'fischer': overtime_count = 0 timing = Timing(game=game, system=timing_system, start_at=start_at, timing_updated_at=start_at, next_move_at=start_at, capped=capped, main=maintime, overtime=overtime, overtime_count=overtime_count, black_main=maintime, black_overtime=overtime * overtime_count, white_main=maintime, white_overtime=overtime * overtime_count) self.db.add(room) self.db.add(ru) self.db.add(ru2) self.db.add(game) self.db.add(timing) return game
def test_game_to_sgf_basic(): game = Game(board=Board(19), black_display='black', white_display='white', created_at=datetime(2016, 5, 7), result='B+1.5', komi=7.5) game.board.play(coord2d(4, 4, 19)) game.board.play(coord2d(16, 17, 19)) sgf = game_to_sgf(game) expected = '(;SO[weiqi.gs]FF[4]DT[2016-05-07]PW[white]PB[black]KM[7.5]SZ[19]RE[B+1.5];B[dd];W[pq])' assert sgf.replace('\n', '') == expected
def test_winner_loser(): black = UserFactory() white = UserFactory() tests = [ ['B+1.5', (black, white)], ['W+1.5', (white, black)], ['B+R', (black, white)], ['W+R', (white, black)], ['B+T', (black, white)], ['W+T', (white, black)], ] for t in tests: game = Game(black_user=black, white_user=white, result=t[0]) assert game.winner_loser == t[1]
def create_demo(self, title, size): room = Room(type='game') self.db.add(room) game = Game(is_demo=True, is_ranked=False, room=room, title=title, board=Board(int(size)), komi=settings.DEFAULT_KOMI, stage='finished', demo_owner=self.user, demo_owner_display=self.user.display, demo_owner_rating=self.user.rating, demo_control=self.user, demo_control_display=self.user.display) self.db.add(game) self.db.commit() return game.id
def _connection_data(self): data = {} if self.user: data.update({ 'user_id': self.user.id, 'user_display': self.user.display, 'rating': self.user.rating, 'wins': Game.count_wins(self.db, self.user), 'automatch': self.db.query(Automatch).filter_by(user=self.user).count() > 0, }) data.update(self._connection_data_rooms()) data.update(self._connection_data_games()) data.update(self._connection_data_direct_rooms()) data.update(self._connection_data_active_games()) data.update(self._connection_data_challenges()) return data
def test_active_games_demo_not_in_room(db): DemoGameFactory(demo_owner__is_online=False) assert Game.active_games(db).count() == 0
def test_active_games_demo_offline(db): demo = DemoGameFactory(demo_owner__is_online=False) RoomUserFactory(room=demo.room, user=demo.demo_owner) assert Game.active_games(db).count() == 0
def _connection_data_active_games(self): return { 'active_games': [g.to_frontend() for g in Game.active_games(self.db)] }