fix server

This commit is contained in:
MeexReay 2024-12-22 01:20:54 +03:00
parent 3a6df05fb7
commit 8e007763bf
5 changed files with 34 additions and 28 deletions

View file

@ -3,20 +3,9 @@ import asyncio, time
from network import startServer from network import startServer
from player import Player from player import Player
from block import Block from block import Block
from world import *
from config import * from config import *
def getPlayers():
global WORLD
for b in WORLD:
if type(b) == Player:
yield b
def getPlayer(name):
global WORLD
for b in WORLD:
if type(b) == Player:
if b.name == name:
return b
def current_milli_time(): def current_milli_time():
return round(time.time() * 1000) return round(time.time() * 1000)
@ -45,11 +34,5 @@ async def main():
asyncio.get_event_loop().create_task(renderTimer()) asyncio.get_event_loop().create_task(renderTimer())
await startServer(HOST, PORT) await startServer(HOST, PORT)
WORLD = [
Block(-1, -1, "normal", "#555", True),
Block(0, -1, "spawn", "#2ad", True),
Block(1, -1, "normal", "#555", True)
]
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())

View file

@ -1,8 +1,9 @@
from websockets.server import serve, ServerConnection from websockets.server import serve, ServerConnection
import asyncio import asyncio
from config import BLOCK_TYPES, COLORS, SPAWN from config import BLOCK_TYPES, COLORS, SPAWN
from main import WORLD, getPlayer, getPlayers from world import *
from player import Player from player import Player
from packet import *
import random import random
async def startServer(host, port): async def startServer(host, port):
@ -10,13 +11,6 @@ async def startServer(host, port):
print(f"started server on {host}:{port}") print(f"started server on {host}:{port}")
await asyncio.get_running_loop().create_future() await asyncio.get_running_loop().create_future()
async def readPacket(websocket: ServerConnection) -> tuple[str, list[str]]:
data = await websocket.recv()
return data[0], data[1:].splitlines()
async def writePacket(websocket: ServerConnection, packet_id: str, packet_data: list[str]):
await websocket.send(packet_id + ("\n".join(packet_data)))
async def handler(websocket: ServerConnection): async def handler(websocket: ServerConnection):
packet_id, packet_data = await readPacket(websocket) packet_id, packet_data = await readPacket(websocket)

8
server/src/packet.py Normal file
View file

@ -0,0 +1,8 @@
from websockets.server import ServerConnection
async def readPacket(websocket: ServerConnection) -> tuple[str, list[str]]:
data = await websocket.recv()
return data[0], data[1:].splitlines()
async def writePacket(websocket: ServerConnection, packet_id: str, packet_data: list[str]):
await websocket.send(packet_id + ("\n".join(packet_data)))

View file

@ -1,7 +1,7 @@
from block import Block from block import Block
from network import writePacket from packet import writePacket
from config import SPAWN, REACH_DISTANCE, BLOCK_TYPES from config import SPAWN, REACH_DISTANCE, BLOCK_TYPES
from main import getPlayers, WORLD from world import *
import time import time
class Player(Block): class Player(Block):

21
server/src/world.py Normal file
View file

@ -0,0 +1,21 @@
from player import Player
from block import Block
def getPlayers():
global WORLD
for b in WORLD:
if type(b) == Player:
yield b
def getPlayer(name):
global WORLD
for b in WORLD:
if type(b) == Player:
if b.name == name:
return b
WORLD = [
Block(-1, -1, "normal", "#555", True),
Block(0, -1, "spawn", "#2ad", True),
Block(1, -1, "normal", "#555", True)
]