2020-09-20 11:55:44 -04:00
|
|
|
package d2hero
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
|
2020-09-20 11:55:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// HeroSkill stores additional payload for a skill of a hero.
|
|
|
|
type HeroSkill struct {
|
2020-09-20 17:52:01 -04:00
|
|
|
*d2records.SkillRecord
|
|
|
|
*d2records.SkillDescriptionRecord
|
2020-09-20 11:55:44 -04:00
|
|
|
SkillPoints int
|
2020-11-18 03:46:03 -05:00
|
|
|
Shallow *shallowHeroSkill
|
2020-09-20 11:55:44 -04:00
|
|
|
}
|
|
|
|
|
2020-10-26 05:04:50 -04:00
|
|
|
// An auxiliary struct which only stores the ID of the SkillRecord, instead of the whole SkillRecord
|
|
|
|
// and SkillDescrptionRecord.
|
2020-09-20 11:55:44 -04:00
|
|
|
type shallowHeroSkill struct {
|
|
|
|
SkillID int `json:"skillId"`
|
|
|
|
SkillPoints int `json:"skillPoints"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON overrides the default logic used when the HeroSkill is serialized to a byte array.
|
|
|
|
func (hs *HeroSkill) MarshalJSON() ([]byte, error) {
|
2020-11-18 03:46:03 -05:00
|
|
|
// only serialize the Shallow object instead of the SkillRecord & SkillDescriptionRecord
|
|
|
|
bytes, err := json.Marshal(hs.Shallow)
|
2020-09-20 11:55:44 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
return nil, err
|
2020-09-20 11:55:44 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
return bytes, nil
|
2020-09-20 11:55:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON overrides the default logic used when the HeroSkill is deserialized from a byte array.
|
|
|
|
func (hs *HeroSkill) UnmarshalJSON(data []byte) error {
|
2020-09-20 17:52:01 -04:00
|
|
|
shallow := &shallowHeroSkill{}
|
|
|
|
if err := json.Unmarshal(data, shallow); err != nil {
|
2020-09-20 11:55:44 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-18 03:46:03 -05:00
|
|
|
hs.Shallow = shallow
|
2020-09-20 11:55:44 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|