Files
metin2-server/tests/features/test_queries.py
T
shenleiandClaude Opus 5.5 afb3dfb1db Add server-authoritative switchbot (automatic attribute change) v1
/mt_switchbot 1 attrs|start|stop|status runs one bot per character that
calls the original UseItem path every 250 ms, so checks, probabilities,
ItemLog CHANGE_ATTRIBUTE and scroll use are the manual path's. Up to two
alternatives of 1-5 targets; stops on matched/limit/no_scroll/item_gone/
rejected/busy/stopped, erased on logout or warp. Per-roll INFO lines are
held back via a quiet flag on ChatPacket. MTF1 cap mask advertises 64.

Verified live on 203 with an extra channel-5 core (port 13040) running the
isolated build: limit, matched, already-matched, move during run, stop and
logout during run; 72 ItemLog rows for 72 rolls. No attribute lock yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:45:10 +09:00

133 lines
8.0 KiB
Python

#!/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 127 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)