Skip to content

Commit

Permalink
chore: logging.error only in Deck and Match.start
Browse files Browse the repository at this point in the history
  • Loading branch information
zyr17 committed Aug 30, 2023
1 parent 12b4fc8 commit 078049c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
20 changes: 6 additions & 14 deletions server/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def check_request_exist(self, request: Requests) -> bool:
return True
return False

def respond(self, response: Responses) -> bool:
def respond(self, response: Responses) -> None:
"""
Deal with the response. After start, the match will simulate
until it needs response from player, and `self.requests` will have
Expand All @@ -507,22 +507,16 @@ def respond(self, response: Responses) -> bool:
different respond function to deal with, and remove requests that do
not need to respond or have responded. If all requests have been
removed, call `self.step()` manually to continue simulation.
Returns:
bool: True if success, False if error occurs.
"""
logging.info(f'Response received: {response}')
if len(self.requests) == 0:
logging.error('Match is not waiting for response.')
return False
raise ValueError('Match is not waiting for response.')
# check if the response is valid
if not response.is_valid(self):
logging.error('Response is not valid.')
return False
raise ValueError('Response is not valid.')
# check if the request exist
if not self.check_request_exist(response.request):
logging.error('Request does not exist.')
return False
raise ValueError('Request does not exist.')
# call different respond functions based on the type of response
if isinstance(response, SwitchCharactorResponse):
self._respond_switch_charactor(response)
Expand All @@ -543,8 +537,6 @@ def respond(self, response: Responses) -> bool:
else:
raise AssertionError(
f'Response type {type(response)} not recognized.')
return False
return True

def is_game_end(self) -> bool:
"""
Expand Down Expand Up @@ -1358,8 +1350,8 @@ def _action_draw_card(
or len(action.blacklist_types) > 0
):
self._set_match_state(MatchState.ERROR) # pragma no cover
logging.error('Whitelist and blacklist cannot be both '
'specified.')
raise AssertionError('Whitelist and blacklist cannot be both '
'specified.')
# whitelist set
while len(table.table_deck) > 0 and len(draw_cards) < number:
card = table.table_deck.pop(0)
Expand Down
13 changes: 8 additions & 5 deletions tests/server/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def test_match_config_and_match_errors():
assert not match.need_respond(0)
assert not match.need_respond(1)
assert resp is not None
assert not match.respond(resp)
with pytest.raises(ValueError):
match.respond(resp)
if match.need_respond(0):
if not non_exist_tested:
assert match.need_respond(1)
Expand All @@ -176,10 +177,12 @@ def test_match_config_and_match_errors():
assert match.check_request_exist(req)
resp = SwitchCardResponse(
request = req, card_ids = [-1]) # type: ignore
assert not match.respond(resp)
with pytest.raises(ValueError):
match.respond(resp)
resp.card_ids = []
resp.request.player_id = 2
assert not match.respond(resp)
with pytest.raises(ValueError):
match.respond(resp)
resp = agent_0.generate_response(match)
assert resp is not None
match.respond(resp)
Expand Down Expand Up @@ -241,5 +244,5 @@ def test_create_dice():
if __name__ == '__main__':
# test_object_position_validation()
# test_object_position_validation()
# test_match_config_and_match_errors()
test_create_dice()
test_match_config_and_match_errors()
# test_create_dice()

0 comments on commit 078049c

Please sign in to comment.