From 70ebe4f05ac44b6589c9152b01dc1b38a88d0021 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 28 Jan 2024 16:47:41 +0100 Subject: [PATCH] Implemented disconnects --- client/network/PlayerRemove.gd | 12 ++++++++++++ client/world/Main.tscn | 7 ++++++- server/game/Login.go | 18 ++---------------- server/game/Player.go | 18 +++++++++++++++++- server/game/PlayerManager.go | 8 +++----- 5 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 client/network/PlayerRemove.gd diff --git a/client/network/PlayerRemove.gd b/client/network/PlayerRemove.gd new file mode 100644 index 0000000..de08f3f --- /dev/null +++ b/client/network/PlayerRemove.gd @@ -0,0 +1,12 @@ +extends PacketHandler + +func handle_packet(data: PackedByteArray, _peer: PacketPeer): + var player_id := data.get_string_from_ascii() + print("[%s] Remove player: %s" % [Global.username, player_id]) + + var player := %Players.get_node(player_id) + + if is_instance_valid(player): + player.queue_free() + else: + push_warning("PlayerRemove: Player with ID %s doesn't exist") \ No newline at end of file diff --git a/client/world/Main.tscn b/client/world/Main.tscn index 7ba6e89..b6d44ca 100644 --- a/client/world/Main.tscn +++ b/client/world/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=19 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=20 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Game.gd" id="1_uxop2"] [ext_resource type="Script" path="res://network/Client.gd" id="2_00qe8"] @@ -7,6 +7,7 @@ [ext_resource type="Script" path="res://network/PlayerAdd.gd" id="5_sd8xh"] [ext_resource type="PackedScene" uid="uid://2lcnu3dy54lx" path="res://player/Player.tscn" id="6_jxdko"] [ext_resource type="Script" path="res://network/PlayerMove.gd" id="7_6xfox"] +[ext_resource type="Script" path="res://network/PlayerRemove.gd" id="7_ubce6"] [ext_resource type="Script" path="res://world/Sun.gd" id="8_leyo3"] [ext_resource type="Script" path="res://network/PlayerJump.gd" id="8_p6kmr"] [ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="9_4u3r5"] @@ -53,6 +54,10 @@ script = ExtResource("5_sd8xh") player_scene = ExtResource("6_jxdko") packet_type = 10 +[node name="PlayerRemove" type="Node" parent="Client"] +script = ExtResource("7_ubce6") +packet_type = 11 + [node name="PlayerMove" type="Node" parent="Client"] script = ExtResource("7_6xfox") packet_type = 12 diff --git a/server/game/Login.go b/server/game/Login.go index 59210ae..ea40612 100644 --- a/server/game/Login.go +++ b/server/game/Login.go @@ -40,7 +40,7 @@ func (game *Game) Login(data []byte, address *net.UDPAddr, server *Server) error return errors.New("login failure") } - player := game.players.Add(address, account) + player := NewPlayer(address, account, game) player.authToken = createAuthToken() player.KeepAlive() @@ -49,24 +49,10 @@ func (game *Game) Login(data []byte, address *net.UDPAddr, server *Server) error response = AppendString(response, player.authToken) server.Send(packet.Login, response, address) - game.onLogin(player) + game.players.Add(player) return nil } -// Inform the newly logged in player about existing players. -// Also inform existing players about the newly logged in player. -func (game *Game) onLogin(player *Player) { - game.players.Each(func(other *Player) bool { - game.server.Send(packet.PlayerAdd, other.State(), player.address) - - if other != player { - game.server.Send(packet.PlayerAdd, player.State(), other.address) - } - - return true - }) -} - func getLoginData(data []byte) (string, string, error) { loginRequest := [2]string{} err := json.Unmarshal(data, &loginRequest) diff --git a/server/game/Player.go b/server/game/Player.go index a1278ea..b4e98b9 100644 --- a/server/game/Player.go +++ b/server/game/Player.go @@ -3,22 +3,25 @@ package game import ( "fmt" "net" + "server/game/packet" "time" ) // Player represents a logged in client. type Player struct { *Account + game *Game authToken string address *net.UDPAddr lastPacket time.Time } // NewPlayer creates a new player. -func NewPlayer(account *Account, address *net.UDPAddr) *Player { +func NewPlayer(address *net.UDPAddr, account *Account, game *Game) *Player { return &Player{ Account: account, address: address, + game: game, } } @@ -48,8 +51,21 @@ func (player *Player) State() []byte { func (player *Player) OnConnect() { fmt.Printf("%s connected.\n", player.Name) + players := player.game.players + server := player.game.server + + players.Each(func(other *Player) bool { + server.Send(packet.PlayerAdd, other.State(), player.address) + + if other != player { + server.Send(packet.PlayerAdd, player.State(), other.address) + } + + return true + }) } func (player *Player) OnDisconnect() { fmt.Printf("%s disconnected.\n", player.Name) + player.game.BroadcastOthers(packet.PlayerRemove, []byte(player.ID), player) } diff --git a/server/game/PlayerManager.go b/server/game/PlayerManager.go index 8251361..1e34d13 100644 --- a/server/game/PlayerManager.go +++ b/server/game/PlayerManager.go @@ -30,9 +30,9 @@ func NewPlayerManager() *PlayerManager { player := value.(*Player) if !player.lastPacket.IsZero() && now.After(player.lastPacket.Add(timeout)) { - player.OnDisconnect() m.players.Delete(key) m.count.Add(-1) + player.OnDisconnect() } return true @@ -44,12 +44,10 @@ func NewPlayerManager() *PlayerManager { } // Add adds a new player with the given address and account. -func (m *PlayerManager) Add(address *net.UDPAddr, account *Account) *Player { - player := NewPlayer(account, address) - m.players.Store(address.String(), player) +func (m *PlayerManager) Add(player *Player) { + m.players.Store(player.address.String(), player) m.count.Add(1) player.OnConnect() - return player } // Contains tells you whether the address is already a registered client.