Skip to content
This repository was archived by the owner on May 10, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion internal/answer/config_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"strings"
"testing"
"time"
"unicode"

"github.com/ggmolly/belfast/internal/connection"
Expand Down Expand Up @@ -613,10 +614,13 @@ func TestDormDataUsesDormTemplate(t *testing.T) {

func TestResourcesInfoUsesTemplates(t *testing.T) {
client := setupConfigTest(t)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "1", `{"level":2,"time":30}`)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "1", `{"level":1,"time":30}`)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "2", `{"level":2,"time":60}`)
seedConfigEntry(t, "ShareCfg/class_upgrade_template.json", "1", `{"level":3,"time":40}`)
seedConfigEntry(t, "ShareCfg/navalacademy_data_template.json", "1", `{"id":1}`)
seedConfigEntry(t, "ShareCfg/navalacademy_shoppingstreet_template.json", "1", `{"special_goods_num":9}`)
now := uint32(time.Now().UTC().Unix())
seedConfigEntry(t, "Runtime/naval_academy_runtime.json", "1", fmt.Sprintf(`{"commander_id":1,"oil_well_level":2,"gold_well_level":1,"oil_upgrade_complete_time":%d,"gold_upgrade_complete_time":%d}`, now+123, now+456))

buffer := []byte{}
if _, _, err := ResourcesInfo(&buffer, client); err != nil {
Expand All @@ -628,11 +632,103 @@ func TestResourcesInfoUsesTemplates(t *testing.T) {
if response.GetOilWellLevel() != 2 || response.GetClassLv() != 3 {
t.Fatalf("expected oil level 2 and class level 3")
}
if response.GetOilWellLvUpTime() != now+123 || response.GetGoldWellLvUpTime() != now+456 {
t.Fatalf("expected upgrade timers from runtime")
}
if response.GetSkillClassNum() != 1 || response.GetDailyFinishBuffCnt() != 9 {
t.Fatalf("expected academy counts to be set")
}
}

func TestResourcesInfoClampsNavalAcademyRuntimeLevels(t *testing.T) {
client := setupConfigTest(t)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "1", `{"level":1,"production":20,"store":300,"hour_time":3,"time":10}`)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "2", `{"level":2,"production":21,"store":600,"hour_time":3,"time":900}`)
seedConfigEntry(t, "Runtime/naval_academy_runtime.json", "1", `{"commander_id":1,"oil_well_level":99,"gold_well_level":0}`)

buffer := []byte{}
if _, _, err := ResourcesInfo(&buffer, client); err != nil {
t.Fatalf("resources info failed: %v", err)
}

var response protobuf.SC_22001
decodeResponse(t, client, &response)
if response.GetOilWellLevel() != 2 {
t.Fatalf("expected oil level clamped to 2, got %d", response.GetOilWellLevel())
}
if response.GetGoldWellLevel() != 1 {
t.Fatalf("expected gold level clamped to 1, got %d", response.GetGoldWellLevel())
}
}

func TestLastLoginCatchesUpNavalAcademyResources(t *testing.T) {
client := setupConfigTest(t)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "1", `{"level":1,"production":120,"store":9999,"hour_time":1,"time":10}`)

now := time.Now().UTC()
collectAt := uint32(now.Unix()) - 7200
seedConfigEntry(t, "Runtime/naval_academy_runtime.json", "1", fmt.Sprintf(`{"commander_id":1,"oil_well_level":1,"gold_well_level":1,"oil_collect_timestamp":%d,"gold_collect_timestamp":%d}`, collectAt, collectAt))

oilBefore := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(2))
coinBefore := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(1))

buffer := []byte{}
if _, _, err := LastLogin(&buffer, client); err != nil {
t.Fatalf("last login failed: %v", err)
}

oilAfter := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(2))
coinAfter := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(1))

oilDelta := oilAfter - oilBefore
coinDelta := coinAfter - coinBefore
if oilDelta < 240 || oilDelta > 241 {
t.Fatalf("expected oil catch-up in [240,241], got %d", oilDelta)
}
if coinDelta < 240 || coinDelta > 241 {
t.Fatalf("expected coin catch-up in [240,241], got %d", coinDelta)
}
}

func TestLastLoginPreservesPreUpgradeNavalAcademyAccrual(t *testing.T) {
client := setupConfigTest(t)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "1", `{"level":1,"production":120,"store":9999,"hour_time":1,"time":10}`)
seedConfigEntry(t, "ShareCfg/oilfield_template.json", "2", `{"level":2,"production":120,"store":9999,"hour_time":1,"time":10}`)

now := time.Now().UTC()
collectAt := uint32(now.Unix()) - 4*3600
upgradeStart := uint32(now.Unix()) - 3*3600
upgradeFinish := uint32(now.Unix()) - 2*3600
seedConfigEntry(t, "Runtime/naval_academy_runtime.json", "1", fmt.Sprintf(`{"commander_id":1,"oil_well_level":1,"gold_well_level":1,"oil_collect_timestamp":%d,"gold_collect_timestamp":%d,"oil_upgrade_start_time":%d,"oil_upgrade_complete_time":%d,"gold_upgrade_start_time":%d,"gold_upgrade_complete_time":%d}`,
collectAt,
collectAt,
upgradeStart,
upgradeFinish,
upgradeStart,
upgradeFinish,
))

oilBefore := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(2))
coinBefore := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(1))

buffer := []byte{}
if _, _, err := LastLogin(&buffer, client); err != nil {
t.Fatalf("last login failed: %v", err)
}

oilAfter := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(2))
coinAfter := queryAnswerTestInt64(t, "SELECT COALESCE((SELECT amount FROM owned_resources WHERE commander_id = $1 AND resource_id = $2), 0)", int64(client.Commander.CommanderID), int64(1))

oilDelta := oilAfter - oilBefore
coinDelta := coinAfter - coinBefore
if oilDelta < 359 || oilDelta > 360 {
t.Fatalf("expected oil catch-up in [359,360], got %d", oilDelta)
}
if coinDelta < 359 || coinDelta > 360 {
t.Fatalf("expected coin catch-up in [359,360], got %d", coinDelta)
}
}

func TestEquipedSpecialWeaponsUsesConfig(t *testing.T) {
client := setupConfigTest(t)
seedConfigEntry(t, "ShareCfg/spweapon_data_statistics.json", "1", `{"id":1}`)
Expand Down
4 changes: 2 additions & 2 deletions internal/answer/meta_character_packets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestMetaTacticsUnlockAndLevelUpFlow(t *testing.T) {
seedConfigEntry(t, "sharecfgdata/ship_data_template.json", "9701011", `{"id":9701011,"group_type":970101,"max_level":70,"buff_list_display":[800040]}`)
seedConfigEntry(t, "ShareCfg/ship_meta_skilltask.json", "1", `{"id":1,"level":1,"need_exp":100,"skill_ID":800040,"skill_levelup_task":[],"skill_unlock":[[2,16003,5]]}`)
seedConfigEntry(t, "ShareCfg/ship_meta_skilltask.json", "2", `{"id":2,"level":2,"need_exp":200,"skill_ID":800040,"skill_levelup_task":[],"skill_unlock":[]}`)
seedConfigEntry(t, "sharecfgdata/skill_data_template.json", "800040", `{"id":800040,"max_level":10}`)
seedConfigEntry(t, "ShareCfg/skill_data_template.json", "800040", `{"id":800040,"max_level":10}`)
seedConfigEntry(t, "sharecfgdata/item_data_statistics.json", "16031", `{"id":16031,"type":25,"usage_arg":"100"}`)

unlockPayload := protobuf.CS_63311{ShipId: proto.Uint32(7101), SkillId: proto.Uint32(800040), Index: proto.Uint32(2)}
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestMetaQuickTacticsUseBooksRejectsMaxLevel(t *testing.T) {
seedCommanderItem(t, client, 16031, 1)
seedConfigEntry(t, "sharecfgdata/ship_data_template.json", "9703011", `{"id":9703011,"group_type":970301,"max_level":70,"buff_list_display":[800041]}`)
seedConfigEntry(t, "ShareCfg/ship_meta_skilltask.json", "11", `{"id":11,"level":1,"need_exp":100,"skill_ID":800041,"skill_levelup_task":[],"skill_unlock":[]}`)
seedConfigEntry(t, "sharecfgdata/skill_data_template.json", "800041", `{"id":800041,"max_level":2}`)
seedConfigEntry(t, "ShareCfg/skill_data_template.json", "800041", `{"id":800041,"max_level":2}`)
seedConfigEntry(t, "sharecfgdata/item_data_statistics.json", "16031", `{"id":16031,"type":25,"usage_arg":"100"}`)
execAnswerTestSQLT(t, `
INSERT INTO commander_meta_tactics_skill_states (commander_id, ship_id, skill_id, skill_pos, level, exp)
Expand Down
6 changes: 5 additions & 1 deletion internal/answer/playerops/last_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import (

// Reimplementation of SC_11000
func LastLogin(buffer *[]byte, client *connection.Client) (int, int, error) {
now := time.Now().UTC()
sc11000 := protobuf.SC_11000{
Timestamp: proto.Uint32(uint32(time.Now().Unix())),
Timestamp: proto.Uint32(uint32(now.Unix())),
Monday_0OclockTimestamp: proto.Uint32(1606114800), // 23/11/2020 08:00:00
}
client.PreviousLoginAt = client.Commander.LastLogin
if err := applyNavalAcademyLoginCatchup(client, now); err != nil {
return 0, 11000, err
}
client.Commander.BumpLastLogin()
logger.LogEvent("Server", "SC_11000", "Updated last login of uid="+fmt.Sprint(client.Commander.CommanderID), logger.LOG_LEVEL_INFO)
return client.SendMessage(11000, &sc11000)
Expand Down
Loading