Implemented disconnects
This commit is contained in:
parent
5c6df98909
commit
70ebe4f05a
12
client/network/PlayerRemove.gd
Normal file
12
client/network/PlayerRemove.gd
Normal file
@ -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")
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user