Add feature query protocol and scheduled event drop gates

This commit is contained in:
shen
2026-10-15 02:28:03 -07:00
parent 36157d2bd1
commit 1a367197f5
18 changed files with 721 additions and 2 deletions
+72
View File
@@ -0,0 +1,72 @@
// Run on FreeBSD against the project's real Lua 5.0 library and installed compiled quest states.
// No game process, DB, network or live state is modified.
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
#define __QUEST_PC_H
#define CHAT_TYPE_COMMAND 5
namespace quest {
struct PC {
bool loaded=true,running=false;std::map<std::string,int> flags;
bool IsLoaded(){return loaded;}bool IsRunning(){return running;}
int GetFlag(const std::string& key){return flags[key];}
};
struct CQuestManager {
lua_State* L=nullptr;PC pc;std::map<std::string,int> events;int currentPlayer=98765;
static CQuestManager& instance(){static CQuestManager q;return q;}
lua_State* GetLuaState(){return L;}
PC* GetPCForce(unsigned id){assert(id==123);return &pc;}
int GetEventFlag(const std::string& name){return events[name];}
unsigned GetQuestIndexByName(const std::string&){return 1;}
int GetQuestStateIndex(const std::string&,const std::string&){
const int top=lua_gettop(L);lua_getglobal(L,"dragon_lair_weekly");lua_pushstring(L,"hunt_item");lua_rawget(L,-2);
int result=int(lua_tonumber(L,-1));lua_settop(L,top);return result;
}
};
}
struct ITEM_MANAGER {
static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;}
void* GetTable(unsigned vnum){return vnum==30179?this:nullptr;}
};
static long long now=1700000000;static long long get_global_time(){return now;}
struct Character {
std::vector<std::string> messages;
unsigned GetPlayerID(){return 123;}int CountSpecifyItem(unsigned){return 7;}
void ChatPacket(int,const char* format,...) {
char out[1024];va_list args;va_start(args,format);vsnprintf(out,sizeof(out),format,args);va_end(args);messages.emplace_back(out);
}
};
using LPCHARACTER=Character*;
#include "port_dungeons.h"
static int eventFlag(lua_State* L) {
lua_pushnumber(L,quest::CQuestManager::instance().GetEventFlag(lua_tostring(L,1)));return 1;
}
static bool contains(const Character& c,const char* text){for(auto& s:c.messages)if(s.find(text)!=std::string::npos)return true;return false;}
int main(int argc,char** argv){
assert(argc==3);auto& q=quest::CQuestManager::instance();q.L=lua_open();assert(q.L);
lua_newtable(q.L);lua_pushstring(q.L,"get_event_flag");lua_pushcfunction(q.L,eventFlag);lua_rawset(q.L,-3);lua_setglobal(q.L,"game");
assert(luaL_loadfile(q.L,argv[1])==0 && lua_pcall(q.L,0,0,0)==0);
assert(luaL_loadfile(q.L,argv[2])==0 && lua_pcall(q.L,0,0,0)==0);
q.events["dragon_lair_time"]=now-100;q.events["dragon_lair_alive"]=1;
q.pc.flags["dragon_lair_weekly.lasttime"]=now-1000;
q.pc.flags["dragon_lair_weekly.__status"]=q.GetQuestStateIndex("dragon_lair_weekly","hunt_item");
Character ch;const int top=lua_gettop(q.L);PortDungeonQuery(&ch,1);
assert(contains(ch,"lair_tickets 3") && contains(ch,"lair_phase 1") && contains(ch,"weekly_active 1"));
assert(contains(ch,"weekly_end 1700560600") && contains(ch,"lair_owned 7"));
assert(q.currentPlayer==98765 && lua_gettop(q.L)==top);
q.events["dragon_lair_amount"]=-1;ch.messages.clear();PortDungeonQuery(&ch,2);assert(contains(ch,"lair_tickets 0"));
q.events["dragon_lair_amount"]=5;ch.messages.clear();PortDungeonQuery(&ch,3);assert(contains(ch,"lair_tickets 5"));
now+=3500;ch.messages.clear();PortDungeonQuery(&ch,4);assert(contains(ch,"lair_phase 2"));
++now;ch.messages.clear();PortDungeonQuery(&ch,5);assert(contains(ch,"lair_phase 0"));
q.pc.running=true;ch.messages.clear();PortDungeonQuery(&ch,6);assert(ch.messages.back()=="MTF1 error 6 busy");q.pc.running=false;
lua_pushnil(q.L);lua_setglobal(q.L,"dragon_lair_access");ch.messages.clear();PortDungeonQuery(&ch,7);assert(ch.messages.back()=="MTF1 error 7 unsupported");
assert(lua_gettop(q.L)==top && q.currentPlayer==98765);
lua_close(q.L);puts("PASS: installed quest settings in real Lua 5.0; shared/personal deadlines, ticket overrides, boundaries, stack/context, busy/missing");
}
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
"""Exercise production interval/parser bodies, including restarts, scopes and atomic reload."""
from pathlib import Path
import subprocess
import tempfile
SRC=Path(__file__).resolve().parents[2]/'src/server/game/src'
CODE=r'''
#include <cassert>
#include <cstdio>
#include <cstdarg>
#include "port_calendar.h"
#define CHAT_TYPE_COMMAND 5
static unsigned tick=20000;static long long now=150;static unsigned char g_bChannel=2;
static std::string base;
const std::string& LocaleService_GetBasePath(){return base;}
static unsigned get_dword_time(){return tick;}
static long long get_global_time(){return now;}
struct Character {
unsigned empire=3;std::vector<std::string> messages;
unsigned GetEmpire(){return empire;}
void ChatPacket(int,const char* format,...) {
char out[1024];va_list args;va_start(args,format);vsnprintf(out,sizeof(out),format,args);va_end(args);messages.emplace_back(out);
}
};
using LPCHARACTER=Character*;
#include "port_calendar_runtime.h"
static bool load(PortCalendar& c,const char* text){std::istringstream in(text);return c.Parse(in);}
int main(int argc,char** argv){
assert(argc==2);PortCalendar c;
assert(load(c,"MTCALENDAR 1\n2 1 100 200 0 0\n1 2 100 200 2 3\n3 1 150 250 0 0\n"));
assert(c.events[0].id==1 && !c.Active(1,99,1,1) && c.Active(1,100,1,1));
assert(c.Active(1,200,1,1) && !c.Active(1,250,1,1)); // overlapping intervals do not multiply
assert(c.Active(2,199,2,3) && !c.Active(2,200,2,3));
assert(!c.Active(2,150,1,3) && !c.Active(2,150,2,1));
for(const auto* bad:{"", "MTCALENDAR 2\n", "MTCALENDAR 1\n1 3 1 2 0 0\n", "MTCALENDAR 1\n1 1 2 2 0 0\n",
"MTCALENDAR 1\n1 1 1 2 0 0\n1 2 2 3 0 0\n", "MTCALENDAR 1\n1 1 -1 2 0 0\n", "MTCALENDAR 1\n1 1 1 4294967296 0 0\n"}) {
assert(!load(c,bad));assert(!c.valid && c.events.empty() && !c.Active(1,150,1,1));
}
const std::string path=argv[1],next=path+".next";
{std::ofstream f(path);f<<"MTCALENDAR 1\n1 1 100 200 0 0\n";}
c.Refresh(path,100);assert(c.Active(1,150,1,1));
{std::ofstream f(next);f<<"MTCALENDAR 1\n1 2 100 200 0 0\n";}
assert(rename(next.c_str(),path.c_str())==0);
c.Refresh(path,200);assert(c.Active(1,150,1,1)); // explicit reload bound
c.Refresh(path,5100);assert(!c.Active(1,150,1,1) && c.Active(2,150,1,1));
PortCalendar restarted;restarted.Refresh(path,1);assert(restarted.Active(2,150,1,1));
{std::ofstream f(next);f<<"broken";}assert(rename(next.c_str(),path.c_str())==0);
c.Refresh(path,10100);assert(!c.valid && c.events.empty());
remove(path.c_str());c.Refresh(path,15100);assert(!c.valid);
assert(load(c,"MTCALENDAR 1\n") && c.events.empty());
base=path.substr(0,path.find_last_of('/'));
{std::ofstream f(path);f<<"MTCALENDAR 1\n1 1 100 200 2 3\n2 2 100 200 1 1\n";}
Character ch;PortCalendarQuery(&ch,1,0);
assert(ch.messages.front()=="MTF1 begin 1 calendar 1 0 2 0 150");
assert(ch.messages[1]=="MTF1 event 1 0 1 1 100 200 2 3 1");
assert(ch.messages[2]=="MTF1 event 1 1 2 2 100 200 1 1 0");
assert(PortScheduledDrop(1,&ch) && !PortScheduledDrop(2,&ch));
now=200;assert(!PortScheduledDrop(1,&ch));
PortCalendarQuery(&ch,2,999);assert(ch.messages.back()=="MTF1 error 2 offset");
remove(path.c_str());tick+=5000;PortCalendarQuery(&ch,3,0);
assert(ch.messages.back()=="MTF1 error 3 schedule_unavailable" && !PortScheduledDrop(1,&ch));
puts("PASS: schedule parsing, UTC interval edges, scope, overlaps, restart, atomic reload, invalid/missing fail closed");
}
'''
with tempfile.TemporaryDirectory(prefix='mt-calendar-') as folder:
p=Path(folder);(p/'test.cpp').write_text(CODE)
subprocess.run(['c++','-std=c++17','-I'+str(SRC),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
subprocess.run([str(p/'test'),str(p/'port_events.tsv')],check=True)
# Both real quest-drop branches use this gate, retaining the original probability/eligibility code.
source=(SRC/'item_manager.cpp').read_bytes()
assert b'GetEventFlag("drop_moon") || PortScheduledDrop(1,pkKiller)' in source
assert b'GetEventFlag("xmas_sock") || PortScheduledDrop(2,pkKiller)' in source
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
"""Compile the production query/protocol bodies against original group classes and deterministic fixtures."""
from pathlib import Path
import re
import subprocess
import tempfile
ROOT=Path(__file__).resolve().parents[2]
SRC=ROOT/'src/server/game/src'
header=(SRC/'item_manager.h').read_bytes().decode('latin1')
groups=header[header.index('class CSpecialAttrGroup'):header.index('class ITEM;')]
points=sorted(set(re.findall(r'\bPOINT_[A-Z_]+', (SRC/'port_features.h').read_text())))
code=r'''
#include <algorithm>
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace std;
using DWORD=unsigned;using BYTE=unsigned char;
#define itertype(v) decltype((v).begin())
static int rng=0;int number(int,int){++rng;return 1;}
enum {ITEM_POLYMORPH=19,ITEM_GIFTBOX=23,ITEM_TREASURE_BOX=20,CHAR_TYPE_MONSTER=0,CHAR_TYPE_STONE=2,MOB_RANK_MAX_NUM=6,ITEM_NAME_MAX_LEN=24};
POINTS
GROUPS
#include "port_feature_types.h"
#include "item_manager_private_types.h"
std::vector<CItemDropInfo> g_vec_pkCommonDropItem[MOB_RANK_MAX_NUM];
struct TItemTable{unsigned dwVnum;unsigned bType=0;};
struct MobTable{unsigned bType=0,bRank=0,dwPolymorphItemVnum=0,dwDropItemVnum=0;};
struct CMob{MobTable m_table;};
class CMobManager {
public:std::map<DWORD,CMob*> mobs;
static CMobManager& instance(){static CMobManager m;return m;}
const CMob* Get(unsigned id){auto it=mobs.find(id);return it==mobs.end()?nullptr:it->second;}
auto begin(){return mobs.begin();}auto end(){return mobs.end();}
};
class ITEM_MANAGER {
public:
std::vector<TItemTable> tables;
std::map<DWORD,CDropItemGroup*> m_map_pkDropItemGroup;
std::map<DWORD,CMobItemGroup*> m_map_pkMobItemGroup;
std::map<DWORD,CLevelItemGroup*> m_map_pkLevelItemGroup;
std::map<DWORD,CBuyerThiefGlovesItemGroup*> m_map_pkGloveItemGroup;
std::map<DWORD,DWORD> m_map_dwEtcItemDropProb;
std::map<DWORD,CSpecialItemGroup*> special;
static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;}
TItemTable* GetTable(unsigned id){for(auto& t:tables)if(t.dwVnum==id)return &t;return nullptr;}
const std::vector<TItemTable>& GetTable(){return tables;}
const CSpecialItemGroup* GetSpecialItemGroup(unsigned id){auto it=special.find(id);return it==special.end()?nullptr:it->second;}
std::vector<PortWikiRow> PortWikiDrops(DWORD race);
std::vector<PortWikiRow> PortWikiChest(DWORD id);
};
#include "port_wiki_queries.inc"
static DWORD clockms=1000;unsigned get_dword_time(){return clockms;}long long get_global_time(){return 1700000000;}
struct Character {
unsigned m_portFeatureLastQuery=0;bool pc=true;std::vector<std::string> messages;std::map<unsigned,int> points;
bool IsPC(){return pc;}void* GetDesc(){return this;}
int GetPoint(unsigned point){return points[point];}
int GetHP(){return 123;}int GetMaxHP(){return 500;}int GetSP(){return 42;}int GetMaxSP(){return 100;}int GetLevel(){return 30;}
void ChatPacket(int,const char* format,...) {
char out[1024];va_list args;va_start(args,format);vsnprintf(out,sizeof(out),format,args);va_end(args);
assert(strlen(out)<512);messages.emplace_back(out);
}
};
using LPCHARACTER=Character*;
#define CHAT_TYPE_COMMAND 5
#define ACMD(name) void name(LPCHARACTER ch,const char* argument)
#include <cstring>
static void PortCalendarQuery(LPCHARACTER,unsigned,unsigned){}
static bool PortDungeonQuery(LPCHARACTER,unsigned){return true;}
#include "port_features.h"
#include "port_dungeon_rules.h"
static void query(Character& ch,const char* text){clockms+=300;do_port_features(&ch,text);}
int main(){
assert(PortLair(100,3600,300,400).phase==1); // inclusive join boundary
assert(PortLair(100,3600,300,401).phase==2);
assert(PortLair(100,3600,300,3700).phase==2); // original strict-negative availability
assert(PortLair(100,3600,300,3701).phase==0);
assert(PortLair(0,3600,300,1700000000).phase==0);
auto& items=ITEM_MANAGER::instance();auto& mobs=CMobManager::instance();
items.tables={{19,0},{30006,0},{30220,0},{50011,23},{50012,23},{50013,23},{70104,19}};
CMob mob;mob.m_table.dwDropItemVnum=30220;mobs.mobs[101]=&mob;
CMob npc;npc.m_table.bType=1;mobs.mobs[9001]=&npc;
g_vec_pkCommonDropItem[0].emplace_back(1,30,50,19);
g_vec_pkCommonDropItem[0].emplace_back(1,30,50,70104); // Wrong polymorph mob must be excluded.
CDropItemGroup drop(0,101,"fixture");drop.AddItem(30006,100,2);items.m_map_pkDropItemGroup[101]=&drop;
CMobItemGroup kill(101,50,"fixture");kill.AddItem(19,1,20,0);kill.AddItem(30006,3,80,5);items.m_map_pkMobItemGroup[101]=&kill;
CLevelItemGroup level(20);level.AddItem(30220,10,1);items.m_map_pkLevelItemGroup[101]=&level;
CBuyerThiefGlovesItemGroup glove(0,101,"fixture");glove.AddItem(19,25,1);items.m_map_pkGloveItemGroup[101]=&glove;
items.m_map_dwEtcItemDropProb[30220]=40;
auto rows=items.PortWikiDrops(101);assert(rows.size()==7);
assert(rows[0].rule==WIKI_COMMON && rows[0].minimum==1 && rows[0].maximum==30);
assert(rows[1].item==30006 && rows[1].count==2);
assert(rows[2].weight==20 && rows[3].weight==80 && rows[3].total==100 && rows[3].rare==5);
assert(rows[4].rule==WIKI_LEVEL && rows[4].minimum==20);
assert(rows[5].rule==WIKI_GLOVE && rows[6].rule==WIKI_ETC);
assert(items.PortWikiDrops(9001).empty() && items.PortWikiDrops(99999).empty());
drop.AddItem(30220,25,1);assert(items.PortWikiDrops(101).size()==8);
drop.AddItem(70104,1,1);assert(items.PortWikiDrops(101).size()==9); // Non-common groups retain configured polymorph candidates. // Sees active changes, not exported text.
CSpecialItemGroup chest(50011);chest.AddItem(19,1,80,0);chest.AddItem(1,500,20,0);items.special[50011]=&chest;
CSpecialItemGroup multi(50012,CSpecialItemGroup::PCT);multi.AddItem(19,2,20,0);multi.AddItem(3,101,150,0);items.special[50012]=&multi;
CSpecialItemGroup attr(50013,CSpecialItemGroup::SPECIAL);attr.AddItem(19,1,100,0);items.special[50013]=&attr;
auto rewards=items.PortWikiChest(50011);assert(rewards.size()==2 && rewards[0].weight==80 && rewards[1].weight==20 && rewards[1].item==1 && rewards[1].count==500);
assert(rewards[0].total==100 && rewards[0].rule==WIKI_SINGLE);
rewards=items.PortWikiChest(50012);assert(rewards[1].rule==WIKI_INDEPENDENT && rewards[1].weight==150 && rewards[1].total==100);
assert(items.PortWikiChest(50013).empty() && items.PortWikiChest(19).empty());
Character ch;query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 63 1700000000");
ch.messages.clear();query(ch,"1 2 mob 101 0");assert(ch.messages.size()==10 && ch.messages.back()=="MTF1 end 2 8");
query(ch,"1 3 mob 101 999");assert(ch.messages.back()=="MTF1 error 3 offset");
query(ch,"1 4 mob 9001 0");assert(ch.messages.back()=="MTF1 error 4 missing");
query(ch,"1 5 chest 50013 0");assert(ch.messages.back()=="MTF1 error 5 unsupported_chest");
query(ch,"1 6 sources 30006 0");assert(ch.messages.back()=="MTF1 end 6 2");
ch.messages.clear();ch.points[POINT_ATTBONUS_STONE]=10;query(ch,"1 7 stats");
bool stone=false,hp=false;for(auto& line:ch.messages){stone|=line.find(" stone 10")!=std::string::npos;hp|=line.find(" hp 123")!=std::string::npos;}
assert(stone && hp && ch.messages.size()==sizeof(kPortStats)/sizeof(kPortStats[0])+2);
do_port_features(&ch,"1 8 stats");assert(ch.messages.back()=="MTF1 error 8 rate");
for(auto bad:{"2 9 stats","1 0 cap","1 -1 cap","1 4294967296 cap","1 10 cap too many extra"}) {
auto size=ch.messages.size();query(ch,bad);assert(ch.messages.size()==size);
}
assert(rng==0); // Neither previews nor reverse lookup advance gameplay RNG.
puts("PASS: actual query/protocol bodies; active groups, conditions, weights, special rewards, stats, pagination, validation; RNG untouched");
}
'''
# cstring is required by the stub logger, before its declaration.
code=code.replace('#include <algorithm>','#include <algorithm>\n#include <cstring>')
code=code.replace('POINTS','enum {'+', '.join([points[0]+'=1']+points[1:])+'};').replace('GROUPS',groups)
with tempfile.TemporaryDirectory(prefix='mt-feature-query-') as folder:
path=Path(folder);(path/'test.cpp').write_text(code)
subprocess.run(['c++','-std=c++17','-I'+str(SRC),str(path/'test.cpp'),'-o',str(path/'test')],check=True)
subprocess.run([str(path/'test')],check=True)