Skip to content

Commit

Permalink
feat(#267): positon update performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Nov 13, 2024
1 parent 15faa99 commit 6687547
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
9 changes: 8 additions & 1 deletion pkg/server/pokete/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package users

import (
"errors"
"fmt"
"log/slog"

"github.com/lxgr-linux/pokete/server/pokete/positions"
"github.com/lxgr-linux/pokete/server/pokete/user"
Expand Down Expand Up @@ -74,7 +76,12 @@ func (u Users) SetNewPositionToUser(conId uint64, newPosition user.Position) err
us := (*u.users)[conId]
err := us.Position.Change(newPosition)
(*u.users)[conId] = us
err = u.positions.BroadcastChange(conId, us)
go func() {
err := u.positions.BroadcastChange(conId, us)
if err != nil {
slog.Error(fmt.Sprintf("Error broadcasting position update: %s", err))
}
}()
return err
}

Expand Down
32 changes: 22 additions & 10 deletions pokete_classes/multiplayer/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CommunicationService:
def __init__(self):
self.client: bs_rpc.Client
self.saved_pos = ()
self.__positions_channel = bs_rpc.Channel[tuple[str, int, int]]()

def __subscribe_position_updates(self):
gen = self.client.call_for_responses(
Expand All @@ -37,11 +38,30 @@ def __subscribe_position_updates(self):
pos_data: position.RemoveData = body.data
pc_manager.remove(pos_data["user_name"])


def __send_position_updates(self):
gen = bs_rpc.ChannelGenerator(self.__positions_channel, None)
for coords in gen():
resp = self.client.call_for_response(
position.Update({
"name": "",
"position": {
"map": coords[0],
"x": coords[1],
"y": coords[2],
},
}))
# Handle Err here

def __call__(self):
threading.Thread(
target=self.__subscribe_position_updates,
daemon=True
).start()
threading.Thread(
target=self.__send_position_updates,
daemon=True
).start()

def connect(self, host: str, port: int):
con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down Expand Up @@ -117,21 +137,13 @@ def request_fight(self, name: str) -> bool | None:
def join_fight(self, fight_id: int) -> bs_rpc.ChannelGenerator:
return self.client.call_for_responses(fight.Fight({"fight_id": fight_id}))

def pos_update(self, _map, x, y):
def pos_update(self, _map:str, x:int, y:int):
"""Sends a position update to the server
ARGS:
_map: Name of the map the player is on
x: X-coordinate
y: Y-coordinate"""
resp = self.client.call_for_response(
position.Update({
"name": "",
"position": {
"map": _map,
"x": x,
"y": y,
},
}))
self.__positions_channel.push((_map, x, y))

def get_player(self, name) -> player.User:
resp = self.client.call_for_response(
Expand Down

0 comments on commit 6687547

Please sign in to comment.