2020-06-13 18:32:09 -04:00
|
|
|
package d2netpacket
|
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"encoding/json"
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-25 14:56:49 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2inventory"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// AddPlayerPacket contains the data required to create a Player entity.
|
|
|
|
// It is sent by the server to create the entity for a newly connected
|
|
|
|
// player on a client.
|
2020-06-13 18:32:09 -04:00
|
|
|
type AddPlayerPacket struct {
|
2020-10-31 14:30:08 -04:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
HeroType d2enum.Hero `json:"hero"`
|
|
|
|
Equipment d2inventory.CharacterEquipment `json:"equipment"`
|
|
|
|
Stats *d2hero.HeroStatsState `json:"heroStats"`
|
|
|
|
Skills map[int]*d2hero.HeroSkill `json:"heroSkills"`
|
|
|
|
LeftSkill int `json:"leftSkill"`
|
|
|
|
RightSkill int `json:"rightSkill"`
|
2020-11-22 17:53:33 -05:00
|
|
|
Gold int
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreateAddPlayerPacket returns a NetPacket which declares an
|
|
|
|
// AddPlayerPacket with the data in given parameters.
|
2020-11-22 17:53:33 -05:00
|
|
|
func CreateAddPlayerPacket(
|
|
|
|
id, name string,
|
|
|
|
x, y int,
|
|
|
|
heroType d2enum.Hero,
|
|
|
|
stats *d2hero.HeroStatsState,
|
|
|
|
skills map[int]*d2hero.HeroSkill,
|
|
|
|
equipment d2inventory.CharacterEquipment,
|
|
|
|
leftSkill, rightSkill int,
|
2020-11-23 08:18:30 -05:00
|
|
|
gold int) (NetPacket, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
addPlayerPacket := AddPlayerPacket{
|
2020-10-31 14:30:08 -04:00
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
|
|
|
HeroType: heroType,
|
|
|
|
Equipment: equipment,
|
|
|
|
Stats: stats,
|
|
|
|
Skills: skills,
|
|
|
|
LeftSkill: leftSkill,
|
|
|
|
RightSkill: rightSkill,
|
2020-11-22 17:53:33 -05:00
|
|
|
Gold: gold,
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
b, err := json.Marshal(addPlayerPacket)
|
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
return NetPacket{PacketType: d2netpackettype.AddPlayer}, 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.AddPlayer,
|
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
|
|
|
// UnmarshalAddPlayer unmarshals the packet data into an AddPlayerPacket struct
|
2020-08-09 20:32:47 -04:00
|
|
|
func UnmarshalAddPlayer(packet []byte) (AddPlayerPacket, error) {
|
|
|
|
var p AddPlayerPacket
|
|
|
|
if err := json.Unmarshal(packet, &p); err != nil {
|
|
|
|
return p, err
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
|
|
|
|
return p, nil
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|