1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 19:06:45 -05:00
OpenDiablo2/d2networking/d2netpacket/packet_update_server_info.go

45 lines
1.2 KiB
Go
Raw Normal View History

package d2netpacket
import (
"encoding/json"
2020-09-12 16:25:09 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
)
// UpdateServerInfoPacket contains the ID for a player and the map seed.
// It is sent by the server to synchronize these values on the client.
type UpdateServerInfoPacket struct {
Seed int64 `json:"seed"`
PlayerID string `json:"playerId"`
}
// CreateUpdateServerInfoPacket returns a NetPacket which declares an
// UpdateServerInfoPacket with the given player ID and map seed.
2020-11-23 08:18:30 -05:00
func CreateUpdateServerInfoPacket(seed int64, playerID string) (NetPacket, error) {
updateServerInfo := UpdateServerInfoPacket{
Seed: seed,
PlayerID: playerID,
}
b, err := json.Marshal(updateServerInfo)
if err != nil {
2020-11-23 08:18:30 -05:00
return NetPacket{PacketType: d2netpackettype.UpdateServerInfo}, err
}
return NetPacket{
PacketType: d2netpackettype.UpdateServerInfo,
PacketData: b,
2020-11-23 08:18:30 -05:00
}, nil
}
// UnmarshalUpdateServerInfo unmarshals the data to a UpdateServerInfoPacket struct
func UnmarshalUpdateServerInfo(packet []byte) (UpdateServerInfoPacket, error) {
var resp UpdateServerInfoPacket
if err := json.Unmarshal(packet, &resp); err != nil {
return resp, err
}
2020-09-12 16:25:09 -04:00
return resp, nil
}