2020-06-18 14:11:04 -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-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// PlayerConnectionRequestPacket contains a player ID and game state.
|
|
|
|
// It is sent by a remote client to initiate a connection (join a game).
|
2020-06-18 14:11:04 -04:00
|
|
|
type PlayerConnectionRequestPacket struct {
|
2020-09-20 17:52:01 -04:00
|
|
|
ID string `json:"id"`
|
|
|
|
PlayerState *d2hero.HeroState `json:"gameState"`
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreatePlayerConnectionRequestPacket returns a NetPacket which defines a
|
|
|
|
// PlayerConnectionRequestPacket with the given ID and game state.
|
2020-11-23 08:18:30 -05:00
|
|
|
func CreatePlayerConnectionRequestPacket(id string, playerState *d2hero.HeroState) (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
playerConnectionRequest := PlayerConnectionRequestPacket{
|
|
|
|
ID: id,
|
|
|
|
PlayerState: playerState,
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(playerConnectionRequest)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.PlayerConnectionRequest}, err
|
2020-09-23 13:30:54 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.PlayerConnectionRequest,
|
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
|
|
|
// UnmarshalPlayerConnectionRequest unmarshals the given data to a
|
|
|
|
// PlayerConnectionRequestPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
func UnmarshalPlayerConnectionRequest(packet []byte) (PlayerConnectionRequestPacket, error) {
|
|
|
|
var resp PlayerConnectionRequestPacket
|
|
|
|
|
|
|
|
if err := json.Unmarshal(packet, &resp); err != nil {
|
|
|
|
return PlayerConnectionRequestPacket{}, err
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|