def test_play_roll_dice_3(monkeypatch): # Given board = Board.create(players=[1, 3]) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 3) game = GameEngine(board, dice) # When new_state = game.play(GameMove.roll_dice(player=1)) # Then assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 3 assert new_state.valid_actions == [GameMove.roll_dice(player=3)] # And When new_state = game.play(GameMove.roll_dice(player=3)) # Then assert new_state == game.get_state() assert new_state.number == 2 assert new_state.dice == 3 assert new_state.valid_actions == [GameMove.roll_dice(player=1)]
def test_do_not_move_piece_to_end_on_bigger_dice(monkeypatch): # Given we have started the game and a piece is in the safe zone board = Board.create(players=[0, 2], pieces_per_player=1) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 5) game = GameEngine(board, dice) state.board.pieces[0].progress = board.end_progress - 3 # When we roll the dice with 5 which is bigger then we need to # get to the goal new_state = game.play(GameMove.roll_dice(player=0)) # Then the piece should not go to the goal and it should be the # next player turn assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 5 assert new_state.valid_actions == [ GameMove.roll_dice(player=2), ] assert new_state.board.pieces == [ Piece(number=0, player=0, progress=board.end_progress - 3), Piece(number=0, player=2, progress=0), ] assert new_state.winners == []
def application(environ, start_response): try: if environ['REQUEST_METHOD'] == 'POST': request_body_size = int(environ.get('CONTENT_LENGTH', '0')) request_body = environ['wsgi.input'].read(request_body_size) data = json.loads(request_body.decode()) WORLDS = {} world = data['world'] if world not in WORLDS: with open('public/' + world + '/OBJECTS.json') as f: objects = json.load(f) with open('public/' + world + '/ROOMS.json') as f: rooms = json.load(f) WORLDS['world'] = [objects, rooms] world = WORLDS['world'] game = GameEngine(world[0], world[1]) game.set_state(data['state']) cmd = game.decode_button_command(data['user_command']) prs = game.process_command(cmd) audio_prs = game.prompts_only(prs) text_prs = game.text_only(prs) ndata = { 'text': text_prs, 'audio': audio_prs, 'state': game.get_state() } start_response('200 OK', [('Content-Type', 'application/json')]) return [json.dumps(ndata).encode()] else: # If we are running alongside a web server then the server will handle static files. # This is for the stand-alone case (like running on the Farmer Says) fn = environ['PATH_INFO'] if fn == '/': fn = '/index.html' print('*', fn, '*') with open('public' + fn, 'rb') as f: data = f.read() if fn.endswith('.json'): start_response('200 OK', [('Content-Type', 'application/json')]) else: start_response('200 OK', [('Content-Type', 'text/html')]) return [data] except Exception: with open('ex.txt', 'w') as f: f.write(traceback.format_exc()) raise
def test_roll(monkeypatch, client): # Given 4 players had joined in the previous tests and the game had starte # When we try to play with the correct user token rv = client.get("/play/roll", headers={"4oBe4e-user-token": player1_token}) game_state = json.loads(rv.data) board = Board.create(players=[0, 1, 2, 3]) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: game_state["dice"]) game = GameEngine(board, dice) game.play(GameMove.roll_dice(player=0)) # Then we want the same state as the default for 4 players assert game_state == dataclasses.asdict(game.get_state())
def test_play_until_the_end_two_players_once_piece(monkeypatch): # Given we have started the game board = Board.create(players=[0, 2], pieces_per_player=1) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 6) game = GameEngine(board, dice) # When we roll the dice new_state = game.play(GameMove.roll_dice(player=0)) # Then the state should be as expected assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 6 assert new_state.valid_actions == [ GameMove.piece_out(player=0, piece=0, dice=6), ] # And When we play getting out with the first peice new_state = game.play(GameMove.piece_out(player=0, piece=0, dice=6)) # Then the first piece should be out assert new_state.current_player == 0 assert new_state.number == 2 assert new_state.dice == 6 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=1), Piece(number=0, player=2, progress=0), ] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we row the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should should be able to move the piece forward assert new_state.number == 3 assert new_state.dice == 6 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then it should go forward and we should be able to roll the dice again assert new_state.number == 4 assert new_state.winners == [] assert new_state.board.pieces == [ Piece(number=0, player=0, progress=7), Piece(number=0, player=2, progress=0), ] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we roll the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should be able to move the piece forward assert new_state.dice == 6 assert new_state.number == 5 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then the piece should go forward and we should be able to roll the dice again assert new_state.number == 6 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=13), Piece(number=0, player=2, progress=0), ] assert new_state.winners == [] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we position the piece toward the end of the board new_state.board.pieces[0].progress = board.end_progress - 6 # And When we roll the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should be able to move the piece forward into the safe zone and to the goal assert new_state.dice == 6 assert new_state.number == 7 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then the piece should go forward and we should be able to roll the dice again assert new_state.number == 8 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=board.end_progress), Piece(number=0, player=2, progress=0), ] assert new_state.winners == [0] assert new_state.valid_actions == []