fix: python keep alive

This commit is contained in:
2025-11-20 14:54:32 -05:00
Unverified
parent 67a922767d
commit 348e8ae786
2 changed files with 26 additions and 26 deletions

View File

@@ -1,15 +1,13 @@
import asyncio import asyncio
import itertools
import websockets import websockets
from enum import Enum from enum import Enum
from websockets import ConnectionClosed
class Slot(Enum):
NONE = 0
RED = 1
BLUE = 2
def calculate_move(opponent_move, board, our_color, opponent_color): def calculate_move(opponent_move, board):
if opponent_move is not None: if opponent_move is not None:
print(f"Opponent played column {opponent_move}") print(f"Opponent played column {opponent_move}")
# TODO: Implement your move calculation logic here instead # TODO: Implement your move calculation logic here instead
@@ -18,9 +16,7 @@ def calculate_move(opponent_move, board, our_color, opponent_color):
async def gameloop(socket): async def gameloop(socket):
board = [[Slot.NONE] * 5 for _ in range(6)] board = [[None] * 5 for _ in range(6)]
our_color = Slot.NONE
opponent_color = Slot.NONE
while True: # While game is active, continually anticipate messages while True: # While game is active, continually anticipate messages
message = (await socket.recv()).split(':') # Receive message from server message = (await socket.recv()).split(':') # Receive message from server
@@ -31,40 +27,41 @@ async def gameloop(socket):
case 'GAME': case 'GAME':
if message[1] == 'START': if message[1] == 'START':
if message[2] == '1': if message[2] == '1':
our_color = Slot.RED col = calculate_move(None, board) # calculate_move is some arbitrary function you have created to figure out the next move
opponent_color = Slot.BLUE
col = calculate_move(None, board, our_color,
opponent_color) # calculate_move is some arbitrary function you have created to figure out the next move
await socket.send(f'PLAY:{col}') # Send your move to the sever await socket.send(f'PLAY:{col}') # Send your move to the sever
else:
our_color = Slot.BLUE
opponent_color = Slot.RED
if (message[1] == 'WINS') | (message[1] == 'LOSS') | (message[1] == 'DRAW') | (message[1] == 'TERMINATED'): # Game has ended if (message[1] == 'WINS') | (message[1] == 'LOSS') | (message[1] == 'DRAW') | (message[1] == 'TERMINATED'): # Game has ended
print(message[0]) print(message[0]+message[1])
board = [[Slot.NONE] * 5 for _ in range(6)] board = [[None] * 5 for _ in range(6)]
our_color = None our_color = None
opponent_color = None opponent_color = None
await socket.send('READY') await socket.send('READY')
case 'OPPONENT': # Opponent has gone; calculate next move case 'OPPONENT': # Opponent has gone; calculate next move
col = calculate_move(message[1], board, our_color, opponent_color) # Give your function your opponent's move col = calculate_move(message[1], board) # Give your function your opponent's move
await socket.send(f'PLAY:{col}') # Send your move to the sever await socket.send(f'PLAY:{col}') # Send your move to the sever
# case 'KICK':
# print("You have been kicked from the game")
# break
case 'ERROR': case 'ERROR':
print(f"{message[0]}: {':'.join(message[1:])}") print(f"{message[0]}: {':'.join(message[1:])}")
break break
await socket.close() await socket.close()
async def keepalive(websocket, ping_interval=30):
for ping in itertools.count():
await asyncio.sleep(ping_interval)
try:
await websocket.send("PING")
except ConnectionClosed:
break
async def join_server(username): async def join_server(username):
async with websockets.connect(f'wss://connect4.abunchofknowitalls.com') as socket: # Establish websocket connection async with websockets.connect(f'wss://connect4.abunchofknowitalls.com') as socket: # Establish websocket connection
keepalive_task = asyncio.create_task(keepalive(socket))
try:
await socket.send(f'CONNECT:{username}') await socket.send(f'CONNECT:{username}')
await gameloop(socket) await gameloop(socket)
finally:
keepalive_task.cancel()
if __name__ == '__main__': # Program entrypoint if __name__ == '__main__': # Program entrypoint

View File

@@ -486,6 +486,9 @@ async fn handle_connection(
// TODO: Start tournaments // TODO: Start tournaments
else if text == "PING" {
let _ = send(&tx, "PONG");
}
else { else {
let _ = send(&tx, "ERROR:UNKNOWN"); let _ = send(&tx, "ERROR:UNKNOWN");
} }