2020-06-22 20:31:42 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"encoding/json"
|
2020-06-22 20:31:42 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// PlayerDisconnectRequestPacket contains a player ID and game state.
|
|
|
|
// It is sent by a remote client to close the connection (leave a game).
|
2020-06-22 20:31:42 -04:00
|
|
|
type PlayerDisconnectRequestPacket struct {
|
2020-07-17 22:11:16 -04:00
|
|
|
ID string `json:"id"`
|
2020-06-29 17:01:26 -04:00
|
|
|
PlayerState *d2player.PlayerState `json:"gameState"` // TODO: remove this? It isn't used.
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreatePlayerDisconnectRequestPacket returns a NetPacket which defines a
|
|
|
|
// PlayerDisconnectRequestPacket with the given ID.
|
2020-06-22 20:31:42 -04:00
|
|
|
func CreatePlayerDisconnectRequestPacket(id string) NetPacket {
|
2020-08-09 20:32:47 -04:00
|
|
|
playerDisconnectRequest := PlayerDisconnectRequestPacket{
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(playerDisconnectRequest)
|
|
|
|
|
2020-06-22 20:31:42 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.PlayerDisconnectionNotification,
|
2020-08-09 20:32:47 -04:00
|
|
|
PacketData: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalPlayerDisconnectionRequest(packet []byte) (PlayerDisconnectRequestPacket, error) {
|
|
|
|
var resp PlayerDisconnectRequestPacket
|
|
|
|
|
|
|
|
if err := json.Unmarshal(packet, &resp); err != nil {
|
|
|
|
return resp, err
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|