2020-06-22 20:31:42 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"encoding/json"
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
|
|
|
|
2020-06-22 20:31:42 -04:00
|
|
|
"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-09-20 17:52:01 -04:00
|
|
|
ID string `json:"id"`
|
2020-10-25 18:36:12 -04:00
|
|
|
PlayerState *d2hero.HeroState `json:"gameState"`
|
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-11-23 08:18:30 -05:00
|
|
|
func CreatePlayerDisconnectRequestPacket(id string) (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
playerDisconnectRequest := PlayerDisconnectRequestPacket{
|
|
|
|
ID: id,
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(playerDisconnectRequest)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.PlayerDisconnectionNotification}, err
|
2020-09-23 13:30:54 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
|
2020-06-22 20:31:42 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.PlayerDisconnectionNotification,
|
2020-08-09 20:32:47 -04:00
|
|
|
PacketData: b,
|
2020-11-23 08:18:30 -05:00
|
|
|
}, nil
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:41:21 -04:00
|
|
|
// UnmarshalPlayerDisconnectionRequest unmarshals the given data to a
|
|
|
|
// PlayerDisconnectRequestPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
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-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-22 20:31:42 -04:00
|
|
|
}
|