def test_state(): random.seed(1234) state = TwentyFortyEight().state expected_state = { 'board': [[2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 2, 0]], 'score': 0, 'gameover': False, } assert state == expected_state
def move(payload): errors = MovePayload().validate(payload) if errors: raise MovePayloadValidationError # Validate session sessions = redis.Redis.from_url(REDIS_URL) session_id = payload['session_id'] serialized_session = sessions.get(session_id) or '{}' session = json.loads(serialized_session) errors = PaidSession().validate(session) if errors: raise PaidSessionValidationError # Arcade protocol user = session['user'] INFURA_PROJECT_ID = os.environ['INFURA_PROJECT_ID'] INFURA_PROJECT_SECRET = os.environ['INFURA_PROJECT_SECRET'] headers = {"auth": ("", INFURA_PROJECT_SECRET)} uri = 'https://ropsten.infura.io/v3/%s' % (INFURA_PROJECT_ID) provider = HTTPProvider(uri, headers) contract = arcade_protocol.Contract( provider, ADDRESS, ABI, TwentyFortyEight.id, ) game_interface = arcade_protocol.GameInterface( contract, player=user, ) # Move state = session['gamestate'] direction = payload['direction'] next_state = TwentyFortyEight.update(state, direction) signed_score = game_interface.sign(KEY, next_state['score']) # Set session session_data = { 'paid': True, 'gamestate': next_state, 'user': user, } sessions.set(session_id, json.dumps(session_data)) # Set payload payload = { 'board': next_state['board'], 'score': next_state['score'], 'gameover': next_state['gameover'], 'signed_score': signed_score, } return json.dumps(payload)
def test_gameover(): state = { 'board': [ [2, 8], [4, 0], ], 'score': 0, 'gameover': False, } game = TwentyFortyEight._load(state)._move(DOWN) assert game.gameover
def test_board(): expected_state = { 'board': [[2, 8], [4, 0]], 'score': 0, 'gameover': False, } state = { 'board': [[2, 8], [4, 0]], 'score': 0, 'gameover': False, } game = TwentyFortyEight._load(state) assert game.state == expected_state
def test_update(): random.seed(1234) expected_state = { 'board': [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [2, 4, 2, 0]], 'score': 0, 'gameover': False, } state = { 'board': [[2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 2, 0]], 'score': 0, 'gameover': False, } down = 2 state = TwentyFortyEight.update(state, direction=down) assert state == expected_state
def test_double_move(): expected_first_score = 8 + 8 expected_second_score = expected_first_score + 16 + 16 state = { 'board': [ [0, 8], [16, 8], ], 'score': 0, 'gameover': False, } game = TwentyFortyEight._load(state) game._move(DOWN) assert game.score == expected_first_score game._move(RIGHT) assert game.score == expected_second_score
def test_move_down(): random.seed(1234) expected_state = { 'board': [ [2, 0], [4, 16], ], 'score': 16, 'gameover': False, } state = { 'board': [ [0, 8], [4, 8], ], 'score': 0, 'gameover': False, } game = TwentyFortyEight._load(state)._move(DOWN) assert game.state == expected_state
def test_blank_board_rows_cols(): expected_num_position = 2 game = TwentyFortyEight(rows=2, cols=2) available_positions = game._get_available_positions() assert available_positions is not None assert len(available_positions) == expected_num_position