From 177b018d1d6f54359884a7d18f56de30fe24d936 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 12 Nov 2024 03:33:01 +0100 Subject: [PATCH] feat(#267): Fixed evil race condition in bs_rpc --- pkg/server/pokete/msg/handshake.go | 23 +++++++------------ pkg/server/pokete/user/user.go | 1 + pokete.py | 6 +++-- pokete_classes/multiplayer/communication.py | 13 ++++++++--- pokete_classes/multiplayer/menu.py | 1 + pokete_classes/multiplayer/msg/handshake.py | 4 ++-- .../multiplayer/msg/player/player.py | 2 +- .../multiplayer/pc_manager/remote_player.py | 2 -- 8 files changed, 27 insertions(+), 25 deletions(-) diff --git a/pkg/server/pokete/msg/handshake.go b/pkg/server/pokete/msg/handshake.go index 536bd97d..aba7c129 100644 --- a/pkg/server/pokete/msg/handshake.go +++ b/pkg/server/pokete/msg/handshake.go @@ -4,15 +4,12 @@ import ( "context" "fmt" - "github.com/lxgr-linux/pokete/server/pokete/poke" - "github.com/lxgr-linux/pokete/server/pokete/troll" - - "github.com/lxgr-linux/pokete/server/pokete/msg/map_info" - "github.com/lxgr-linux/pokete/bs_rpc/msg" "github.com/lxgr-linux/pokete/server/config" pctx "github.com/lxgr-linux/pokete/server/context" error2 "github.com/lxgr-linux/pokete/server/pokete/msg/error" + "github.com/lxgr-linux/pokete/server/pokete/msg/map_info" + "github.com/lxgr-linux/pokete/server/pokete/troll" "github.com/lxgr-linux/pokete/server/pokete/user" "github.com/lxgr-linux/pokete/server/pokete/users" ) @@ -21,9 +18,8 @@ const HandshakeType msg.Type = "pokete.handshake" type Handshake struct { msg.BaseMsg - UserName string `json:"user_name"` - Version string `json:"version"` - Pokes []poke.Instance `json:"pokes"` + User user.User `json:"user"` + Version string `json:"version"` } func (h Handshake) GetType() msg.Type { @@ -40,17 +36,14 @@ func (h Handshake) CallForResponse(ctx context.Context) (msg.Body, error) { position := getStartPosition(cfg) - err := troll.CheckPokes(res.BaseAssets.Pokes, h.Pokes) + err := troll.CheckPokes(res.BaseAssets.Pokes, h.User.Pokes) if err != nil { return error2.NewInvalidPoke(err), err } - newUser := user.User{ - Name: h.UserName, - Client: client, - Position: position, - Pokes: h.Pokes, - } + newUser := h.User + newUser.Client = client + newUser.Position = position if h.Version != cfg.ClientVersion { return error2.NewVersionMismatch(cfg.ClientVersion), fmt.Errorf("connection closed") diff --git a/pkg/server/pokete/user/user.go b/pkg/server/pokete/user/user.go index 22b143ec..389ae198 100644 --- a/pkg/server/pokete/user/user.go +++ b/pkg/server/pokete/user/user.go @@ -10,4 +10,5 @@ type User struct { Position Position `json:"position"` Client *bs_rpc.Client `json:"client"` // TODO: Maybe remove Pokes []poke.Instance `json:"pokes"` + Items map[string]uint `json:"items"` } diff --git a/pokete.py b/pokete.py index ce0da7f8..7579ae64 100755 --- a/pokete.py +++ b/pokete.py @@ -140,12 +140,14 @@ def set_args(self, _si): mvp.movemap.add_obs() def set(self, x, y): - if super().set(x, y) == 0: + if (ret := super().set(x, y)) == 0: self.update_server_pos() + return ret def add(self, _map, x, y): - if super().add(_map, x, y) == 0: + if (ret := super().add(_map, x, y)) == 0: self.update_server_pos() + return ret def update_server_pos(self): if modeProvider.mode == Mode.MULTI: diff --git a/pokete_classes/multiplayer/communication.py b/pokete_classes/multiplayer/communication.py index 283e68e0..04df54fd 100644 --- a/pokete_classes/multiplayer/communication.py +++ b/pokete_classes/multiplayer/communication.py @@ -9,7 +9,7 @@ from pokete_classes.multiplayer.exceptions import ConnectionException, \ VersionMismatchException, UserPresentException, InvalidPokeException from pokete_classes.multiplayer.msg import player, position, error, map_info, fight -from pokete_classes.multiplayer.msg.position.update import UpdateDict +from pokete_classes.multiplayer.msg.position.update import Position, UpdateDict from pokete_classes.multiplayer.pc_manager import pc_manager @@ -69,9 +69,14 @@ def handshake( """Sends and handles the handshake with the server""" resp = self.client.call_for_response( msg.Handshake({ - "user_name": user_name, + "user": { + "name": user_name, + "pokes": [p.dict() for p in ctx.figure.pokes], + "items": ctx.figure.inv, + "client": None, + "position": {"map": "", "x":0, "y": 0} # Null position + }, "version": version, - "pokes": [p.dict() for p in ctx.figure.pokes] })) match resp.get_type(): case error.VERSION_MISMATCH_TYPE: @@ -98,6 +103,8 @@ def handshake( ctx.figure.y = pos["y"] pc_manager.waiting_users = data["users"] return data["greeting_text"] + case _: + assert False, resp.get_type() def request_fight(self, name: str) -> bool | None: resp = self.client.call_for_response(fight.Request({"name": name})) diff --git a/pokete_classes/multiplayer/menu.py b/pokete_classes/multiplayer/menu.py index 1ea7a45c..4e87d539 100644 --- a/pokete_classes/multiplayer/menu.py +++ b/pokete_classes/multiplayer/menu.py @@ -1,5 +1,6 @@ """Contains classes ralated to the mode choosing meni""" +import logging import sys import scrap_engine as se diff --git a/pokete_classes/multiplayer/msg/handshake.py b/pokete_classes/multiplayer/msg/handshake.py index cf8e22c9..d4e1c0c4 100644 --- a/pokete_classes/multiplayer/msg/handshake.py +++ b/pokete_classes/multiplayer/msg/handshake.py @@ -2,12 +2,12 @@ import bs_rpc from pokete_classes.asset_service.resources.base import PokeDict +from pokete_classes.multiplayer.msg.player import player class HandshakeData(TypedDict): - user_name: str + user: player.User version: str - pokes: list[PokeDict] class Handshake(bs_rpc.Body): diff --git a/pokete_classes/multiplayer/msg/player/player.py b/pokete_classes/multiplayer/msg/player/player.py index 6d51c764..578e18cc 100644 --- a/pokete_classes/multiplayer/msg/player/player.py +++ b/pokete_classes/multiplayer/msg/player/player.py @@ -9,7 +9,7 @@ class User(TypedDict): name: str - positon: Position + position: Position client: None pokes: list[PokeDict] items: dict[str, int] diff --git a/pokete_classes/multiplayer/pc_manager/remote_player.py b/pokete_classes/multiplayer/pc_manager/remote_player.py index ab3f5893..1536c8dc 100644 --- a/pokete_classes/multiplayer/pc_manager/remote_player.py +++ b/pokete_classes/multiplayer/pc_manager/remote_player.py @@ -3,8 +3,6 @@ from ...color import Color from ...interactions import Interactor, MultiTextChooseBox from ...landscape import MapInteract -#from ... import movemap as mvp - class RemotePlayer(se.Object, MapInteract, Interactor): """A remote player