small fix x_x

This commit is contained in:
MeexReay 2024-12-22 02:32:48 +03:00
parent 3d11a2871f
commit 7fe48d6993
9 changed files with 77 additions and 76 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
server/__pycache__/
server/src/__pycache__/
client/dest

View file

@ -1,13 +1,13 @@
#!/bin/sh
tsc
mkdir -p dest
cat dest/main.js > dest/script.js
cat dest/block.js >> dest/script.js
cat dest/network.js >> dest/script.js
cat dest/player.js >> dest/script.js
cat dest/core.js >> dest/script.js
terser dest/script.js -o dest/script.min.js --compress --mangle
rm dest/script.js
rm dest/main.js
rm dest/block.js
rm dest/network.js

File diff suppressed because one or more lines are too long

View file

@ -75,6 +75,6 @@
<a href="https://github.com/MeexReay/cubicjs">репозиторий гитхаб. там же протокол сервер и готовая реализация сервера на Python</a><br>
</div>
<script src="dest/script.min.js"></script>
<script src="dest/script.js"></script>
</body>
</html>

View file

@ -27,19 +27,26 @@ const allowed_key_to_send = [
"F1", "F2", "KeyZ", "KeyX", "KeyC"
]
function connectServer(address: string, name: string) {
async function connectServer(address: string, name: string) {
player.closeConnection()
player.onConnect(name)
try {
let conn = new Connection(address, player.onPacket, (e) => {
player.conn = new Connection(
await Connection.createSocket(
address,
(p) => player.onPacket(p),
(e) => {
player.conn = null
setServerError(e == null ? "Connection closed due to error" : e)
resetWorld()
})
conn.send(new JoinPacket(name))
}
)
)
player.conn.send(new JoinPacket(name))
} catch (exception) {
setServerError(exception)
console.log(exception)
}
}

View file

@ -59,36 +59,27 @@ class VelocityPacket extends Packet {
class Connection {
private socket: WebSocket
private on_packet: (packet: Packet) => void
private on_close: (error: string | null) => void
constructor(
address: string,
static createSocket(address: string,
on_packet: (packet: Packet) => void,
on_close: (error: string | null) => void
) {
this.socket = new WebSocket(
): Promise<WebSocket> {
return new Promise((resolve, _) => {
let socket = new WebSocket(
"ws://"+address,
"cubic",
)
this.on_packet = on_packet
this.on_close = on_close
this.socket.onmessage = this._on_message
this.socket.onclose = this._on_close
this.socket.onerror = this._on_error
socket.onmessage = (e) => on_packet(Packet.fromString(e.data))
socket.onclose = (_) => on_close(null)
socket.onerror = (e) => on_close(e.toString())
socket.onopen = () => resolve(socket)
})
}
private _on_message(event: MessageEvent) {
this.on_packet(Packet.fromString(event.data))
}
private _on_close(event: CloseEvent) {
this.on_close(null)
}
private _on_error(event: Event) {
this.on_close(event.toString())
constructor(
socket: WebSocket
) {
this.socket = socket
}
close() {
@ -96,6 +87,6 @@ class Connection {
}
send(packet: Packet) {
this.socket.send(packet.getId()+packet.getData())
this.socket.send(packet.getId()+packet.getData().join("\n"))
}
}

View file

@ -18,14 +18,13 @@ class Block:
if x != 0: player.vel_x = self.x + x - player.x
if y != 0: player.vel_y = self.y + y - player.y
if x != 0 or y != 0: # special blocks
if self.type == "jump_boost":
player.setGravitySpeed(1.25)
player.setJumpSpeed(5)
await player.setGravitySpeed(1.25)
await player.setJumpSpeed(5)
player.on_ground = True
elif player.jump_speed != 2:
player.setGravitySpeed(0.5)
player.setJumpSpeed(2)
await player.setGravitySpeed(0.5)
await player.setJumpSpeed(2)
if self.type == "killer":
await player.setPos(*SPAWN)

View file

@ -5,6 +5,7 @@ from world import *
from player import Player
from packet import *
import random
import traceback
async def startServer(host, port):
async with serve(handler, host, port):
@ -43,8 +44,10 @@ async def handler(websocket: ServerConnection):
while True:
packet_id, packet_data = await readPacket(websocket)
player.onPacket(packet_id, packet_data)
await player.onPacket(packet_id, packet_data)
except Exception as exc:
traceback.print_exc()
WORLD.remove(player)
for p in getPlayers():

View file

@ -1,12 +1,13 @@
from block import Block
from packet import writePacket
from config import SPAWN, REACH_DISTANCE, BLOCK_TYPES
from world import *
import world
import time
class Player(Block):
def __init__(self, websocket, x=None, y=None, name=None, color=None, vel_x=None, vel_y=None):
def __init__(self, websocket, x=0, y=0, name=None, color=None, vel_x=None, vel_y=None):
super().__init__(x, y, None, color, True)
self.x = x
self.y = y
@ -27,7 +28,7 @@ class Player(Block):
self.lsd_time = time.time()
async def sendPacket(self, packet_id, packet_data):
await writePacket(self.websocket, packet_id, packet_data)
await self.sendPacket(packet_id, packet_data)
async def onPacket(self, packet_id, packet_data):
if packet_id == "V":
@ -77,7 +78,7 @@ class Player(Block):
message = packet_data[0]
message = f"{self.name} > {message}"
for p in getPlayers():
for p in world.getPlayers():
await p.sendMessage(message)
print(message)
@ -87,7 +88,7 @@ class Player(Block):
x,y = int(x),int(y)
block = None
for i in WORLD:
for i in world.WORLD:
if type(i) == Player:
continue
if i.x == x and i.y == y:
@ -100,9 +101,9 @@ class Player(Block):
if abs(x - self.x) ** 2 + abs(y - self.y) ** 2 > REACH_DISTANCE ** 2:
return
WORLD.remove(block)
world.WORLD.remove(block)
for p in getPlayers():
for p in world.getPlayers():
await p.sendWorld([block.toStatement(False)])
if packet_id == "P":
@ -116,7 +117,7 @@ class Player(Block):
return
found_block = False
for i in WORLD:
for i in world.WORLD:
if type(i) == Player:
continue
if i.x == x and i.y == y:
@ -126,25 +127,25 @@ class Player(Block):
block = Block(x,y,block_type,BLOCK_TYPES[block_type],True)
WORLD.append(block)
world.WORLD.append(block)
for p in getPlayers():
for p in world.getPlayers():
await p.sendWorld([block.toStatement()])
async def setWalkSpeed(self, speed):
await writePacket(self.websocket, "S", ["W", str(speed)])
await self.sendPacket("S", ["W", str(speed)])
self.walk_speed = speed
async def setGravitySpeed(self, speed):
await writePacket(self.websocket, "S", ["G", str(speed)])
await self.sendPacket("S", ["G", str(speed)])
self.gravity_speed = speed
async def setJumpSpeed(self, speed):
await writePacket(self.websocket, "S", ["J", str(speed)])
await self.sendPacket("S", ["J", str(speed)])
self.jump_speed = speed
async def sendName(self, name):
await writePacket(self.websocket, "N", [name])
await self.sendPacket("N", [name])
async def setName(self, name):
self.name = name
@ -152,7 +153,7 @@ class Player(Block):
async def setColor(self, color):
self.color = color
await writePacket(self.websocket, "C", [color])
await self.sendPacket("C", [color])
async def setVel(self, x, y):
if x == self.vel_x and y == self.vel_y: return
@ -171,23 +172,23 @@ class Player(Block):
await self.sendPos(x, y)
async def sendVel(self, x, y):
await writePacket(self.websocket, "V", [str(x), str(y)])
await self.sendPacket("V", [str(x), str(y)])
async def sendPos(self, x, y):
await writePacket(self.websocket, "P", [str(x), str(y)])
await self.sendPacket("P", [str(x), str(y)])
async def sendMessage(self, message):
await writePacket(self.websocket, "M", message.split("\n"))
await self.sendPacket("M", message.split("\n"))
async def sendWorld(self, statements):
if len(statements) == 0: return
await writePacket(self.websocket, "W", statements)
await self.sendPacket("W", statements)
async def sendBlockTypes(self, types):
await writePacket(self.websocket, "B", types)
await self.sendPacket("B", types)
async def sendToPlayers(self):
for p in getPlayers():
for p in world.getPlayers():
if p != self:
await p.sendWorld([self.toStatement()])
@ -203,11 +204,9 @@ class Player(Block):
await self.collide()
async def collide(self):
global WORLD
self.on_ground = False
for block in WORLD:
for block in world.WORLD:
if not block.collides: continue
if block == self: continue
@ -227,6 +226,7 @@ class Player(Block):
if self.x < block.x and self.x + self.vel_x > block.x - 1:
collide_x = -1
if collide_x != 0 or collide_y != 0:
await block.onCollide(self, collide_x, collide_y)
async def onCollide(self, player, x, y):
@ -249,7 +249,7 @@ class Player(Block):
return self.vel_x != 0 or self.vel_y != 0
async def keepAlive(self):
await writePacket(self.websocket, "R", [str(self.x), str(self.y), str(self.vel_x), str(self.vel_y)])
await self.sendPacket("R", [str(self.x), str(self.y), str(self.vel_x), str(self.vel_y)])
def toStatement(self, add=True):
return f"P1{self.name},{self.x},{self.y},{self.vel_x},{self.vel_y},{self.color}" if add else f"P0{self.name}"