def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player2 = Center(1, "Dwight", "Howard", 210, 90, 2002, "center", 1054, "Aggresive") self.player_manager.add_player(self.player2) self.assertIsNotNone(self.player_manager)
def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.player_manager.add_player(self.player1) self.assertIsNotNone(self.player_manager)
def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player1 = Guard(1, "Rajon", "Rondo", 190, 76, 2004, "guard", 909, 1203) self.player_manager.add_player(self.player1) self.assertIsNotNone(self.player_manager)
def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, 1028, 690) self.player2 = Center(2, "Dwight", "Howard", 210, 90, 2002, 1054, "Aggresive") self.player3 = Guard(3, "Rajon", "Rondo", 190, 76, 2004, 909, 1203) self.player_manager.add_player(self.player1) self.player_manager.add_player(self.player2) self.player_manager.add_player(self.player3) self.assertIsNotNone(self.player_manager)
def __init__(self, size, name, player_positions) -> None: self.id: str = str(uuid.uuid1()) self.name: str = name self.instances[self.id] = self players = [Player(str(i), row, col) for i, (row, col) in enumerate(player_positions, 1)] self.game: WallGame = WallGame(size, players) self.manager = PlayerManager(self.game.players) self.status: RoomStatus = RoomStatus.waiting self.players_initial_poses: Dict[str, Tuple] = {player: (player.row, player.col) for player in self.manager.players} if len(self.players_initial_poses) != len(self.manager.players): raise ValueError('duplicated player_positions') self.task = None
class TestCenter(TestCase): """ Unit tests for the Center class """ def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player2 = Center(1, "Dwight", "Howard", 210, 90, 2002, "center", 1054, "Aggresive") self.player_manager.add_player(self.player2) self.assertIsNotNone(self.player_manager) def tearDown(self): """ Destroys data and sets up logPoint """ self.logPoint() def logPoint(self): currentTest = self.id().split('.')[-1] callingFunction = inspect.stack()[1][3] print('in %s - %s()' % (currentTest, callingFunction)) def test_constructor_valid_input(self): """ Tests the constructor with valid inputs """ self.assertEqual(1, self.player2.get_player_id(), "Player number should be number 1") self.assertEqual("Los Angeles Lakers", self.player_manager.get_team_name(), "Team Name should be Los Angeles Lakers") def test_constructor_invalid_input(self): """ Tests the constructor with invalid inputs """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Center, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of rebounds should be positive", Center, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Play-type should be a string", Center, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) def test_get_description(self): """ Tests the get_description method """ string = "1: Dwight Howard is 210.00 cm tall, weighs 90.00 kg, drafted on 2002, has 1054 rebounds and plays Aggresive" self.assertEqual(string, self.player2.get_description(), "These two strings should be equal") def test_get_num_rebounds(self): """ Tests the get_num_rebounds method """ self.assertEqual(1054, self.player2.get_num_rebounds(), "These two values should be equal") def test_get_play_type(self): """ Tests the get_play_type method """ self.assertEqual("Aggresive", self.player2.get_play_type(), "These two strings should be equal") def test_get_type(self): """ Tests the get_type method """ self.assertEqual("CENTER", self.player2.get_type(), "These two strings should be equal")
def setUp(self): engine = create_engine('sqlite:///test_players.sqlite') # Create all the tables Base.metadata.create_all(engine) Base.metadata.bind = engine """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("test_players.sqlite") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.player2 = Center(2, "Dwight", "Howard", 210, 90, 2002, "guard", 1054, "Aggresive") self.player3 = Guard(3, "Rajon", "Rondo", 190, 76, 2004, "center", 909, 1203) self.player_manager.add_player(self.player1) self.player_manager.add_player(self.player2) self.player_manager.add_player(self.player3) self.assertIsNotNone(self.player_manager)
def test_constructor(): """Test the PlayerManager __init__ function""" gc = GameController(WIDTH, HEIGHT) board = Board(GRID_COL_ROW_COUNT, CELL_W_H, WIDTH, HEIGHT, gc) pm = PlayerManager(board, gc, MORE_DIFFICULT) assert pm.board is board assert pm.gc is gc assert pm.more_difficult is True assert pm.player_turn == HUMAN assert pm.player_turn != COMPUTER assert len(pm.legal_moves) == 4
def test_humanMove(): """Test the PlayerManager humanMove() function""" gc = GameController(WIDTH, HEIGHT) board = Board(GRID_COL_ROW_COUNT, CELL_W_H, WIDTH, HEIGHT, gc) pm = PlayerManager(board, gc, MORE_DIFFICULT) pm.humanMove(15, 15) assert board.grid[(1, 1)].piece is None pm.humanMove(200, 300) assert board.grid[(3, 3)].piece is None assert board.grid[(2, 3)].piece is None assert board.grid[(3, 4)].piece is not None assert board.grid[(3, 4)].piece.player == HUMAN
def test_computerMove(): """Test the PlayerManager computerMove() function""" gc = GameController(WIDTH, HEIGHT) board = Board(GRID_COL_ROW_COUNT, CELL_W_H, WIDTH, HEIGHT, gc) pm = PlayerManager(board, gc, MORE_DIFFICULT) assert gc.computer_score == 0 pm.humanMove(200, 300) assert gc.computer_score == 1 assert gc.human_score == 4 pm.computerMove() assert gc.computer_score == 3 assert gc.human_score == 3
def test_switchPlayer(): """Test the PlayerManager switchPlayer() function""" gc = GameController(WIDTH, HEIGHT) board = Board(GRID_COL_ROW_COUNT, CELL_W_H, WIDTH, HEIGHT, gc) pm = PlayerManager(board, gc, MORE_DIFFICULT) assert pm.player_turn == HUMAN assert pm.player_turn != COMPUTER pm.switchPlayer() assert pm.player_turn == COMPUTER assert pm.player_turn != HUMAN pm.switchPlayer() assert pm.player_turn != COMPUTER assert pm.player_turn == HUMAN
def main(): player_manager = PlayerManager("Los Angeles Lakers", "file.txt") player1 = Forward(23, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) player2 = Center(12, "Dwight", "Howard", 210, 90, 2002, "center", 1054, "Aggresive") player3 = Guard(10, "Rajon", "Rondo", 190, 76, 2004, "guard", 909, 1203) player_manager.add_player(player1) player_manager.add_player(player2) player_manager.add_player(player3) print_report(player_manager) player_manager.delete_player(12) player_manager.delete_player(10) print_report(player_manager) player1 = Forward(23, "Yeet", "James", 69, 81, 2003, "forward", 1028, 690) player_manager.update_player(player1) print_report(player_manager) print(player_manager.get_player(23))
class TestPlayer(TestCase): """ Unit tests for the Player Class """ def setUp(self): engine = create_engine('sqlite:///test_players.sqlite') # Create all the tables Base.metadata.create_all(engine) Base.metadata.bind = engine """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("test_players.sqlite") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.player2 = Center(2, "Dwight", "Howard", 210, 90, 2002, "guard", 1054, "Aggresive") self.player3 = Guard(3, "Rajon", "Rondo", 190, 76, 2004, "center", 909, 1203) self.player_manager.add_player(self.player1) self.player_manager.add_player(self.player2) self.player_manager.add_player(self.player3) self.assertIsNotNone(self.player_manager) def tearDown(self): """ Destroys data and sets up logPoint """ os.remove("test_players.sqlite") self.logPoint() def logPoint(self): currentTest = self.id().split('.')[-1] callingFunction = inspect.stack()[1][3] print('in %s - %s()' % (currentTest, callingFunction)) def test_constructor_valid_input(self): """ Tests the constructor with valid inputs """ self.assertEqual(1, self.player1.get_player_id(), "Player number should be number 1") self.assertEqual(2, self.player2.get_player_id(), "Player number should be number 2") self.assertEqual(3, self.player3.get_player_id(), "Player number should be number 3") self.assertEqual("Los Angeles Lakers", self.player_manager.get_team_name(), "Team Name should be Los Angeles Lakers") def test_constructor_invalid_input(self): """ Tests the constructor with invalid inputs """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Forward, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of shots took should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of shots made should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Guard, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of steals made should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of assists should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Center, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of rebounds should be positive", Center, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Play-type should be a string", Center, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) def test_get_player(self): """ Tests the get_player method """ player23 = self.player_manager.get_player(3) self.assertEqual(3, player23.get_player_id(), "Player should be number 23") def test_get_all(self): """ Tests the get_all method """ self.assertEqual(3, len(self.player_manager.get_all()), "Team should have 3 players") def test_get_player_stats(self): """ Tests the get_player_stats method """ stats = self.player_manager.get_players_stats() self.assertEqual(3, stats.get_total_num_players(), "Team should have 3 players") self.assertEqual(1, stats.get_num_guards(), "Team should have 1 guard") self.assertEqual(1, stats.get_num_forwards(), "Team should have 1 forward") self.assertEqual(1, stats.get_num_centers(), "Team should have 1 center") def test_add_player_valid_input(self): """ Tests the add_player method with valid inputs """ self.assertEqual( 3, self.player_manager.get_players_stats().get_total_num_players(), "Team should have 3 players") player4 = Guard(7, "June", "Ka", 190, 76, 2004, 909, 1203) self.player_manager.add_player(player4) self.assertEqual( 4, self.player_manager.get_players_stats().get_total_num_players(), "Team should have 4 players") def test_add_player_invalid_input(self): """ Tests the add_player method with invalid inputs """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Forward, "STRING", "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.assertRaisesRegex(ValueError, "Number of shots took should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, "forward", -1028, 690) self.assertRaisesRegex(ValueError, "Number of shots made should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, "forward", 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Guard, "STRING", "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.assertRaisesRegex(ValueError, "Number of steals made should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, "forward", -1028, 690) self.assertRaisesRegex(ValueError, "Number of assists should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, "forward", 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Center, "STRING", "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.assertRaisesRegex(ValueError, "Number of rebounds should be positive", Center, 12, "Lebron", "James", 201, 81, 2003, "forward", -1028, 690) self.assertRaisesRegex(ValueError, "Play-type should be a string", Center, 12, "Lebron", "James", 201, 81, 2003, "forward", 1028, -690) def test_delete_player_valid_input(self): """ Tests the delete_player with valid inputs """ self.assertEqual( 3, self.player_manager.get_player_stats().get_total_num_players(), "Team should have 3 players") player4 = Guard(4, "June", "Ka", 190, 76, 2004, 909, 1203) self.player_manager.add_player(player4) self.assertEqual( 4, self.player_manager.get_player_stats().get_total_num_players(), "Team should have 4 players") self.player_manager.delete_player(4) self.assertEqual( 3, self.player_manager.get_player_stats().get_total_num_players(), "Team should have 3 players") def test_delete_player_invalid_input(self): """ Tests the delete_player with invalid inputs """ self.assertEqual("Player ID should be an integer value", self.player_manager.delete_player("STRING"), "Input should be an integer value") def test_get_team_name(self): """ Tests the get_team_name method """ self.assertEqual("Los Angeles Lakers", self.player_manager.get_team_name(), "Team name should be Los Angeles Lakers") def test_update_valid_input(self): """ Tests the update method with valid inputs """ string = "3: Rajon Rondo is 190.00 cm tall, weighs 76.00 kg, drafted on 2004, has 909 steals and 1203 assists" self.assertEqual(string, self.player3.get_description(), "These two strings should be equal") self.player3 = Guard(3, "June", "Ka", 180, 81, 2019, 0, 0) self.player_manager.update_player(self.player3) string2 = "3: June Ka is 180.00 cm tall, weighs 81.00 kg, drafted on 2019, has 0 steals and 0 assists" self.assertEqual(string2, self.player3.get_description(), "These two strings should be equal") def test_get_all_by_type(self): """ Tests get_all_by_type method """ string = self.player_manager.get_all_by_type() self.assertEqual(string, self.player_manager.get_all_by_type(), "These two strings should be equal") def test_update_invalid_input(self): """ Tests update method with invalid inputs """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Forward, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of shots took should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of shots made should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Guard, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of steals made should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of assists should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) self.assertRaisesRegex(ValueError, "Player number should be an integer value", Center, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of rebounds should be positive", Center, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Play-type should be a string", Center, 12, "Lebron", "James", 201, 81, 2003, 1028, -690)
def main(): pm = PlayerManager() # player manager print("Welcome to our player-manager...") pm.read_players() while True: choice = input( "Do you wish to see the current database of players? (Y/N) " ).upper() if choice == "Y" or choice == "YES": pm.print_players() choice = input("Do you wish to add a player? (Y/N) ").upper() if choice != "N" and choice != "NO": pm.add_player() choice = input("Are you done adding players? (Y/N) ").upper() if choice == "Y" or choice == "YES": break pm.print_players() choice = input("Do you wish to save the current database? (Y/N) ").upper() if choice == "Y" or choice == "YES": pm.save_players()
from deuces import Deck from game import Game from hand import Hand from player import Player from player_command import RaiseCommand, FoldCommand, CallCommand, CheckCommand, NextGameCommand from player_manager import PlayerManager from game_session import GameSession app = Flask(__name__) config.load_config(app) player_names = ["alfred", "betty", "chris", "dee"] players = [ Player(handle=name, hand=Hand([]), id=uuid.uuid4()) for name in player_names ] player_manager = PlayerManager(players) print(players) game = Game(players, Deck(), little_ante=10, big_ante=20) game_session = GameSession(game=game) command_evaluator = CommandEvaluator(game_session) game_session.current_round.deal_cards_1_2() service = game_session.current_round app.secret_key = config.SECRET @app.route("/god_mode") def friendly_god_game_view(): return render_template('root_template.html', session=game_session)
class TestCenter(TestCase): """ Unit tests for the Center class """ def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.player_manager.add_player(self.player1) self.assertIsNotNone(self.player_manager) def tearDown(self): """ Destroys data and sets up logPoint """ self.logPoint() def logPoint(self): currentTest = self.id().split('.')[-1] callingFunction = inspect.stack()[1][3] print('in %s - %s()' % (currentTest, callingFunction)) def test_constructor_valid_input(self): """ Tests the constructor with valid inputs """ self.assertEqual(1, self.player1.get_player_id(), "Player number should be number 1") self.assertEqual("Los Angeles Lakers", self.player_manager.get_team_name(), "Team Name should be Los Angeles Lakers") def test_constructor_invalid_input(self): """ Tests the constructor with invalid inputs """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Forward, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of shots took should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of shots made should be positive", Forward, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) def test_get_description(self): """ Tests the get_description method """ string = "1: Lebron James is 201.00 cm tall, weighs 81.00 kg, drafted on 2003, took 1028 shots and made 690" self.assertEqual(string, self.player1.get_description(), "These two strings should be equal") def test_get_num_shots_took(self): """ Tests the get_num_shots_took method """ self.assertEqual(1028, self.player1.get_num_shots_took(), "These two values should be equal") def test_get_num_shots_made(self): """ Tests thge get_num_shots_made method """ self.assertEqual(690, self.player1.get_num_shots_made(), "These two values should be equal") def test_get_type(self): """ Tests the get_type method """ self.assertEqual("FORWARD", self.player1.get_type(), "These two strings should be equal")
import time import threading from gameboard import Gameboard from terminal_renderer import TerminalRenderer from keyboard_input import KeyboardInput from player_manager import PlayerManager from game_manager import GameManager gboard = Gameboard() renderer = TerminalRenderer(gboard) inputer = KeyboardInput() player = PlayerManager(gboard) game = GameManager(gboard) inputer.start() game.start() def game_loop(): for i in range(25): nput = inputer.get_input() player.update(nput) game.update() renderer.render() time.sleep(.05) # TODO: Calculate exact sleep time thread = threading.Thread(target=game_loop) thread.start() thread.join()
class TestForward(TestCase): """ Unit tests for the Center class """ def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player1 = Guard(1, "Rajon", "Rondo", 190, 76, 2004, "guard", 909, 1203) self.player_manager.add_player(self.player1) self.assertIsNotNone(self.player_manager) def tearDown(self): """ Destroys data and sets up logPoint """ self.logPoint() def logPoint(self): currentTest = self.id().split('.')[-1] callingFunction = inspect.stack()[1][3] print('in %s - %s()' % (currentTest, callingFunction)) def test_constructor_valid_input(self): """ Tests for constructor with valid input """ self.assertEqual(1, self.player1.get_player_id(), "Player number should be number 1") self.assertEqual("Los Angeles Lakers", self.player_manager.get_team_name(), "Team Name should be Los Angeles Lakers") def test_constructor_invalid_input(self): """ Tests for constructor with invalid input """ self.assertRaisesRegex(ValueError, "Player number should be an integer value", Guard, "STRING", "Lebron", "James", 201, 81, 2003, 1028, 690) self.assertRaisesRegex(ValueError, "Number of steals made should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, -1028, 690) self.assertRaisesRegex(ValueError, "Number of assists should be positive", Guard, 12, "Lebron", "James", 201, 81, 2003, 1028, -690) def test_get_description(self): """ Tests the get_description method """ string = "1: Rajon Rondo is 190.00 cm tall, weighs 76.00 kg, drafted on 2004, has 909 steals and 1203 assists" self.assertEqual(string, self.player1.get_description(), "These two strings should be equal") def test_get_num_steals(self): """ Tests the get_num_steals method """ self.assertEqual(909, self.player1.get_num_steals(), "These two values should be equal") def test_get_num_assists(self): """ Tests the get_num_assists method """ self.assertEqual(1203, self.player1.get_num_assists(), "These two values should be equal") def test_get_type(self): """ Tests the get_type method """ self.assertEqual("GUARD", self.player1.get_type(), "These two strings should be equal")
from flask import Flask, request from guard import Guard from forward import Forward from center import Center from player_stats import PlayerStats from player_manager import PlayerManager import json app = Flask(__name__) player_manager = PlayerManager("players.sqlite") @app.route('/playermanager/players', methods=['POST']) def add_player(): """ Adds a player to the player manager """ content = request.json try: if content["player_type"] == "center": center = Center(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_rebounds"], content["play_type"]) player_manager.add_player(center) elif content["player_type"] == "forward": forward = Forward(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"],
class GameRoom: instances: 'Dict[str, GameRoom]' = {} @property def players(self): return self.manager.players def __init__(self, size, name, player_positions) -> None: self.id: str = str(uuid.uuid1()) self.name: str = name self.instances[self.id] = self players = [Player(str(i), row, col) for i, (row, col) in enumerate(player_positions, 1)] self.game: WallGame = WallGame(size, players) self.manager = PlayerManager(self.game.players) self.status: RoomStatus = RoomStatus.waiting self.players_initial_poses: Dict[str, Tuple] = {player: (player.row, player.col) for player in self.manager.players} if len(self.players_initial_poses) != len(self.manager.players): raise ValueError('duplicated player_positions') self.task = None async def register_player(self, sid: str, player: Player, ws: WebSocketResponse) -> Queue: queue = self.manager.register_player(sid, player, ws) await self.manager.send_to(player, { 'event': Event.joined, 'player': player.symbol }) await self.manager.send_to_everyone({ 'event': Event.new_player, 'player': player }) if len(self.manager.unregistered_players) == 0: await self.start_game() return queue async def start_game(self): await self.manager.send_to_everyone({'event': Event.game_start}) self.task = create_task(self.game_loop()) async def reconnect(self, user: str, ws: WebSocketResponse): await self.manager.reconnect(user, ws) player = self.manager.users_players[user] await self.manager.send_to(user, { 'event': Event.reconnected, 'player': player, 'pos': [player.row, player.col], 'status': self.status.value }) if self.status != RoomStatus.finished: await self.manager.send_to(user, self.update_game_map_message()) await self.manager.resend_messages(user) async def game_loop(self): self.status = RoomStatus.running loop = self.game.game_loop() reply = None try: while True: event, *args = loop.send(reply) if event is Event.ask_player_action: player, msg = args data = await self.manager.ask(player, { 'event': event, 'msg': msg, 'reachable_points': [ [row, col] for row, col in self.game.get_reachable_points(player) ] }) reply = data['motions'], Direction[data['wall_dir']] elif event is Event.update_game_map: await self.manager.send_to_everyone(self.update_game_map_message()) elif event is Event.player_out: player, score = args await self.manager.send_to_everyone({ 'event': event, 'player': player.symbol, 'score': score }) except StopIteration as exc: self.status = RoomStatus.finished data = [[f'{self.manager.players_users[player]}({player.symbol})', score] for player, score in exc.value.items()] data.sort(key=lambda item: item[1], reverse=True) # 发送游戏结果并询问是否重新开始 async for user, reply in self.manager.ask_everyone({ 'event': Event.game_over, 'result': data}): if reply.get('agree'): await self.manager.send_to_everyone({'event': Event.agreed_restarting, 'user': user}) else: await self.manager.send_to_everyone({'event': 'error', 'message': f'由于{user}拒绝重新开始游戏,游戏房间将被销毁'}) del self.instances[self.id] return # 重新开始 for player, (row, col) in self.players_initial_poses.items(): player.row = row player.col = col self.game.__init__(self.game.size, self.game.players) await self.start_game() def update_game_map_message(self): wall_top = [''.join('1' if char else '0' for char in row) for row in self.game.wall_top] wall_left = [''.join('1' if char else '0' for char in row) for row in self.game.wall_left] players_info = [(player.row, player.col, player.symbol) for player in self.game.players] return { 'event': Event.update_game_map, 'wall_top': wall_top, 'wall_left': wall_left, 'players_info': players_info }
from trueskill import Rating, rate_1vs1 from player_manager import PlayerManager from constants import BeltColor as belt player_exec = PlayerManager() def create_test_players(): p1_rating = Rating() p2_rating = Rating() p3_rating = Rating() p4_rating = Rating() p5_rating = Rating() player_exec.create_player('Saad', 145, belt.BLUE, p1_rating.mu, p1_rating.sigma) player_exec.create_player('Joseph', 180, belt.WHITE, p2_rating.mu, p2_rating.sigma) player_exec.create_player('Brandon', 145, belt.PURPLE, p3_rating.mu, p3_rating.sigma) player_exec.create_player('Omar', 145, belt.WHITE, p4_rating.mu, p4_rating.sigma) player_exec.create_player('Matt', 145, belt.BLUE, p5_rating.mu, p5_rating.sigma) def create_match(): # create player tuples and corresponding rating objects player_joseph = player_exec.get_player(2) player_omar = player_exec.get_player(4) joseph_rating = Rating(player_joseph[2], player_joseph[3]) omar_rating = Rating(player_omar[2], player_omar[3])
from flask import Flask, request from guard import Guard from forward import Forward from center import Center from player_stats import PlayerStats from player_manager import PlayerManager import json app = Flask(__name__) player_manager = PlayerManager("Los Angeles Lakers", "file.txt") @app.route('/playermanager/players', methods=['POST']) def add_player(): """ Adds a player to the player manager """ content = request.json try: if content["player_type"] == "center": center = Center(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_rebounds"], content["play_type"]) player_manager.add_player(center) elif content["player_type"] == "forward": forward = Forward(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"],