add draft server with join action implemented (#5)
* add draft server with join action implemented * check if message is None * add websockets library * some fixes
This commit is contained in:
1 parent
3bc69e670a
commit
abf72c8093
7 files changed
+134
-6
No files matched your search
+24
-6
@@ -1,8 +1,26 @@
|
||||
from flask import Flask
|
||||
|
||||
app = Flask("Autobeerpong")
|
||||
import asyncio
|
||||
import socket
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "Hello, World!"
|
||||
async def handle(reader, writer):
|
||||
addr = writer.get_extra_info("peername")
|
||||
print("Connected by", addr)
|
||||
try:
|
||||
while True:
|
||||
data = await reader.read(1024)
|
||||
if not data:
|
||||
break
|
||||
writer.write(data)
|
||||
await writer.drain()
|
||||
finally:
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
||||
|
||||
async def main():
|
||||
server = await asyncio.start_server(handle, "0.0.0.0", 15555)
|
||||
async with server:
|
||||
await server.serve_forever()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,18 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ClientType(Enum):
|
||||
PLAYER = 0
|
||||
SPECTATOR = 1
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(
|
||||
self,
|
||||
client_id: str,
|
||||
client_type: ClientType,
|
||||
user_name: str,
|
||||
):
|
||||
self.id = client_id
|
||||
self.type = client_type
|
||||
self.user_name = user_name
|
||||
@@ -0,0 +1,23 @@
|
||||
import json
|
||||
import asyncio
|
||||
|
||||
|
||||
async def tcp_echo_client(message):
|
||||
reader, writer = await asyncio.open_connection("127.0.0.1", 8888)
|
||||
|
||||
data = {
|
||||
"action": "join",
|
||||
"name": "Johnny",
|
||||
}
|
||||
|
||||
writer.write(json.dumps(data).encode())
|
||||
writer.write_eof()
|
||||
await writer.drain()
|
||||
|
||||
msg = await reader.read()
|
||||
print(json.loads(msg.decode()))
|
||||
|
||||
|
||||
data = {"key": "value"}
|
||||
|
||||
asyncio.run(tcp_echo_client(json.dumps(data)))
|
||||
@@ -0,0 +1,12 @@
|
||||
from client import Client, ClientType
|
||||
|
||||
|
||||
class Lobby:
|
||||
def __init__(self):
|
||||
self.clients: dict[str, Client] = {}
|
||||
|
||||
def add_client(self, client: Client):
|
||||
self.clients[client.id] = client
|
||||
|
||||
def change_client_type(self, client_id: str, new_client_type: ClientType):
|
||||
self.clients[client_id].type = new_client_type
|
||||
@@ -0,0 +1,3 @@
|
||||
class Protocol:
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -1 +1,2 @@
|
||||
Flask==3.1.2
|
||||
websockets==15.0.1
|
||||
@@ -0,0 +1,53 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from itertools import count
|
||||
from websockets.asyncio.server import serve, ServerConnection
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(
|
||||
self,
|
||||
client_id: int,
|
||||
name: str,
|
||||
):
|
||||
self.client_id = client_id
|
||||
self.name = name
|
||||
self.is_player: bool = True
|
||||
|
||||
|
||||
CLIENT_IDS = count()
|
||||
clients = {}
|
||||
|
||||
|
||||
async def handle_echo(websocket: ServerConnection):
|
||||
client_id = next(CLIENT_IDS)
|
||||
|
||||
print("got a client")
|
||||
|
||||
msg = await websocket.recv()
|
||||
msg: dict = json.loads(msg)
|
||||
|
||||
if not msg["action"] == "join":
|
||||
raise Exception("wtfffff")
|
||||
|
||||
name = msg["name"]
|
||||
|
||||
clients[client_id] = Client(client_id, name)
|
||||
|
||||
data = {
|
||||
"gamestate": {
|
||||
"players": [client.name for client in clients.values()],
|
||||
"spectators": [],
|
||||
}
|
||||
}
|
||||
|
||||
await websocket.send(json.dumps(data))
|
||||
|
||||
|
||||
async def start_le_server():
|
||||
async with serve(handle_echo, "localhost", 8888) as server:
|
||||
await server.serve_forever()
|
||||
|
||||
|
||||
asyncio.run(start_le_server())
|
||||
Reference in new issue
Block a user