Files
metin2-server/tests/features/test_switchbot_matching.py

53 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Execute the actual server CheckItem body; cast speed must not match attack speed."""
from pathlib import Path
import subprocess
import tempfile
root=Path(__file__).resolve().parents[2]
source=(root/'src/server/game/src/port_switchbot.cpp').read_text()
start=source.index('bool CSwitchbot::CheckItem(')
end=source.index('\nvoid CSwitchbot::SendItemUpdate',start)
body=source[start:end]
preamble=r"""
#include <cassert>
using BYTE=unsigned char;
constexpr int SWITCHBOT_ALTERNATIVE_COUNT=2, SWITCHBOT_NORM_ATTR_NUM=5;
struct TPlayerItemAttribute { BYTE bType=0; short sValue=0; };
struct TSwitchbotAttributeAlternativeTable {
TPlayerItemAttribute attributes[5];
bool IsConfigured() const {for(const auto& a:attributes)if(a.bType && a.sValue)return true;return false;}
};
struct Item { TPlayerItemAttribute attributes[7];
const TPlayerItemAttribute& GetAttribute(int i) const {return attributes[i];}
};
using LPITEM=Item*;
struct CSwitchbot {
struct {TSwitchbotAttributeAlternativeTable alternatives[5][2];}m_table;
bool ValidPosition(BYTE slot)const{return slot<5;}
bool CheckItem(LPITEM,BYTE);
};
"""
checks=r"""
int main(){
CSwitchbot bot{}; Item item{};
bot.m_table.alternatives[0][0].attributes[0]={9,20};
item.attributes[0]={7,20};
assert(!bot.CheckItem(&item,0)); // attack speed cannot satisfy cast speed
item.attributes[0]={9,19}; assert(!bot.CheckItem(&item,0));
item.attributes[0]={9,20}; assert(bot.CheckItem(&item,0));
item.attributes[0]={9,21}; assert(bot.CheckItem(&item,0));
item.attributes[0]={7,20};
bot.m_table.alternatives[0][1].attributes[0]={7,20};
assert(bot.CheckItem(&item,0)); // alternatives are OR, each configured row is AND
bot.m_table.alternatives[0][1].attributes[1]={9,20};
assert(!bot.CheckItem(&item,0));
item.attributes[1]={9,20}; assert(bot.CheckItem(&item,0));
}
"""
with tempfile.TemporaryDirectory(prefix='switchbot-matching-') as folder:
p=Path(folder);(p/'matching.cpp').write_text(preamble+body+checks)
subprocess.run(['c++','-std=c++14',str(p/'matching.cpp'),'-o',str(p/'matching')],check=True)
subprocess.run([str(p/'matching')],check=True)
print('PASS: actual server matcher distinguishes cast(9)/attack(7), thresholds, AND rows, OR alternatives')