2020-06-18 14:11:04 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"encoding/json"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
|
|
|
"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-07-17 22:11:16 -04:00
|
|
|
ID string `json:"id"`
|
2020-06-18 14:11:04 -04:00
|
|
|
PlayerState *d2player.PlayerState `json:"gameState"`
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreatePlayerConnectionRequestPacket returns a NetPacket which defines a
|
|
|
|
// PlayerConnectionRequestPacket with the given ID and game state.
|
2020-06-18 14:11:04 -04:00
|
|
|
func CreatePlayerConnectionRequestPacket(id string, playerState *d2player.PlayerState) NetPacket {
|
2020-08-09 20:32:47 -04:00
|
|
|
playerConnectionRequest := PlayerConnectionRequestPacket{
|
|
|
|
ID: id,
|
|
|
|
PlayerState: playerState,
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(playerConnectionRequest)
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.PlayerConnectionRequest,
|
2020-08-09 20:32:47 -04:00
|
|
|
PacketData: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|