2020-06-13 18:32:09 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
2020-06-13 18:32:09 -04:00
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// UpdateServerInfoPacket contains the ID for a player and the map seed.
|
2020-07-17 22:11:16 -04:00
|
|
|
// It is sent by the server to synchronize these values on the client.
|
2020-06-13 18:32:09 -04:00
|
|
|
type UpdateServerInfoPacket struct {
|
|
|
|
Seed int64 `json:"seed"`
|
2020-07-17 22:11:16 -04:00
|
|
|
PlayerID string `json:"playerId"`
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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) {
|
2020-08-09 20:32:47 -04:00
|
|
|
updateServerInfo := UpdateServerInfoPacket{
|
|
|
|
Seed: seed,
|
|
|
|
PlayerID: playerID,
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(updateServerInfo)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.UpdateServerInfo}, err
|
2020-09-23 13:30:54 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
return NetPacket{
|
|
|
|
PacketType: d2netpackettype.UpdateServerInfo,
|
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
|
|
|
// UnmarshalUpdateServerInfo unmarshals the data to a UpdateServerInfoPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
func UnmarshalUpdateServerInfo(packet []byte) (UpdateServerInfoPacket, error) {
|
|
|
|
var resp UpdateServerInfoPacket
|
|
|
|
|
|
|
|
if err := json.Unmarshal(packet, &resp); err != nil {
|
|
|
|
return resp, err
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return resp, nil
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|