ping fix
This commit is contained in:
parent
75eaf59c79
commit
6392bf00d6
3 changed files with 37 additions and 10 deletions
|
@ -104,11 +104,11 @@
|
|||
идти выкл (controls_x = 0) [C]: "W", "0"
|
||||
идти левее (controls_x = -1) [C]: "W", "1"
|
||||
идти правее (controls_x = 1) [C]: "W", "2"
|
||||
корректировка движения [R]: {x}, {y}, {vel_x}, {vel_y}
|
||||
установить блок [P]: {x}, {y}, {тип}
|
||||
сломать блок [D]: {x}, {y}
|
||||
нажатие кнопки (список кнопок ниже) [K]: {кнопка}, {нажата ли}
|
||||
отправить сообщение [M]: {сообщение}
|
||||
корректировка движения [R]: {рандомное айди}, {x}, {y}
|
||||
|
||||
сервер отправляет:
|
||||
кикнуть игрока с ошибкой [K]: {ошибка}
|
||||
|
@ -122,6 +122,7 @@
|
|||
отправить мир [W]: {изм. мира}, {изм. мира}, ...
|
||||
отправить все типы блоков: [B]: {тип_1}, ..., {тип_9}
|
||||
отправить сообщение [M]: {сообщение}, {сообщение}, ...
|
||||
корректировка движения [R]: {рандомное айди}, {время}, {vel_x}, {vel_y}
|
||||
|
||||
список кнопок которые может отправить игрок через отдельный пакет:
|
||||
["KeyR", "KeyW", "KeyE", "KeyQ", "KeyS", "KeyZ", "KeyX", "KeyC"
|
||||
|
|
26
script.js
26
script.js
|
@ -279,6 +279,8 @@ class MainPlayer extends Player {
|
|||
"normal", "normal", "normal", "normal", "normal",
|
||||
"normal", "normal", "normal", "normal", "normal"
|
||||
]
|
||||
|
||||
this.ping = -1
|
||||
}
|
||||
|
||||
on_connect(name) {
|
||||
|
@ -324,13 +326,13 @@ class MainPlayer extends Player {
|
|||
} else {
|
||||
if (key == "KeyD") {
|
||||
this.send_packet("C","W","2")
|
||||
this.controls_x = 1
|
||||
setTimeout(() => { this.controls_x = 1 }, this.ping)
|
||||
} else if (key == "KeyA") {
|
||||
this.send_packet("C","W","1")
|
||||
this.controls_x = -1
|
||||
setTimeout(() => { this.controls_x = -1 }, this.ping)
|
||||
} else if (key == "Space") {
|
||||
this.send_packet("C","J","1")
|
||||
this.controls_jump = true
|
||||
setTimeout(() => { this.controls_jump = true }, this.ping)
|
||||
e.preventDefault()
|
||||
return false
|
||||
} else if (key == "KeyR") {
|
||||
|
@ -368,10 +370,10 @@ class MainPlayer extends Player {
|
|||
if ((key == "KeyD" && this.controls_x == 1)
|
||||
|| (key == "KeyA" && this.controls_x == -1)) {
|
||||
this.send_packet("C","W","0")
|
||||
this.controls_x = 0
|
||||
setTimeout(() => { this.controls_x = 0 }, this.ping)
|
||||
} else if (key == "Space" && this.controls_jump) {
|
||||
this.send_packet("C","J","0")
|
||||
this.controls_jump = false
|
||||
setTimeout(() => { this.controls_jump = false }, this.ping)
|
||||
}
|
||||
|
||||
if (allowed_key_to_send.includes(key)) {
|
||||
|
@ -407,13 +409,17 @@ class MainPlayer extends Player {
|
|||
this.send_packet("D",x,y)
|
||||
}
|
||||
})
|
||||
|
||||
setInterval(() => {
|
||||
this.send_packet("R",Date.now(),this.x,this.y)
|
||||
}, 100)
|
||||
}
|
||||
|
||||
recv_packet(packet) {
|
||||
let packet_id = packet[0]
|
||||
let packet_data = packet.slice(1).split("\n")
|
||||
|
||||
console.log(packet_id, packet_data)
|
||||
// console.log(packet_id, packet_data)
|
||||
|
||||
if (packet_id == "K") {
|
||||
server_error.innerText = packet_data[0]
|
||||
|
@ -433,6 +439,14 @@ class MainPlayer extends Player {
|
|||
chatMessages.unshift(...packet_data)
|
||||
}
|
||||
|
||||
if (packet_id == "R") {
|
||||
let ping = parseFloat(packet_data[1]) - parseFloat(packet_data[0])
|
||||
if (player.ping == -1) player.ping = ping
|
||||
else player.ping = (player.ping + ping) / 2
|
||||
player.velocity_x = parseFloat(packet_data[2])
|
||||
player.velocity_y = parseFloat(packet_data[3])
|
||||
}
|
||||
|
||||
if (packet_id == "P") {
|
||||
let x = parseFloat(packet_data[0])
|
||||
let y = parseFloat(packet_data[1])
|
||||
|
|
18
server.py
18
server.py
|
@ -1,5 +1,5 @@
|
|||
from websockets.server import serve, ServerConnection
|
||||
import random, sys, asyncio
|
||||
import random, sys, asyncio, time
|
||||
|
||||
class Block:
|
||||
def __init__(self, x, y, block_type, color, collides):
|
||||
|
@ -169,8 +169,12 @@ class Player(Block):
|
|||
# pass
|
||||
|
||||
async def render(self):
|
||||
await self.setVel(self.vel_x * 0.5, self.vel_y * 0.5)
|
||||
await self.setPos(self.x + self.vel_x, self.y + self.vel_y)
|
||||
self.vel_x *= 0.5
|
||||
self.vel_y *= 0.5
|
||||
self.x += self.vel_x
|
||||
self.y += self.vel_y
|
||||
# await self.setVel(self.vel_x * 0.5, self.vel_y * 0.5)
|
||||
# await self.setPos(self.x + self.vel_x, self.y + self.vel_y)
|
||||
return self.vel_x != 0 or self.vel_y != 0
|
||||
|
||||
def toStatement(self, add=True):
|
||||
|
@ -189,6 +193,9 @@ def getPlayer(name):
|
|||
if b.name == name:
|
||||
return b
|
||||
|
||||
def current_milli_time():
|
||||
return round(time.time() * 1000)
|
||||
|
||||
async def readPacket(websocket: ServerConnection) -> tuple[str, list[str]]:
|
||||
data = await websocket.recv()
|
||||
return data[0], data[1:].splitlines()
|
||||
|
@ -264,6 +271,11 @@ async def handler(websocket: ServerConnection):
|
|||
|
||||
print(message)
|
||||
|
||||
if packet_id == "R":
|
||||
rid, x, y = packet_data
|
||||
|
||||
writePacket(websocket, "R", [rid, str(current_milli_time()), str(player.vel_x + (x - player.x)), str(player.vel_y + (y - player.y))])
|
||||
|
||||
if packet_id == "D":
|
||||
x,y = packet_data
|
||||
x,y = int(x),int(y)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue