Ejemplo n.º 1
0
    def reload_tiles(self, current_player, board_model, board):

        player_board = PlayerBoardService().get(board=board_model, player=current_player)
        tiles_count = player_board.tiles.count()

        if tiles_count < SCRABBLE_RULES["max_tiles_per_player"]:

            letters = board.get_random_tiles(SCRABBLE_RULES["max_tiles_per_player"] - tiles_count)

            for letter in letters:
                tile = TileService().new(letter=letter, x=0, y=0)
                tile.save()
                player_board.tiles.add(tile)

            player_board.save()

        return player_board
Ejemplo n.º 2
0
    def play(self, board_model, tiles, current_player):

        if board_model.turn.id != current_player.id:
            return {"status": "error", "message": "Is not your turn!" }

        board, player_board = self.new(board_model, current_player)

        matrix = dict([((tile.x, tile.y), Tile(tile.letter, (tile.x, tile.y))) for tile in board_model.tiles.all()])
        board.matrix.update(matrix)

        word_tiles = []
        tiles_model = []

        for tile in tiles:
                    
            tile_model = TileService().get(id=tile["id"])
            tiles_model.append(tile_model)
            word_tiles.append(Tile(tile_model.letter, (int(tile["x"]), int(tile["y"]))))
        
        word = Word(word_tiles)

        try:
            points = board.play(word)

            total_points = PlayerBoardService().update_board_state(board_model, points, current_player)
            board_model = BoardService().save(board_model, word, current_player)

            for tile, tile_model in zip(tiles, tiles_model):
                tile["letter"] = tile_model.letter
                tile_model.delete()

            RealTimeService().publish('play', tiles=tiles, player=current_player.remote_id, points=total_points)

            return {"status": "ok", "points": total_points, "next_player": board_model.turn.user.username}

        except InvalidPlayError, e:

            return {"status": "error", "message": unicode(e) }