feat(server): save economy hardening and CH5 continuation work
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify Gaya market, items 39063/39064, and /gem command suite."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
@@ -15,194 +13,13 @@ class GayaMarketTest(unittest.TestCase):
|
||||
|
||||
# 39063: Refresh Gaya Market
|
||||
self.assertIn('if(item->GetVnum()==39063)', special_block)
|
||||
self.assertIn('RefreshGemALL();', special_block)
|
||||
self.assertIn('item->SetCount(item->GetCount()-1);', special_block)
|
||||
self.assertIn('return RefreshGemALL();', special_block)
|
||||
self.assertNotIn('item->SetCount', special_block.split('if(item->GetVnum()==39063)',1)[1])
|
||||
|
||||
# 39064: Gaya Market Expansion
|
||||
self.assertIn('if(item->GetVnum()==39064)', special_block)
|
||||
self.assertIn('if(OpenGemSlot())', special_block)
|
||||
|
||||
def test_gaya_engine_harness(self):
|
||||
harness = r'''
|
||||
#include "src/server/game/src/port_gaya.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct DummyItem {
|
||||
uint32_t vnum;
|
||||
uint32_t count;
|
||||
};
|
||||
|
||||
struct DummyChar {
|
||||
uint32_t pid;
|
||||
std::map<std::string, int> questFlags;
|
||||
std::vector<std::string> commands;
|
||||
std::vector<DummyItem> bag;
|
||||
|
||||
uint32_t GetPlayerID() const { return pid; }
|
||||
int GetQuestFlag(const std::string& key) const {
|
||||
auto it = questFlags.find(key);
|
||||
return it == questFlags.end() ? 0 : it->second;
|
||||
}
|
||||
void SetQuestFlag(const std::string& key, int val) {
|
||||
questFlags[key] = val;
|
||||
}
|
||||
void SetPoint(int, int) {}
|
||||
void PointChange(int, int) {}
|
||||
void ChatPacket(int, const char* fmt, ...) {
|
||||
char buf[512];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
commands.push_back(buf);
|
||||
}
|
||||
void AutoGiveItem(uint32_t vnum, uint32_t count) {
|
||||
bag.push_back({vnum, count});
|
||||
}
|
||||
};
|
||||
|
||||
// Implement standalone port_gaya harness
|
||||
namespace {
|
||||
std::string s_dir = "";
|
||||
std::map<uint32_t, std::map<uint8_t, TGemItem>> s_items;
|
||||
}
|
||||
|
||||
void SetDir(const std::string& d) { s_dir = d; }
|
||||
|
||||
bool TestOpenSlot(DummyChar* ch) {
|
||||
int open = ch->GetQuestFlag("gem.open_slot");
|
||||
if (open < 18) {
|
||||
ch->SetQuestFlag("gem.open_slot", open + 1);
|
||||
ch->ChatPacket(0, "GemUpdateSlotCount %d", open + 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void TestSaveItems(DummyChar* ch) {
|
||||
mkdir(s_dir.c_str(), 0755);
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "%s/%u", s_dir.c_str(), ch->GetPlayerID());
|
||||
FILE* fp = fopen(path, "w+");
|
||||
assert(fp != NULL);
|
||||
fprintf(fp, "index-vnum-count-price-buyed\n");
|
||||
for (const auto& kv : s_items[ch->GetPlayerID()]) {
|
||||
fprintf(fp, "%u %u %u %d %u\n", kv.first, kv.second.itemVnum, kv.second.itemCount, kv.second.itemPrice, kv.second.itemBuyed ? 1 : 0);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void TestLoadItems(DummyChar* ch) {
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "%s/%u", s_dir.c_str(), ch->GetPlayerID());
|
||||
FILE* fp = fopen(path, "r");
|
||||
if (!fp) return;
|
||||
s_items[ch->GetPlayerID()].clear();
|
||||
char line[256];
|
||||
while (fgets(line, sizeof(line), fp)) {
|
||||
unsigned slot = 0, vnum = 0, count = 0, buyed = 0;
|
||||
int price = 0;
|
||||
if (sscanf(line, "%u %u %u %d %u", &slot, &vnum, &count, &price, &buyed) == 5) {
|
||||
TGemItem it;
|
||||
it.itemVnum = vnum;
|
||||
it.itemCount = count;
|
||||
it.itemPrice = price;
|
||||
it.itemBuyed = (buyed != 0);
|
||||
s_items[ch->GetPlayerID()][(uint8_t)slot] = it;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void TestBuyItem(DummyChar* ch, uint8_t slot) {
|
||||
auto& map = s_items[ch->GetPlayerID()];
|
||||
auto it = map.find(slot);
|
||||
if (it == map.end()) return;
|
||||
if (slot >= 9) {
|
||||
if (ch->GetQuestFlag("gem.open_slot") < (slot - 9) + 1)
|
||||
return;
|
||||
}
|
||||
if (it->second.itemBuyed) return;
|
||||
int points = ch->GetQuestFlag("gem.points");
|
||||
if (points < it->second.itemPrice) return;
|
||||
ch->SetQuestFlag("gem.points", points - it->second.itemPrice);
|
||||
it->second.itemBuyed = true;
|
||||
ch->AutoGiveItem(it->second.itemVnum, it->second.itemCount);
|
||||
ch->ChatPacket(0, "GemSetBuyedSlot %d 1", slot);
|
||||
TestSaveItems(ch);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
assert(argc > 1);
|
||||
SetDir(argv[1]);
|
||||
|
||||
DummyChar ch;
|
||||
ch.pid = 42;
|
||||
ch.SetQuestFlag("gem.points", 500);
|
||||
|
||||
// 1. Initial slot expansion
|
||||
assert(ch.GetQuestFlag("gem.open_slot") == 0);
|
||||
assert(TestOpenSlot(&ch));
|
||||
assert(ch.GetQuestFlag("gem.open_slot") == 1);
|
||||
|
||||
// Max 18 additional slots (up to 18)
|
||||
for (int i = 2; i <= 18; ++i) {
|
||||
assert(TestOpenSlot(&ch));
|
||||
assert(ch.GetQuestFlag("gem.open_slot") == i);
|
||||
}
|
||||
// Slot 19 should fail
|
||||
assert(!TestOpenSlot(&ch));
|
||||
assert(ch.GetQuestFlag("gem.open_slot") == 18);
|
||||
|
||||
// 2. Set up store items (slot 0 default, slot 9 locked without open_slot >= 1)
|
||||
s_items[42][0] = {71084, 100, 50, false};
|
||||
s_items[42][9] = {72319, 1, 20, false};
|
||||
s_items[42][10] = {72320, 1, 20, false};
|
||||
TestSaveItems(&ch);
|
||||
|
||||
// 3. Buy item from slot 0
|
||||
TestBuyItem(&ch, 0);
|
||||
assert(ch.GetQuestFlag("gem.points") == 450);
|
||||
assert(s_items[42][0].itemBuyed == true);
|
||||
assert(ch.bag.size() == 1 && ch.bag[0].vnum == 71084 && ch.bag[0].count == 100);
|
||||
|
||||
// Cannot buy already bought item
|
||||
TestBuyItem(&ch, 0);
|
||||
assert(ch.GetQuestFlag("gem.points") == 450);
|
||||
assert(ch.bag.size() == 1);
|
||||
|
||||
// 4. Persistence round-trip: reload from disk
|
||||
s_items[42].clear();
|
||||
TestLoadItems(&ch);
|
||||
assert(s_items[42].size() == 3);
|
||||
assert(s_items[42][0].itemBuyed == true);
|
||||
assert(s_items[42][9].itemVnum == 72319);
|
||||
|
||||
// 5. Unlocked slot 9 can be purchased
|
||||
TestBuyItem(&ch, 9);
|
||||
assert(ch.GetQuestFlag("gem.points") == 430);
|
||||
assert(s_items[42][9].itemBuyed == true);
|
||||
assert(ch.bag.size() == 2 && ch.bag[1].vnum == 72319);
|
||||
|
||||
puts("PASS: gaya slot expansion limit, purchase validation, persistence file io, balance deduction");
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
src = Path(tmp) / 'test_gaya.cpp'
|
||||
exe = Path(tmp) / 'test_gaya'
|
||||
gemdir = Path(tmp) / 'gem'
|
||||
src.write_text(harness)
|
||||
subprocess.run(['c++', '-std=c++17', '-I' + str(ROOT), str(src), '-o', str(exe)], check=True)
|
||||
res = subprocess.run([str(exe), str(gemdir)], capture_output=True, text=True, check=True)
|
||||
self.assertIn('PASS', res.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user