Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions server/client_events/client_chat_event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import json
from simulation.init_simulation import simulation as sim
from server.server_events import server_chat_event

def handle(data, source_user):
server_chat_event.fire("{1}: {0}".format(data, source_user))
def handle(data, user):
server_chat_event.fire("{1}: {0}".format(data, user.username))
ip = user.websocket.remote_address[0]
if ip == "127.0.0.1" and data[0] == "/":
parse_command(data[1:])

def change_pause(value):
sim.running = value
if sim.running:
server_chat_event.fire("Simulation unpaused.")
else:
server_chat_event.fire("Simulation paused.")

def parse_command(cmd):
match cmd:
case "pause":
change_pause(not sim.running)
3 changes: 2 additions & 1 deletion server/init_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def init_server(websocket):
case packets.ClientPackets.SYNCHRONIZE: #allows the client to request some extra stuff
server_player_position_parameters_update_event.fire_initial(token_object.token)
server_meter_parameters_update_event.fire_initial(token_object.token)
server_chat_event.fire_initial(token_object)

case packets.ClientPackets.CHAT:
client_chat_event.handle(packet_data, token_object.username)
client_chat_event.handle(packet_data, token_object)

case packets.ClientPackets.VOIP:
client_voip_event.handle(packet_data, token_object.username)
Expand Down
10 changes: 9 additions & 1 deletion server/server_events/server_chat_event.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from server.helpers import packet_helper
from server.connection_manager import manager
from server.constants import packets
from simulation.init_simulation import simulation as sim
import json

def fire(message):
manager.broadcast_packet(packet_helper.build(packets.ServerPackets.CHAT, message))

def fire_initial(token_object):
manager.send_user_packet(packet_helper.build(packets.ServerPackets.CHAT, "Connected"), token_object.token)
if token_object.websocket.remote_address[0] == "127.0.0.1":
manager.send_user_packet(packet_helper.build(packets.ServerPackets.CHAT, "Connected (commands are available)"), token_object.token)
else:
manager.send_user_packet(packet_helper.build(packets.ServerPackets.CHAT, "Connected"), token_object.token)

if not sim.running:
manager.send_user_packet(packet_helper.build(packets.ServerPackets.CHAT, "Simulation paused."), token_object.token)

pass
8 changes: 6 additions & 2 deletions simulation/init_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class Simulation:
def __init__(self):
self.running = False

self.timestep = 0.1 # time between model steps
self.default_timestep = 0.1 # what is 1x speed
self.minimum_speedup_drop = 2 # skip sending packets if we exceed this many times 1x speed
Expand All @@ -32,7 +34,8 @@ def __init__(self):
def timer(self):
while True:
start = time.perf_counter()
model.model_run(self.prev_delta)
if self.running:
model.model_run(self.prev_delta)
drop = 0
if (self.default_timestep/self.timestep) >= self.minimum_speedup_drop:
drop = self.timesteps % math.floor((self.default_timestep/self.timestep)/self.minimum_speedup_drop)
Expand All @@ -54,7 +57,8 @@ def timer(self):
def timer_fast(self):
while True:
start = time.perf_counter()
model.model_run_fast(self.fast_prev_delta)
if self.running:
model.model_run_fast(self.fast_prev_delta)
end = time.perf_counter()
delta = end - start

Expand Down