fix(server): grant non-stackable chapter rewards and expose all completed packs

This commit is contained in:
2026-10-04 15:05:12 +08:00
parent e1ec47bfae
commit 4e5c08a8c7
6 changed files with 98 additions and 17 deletions
+39 -5
View File
@@ -47,7 +47,7 @@ func (s *Service) handlePackBuy(request []byte) (int, []byte, bool, error) {
func (s *Service) grantPurchaseRewards(identity string, rewards []gamedata.Reward) ([]player.Item, error) {
var itemRewards []gamedata.BattleReward
for _, reward := range rewards {
if reward.Count == 0 {
if reward.Count == 0 && reward.Type != 11 {
return nil, fmt.Errorf("world: empty pack purchase reward")
}
if reward.Type == 19 {
@@ -57,6 +57,36 @@ func (s *Service) grantPurchaseRewards(identity string, rewards []gamedata.Rewar
itemRewards = append(itemRewards, gamedata.BattleReward{Type: reward.Type, ID: reward.ID, Count: reward.Count})
}
}
var costumeIDs []uint64
for _, reward := range rewards {
if reward.Type == 11 {
if s.questCostumes == nil {
return nil, fmt.Errorf("world: missing purchase costume design")
}
if _, found := s.questCostumes.Character(reward.ID); !found {
return nil, fmt.Errorf("world: missing purchase costume %d", reward.ID)
}
costumeIDs = append(costumeIDs, reward.ID)
}
}
if len(costumeIDs) > 0 {
grant, err := s.collection.GrantCostumes(identity+":costumes", costumeIDs, s.questCostumes)
if err != nil {
return nil, err
}
var exchanges []gamedata.Reward
for _, x := range grant.Exchanges {
if x.ExchangeItemType != 20 {
return nil, fmt.Errorf("world: unsupported purchase costume exchange")
}
exchanges = append(exchanges, gamedata.Reward{Type: x.ExchangeItemType, ID: x.ExchangeItemID, Count: x.ExchangeCount})
}
if len(exchanges) > 0 {
if _, err := s.wallet.GrantQuestOnce(identity+":costumes:exchange", exchanges); err != nil {
return nil, err
}
}
}
if _, err := s.wallet.GrantQuestOnce(identity+":currency", rewards); err != nil {
return nil, err
}
@@ -78,6 +108,7 @@ func (s *Service) purchaseStoryPack(id int, initial bool) ([]byte, error) {
if !exists {
return nil, fmt.Errorf("%w: unknown purchase pack %d", ErrInvalidRequest, id)
}
buyRewards := pack.BuyRewards
identity := packPurchaseIdentity(id)
if _, owned := s.collection.Grant(identity); owned {
return []byte{}, nil
@@ -85,12 +116,12 @@ func (s *Service) purchaseStoryPack(id int, initial bool) ([]byte, error) {
if !initial && pack.BuyPrice != 0 {
return nil, fmt.Errorf("%w: paid pack purchase type %d is unsupported", ErrInvalidRequest, pack.BuyType)
}
for _, reward := range pack.BuyRewards {
if reward.Type != 3 && reward.Type != 4 && reward.Type != 12 && reward.Type != 19 {
for _, reward := range buyRewards {
if reward.Type != 3 && reward.Type != 4 && reward.Type != 12 && reward.Type != 19 && reward.Type != 11 {
return nil, fmt.Errorf("world: unsupported pack purchase reward type %d", reward.Type)
}
}
items, err := s.grantPurchaseRewards(identity, pack.BuyRewards)
items, err := s.grantPurchaseRewards(identity, buyRewards)
if err != nil {
return nil, err
}
@@ -98,7 +129,10 @@ func (s *Service) purchaseStoryPack(id int, initial bool) ([]byte, error) {
return nil, err
}
var bundle []byte
for _, reward := range pack.BuyRewards {
if grant, found := s.collection.Grant(identity + ":costumes"); found {
bundle = append(bundle, player.CollectionRewardBundle(s.collection, grant)...)
}
for _, reward := range buyRewards {
if reward.Type == 3 || reward.Type == 4 || reward.Type == 12 {
currency := wire.AppendVarint(wire.AppendVarint(nil, 3, reward.Type), 4, reward.Count)
bundle = wire.AppendBytes(bundle, 1, currency)
+32 -1
View File
@@ -8,6 +8,13 @@ import (
"testing"
)
type purchaseTestCostumes map[uint64]gamedata.CharacterDesign
func (c purchaseTestCostumes) Character(id uint64) (gamedata.CharacterDesign, bool) {
d, ok := c[id]
return d, ok
}
func TestStoryCatalogUsesTicketsAndQuestLinksAcrossIndependentPacks(t *testing.T) {
s := testService()
s.seed.PackID = 701
@@ -67,6 +74,26 @@ func TestStoryCatalogUsesTicketsAndQuestLinksAcrossIndependentPacks(t *testing.T
if len(ids) != 2 || ids[0] != 405 || ids[1] != 701 {
t.Fatalf("terminal updates %v", ids)
}
if err := s.state.ClearQuest(33, 405); err != nil {
t.Fatal(err)
}
if err := s.state.ClearQuest(50, 701); err != nil {
t.Fatal(err)
}
var counts = map[uint64]uint64{}
if err := wire.Walk(s.accountPackInfo(), func(f wire.Field) error {
if f.Number == 2 {
id, _, _ := wire.Varint(f.Value, 1)
count, _, _ := wire.Varint(f.Value, 3)
counts[id] = count
}
return nil
}); err != nil {
t.Fatal(err)
}
if len(counts) != 2 || counts[405] != 1 || counts[701] != 2 {
t.Fatalf("completed normal levels count sidequests or omit nonseed pack: %v", counts)
}
}
func TestInitialPackPurchaseUsesDesignAndDurableIdentity(t *testing.T) {
@@ -86,7 +113,8 @@ func TestInitialPackPurchaseUsesDesignAndDurableIdentity(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s.storyCatalog = &gamedata.StoryCatalog{Packs: map[int]gamedata.StoryPack{707: {ID: 707, BuyRewards: []gamedata.Reward{{Type: 12, Count: 17}, {Type: 19, ID: 88, Count: 1}}}}}
s.storyCatalog = &gamedata.StoryCatalog{Packs: map[int]gamedata.StoryPack{707: {ID: 707, BuyRewards: []gamedata.Reward{{Type: 12, Count: 17}, {Type: 4, Count: 31}, {Type: 19, ID: 88, Count: 1}, {Type: 11, ID: 101, Count: 0}}}}}
s.questCostumes = purchaseTestCostumes{101: {ID: 10, HP: 100, CostumeMaxLevel: 5}}
for i := 0; i < 2; i++ {
if err := s.EnsureInitialPackPurchase(); err != nil {
t.Fatal(err)
@@ -95,6 +123,9 @@ func TestInitialPackPurchaseUsesDesignAndDurableIdentity(t *testing.T) {
if s.wallet.CatalystBalance() != 26 || len(s.inventory.All()) != 1 {
t.Fatal("purchase reward repeated or hardcoded")
}
if s.wallet.Snapshot().Gold != 31 || len(s.collection.Costumes()) != 1 {
t.Fatal("purchase gold or non-stackable level-zero costume handled incorrectly")
}
if _, owned := s.collection.Grant("pack-purchase:707"); !owned {
t.Fatal("missing purchase marker")
}
+8 -4
View File
@@ -67,7 +67,7 @@ func Load(seedPath, gameDataRoot, gameDataVersion string, storage stateio.Store,
packs[id] = pack.Quests
transitions[id] = gamedata.PackTransition{PackID: id, NextPackID: pack.NextPackID}
}
questCostumes, err := gamedata.LoadQuestCostumes(gameDataRoot, gameDataVersion, packs)
questCostumes, err := gamedata.LoadQuestCostumes(gameDataRoot, gameDataVersion, packs, storyCatalog)
if err != nil {
return nil, err
}
@@ -685,9 +685,13 @@ func (s *Service) accountPackInfo() []byte {
for _, info := range s.packDBInfoRows() {
out = wire.AppendBytes(out, 1, info)
}
if s.packComplete() {
level := wire.AppendVarint(nil, 1, uint64(s.seed.PackID))
level = wire.AppendVarint(level, 3, uint64(len(s.state.ClearedQuests(s.seed.PackID))))
for _, info := range s.packDBInfoRows() {
id, _, _ := wire.Varint(info, 1)
if !s.packCompleteFor(int(id)) {
continue
}
level := wire.AppendVarint(nil, 1, id)
level = wire.AppendVarint(level, 3, uint64(len(s.storyCatalog.Packs[int(id)].MainQuestIDs)))
level = wire.AppendVarint(level, 5, 1)
out = wire.AppendBytes(out, 2, level)
}