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