I previously asked the question on Stack Overflow, but sadly without any response.
I’m developing a website to play a special with kind of chess with React and Django.
It pretty much requires two games to be played at the same time. The website allows to add AI players, so the common scenario is to have board 1 (human vs AI) and board 2 (AI vs AI). I want the computer players to behave in a manner that they play a move, wait a second, then let another one play, not blocking other games. The problem is that the following code makes the entire AI vs AI game play in a blink of an eye and makes it impossible to send and receive any event (even the game.start event does not get sent).
This is my code that triggers the computers playing against each other:
for game in started_games:
await self.channel_layer.group_send(
self.room_group_name, {'type': 'game.start'})
if self.is_ai_turn_in_game(game):
if game.gamemode == GameMode.CLASSICAL.value:
board = chess.Board(fen=game.fen)
else:
board = chess.variant.CrazyhouseBoard(fen=game.fen)
print(self.is_ai_turn_in_game(game))
while self.is_ai_turn_in_game(game):
await self.handle_ai_turn(game, board)
await self.send_move_to_clients(game)
self.send_move_to_clients
processes some data, then calls
await self.channel_layer.group_send(
self.room_group_name, {'type': 'game.move', 'message': response_data}
)
which executes the following code:
async def game_move(self, event):
move = event['message']
await self.send(json.dumps(move))
await asyncio.sleep(1)
I am wondering if I should use a thread here, or maybe I’m misunderstanding how async event loops function.