Files
metin2-server/tests/features/test_calendar.py
T

73 lines
3.9 KiB
Python

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