Files
metin2-client/tools/server/ring-warp/tests/test_ring.py
T

197 lines
8.7 KiB
Python

# coding: utf-8
"""Catalog coverage plus Lua 5.0 menu/permission/warp integration checks.
Usage: python3 test_ring.py SERVER_LIBLUA_DIRECTORY
"""
import importlib.util
import json
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('ring_generate', ROOT / 'generate.py')
generator = importlib.util.module_from_spec(spec)
spec.loader.exec_module(generator)
quest = generator.generate()
assert quest == (ROOT / 'ring_warp.quest').read_text(encoding='utf-8'), 'regenerate quest'
catalog = json.loads((ROOT / 'maps.json').read_text(encoding='utf-8'))
categories = catalog['categories']
assert len(categories) == 11, 'AVA2 11 regional categories expected'
# Validate the destinations against the 203 map catalogue, including overlapping map rectangles.
classic = json.loads((ROOT / 'maps_classic_65.json').read_text(encoding='utf-8'))
regions = {m['id']: m for c in classic['categories'] for m in c['maps']}
regions[210] = dict(base=[768000, 1408000], size=[3, 4])
expected = [[1, 3, 21, 23, 41, 43], [64]*4, [63]*4, [61]*3, [62]*3,
[104, 104, 71, 71], [67]*3+[68]*3, [69, 70], [72, 72, 73, 73],
[301]*3+[302]*3+[304]*3+[303]*3, [210]]
for category, map_ids in zip(categories, expected):
points = [d for group in category.get('subgroups', [category]) for d in group['destinations']]
assert len(points) == len(map_ids)
for point, map_id in zip(points, map_ids):
region = regions[map_id]
x, y = region['base']; w, h = region['size']
assert x <= point['x'] < x+w*25600 and y <= point['y'] < y+h*25600, (point, map_id)
if map_id == 64:
tower = regions[66]; tx, ty = tower['base']; tw, th = tower['size']
assert not (tx <= point['x'] < tx+tw*25600 and ty <= point['y'] < ty+th*25600), point
# Verify GBK safety
encoded = quest.encode('gbk')
assert b'\\' not in encoded, 'GBK backslash found'
# Prepare Lua 5.0 script runner
helper = quest.split(' function destinations()', 1)[1].split(' when 10031.sig_use', 1)[0]
body = quest.split(' when 10031.sig_use or 70058.use or 76042.use begin', 1)[1].rsplit(' end', 1)[0]
script = '''
NOWINDOW = 0
setskin = function(skin) end
say = function(t) end
ring_warp = {}
function ring_warp.destinations()
''' + helper + '''
function use_ring()
''' + body + '''
end
local queue = {}
local cursor = 0
local can_warp = true
local blocked_after_select = false
local level = 90
local warps = {}
pc = {
can_warp = function() return can_warp end,
get_level = function() return level end,
warp = function(x, y) table.insert(warps, {x, y}) end
}
select = function(...)
cursor = cursor + 1
local choice = queue[cursor]
assert(choice and choice >= 1 and choice <= table.getn(arg), "bad choice: " .. tostring(choice))
if blocked_after_select then can_warp = false end
return choice
end
function run(answers, p_level, p_can_warp)
queue = answers; cursor = 0; level = p_level; can_warp = p_can_warp; warps = {}
use_ring()
assert(cursor == table.getn(queue), "navigation mismatch: cursor " .. cursor .. " expected " .. table.getn(queue))
return warps
end
-- Test 1: can_warp = false blocks immediately
local w = run({}, 100, false)
assert(table.getn(w) == 0, "warp should be blocked")
-- Test 2: Category 1 (各国主城), Dest 1 (盛唐一村)
local w = run({1, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 469300 and w[1][2] == 964200, "盛唐一村 failed")
-- Test 3: Category 1 (各国主城), Dest 4 (秦皇二村)
local w = run({1, 4}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 138500 and w[1][2] == 234900, "秦皇二村 failed")
-- Test 4: Category 2 (升龙谷), Dest 1 (中心)
local w = run({2, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 332800 and w[1][2] == 745700, "升龙谷中心 failed")
-- Test 5: Category 3 (影悲沙漠), Dest 2 (绿洲1区)
local w = run({3, 2}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 250600 and w[1][2] == 544200, "沙漠绿洲1区 failed")
-- Test 6: Category 4 (舒寒山), Dest 1 (雪山中心)
local w = run({4, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 434400 and w[1][2] == 214700, "雪山中心 failed")
-- Test 7: Category 5 (导炎大地), Dest 1 (火地中心)
local w = run({5, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 607400 and w[1][2] == 686800, "火地中心 failed")
-- Test 8: Category 6 (蜘蛛洞穴), direct cave warps (all 4 deep spots)
local w = run({6, 1}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 60000 and w[1][2] == 496300, "蜘蛛一层入口 failed")
local w = run({6, 2}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 91800 and w[1][2] == 525400, "蜘蛛一层深处 failed")
local w = run({6, 3}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 704100 and w[1][2] == 463900, "蜘蛛二层入口 failed")
local w = run({6, 4}, 50, true)
assert(table.getn(w) == 1 and w[1][1] == 704000 and w[1][2] == 522700, "蜘蛛二层深处/三层入口 failed")
-- Test 9: Category 7 (鬼怪森林), Sub 1 (幽灵森林) & Sub 2 (红森林)
local w = run({7, 1, 1}, 70, true)
assert(table.getn(w) == 1 and w[1][1] == 290800 and w[1][2] == 5700, "幽灵森林入口 failed")
local w = run({7, 2, 3}, 70, true)
assert(table.getn(w) == 1 and w[1][1] == 1061400 and w[1][2] == 25800, "红森林练级区 2 failed")
-- Test 10: Category 8 (巨人荒地), Dest 1 & 2
local w = run({8, 1}, 60, true)
assert(table.getn(w) == 1 and w[1][1] == 1119200 and w[1][2] == 791500, "巨人领地 1区 failed")
local w = run({8, 2}, 60, true)
assert(table.getn(w) == 1 and w[1][1] == 830500 and w[1][2] == 764000, "蛇之领域 failed")
-- Test 11: Category 9 (流放洞窟), level check < 75 blocked
local w = run({9}, 70, true)
assert(table.getn(w) == 0, "流放洞窟 70级应被拦截")
-- Test 12: Category 9 (流放洞窟), level >= 75 deep cave warps (all 4 spots)
local w = run({9, 1}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 10000 and w[1][2] == 1207800, "流放一层入口 failed")
local w = run({9, 2}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 140300 and w[1][2] == 1323600, "流放一层深处 failed")
local w = run({9, 3}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 241600 and w[1][2] == 1273700, "流放二层入口 failed")
local w = run({9, 4}, 75, true)
assert(table.getn(w) == 1 and w[1][1] == 222000 and w[1][2] == 1220800, "龙穴门前 failed")
-- Test 13: Category 10 (尤哈拉海角), level < 90 blocked
local w = run({10}, 89, true)
assert(table.getn(w) == 0, "海角 89级应被拦截")
-- Test 14: Category 10 (尤哈拉海角), level >= 90 all 4 sub-regions
local w = run({10, 1, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1104800 and w[1][2] == 1788800, "龙头海角中心 failed")
local w = run({10, 2, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1225700 and w[1][2] == 1682000, "迷雾森林中心 failed")
local w = run({10, 3, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1134100 and w[1][2] == 1655400, "雷鸣山脉中心 failed")
local w = run({10, 4, 1}, 90, true)
assert(table.getn(w) == 1 and w[1][1] == 1087900 and w[1][2] == 1649800, "黑沙海湾中心 failed")
-- Test 15: Category 11 (附魔森林), level < 105 blocked
local w = run({11}, 104, true)
assert(table.getn(w) == 0, "附魔森林 104级应被拦截")
-- Test 16: Category 11 (附魔森林), level >= 105
local w = run({11, 1}, 105, true)
assert(table.getn(w) == 1 and w[1][1] == 816700 and w[1][2] == 1505000, "附魔森林 failed")
-- Test 17: Close main menu (option 12)
local w = run({12}, 100, true)
assert(table.getn(w) == 0, "关闭应无传送")
-- State can change while the destination menu is open.
blocked_after_select = true
local w = run({1, 1}, 120, true)
assert(table.getn(w) == 0, "state changed during menu must block warp")
blocked_after_select = false
print("PASS: 11 AVA2 regional categories, deep cave warps, level checks, and warp guard verified")
'''
lua = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT.parents[3] / 'metin2-server/src/server/liblua'
with tempfile.TemporaryDirectory(prefix='ring-warp-test-') as d:
d = Path(d)
sources = [p for p in sorted((lua / 'src').glob('*.c')) if p.name != 'ltests.c']
sources += [lua / 'src/lib' / name for name in ('lauxlib.c', 'lbaselib.c', 'ltablib.c', 'lstrlib.c', 'lmathlib.c')]
command = ['cc', '-w', '-I' + str(lua / 'include'), '-I' + str(lua / 'src'), str(ROOT / 'tests/lua_runner.c'),
*[str(p) for p in sources], '-lm', '-o', str(d / 'lua')]
subprocess.run(command, check=True)
(d / 'test.lua').write_bytes(script.encode('gbk'))
subprocess.run([str(d / 'lua'), str(d / 'test.lua')], check=True)
print('PASS: catalog matches AVA2 regional layout, coordinates in bounds, GBK source safe')