fix(server): correct life rewards and expand operator item grants
Use native millisecond timestamps for life timers and preserve resource variants on regeneration. Return reward deltas so gathering and crafting caches match persisted balances; log exact material shortages. Expose all life materials, crops, recipes, furnishings and Local Points in the operator catalog. Credit mail through the owning life economy and prevent duplicate claims, with localized categories and regressions.
This commit is contained in:
1 parent
9db4e8356c
commit
2d2678e0b5
16 files changed
+575
-24
No files matched your search
@@ -86,6 +86,12 @@
|
||||
"console.mail.category.collection": "Collectibles",
|
||||
"console.mail.category.my_room": "My Room items",
|
||||
"console.mail.category.instant_use": "Instant-use items",
|
||||
"console.mail.category.life_furniture": "Life furniture",
|
||||
"console.mail.category.life_building": "Life buildings",
|
||||
"console.mail.category.life_decoration": "Life decorations",
|
||||
"console.mail.category.life_material": "Life materials",
|
||||
"console.mail.category.life_product": "Life produce",
|
||||
"console.mail.category.life_cooking": "Life food",
|
||||
"console.mail.count.all": "%[1]s items",
|
||||
"console.mail.count.matched": "%[1]s of %[2]s items",
|
||||
"console.mail.count.showing": "showing the first %[1]s",
|
||||
|
||||
@@ -86,6 +86,12 @@
|
||||
"console.mail.category.collection": "収集品",
|
||||
"console.mail.category.my_room": "マイルームアイテム",
|
||||
"console.mail.category.instant_use": "即時使用アイテム",
|
||||
"console.mail.category.life_furniture": "生活の家具",
|
||||
"console.mail.category.life_building": "生活の建物",
|
||||
"console.mail.category.life_decoration": "生活の装飾",
|
||||
"console.mail.category.life_material": "生活の素材",
|
||||
"console.mail.category.life_product": "生活の農産物",
|
||||
"console.mail.category.life_cooking": "生活の料理",
|
||||
"console.mail.count.all": "%[1]s 件",
|
||||
"console.mail.count.matched": "%[2]s 件中 %[1]s 件",
|
||||
"console.mail.count.showing": "先頭 %[1]s 件を表示",
|
||||
|
||||
@@ -86,6 +86,12 @@
|
||||
"console.mail.category.collection": "수집품",
|
||||
"console.mail.category.my_room": "마이룸 아이템",
|
||||
"console.mail.category.instant_use": "즉시 사용 아이템",
|
||||
"console.mail.category.life_furniture": "생활 가구",
|
||||
"console.mail.category.life_building": "생활 건물",
|
||||
"console.mail.category.life_decoration": "생활 장식",
|
||||
"console.mail.category.life_material": "생활 재료",
|
||||
"console.mail.category.life_product": "생활 농산물",
|
||||
"console.mail.category.life_cooking": "생활 요리",
|
||||
"console.mail.count.all": "아이템 %[1]s개",
|
||||
"console.mail.count.matched": "전체 %[2]s개 중 %[1]s개",
|
||||
"console.mail.count.showing": "처음 %[1]s개 표시",
|
||||
|
||||
@@ -86,6 +86,12 @@
|
||||
"console.mail.category.collection": "收藏品",
|
||||
"console.mail.category.my_room": "我的房间物品",
|
||||
"console.mail.category.instant_use": "即时使用物品",
|
||||
"console.mail.category.life_furniture": "生活家具",
|
||||
"console.mail.category.life_building": "生活建筑",
|
||||
"console.mail.category.life_decoration": "生活装饰",
|
||||
"console.mail.category.life_material": "生活材料",
|
||||
"console.mail.category.life_product": "生活农产品",
|
||||
"console.mail.category.life_cooking": "生活料理",
|
||||
"console.mail.count.all": "共 %[1]s 项",
|
||||
"console.mail.count.matched": "匹配 %[1]s / %[2]s 项",
|
||||
"console.mail.count.showing": "显示前 %[1]s 项",
|
||||
|
||||
@@ -86,6 +86,12 @@
|
||||
"console.mail.category.collection": "收藏品",
|
||||
"console.mail.category.my_room": "我的房間物品",
|
||||
"console.mail.category.instant_use": "即時使用物品",
|
||||
"console.mail.category.life_furniture": "生活家具",
|
||||
"console.mail.category.life_building": "生活建築",
|
||||
"console.mail.category.life_decoration": "生活裝飾",
|
||||
"console.mail.category.life_material": "生活材料",
|
||||
"console.mail.category.life_product": "生活農產品",
|
||||
"console.mail.category.life_cooking": "生活料理",
|
||||
"console.mail.count.all": "共 %[1]s 項",
|
||||
"console.mail.count.matched": "符合 %[1]s / %[2]s 項",
|
||||
"console.mail.count.showing": "顯示前 %[1]s 項",
|
||||
|
||||
@@ -51,6 +51,8 @@ var mailItemSources = []mailItemSource{
|
||||
{"CollectionTable", 17, 3, 7, "collection", "NameTextTable"},
|
||||
{"MyRoomItemTable", 27, 7, 17, "my_room", "NameTextTable"},
|
||||
{"InstantUseItemTable", 29, 1, 3, "instant_use", "NameTextTable"},
|
||||
{"LifeObjectGroupTable", 65, 5, 11, "life_furniture", "NameTextTable"},
|
||||
{"LifeItemTable", 66, 5, 7, "life_material", "NameTextTable"},
|
||||
}
|
||||
|
||||
// MailCurrencyTypes are the account currencies whose mail claim is credited
|
||||
@@ -61,7 +63,7 @@ var MailCurrencyTypes = []uint64{2, 3, 4, 12, 20}
|
||||
// carry: their claim is credited by the event economy (wallet hope powder and
|
||||
// refining traces, action points and the gameplay-entry balances such as the
|
||||
// Cafeteria and decoration coins). Their reward ID is zero as well.
|
||||
var MailEconomyCurrencyTypes = []uint64{15, 16, 18, 21, 22, 23, 24, 30, 31, 32, 33, 35, 38, 39, 40, 41, 42, 43, 44, 52, 53, 54, 60, 68, 70, 72}
|
||||
var MailEconomyCurrencyTypes = []uint64{15, 16, 18, 21, 22, 23, 24, 30, 31, 32, 33, 35, 38, 39, 40, 41, 42, 43, 44, 52, 53, 54, 60, 64, 68, 70, 72}
|
||||
|
||||
func mailCurrency(elementType uint64) bool {
|
||||
return slices.Contains(MailCurrencyTypes, elementType) || slices.Contains(MailEconomyCurrencyTypes, elementType)
|
||||
@@ -99,6 +101,22 @@ func loadMailCatalog(db *sql.DB) ([]MailCatalogItem, error) {
|
||||
if source.table == "ResourceTable" {
|
||||
item.resourceType = singleVarint(proto, 13)
|
||||
}
|
||||
if source.table == "LifeObjectGroupTable" {
|
||||
switch singleVarint(proto, 12) {
|
||||
case 3:
|
||||
item.Category = "life_building"
|
||||
case 4:
|
||||
item.Category = "life_decoration"
|
||||
}
|
||||
}
|
||||
if source.table == "LifeItemTable" {
|
||||
switch singleVarint(proto, 9) {
|
||||
case 2:
|
||||
item.Category = "life_product"
|
||||
case 3:
|
||||
item.Category = "life_cooking"
|
||||
}
|
||||
}
|
||||
items = append(items, item)
|
||||
return nil
|
||||
})
|
||||
@@ -374,6 +392,12 @@ func SortMailCatalog(items []MailCatalogItem) {
|
||||
for i, source := range mailItemSources {
|
||||
order[source.category] = i + 1
|
||||
}
|
||||
for _, category := range []string{"life_building", "life_decoration"} {
|
||||
order[category] = order["life_furniture"]
|
||||
}
|
||||
for _, category := range []string{"life_product", "life_cooking"} {
|
||||
order[category] = order["life_material"]
|
||||
}
|
||||
sort.SliceStable(items, func(i, j int) bool {
|
||||
a, b := items[i], items[j]
|
||||
if order[a.Category] != order[b.Category] {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package gamedata
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"bd2server/internal/server/protocol/wire"
|
||||
)
|
||||
|
||||
func TestMailCatalogIncludesLifeCurrencyObjectsAndAllItemKinds(t *testing.T) {
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
for _, table := range []string{"NameTextTable", "RandomBoxTextTable", "LocalTextTable", "ResourceTable", "FoodTable", "CookingTable", "RandomBoxTable", "QuestItemTable", "UseItemTable", "CollectionTable", "MyRoomItemTable", "InstantUseItemTable", "LifeObjectGroupTable", "LifeItemTable", "CurrencyTable", "RewardGroupTable", "CashProductTable"} {
|
||||
if _, err := db.Exec(`CREATE TABLE "` + table + `" (id INTEGER PRIMARY KEY, ProtoBuf BLOB NOT NULL)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
encode := func(values ...uint64) []byte {
|
||||
var b []byte
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
b = wire.AppendVarint(b, int(values[i]), values[i+1])
|
||||
}
|
||||
return b
|
||||
}
|
||||
insert := func(table string, id uint64, b []byte) {
|
||||
t.Helper()
|
||||
if _, err := db.Exec(`INSERT INTO "`+table+`" (id, ProtoBuf) VALUES (?, ?)`, id, b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Names and field numbers follow the 2.37.13 LifeItemTable,
|
||||
// LifeObjectGroupTable and CurrencyTable generated Proto definitions.
|
||||
for id, name := range map[uint64]string{1: "Oak plank", 2: "Carrot", 3: "Stew", 4: "Chair", 5: "Workshop", 6: "Garden statue", 7: "Life coins"} {
|
||||
insert("NameTextTable", id, wire.AppendString(encode(2, id), 4, name))
|
||||
}
|
||||
insert("LifeItemTable", 101, encode(5, 101, 7, 1, 9, 1))
|
||||
insert("LifeItemTable", 102, encode(5, 102, 7, 2, 9, 2))
|
||||
insert("LifeItemTable", 103, encode(5, 103, 7, 3, 9, 3))
|
||||
insert("LifeObjectGroupTable", 201, encode(5, 201, 11, 4, 12, 1))
|
||||
insert("LifeObjectGroupTable", 202, encode(5, 202, 11, 5, 12, 3))
|
||||
insert("LifeObjectGroupTable", 203, encode(5, 203, 11, 6, 12, 4))
|
||||
insert("CurrencyTable", 64, encode(3, 64, 5, 7))
|
||||
items, err := loadMailCatalog(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[[2]uint64][2]string{
|
||||
{64, 0}: {"Life coins", "currency"},
|
||||
{65, 201}: {"Chair", "life_furniture"},
|
||||
{65, 202}: {"Workshop", "life_building"},
|
||||
{65, 203}: {"Garden statue", "life_decoration"},
|
||||
{66, 101}: {"Oak plank", "life_material"},
|
||||
{66, 102}: {"Carrot", "life_product"},
|
||||
{66, 103}: {"Stew", "life_cooking"},
|
||||
}
|
||||
if len(items) != len(want) {
|
||||
t.Fatalf("catalog contains %d entries, want %d: %+v", len(items), len(want), items)
|
||||
}
|
||||
for _, item := range items {
|
||||
key := [2]uint64{item.ElementType, item.ID}
|
||||
expected, ok := want[key]
|
||||
if !ok || item.Name != expected[0] || item.Category != expected[1] {
|
||||
t.Fatalf("unexpected catalog entry: %+v", item)
|
||||
}
|
||||
delete(want, key)
|
||||
}
|
||||
if len(want) != 0 {
|
||||
t.Fatalf("missing life entries: %v", want)
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,13 @@ import (
|
||||
"bd2server/internal/server/protocol/wire"
|
||||
)
|
||||
|
||||
func (s *Service) economy(v *snapshot, path string, req []byte) ([]byte, error) {
|
||||
func (s *Service) economy(v *snapshot, path string, req []byte) (body []byte, err error) {
|
||||
id, count := num(req, 2), num(req, 3)
|
||||
defer func() {
|
||||
if err != nil && (path == "/LifeCrafting" || path == "/LifeCooking") {
|
||||
err = fmt.Errorf("%w: operation=%s recipe=%d count=%d", err, path, id, count)
|
||||
}
|
||||
}()
|
||||
switch path {
|
||||
case "/LifeCrafting":
|
||||
d := s.design.Row("LifeCraftingObjectTable", id)
|
||||
@@ -106,7 +111,7 @@ func (s *Service) economy(v *snapshot, path string, req []byte) ([]byte, error)
|
||||
return nil, err
|
||||
}
|
||||
v.Foods[id] = s.options.Now().Unix() + int64(duration)
|
||||
return wire.AppendBytes(nil, 1, row(1, id, 2, uint64(v.Foods[id]))), nil
|
||||
return wire.AppendBytes(nil, 1, row(1, id, 2, uint64(v.Foods[id]*1000))), nil
|
||||
case "/LifeShopBuy":
|
||||
return s.buy(v, req)
|
||||
case "/LifeShopSell":
|
||||
@@ -233,7 +238,7 @@ func (s *Service) shopInfo(v *snapshot) []byte {
|
||||
}
|
||||
week := time.Date(now.Year(), now.Month(), now.Day()+days, 0, 0, 0, 0, time.UTC)
|
||||
month := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
||||
b := row(2, uint64(day.Unix()), 3, uint64(week.Unix()), 4, uint64(month.Unix()))
|
||||
b := row(2, uint64(day.UnixMilli()), 3, uint64(week.UnixMilli()), 4, uint64(month.UnixMilli()))
|
||||
for key, p := range v.Purchases {
|
||||
var group, id uint64
|
||||
if _, err := fmt.Sscanf(key, "%d:%d", &group, &id); err != nil {
|
||||
|
||||
@@ -361,7 +361,7 @@ func (s *Service) helperPreview(v *snapshot) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return wire.AppendBytes(row(1, uint64(earliest)), 2, rewardPreview(gains)), nil
|
||||
return wire.AppendBytes(row(1, uint64(earliest*1000)), 2, rewardPreview(gains)), nil
|
||||
}
|
||||
func (s *Service) settle(v *snapshot, response bool) ([]byte, error) {
|
||||
gains, progress, _, err := s.helperRewards(v)
|
||||
|
||||
@@ -183,7 +183,7 @@ func sorted[V any](m map[uint64]V) []uint64 {
|
||||
return ids
|
||||
}
|
||||
func objectWire(o *object) []byte {
|
||||
b := row(1, uint64(int64(o.X)), 2, uint64(int64(o.Y)), 3, o.Rotate, 4, o.Index, 6, o.ID, 7, o.Status, 8, uint64(o.Start), 9, uint64(o.End))
|
||||
b := row(1, uint64(int64(o.X)), 2, uint64(int64(o.Y)), 3, o.Rotate, 4, o.Index, 6, o.ID, 7, o.Status, 8, uint64(o.Start*1000), 9, uint64(o.End*1000))
|
||||
for _, id := range sorted(o.Inner) {
|
||||
b = wire.AppendBytes(b, 10, objectWire(o.Inner[id]))
|
||||
}
|
||||
@@ -217,7 +217,7 @@ func userWire(v *snapshot) []byte {
|
||||
}
|
||||
func citizenWire(c citizen) []byte { return wire.AppendBytes(row(1, c.Index, 2, c.Slot), 3, c.Avatar) }
|
||||
func helperWire(h helper) []byte {
|
||||
b := row(1, h.Index, 2, h.ID, 3, h.Slot, 6, h.Work, 7, h.WorkID, 8, uint64(h.Assigned))
|
||||
b := row(1, h.Index, 2, h.ID, 3, h.Slot, 6, h.Work, 7, h.WorkID, 8, uint64(h.Assigned*1000))
|
||||
b = wire.AppendString(b, 4, h.Name)
|
||||
return wire.AppendBytes(b, 5, h.Avatar)
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func (s *Service) info(v *snapshot, full bool) []byte {
|
||||
}
|
||||
for _, id := range sorted(v.Foods) {
|
||||
if v.Foods[id] > s.options.Now().Unix() {
|
||||
b = wire.AppendBytes(b, 6, row(1, id, 2, uint64(v.Foods[id])))
|
||||
b = wire.AppendBytes(b, 6, row(1, id, 2, uint64(v.Foods[id]*1000)))
|
||||
}
|
||||
}
|
||||
for _, id := range sorted(v.Collections) {
|
||||
@@ -441,14 +441,14 @@ func (s *Service) spend(v *snapshot, costs []gamedata.Reward) error {
|
||||
switch r.Type {
|
||||
case 64:
|
||||
if v.Coin < r.Count {
|
||||
return ErrFunds
|
||||
return fmt.Errorf("%w: type=%d id=%d need=%d have=%d", ErrFunds, r.Type, r.ID, r.Count, v.Coin)
|
||||
}
|
||||
v.Coin -= r.Count
|
||||
case 65, 66:
|
||||
key := itemKey(r.Type, r.ID)
|
||||
it := v.Items[key]
|
||||
if it.Count < r.Count {
|
||||
return ErrFunds
|
||||
return fmt.Errorf("%w: type=%d id=%d need=%d have=%d", ErrFunds, r.Type, r.ID, r.Count, it.Count)
|
||||
}
|
||||
it.Count -= r.Count
|
||||
v.Items[key] = it
|
||||
@@ -499,6 +499,7 @@ func (s *Service) grant(v *snapshot, gains []gamedata.Reward) ([]byte, error) {
|
||||
for _, id := range s.design.CollectionsByItem[[2]uint64{r.Type, r.ID}] {
|
||||
v.Collections[id] = true
|
||||
}
|
||||
it.Count = r.Count
|
||||
b = wire.AppendBytes(b, 1, itemWire(it))
|
||||
b = wire.AppendBytes(b, 6, itemWire(item{ID: r.ID, Type: r.Type, Count: r.Count}))
|
||||
}
|
||||
|
||||
@@ -165,9 +165,15 @@ func TestResidentsWorkersGrowthAndCurrencySurviveRealActionFlow(t *testing.T) {
|
||||
recruit := child(child(nil, 3, candidate), 4, workerHouse)
|
||||
g.call("/LifeHelperRecruit", recruit)
|
||||
helper := child(proto(1, scalar(t, candidate, 1), 6, 1), 5, avatar)
|
||||
g.call("/LifeHelperAssign", child(child(nil, 2, helper), 3, workerHouse))
|
||||
assigned := g.call("/LifeHelperAssign", child(child(nil, 2, helper), 3, workerHouse))
|
||||
if got := scalar(t, nested(t, assigned, 1), 8); got != uint64(g.now.UnixMilli()) {
|
||||
t.Fatalf("native helper assignment timestamp: got %d, want %d milliseconds", got, g.now.UnixMilli())
|
||||
}
|
||||
g.now = g.now.Add(20 * time.Second)
|
||||
preview := g.call("/LifeHelperRewardInfo", nil)
|
||||
if got := scalar(t, preview, 1); got != uint64(g.now.Add(-20*time.Second).UnixMilli()) {
|
||||
t.Fatalf("native helper preview timestamp: got %d", got)
|
||||
}
|
||||
previewItems := repeated(t, nested(t, preview, 2), 1)
|
||||
if len(previewItems) != 1 || scalar(t, previewItems[0], 4) != 8 {
|
||||
t.Fatalf("two work cycles at four per cycle: %x", preview)
|
||||
@@ -298,3 +304,49 @@ func TestAtomicWriteFailureDoesNotLeakCandidateStateOrReceipt(t *testing.T) {
|
||||
t.Fatal("successful retry must charge once")
|
||||
}
|
||||
}
|
||||
|
||||
// TimerManager.UnixTimeStampToDateTime consumes milliseconds for resource,
|
||||
// construction, food and shop timers; saved life state keeps seconds.
|
||||
func TestLifeWireTimersMatchNativeMilliseconds(t *testing.T) {
|
||||
g := scenario(t)
|
||||
g.call("/LifeWorldChoice", proto(2, 91))
|
||||
info := g.call("/LifeInfo", nil)
|
||||
forest := nested(t, nested(t, info, 1), 2)
|
||||
inner := nested(t, forest, 10)
|
||||
if got := scalar(t, inner, 8); got != uint64(g.now.UnixMilli()) {
|
||||
t.Fatalf("resource creation time: got %d, want %d", got, g.now.UnixMilli())
|
||||
}
|
||||
gather := child(nil, 2, child(proto(1, 1), 2, child(proto(1, 2, 2, 3, 6, 301), 10, proto(4, 0, 6, 1, 7, 1))))
|
||||
harvest := g.call("/LifeWorldObjectGathering", gather)
|
||||
spent := nested(t, nested(t, nested(t, harvest, 3), 2), 10)
|
||||
deadline := time.UnixMilli(int64(scalar(t, spent, 8))).Add(30 * time.Second)
|
||||
if scalar(t, spent, 7) != 0 || !deadline.Equal(g.now.Add(30*time.Second)) {
|
||||
t.Fatalf("native resource must remain spent until %s, got %s", g.now.Add(30*time.Second), deadline)
|
||||
}
|
||||
g.fund("construction", 0, 3)
|
||||
building := nested(t, nested(t, g.call("/LifeWorldObjectPlaceSave", child(nil, 2, placement(302, 5, 6))), 1), 2)
|
||||
if got := scalar(t, building, 9); got != uint64(g.now.Add(5*time.Second).UnixMilli()) {
|
||||
t.Fatalf("native construction deadline: got %d", got)
|
||||
}
|
||||
g.d.Tables["LifeItemTable"][[3]uint64{502}] = proto(5, 502, 1, 601, 8, 1000)
|
||||
g.d.Tables["LifeBuffTable"][[3]uint64{601}] = proto(8, 601, 1, 60)
|
||||
if _, err := g.s.Apply(g.ctx, "food", nil, []gamedata.Reward{{Type: 66, ID: 502, Count: 1}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
food := g.call("/LifeEatFood", child(nil, 2, proto(2, 502, 3, 66, 4, 1)))
|
||||
if got := scalar(t, nested(t, food, 1), 2); got != uint64(g.now.Add(time.Minute).UnixMilli()) {
|
||||
t.Fatalf("native food deadline: got %d", got)
|
||||
}
|
||||
g.reopen()
|
||||
restoredFood := nested(t, g.call("/LifeInfo", nil), 6)
|
||||
if got := scalar(t, restoredFood, 2); got != uint64(g.now.Add(time.Minute).UnixMilli()) {
|
||||
t.Fatalf("food deadline changed after reload: %d", got)
|
||||
}
|
||||
shop := g.call("/LifeShopBuyInfo", nil)
|
||||
expected := []time.Time{time.Date(2026, 4, 13, 0, 0, 0, 0, time.UTC), time.Date(2026, 4, 13, 0, 0, 0, 0, time.UTC), time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)}
|
||||
for i, want := range expected {
|
||||
if got := scalar(t, shop, i+2); got != uint64(want.UnixMilli()) {
|
||||
t.Fatalf("shop reset field %d: got %d, want %d", i+2, got, want.UnixMilli())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package life_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bd2server/internal/server/design/gamedata"
|
||||
"bd2server/internal/server/domain/life"
|
||||
)
|
||||
|
||||
// The 2.37.13 client regenerates the existing slot locally without a request or
|
||||
// an ObjectId change (LifeResourceGenerateObject.OnRegenTimerComplete).
|
||||
func TestRegenerationPreservesResourceVariantAndHarvestRewards(t *testing.T) {
|
||||
g := scenario(t)
|
||||
tables := map[string][][]byte{}
|
||||
for name, rows := range g.d.Tables {
|
||||
for _, row := range rows {
|
||||
tables[name] = append(tables[name], row)
|
||||
}
|
||||
}
|
||||
for i, row := range tables["LifeBuildingObjectTable"] {
|
||||
if scalar(t, row, 4) == 301 {
|
||||
tables["LifeBuildingObjectTable"][i] = proto(4, 301, 2, 1, 3, 60, 5, 1, 7, 1)
|
||||
}
|
||||
}
|
||||
tables["LifeGatheringObjectTable"] = append(tables["LifeGatheringObjectTable"], proto(4, 301, 6, 2, 10, 1, 1, 3, 2, 1, 3, 100, 8, 7, 9, 10))
|
||||
var err error
|
||||
g.d, err = gamedata.NewLifeCatalog(tables)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected := false
|
||||
g.options.Sample = func(limit uint64) (uint64, error) {
|
||||
if !selected {
|
||||
selected = true
|
||||
return 0, nil
|
||||
}
|
||||
return limit - 1, nil
|
||||
}
|
||||
g.reopen()
|
||||
g.call("/LifeWorldChoice", proto(2, 91))
|
||||
resource := func() []byte {
|
||||
t.Helper()
|
||||
place := nested(t, g.call("/LifeInfo", nil), 1)
|
||||
parent := nested(t, place, 2)
|
||||
return nested(t, parent, 10)
|
||||
}
|
||||
if inner := resource(); scalar(t, inner, 6) != 1 || scalar(t, inner, 7) != 1 {
|
||||
t.Fatalf("initial resource must be variant 1 at stage 1: %x", inner)
|
||||
}
|
||||
parent := child(proto(6, 301, 1, 2, 2, 3), 10, proto(6, 1, 4, 0, 7, 1))
|
||||
gather := child(nil, 2, child(proto(1, 1), 2, parent))
|
||||
checkBalance := func(want uint64) {
|
||||
t.Helper()
|
||||
got, err := g.s.Balance(g.ctx, 66, 501)
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("harvest inventory: got %d, want %d (%v)", got, want, err)
|
||||
}
|
||||
}
|
||||
g.call("/LifeWorldObjectGathering", gather)
|
||||
checkBalance(2)
|
||||
g.now = g.now.Add(59 * time.Second)
|
||||
if _, err := g.request("/LifeWorldObjectGathering", gather); !errors.Is(err, life.ErrInvalid) {
|
||||
t.Fatalf("harvest before the 60-second regeneration interval: %v", err)
|
||||
}
|
||||
checkBalance(2)
|
||||
g.reopen()
|
||||
g.now = g.now.Add(time.Second)
|
||||
if inner := resource(); scalar(t, inner, 6) != 1 || scalar(t, inner, 7) != 1 {
|
||||
t.Fatalf("regeneration must preserve variant 1 at stage 1: %x", inner)
|
||||
}
|
||||
g.call("/LifeWorldObjectGathering", gather)
|
||||
checkBalance(4)
|
||||
if _, err := g.request("/LifeWorldObjectGathering", gather); !errors.Is(err, life.ErrInvalid) {
|
||||
t.Fatalf("harvested regenerated resource twice: %v", err)
|
||||
}
|
||||
checkBalance(4)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package life_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bd2server/internal/server/design/gamedata"
|
||||
)
|
||||
|
||||
// LifePacket.AddLifeItemInfo/AddLifeFurnishingItemInfo and AddHaveCurrency
|
||||
// add reward counts. LifeInfo clears those caches before loading full balances.
|
||||
func TestNativeRewardDeltasRemainConsistentThroughGatherCraftAndRelog(t *testing.T) {
|
||||
g := scenario(t)
|
||||
g.d.Tables["LifeObjectGroupTable"][[3]uint64{304}] = proto(5, 304, 12, 3, 7, 1)
|
||||
g.d.Tables["LifeBuildingObjectTable"][[3]uint64{304}] = proto(4, 304, 2, 11)
|
||||
g.d.Tables["LifeObjectGroupTable"][[3]uint64{401}] = proto(5, 401, 12, 1, 7, 1, 9, 10)
|
||||
g.d.Tables["LifeCraftingObjectTable"][[3]uint64{401}] = proto(1, 401, 3, 3, 4, 501, 5, 66)
|
||||
g.call("/LifeWorldChoice", proto(2, 91))
|
||||
g.call("/LifeWorldObjectPlaceSave", child(nil, 2, placement(304, 5, 6)))
|
||||
client := map[uint64]uint64{}
|
||||
apply := func(bundle []byte) {
|
||||
t.Helper()
|
||||
for _, it := range repeated(t, bundle, 1) {
|
||||
client[scalar(t, it, 2)] += scalar(t, it, 4)
|
||||
}
|
||||
}
|
||||
fund, err := g.s.Apply(g.ctx, "initial", nil, []gamedata.Reward{{Type: 66, ID: 501, Count: 5}, {Type: 64, Count: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
apply(fund)
|
||||
bonus, err := g.s.Apply(g.ctx, "bonus", nil, []gamedata.Reward{{Type: 64, Count: 3}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
apply(bonus)
|
||||
if client[0] != 13 {
|
||||
t.Fatalf("native life coin cache=%d, want 13", client[0])
|
||||
}
|
||||
parent := child(proto(6, 301, 1, 2, 2, 3), 10, proto(6, 1, 4, 0, 7, 1))
|
||||
gather := child(nil, 2, child(proto(1, 1), 2, parent))
|
||||
for range 2 {
|
||||
harvest := g.call("/LifeWorldObjectGathering", gather)
|
||||
apply(nested(t, harvest, 2))
|
||||
g.now = g.now.Add(30 * time.Second)
|
||||
}
|
||||
if client[501] != 9 {
|
||||
t.Fatalf("native gathered material cache=%d, want 9", client[501])
|
||||
}
|
||||
var materialIndex uint64
|
||||
for _, it := range repeated(t, g.call("/LifeInfo", nil), 3) {
|
||||
if scalar(t, it, 2) == 501 {
|
||||
materialIndex = scalar(t, it, 1)
|
||||
}
|
||||
}
|
||||
if materialIndex == 0 {
|
||||
t.Fatal("missing material inventory index")
|
||||
}
|
||||
for _, count := range []uint64{2, 1} {
|
||||
request := child(proto(2, 401, 3, count), 4, proto(1, materialIndex, 2, 501, 3, 66, 4, 3*count))
|
||||
g.seq++
|
||||
encoded := append(proto(1, g.seq), request...)
|
||||
_, out, _, err := g.s.Handle(g.ctx, "/LifeCrafting", encoded)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client[501] -= 3 * count
|
||||
apply(nested(t, out, 1))
|
||||
_, retry, _, err := g.s.Handle(g.ctx, "/LifeCrafting", encoded)
|
||||
if err != nil || !bytes.Equal(retry, out) {
|
||||
t.Fatalf("craft retry must replay: %v", err)
|
||||
}
|
||||
}
|
||||
if client[401] != 3 || client[501] != 0 {
|
||||
t.Fatalf("native cache after crafts: %v", client)
|
||||
}
|
||||
g.reopen()
|
||||
info := g.call("/LifeInfo", nil)
|
||||
for _, it := range append(repeated(t, info, 3), repeated(t, info, 4)...) {
|
||||
id, count := scalar(t, it, 2), scalar(t, it, 4)
|
||||
if client[id] != count {
|
||||
t.Fatalf("relogin inventory item %d=%d, native cache=%d", id, count, client[id])
|
||||
}
|
||||
}
|
||||
if got := scalar(t, nested(t, info, 2), 1); got != client[0] {
|
||||
t.Fatalf("relogin coin=%d, native cache=%d", got, client[0])
|
||||
}
|
||||
if balance, err := g.s.Balance(g.ctx, 66, 501); err != nil || balance != 0 {
|
||||
t.Fatalf("materials not spent exactly once: %d (%v)", balance, err)
|
||||
}
|
||||
if balance, err := g.s.Balance(g.ctx, 65, 401); err != nil || balance != 3 {
|
||||
t.Fatalf("craft output not persisted exactly once: %d (%v)", balance, err)
|
||||
}
|
||||
}
|
||||
@@ -401,12 +401,11 @@ func (s *Service) refresh(v *snapshot) error {
|
||||
function := num(d, 2)
|
||||
for slot, in := range o.Inner {
|
||||
if in.Status == 0 && (function == 1 || function == 2) && now >= in.Start+int64(num(d, 3)) {
|
||||
r, err := s.randomGather(o.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
rows := s.design.Gathering[o.ID][in.ID]
|
||||
if len(rows) == 0 {
|
||||
return ErrInvalid
|
||||
}
|
||||
first := s.design.Gathering[o.ID][num(r, 6)][0]
|
||||
in = &object{ID: num(r, 6), Index: slot, Status: num(first, 10), Start: in.Start + int64(num(d, 3)), Inner: map[uint64]*object{}}
|
||||
in = &object{ID: in.ID, Index: slot, Status: num(rows[0], 10), Start: in.Start + int64(num(d, 3)), Inner: map[uint64]*object{}}
|
||||
o.Inner[slot] = in
|
||||
}
|
||||
if function >= 1 && function <= 3 && in.Status > 0 {
|
||||
@@ -484,9 +483,29 @@ func (s *Service) seed(v *snapshot, seeds [][]byte) ([]byte, error) {
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
func (s *Service) gather(v *snapshot, places [][]byte) ([]byte, error) {
|
||||
func (s *Service) gather(v *snapshot, places [][]byte) (body []byte, err error) {
|
||||
var chunk uint64
|
||||
var parent, requested, current *object
|
||||
defer func() {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if parent == nil {
|
||||
err = fmt.Errorf("%w: gathering chunk=%d", err, chunk)
|
||||
return
|
||||
}
|
||||
if requested == nil {
|
||||
err = fmt.Errorf("%w: gathering chunk=%d parent=%s", err, chunk, objectKey(parent))
|
||||
return
|
||||
}
|
||||
actualID, actualStatus := uint64(0), uint64(0)
|
||||
if current != nil {
|
||||
actualID, actualStatus = current.ID, current.Status
|
||||
}
|
||||
err = fmt.Errorf("%w: gathering chunk=%d parent=%s slot=%d requested_id=%d requested_status=%d actual_id=%d actual_status=%d", err, chunk, objectKey(parent), requested.Index, requested.ID, requested.Status, actualID, actualStatus)
|
||||
}()
|
||||
if len(places) == 0 || len(places) > 15 {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: invalid gathering batch size=%d", ErrInvalid, len(places))
|
||||
}
|
||||
var bundle []byte
|
||||
var changed []byte
|
||||
@@ -495,29 +514,34 @@ func (s *Service) gather(v *snapshot, places [][]byte) ([]byte, error) {
|
||||
changedObjects := map[string]*object{}
|
||||
mixedChunks := false
|
||||
for _, raw := range places {
|
||||
chunk := num(raw, 1)
|
||||
chunk = num(raw, 1)
|
||||
parent, requested, current = nil, nil, nil
|
||||
os := messages(raw, 2)
|
||||
if !v.Chunks[chunk] || len(os) == 0 {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: unavailable chunk or empty objects", ErrInvalid)
|
||||
}
|
||||
for _, requestObject := range os {
|
||||
p := parseObject(requestObject)
|
||||
parent, requested, current = p, nil, nil
|
||||
o := v.Objects[chunk][objectKey(p)]
|
||||
if o == nil || o.Status != 1 {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: parent missing or incomplete", ErrInvalid)
|
||||
}
|
||||
ins := messages(requestObject, 10)
|
||||
if len(ins) != 1 {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: inner object count=%d", ErrInvalid, len(ins))
|
||||
}
|
||||
want := parseObject(ins[0])
|
||||
want.Status = num(ins[0], 7)
|
||||
requested = want
|
||||
in := o.Inner[want.Index]
|
||||
current = in
|
||||
if in == nil || in.ID != want.ID || in.Status == 0 {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: resource unavailable or changed", ErrInvalid)
|
||||
}
|
||||
d := s.design.Row("LifeGatheringObjectTable", o.ID, in.ID, in.Status)
|
||||
if d == nil {
|
||||
return nil, ErrInvalid
|
||||
return nil, fmt.Errorf("%w: gathering design missing", ErrInvalid)
|
||||
}
|
||||
if in.End > 0 && s.options.Now().Unix() < in.End {
|
||||
return nil, ErrInvalid
|
||||
|
||||
@@ -78,6 +78,7 @@ var compensationCurrencies = func() map[uint64]bool {
|
||||
|
||||
func extendedCompensationReward(reward gamedata.Reward) bool {
|
||||
return compensationCurrencies[reward.Type] && reward.ID == 0 ||
|
||||
(reward.Type == 65 || reward.Type == 66) && reward.ID != 0 ||
|
||||
(reward.Type == 49 || reward.Type == 50) && reward.ID != 0 && reward.Count == 1
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package mail_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"bd2server/internal/common/versionconfig"
|
||||
"bd2server/internal/server/design/gamedata"
|
||||
"bd2server/internal/server/domain/command"
|
||||
"bd2server/internal/server/domain/events"
|
||||
"bd2server/internal/server/domain/inventory"
|
||||
"bd2server/internal/server/domain/life"
|
||||
"bd2server/internal/server/domain/mail"
|
||||
"bd2server/internal/server/domain/roster"
|
||||
"bd2server/internal/server/protocol/wire"
|
||||
"bd2server/internal/server/storage/stateio"
|
||||
)
|
||||
|
||||
type lifeMailDesign struct{}
|
||||
|
||||
func (lifeMailDesign) Character(uint64) (gamedata.CharacterDesign, bool) {
|
||||
return gamedata.CharacterDesign{}, false
|
||||
}
|
||||
func (lifeMailDesign) RollOptions(uint64) ([]gamedata.EquipmentOptionChoice, []gamedata.EquipmentOptionChoice, *gamedata.EquipmentOptionChoice, error) {
|
||||
return nil, nil, nil, nil
|
||||
}
|
||||
func (lifeMailDesign) Resolve(rewards []gamedata.BattleReward) ([]gamedata.BattleReward, error) {
|
||||
return rewards, nil
|
||||
}
|
||||
|
||||
func TestOperatorLifeRewardsClaimIntoLifeInventoryExactlyOnce(t *testing.T) {
|
||||
encode := func(values ...uint64) []byte {
|
||||
var b []byte
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
b = wire.AppendVarint(b, int(values[i]), values[i+1])
|
||||
}
|
||||
return b
|
||||
}
|
||||
value := func(b []byte, field int) uint64 {
|
||||
t.Helper()
|
||||
v, _, err := wire.Varint(b, field)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
message := func(b []byte, field int) []byte {
|
||||
t.Helper()
|
||||
v, ok, err := wire.Bytes(b, field)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("missing field %d: %x (%v)", field, b, err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
design, err := gamedata.NewLifeCatalog(map[string][][]byte{
|
||||
"LifeDefaultTable": {encode(21, 0)},
|
||||
"LifeWorldTable": {encode(4, 91)},
|
||||
"LifeObjectGroupTable": {encode(5, 101, 12, 1)},
|
||||
"LifeItemTable": {encode(5, 201, 8, 9999)},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := command.Context{Identity: command.Identity{AccountID: "life-mail-recipient", SessionID: "login"}, State: stateio.NewMemory()}
|
||||
spool := filepath.Join(t.TempDir(), "grants.json")
|
||||
var service *mail.Service
|
||||
var living *life.Service
|
||||
open := func() {
|
||||
t.Helper()
|
||||
store := stateio.RootStore{}
|
||||
items, err := inventory.OpenInventory(ctx, store, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wallet, err := inventory.OpenWallet(ctx, store, inventory.Currency{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
collection, err := roster.OpenCollectionStore(ctx, store, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
equipment, err := inventory.OpenEquipmentInventory(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
living, err = life.Open(ctx, design, life.Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
economy, err := events.NewEconomy(ctx, stateio.EntrySnapshotStore{Domain: "collection", Bucket: "gameplay"}, items, wallet, collection, equipment, lifeMailDesign{}, lifeMailDesign{}, lifeMailDesign{}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
economy.AttachLife(living)
|
||||
service, err = mail.OpenService(ctx, store, &mail.Starter{Version: versionconfig.State(), MailCount: 1}, items, wallet)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.AttachAttachmentRewardEconomy(ctx, economy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.AttachGrantSpoolPath(ctx, spool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
open()
|
||||
if _, err := living.Apply(ctx, "existing-life-inventory", nil, []gamedata.Reward{{Type: 64, Count: 10}, {Type: 65, ID: 101, Count: 1}, {Type: 66, ID: 201, Count: 5}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant := mail.Grant{AccountID: ctx.AccountID, Identity: "operator-life-package", Title: "Life supplies", Body: "Furniture, materials and life coins", SentAt: time.Now().UnixMilli(), Rewards: []mail.GrantReward{{Type: 64, Count: 7}, {Type: 65, ID: 101, Count: 2}, {Type: 66, ID: 201, Count: 3}}}
|
||||
if err := mail.AppendGrant(spool, grant); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call := func(path string, request []byte) []byte {
|
||||
t.Helper()
|
||||
_, body, handled, err := service.Handle(ctx, path, request)
|
||||
if err != nil || !handled {
|
||||
t.Fatalf("%s: handled=%t err=%v", path, handled, err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
id := firstMailboxID(call("/MailInfo", encode(1, 1)))
|
||||
if id == 0 {
|
||||
t.Fatal("operator grant was not issued to the mailbox")
|
||||
}
|
||||
claim := encode(1, 2, 2, id)
|
||||
first := call("/MailOpen", claim)
|
||||
check := func() {
|
||||
t.Helper()
|
||||
coin, err := living.Coin(ctx)
|
||||
if err != nil || coin != 17 {
|
||||
t.Fatalf("life coin balance=%d, want 17 (%v)", coin, err)
|
||||
}
|
||||
for _, reward := range []gamedata.Reward{{Type: 65, ID: 101, Count: 3}, {Type: 66, ID: 201, Count: 8}} {
|
||||
got, err := living.Balance(ctx, reward.Type, reward.ID)
|
||||
if err != nil || got != reward.Count {
|
||||
t.Fatalf("life inventory %d:%d=%d, want %d (%v)", reward.Type, reward.ID, got, reward.Count, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
check()
|
||||
gotDeltas := map[uint64]uint64{}
|
||||
if err := wire.Walk(message(first, 1), func(f wire.Field) error {
|
||||
if f.Number == 1 && f.Type == 2 {
|
||||
gotDeltas[value(f.Value, 3)] += value(f.Value, 4)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(gotDeltas) != 3 || gotDeltas[64] != 7 || gotDeltas[65] != 2 || gotDeltas[66] != 3 {
|
||||
t.Fatalf("claim must report granted amounts, not existing balances: %v", gotDeltas)
|
||||
}
|
||||
if again := call("/MailOpen", claim); !bytes.Equal(first, again) {
|
||||
t.Fatal("claim retry changed its reward bundle")
|
||||
}
|
||||
check()
|
||||
open()
|
||||
if id := firstMailboxID(call("/MailInfo", encode(1, 3))); id != 0 {
|
||||
t.Fatal("service reopen reissued an already claimed operator grant")
|
||||
}
|
||||
call("/MailOpen", encode(1, 4, 2, id))
|
||||
check()
|
||||
}
|
||||
Reference in new issue
Block a user