2020-10-31 14:30:08 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-11-25 05:48:23 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
|
2020-10-31 14:30:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapentity"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SavePlayerPacket has the actual selected left and right skill
|
|
|
|
// the Server has to check if these skills are actually allowed for the Player
|
|
|
|
type SavePlayerPacket struct {
|
2020-11-25 05:48:23 -05:00
|
|
|
Player *d2mapentity.Player `json:"Player"`
|
|
|
|
Difficulty d2enum.DifficultyType `json:"Difficulty"`
|
2020-10-31 14:30:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSavePlayerPacket sends a packet which instructs the server to save the Player
|
2020-11-25 05:48:23 -05:00
|
|
|
func CreateSavePlayerPacket(playerState *d2mapentity.Player, difficulty d2enum.DifficultyType) (NetPacket, error) {
|
2020-10-31 14:30:08 -04:00
|
|
|
savePlayerData := SavePlayerPacket{
|
2020-11-25 05:48:23 -05:00
|
|
|
Player: playerState,
|
|
|
|
Difficulty: difficulty,
|
2020-10-31 14:30:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(savePlayerData)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.SavePlayer}, err
|
2020-10-31 14:30:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.SavePlayer,
|
|
|
|
PacketData: b,
|
2020-11-23 08:18:30 -05:00
|
|
|
}, nil
|
2020-10-31 14:30:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalSavePlayer unmarshalls the given data to a SavePlayerPacket struct
|
|
|
|
func UnmarshalSavePlayer(packet []byte) (SavePlayerPacket, error) {
|
|
|
|
var p SavePlayerPacket
|
|
|
|
if err := json.Unmarshal(packet, &p); err != nil {
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|