save player: level, health, mana, experience

Fixes #865
This commit is contained in:
Thomas Christlieb 2020-11-18 09:46:03 +01:00
parent 2153f5ce64
commit 0b76e1a561
6 changed files with 15 additions and 16 deletions

View File

@ -12,7 +12,7 @@ type HeroSkill struct {
*d2records.SkillRecord
*d2records.SkillDescriptionRecord
SkillPoints int
shallow *shallowHeroSkill
Shallow *shallowHeroSkill
}
// An auxiliary struct which only stores the ID of the SkillRecord, instead of the whole SkillRecord
@ -24,8 +24,8 @@ type shallowHeroSkill struct {
// MarshalJSON overrides the default logic used when the HeroSkill is serialized to a byte array.
func (hs *HeroSkill) MarshalJSON() ([]byte, error) {
// only serialize the shallow object instead of the SkillRecord & SkillDescriptionRecord
bytes, err := json.Marshal(hs.shallow)
// only serialize the Shallow object instead of the SkillRecord & SkillDescriptionRecord
bytes, err := json.Marshal(hs.Shallow)
if err != nil {
log.Fatalln(err)
}
@ -40,7 +40,7 @@ func (hs *HeroSkill) UnmarshalJSON(data []byte) error {
return err
}
hs.shallow = shallow
hs.Shallow = shallow
return nil
}

View File

@ -9,7 +9,6 @@ import (
type HeroState struct {
HeroName string `json:"heroName"`
HeroType d2enum.Hero `json:"heroType"`
HeroLevel int `json:"heroLevel"`
Act int `json:"act"`
FilePath string `json:"-"`
Equipment d2inventory.CharacterEquipment `json:"equipment"`

View File

@ -164,7 +164,7 @@ func (f *HeroStateFactory) CreateHeroSkill(points int, name string) (*HeroSkill,
SkillPoints: points,
SkillRecord: skillRecord,
SkillDescriptionRecord: skillDescRecord,
shallow: &shallowHeroSkill{SkillID: skillRecord.ID, SkillPoints: points},
Shallow: &shallowHeroSkill{SkillID: skillRecord.ID, SkillPoints: points},
}
return result, nil
@ -200,7 +200,7 @@ func (f *HeroStateFactory) LoadHeroState(filePath string) *HeroState {
return nil
}
// Here, we turn the shallow skill data back into records from the asset manager.
// Here, we turn the Shallow skill data back into records from the asset manager.
// This is because this factory has a reference to the asset manager with loaded records.
// We cant do this while unmarshalling because there is no reference to the asset manager.
for idx := range result.Skills {
@ -210,9 +210,9 @@ func (f *HeroStateFactory) LoadHeroState(filePath string) *HeroState {
continue
}
hs.SkillRecord = f.asset.Records.Skill.Details[hs.shallow.SkillID]
hs.SkillRecord = f.asset.Records.Skill.Details[hs.Shallow.SkillID]
hs.SkillDescriptionRecord = f.asset.Records.Skill.Descriptions[hs.SkillRecord.Skilldesc]
hs.SkillPoints = hs.shallow.SkillPoints
hs.SkillPoints = hs.Shallow.SkillPoints
}
return result

View File

@ -4,6 +4,7 @@ import (
"image/color"
"math"
"os"
"strconv"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
@ -346,7 +347,7 @@ func (v *CharacterSelect) updateCharacterBoxes() {
}
heroName := v.gameStates[idx].HeroName
heroInfo := "Level 1 " + v.gameStates[idx].HeroType.String()
heroInfo := "Level " + strconv.FormatInt(int64(v.gameStates[idx].Stats.Level), 10) + " " + v.gameStates[idx].HeroType.String()
v.characterNameLabel[i].SetText(d2ui.ColorTokenize(heroName, d2ui.ColorTokenGold))
v.characterStatsLabel[i].SetText(d2ui.ColorTokenize(heroInfo, d2ui.ColorTokenWhite))

View File

@ -11,15 +11,13 @@ import (
// SavePlayerPacket has the actual selected left and right skill
// the Server has to check if these skills are actually allowed for the Player
type SavePlayerPacket struct {
LeftSkill int `json:"leftSkill"`
RightSkill int `json:"rightSkill"`
Player *d2mapentity.Player `json:"Player"`
}
// CreateSavePlayerPacket sends a packet which instructs the server to save the Player
func CreateSavePlayerPacket(playerState *d2mapentity.Player) NetPacket {
savePlayerData := SavePlayerPacket{
LeftSkill: playerState.LeftSkill.ID,
RightSkill: playerState.RightSkill.ID,
Player: playerState,
}
b, err := json.Marshal(savePlayerData)

View File

@ -437,8 +437,9 @@ func (g *GameServer) OnPacketReceived(client ClientConnection, packet d2netpacke
}
playerState := g.connections[client.GetUniqueID()].GetPlayerState()
playerState.LeftSkill = savePacket.LeftSkill
playerState.RightSkill = savePacket.RightSkill
playerState.LeftSkill = savePacket.Player.LeftSkill.Shallow.SkillID
playerState.RightSkill = savePacket.Player.RightSkill.Shallow.SkillID
playerState.Stats = savePacket.Player.Stats
err = g.heroStateFactory.Save(playerState)
if err != nil {