From 1589188357d3aaaec09f6d87ef03d3d7ae567770 Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Thu, 7 Mar 2019 02:09:50 +0100 Subject: [PATCH] Added debug command, WIP sync joining and leaving --- dpnet/netserver.py | 57 ++++++++++++++++++++++++++++----------------- dpnet/session.py | 5 ++-- giants/player.py | 9 ++++--- plugins/commands.py | 10 +++++++- server.py | 17 +++++++++----- 5 files changed, 64 insertions(+), 34 deletions(-) diff --git a/dpnet/netserver.py b/dpnet/netserver.py index 773f439..89c4e89 100644 --- a/dpnet/netserver.py +++ b/dpnet/netserver.py @@ -173,7 +173,7 @@ class Netserver(asyncio.DatagramProtocol): return else: logger.debug("New connection. Sending a CFRAME CONNECTED.") - session = Session(self.remotesocket) + session = Session(self.server, self.remotesocket) session.SessID = cframe.SessID session.ip = addr[0] session.port = addr[1] @@ -262,7 +262,7 @@ class Netserver(asyncio.DatagramProtocol): logger.debug("%s:%s > %s (END_STREAM)", addr[0], addr[1], data.getvalue().hex()) if not session: logger.error("Received a END STREAM packet for a non fully connected session") - session = Session(self.remotesocket) + session = Session(self.server, self.remotesocket) session.ip = addr[0] session.port = addr[1] session.SessID = dframe.SessID @@ -377,25 +377,12 @@ class Netserver(asyncio.DatagramProtocol): await self.server.add_player(player) await player.session.send_gamedata(b'\x3c'+struct.pack(" 3 >= team % 128: + # ignore + logger.info("%s tried to change team to %s but failed" % (player.name, team)) + else: + await player.change_team(team % 128) elif payload[0] == 0x35: # CMSG_SEND_CHAT_MESSAGE: messagetype? (byte), team? (byte), message (string) player = self.get_player(session) if not player: @@ -558,7 +548,8 @@ class Netserver(asyncio.DatagramProtocol): player.o = orientation #logger.info("%s is now at coords (%s,%s,%s)", player.name, player.x, player.y, player.z) - await self.server.broadcast_message("%s is now at coords (%s,%s,%s, %s). Model: %s" % (player.name, player.x, player.y, player.z, player.o, model)) + if player.opts["debug"]: + await player.send_message("%s is now at coords (%s,%s,%s, %s). Model: %s" % (player.name, player.x, player.y, player.z, player.o, model)) #await self.server.broadcast_message("(%s,%s,%s, %s,%s)" % (unknown4, unknown5, unknown6, unknown7, unknown9)) elif payload[0] == 0x3a: @@ -568,8 +559,14 @@ class Netserver(asyncio.DatagramProtocol): logger.debug("Fuck that no player wtf man") return - await self.server.broadcast_event("on_player_left", player) await self.server.remove_player(player) + await self.server.broadcast_gamedata(self.build_players_dpid()) + + plid = 0 + for pplayer in self.server.players: + await self.server.broadcast_gamedata(b'\x3d' + struct.pack(" 1 and command[0] == "team": newteam = command[1].replace("\x00", "") print("Changing team of %s to %s" % (player.name, newteam)) - await self.server.broadcast_message("%s switched to team %s" % (player.name, newteam)) + await self.server.broadcast_message("%s switched to team %s using command" % (player.name, newteam)) await player.change_team(int(newteam)) if len(command) > 1 and command[0] == "ping": @@ -70,6 +70,14 @@ class Commands: await self.server.broadcast_message("Changing model of "+command[1]+" to "+command[2]+"") await self.server.change_model(self.server.get_player_by_index(int(command[1])), int(command[2])) + if command[0] == "debug": + if player.opts["debug"]: + player.opts["debug"] = False + await player.send_message("Debug disabled") + else: + player.opts["debug"] = True + await player.send_message("Debug enabled") + def setup(server): plugin = Commands(server) diff --git a/server.py b/server.py index ed11baa..9730db4 100644 --- a/server.py +++ b/server.py @@ -27,22 +27,22 @@ class Server: self.maxplayers = kwargs.get("maxplayers", 20) self.name = kwargs.get("name", "Default Server Name") - fake_session = Session(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) + fake_session = Session(self, socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) fake_session.ip = "127.0.0.1" fake_session.port = 3333 self.players = [Player("[Server]", fake_session)] - fake_player = Session(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) + fake_player = Session(self, socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) fake_player.ip = "127.0.0.1" fake_player.port = 3334 self.players.append(Player("Bot 1", fake_player)) - fake_player = Session(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) + fake_player = Session(self, socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) fake_player.ip = "127.0.0.1" fake_player.port = 3334 self.players.append(Player("Bot 2", fake_player)) - fake_player = Session(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) + fake_player = Session(self, socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) fake_player.ip = "127.0.0.1" fake_player.port = 3334 self.players.append(Player("Bot 3", fake_player)) @@ -74,7 +74,6 @@ class Server: async def remove_player(self, player): self.players.remove(player) - await self.broadcast_event("on_player_left", player) def add_plugin(self, plugin): self._plugins.append(plugin) @@ -141,6 +140,12 @@ class Server: continue await player.session.send_gamedata(payload, **kwargs) + async def broadcast_gamedata_except(self, player, payload, **kwargs): + for pplayer in self.players: + if pplayer.name == "[Server]" or pplayer == player: + continue + await pplayer.session.send_gamedata(payload, **kwargs) + async def broadcast_message_except(self, player, text, color=ChatColor.Yellow, type=ChatType.All): for pplayer in self.players: if pplayer.name == "[Server]" or pplayer == player: @@ -264,7 +269,7 @@ if __name__ == '__main__': loop = asyncio.get_event_loop() listen = loop.create_datagram_endpoint(lambda: Netserver(server), local_addr=(server.listen_ip, server.listen_port)) transport, protocol = loop.run_until_complete(listen) - loop.create_task(server.ask_command()) + #loop.create_task(server.ask_command()) try: loop.run_forever() except KeyboardInterrupt: