forked from Cryolite/kanachan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·107 lines (83 loc) · 3.2 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
from pathlib import Path
import os
import json
import sys
from kanachan.simulation import (TestModel, test,)
def _on_leaf(test_file_path: Path):
print(f'{test_file_path}: ', end='', flush=True)
with open(test_file_path) as f:
test_data = json.load(f)
uuid = test_data['uuid']
room = test_data['room']
style = test_data['style']
players = test_data['players']
rounds = test_data['rounds']
player_grades = tuple(p['grade'] for p in players)
paishan_list = []
for r in rounds:
paishan = r['paishan']
paishan_list.append(paishan)
simulation_mode = 1 # Non-duplicated simulation mode
if style == 0:
simulation_mode += 2 # Dong Feng Zhan (東風戦)
simulation_mode += (1 << (3 + room))
test_model = TestModel(rounds)
result = test(simulation_mode, player_grades, test_model, paishan_list)
if len(result['rounds']) != len(test_data['rounds']):
raise RuntimeError(f'{result["rounds"]} != {test_data["rounds"]}')
for i in range(len(test_data['rounds'])):
round_data = test_data['rounds'][i]
round_result = result['rounds'][i]
for j in range(4):
delta_score_to_be = round_data['delta_scores'][j]
delta_score_as_is = round_result[j]['delta_score']
if delta_score_as_is != delta_score_to_be:
delta_scores_as_is = [round_result[k]['delta_score'] for k in range(4)]
raise RuntimeError(
f'''{i}-th round:
{delta_scores_as_is}
{round_data["delta_scores"]}''')
final_ranking = [test_data['players'][i]['final_ranking'] for i in range(4)]
if result['ranking'] != final_ranking:
raise RuntimeError(f'{result["ranking"]} != {final_ranking}')
final_scores = [test_data['players'][i]['final_score'] for i in range(4)]
if result['scores'] != final_scores:
raise RuntimeError(f'{result["scores"]} != {final_scores}')
print('PASS')
def main():
if len(sys.argv) < 2:
raise RuntimeError('Too few arguments.')
if len(sys.argv) > 3:
raise RuntimeError('Too many arguments.')
path = Path(sys.argv[1])
if not path.exists():
raise RuntimeError(f'{path}: Does not exist.')
if path.is_file():
_on_leaf(path)
return
skip_list_path = None
if len(sys.argv) == 3:
skip_list_path = Path(sys.argv[2])
if not skip_list_path.is_file():
raise RuntimeError(f'{skip_list_path}: Not a file.')
skip_list = set()
if skip_list_path is not None:
with open(skip_list_path) as f:
for line in f:
line = line.rstrip('\n')
skip_list.add(line)
if not path.is_dir():
raise RuntimeError(f'{path}: Neither a file nor a directory.')
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if not filename.endswith('.json'):
continue
test_file_path = Path(dirpath) / filename
if test_file_path.stem in skip_list:
print(f'{test_file_path}: SKIP')
continue
_on_leaf(test_file_path)
if __name__ == '__main__':
main()
sys.exit(0)