2020-09-20 11:55:44 -04:00
|
|
|
package d2hero
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
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-09-20 17:52:01 -04:00
|
|
|
shallow *shallowHeroSkill
|
2020-09-20 11:55:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// An auxilary struct which only stores the ID of the SkillRecord, instead of the whole SkillRecord and SkillDescrptionRecord.
|
|
|
|
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-10-10 18:47:51 -04: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 {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-20 17:52:01 -04:00
|
|
|
hs.shallow = shallow
|
2020-09-20 11:55:44 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|