forked from blue-llama/e-treasure-hunt
/
level_validation.py
executable file
·170 lines (140 loc) · 6.01 KB
/
level_validation.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""Clientside validator for levels
Some of these checks just make sure that the hunt website won't reject the upload
(without having to actually attempt such an upload).
Other checks are for admin-y things like:
- Tolerances that are suspiciously tight
- README.md files (which are supposed to contain a detailed explanation of the
structure of the level for the GM's use)
being smaller than blurb.txt files (which are supposed to be a hunter-consumable
précis of the level answer/concept once they've solved it)
The checking for the names of the images is stricter than the server requires.
The server will consider the images in alphabetical order, so (say) 1-image.jpg,
2-image.jpg, ... is just a valid a scheme as clue.png, hint1.png, etc.
However, this strict checking does serve to remind the admin to make sure that the
level setter has not come up with their own novel image-naming scheme that wouldn't
work once the server considers the images alphabetically.
"""
from __future__ import annotations
import argparse
import json
import os
import re
import zipfile
from pathlib import Path
from typing import TextIO
CONTENT_TYPES = {
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
}
def unzip_all() -> None:
for filename in os.listdir(ALL_LEVELS_DIR):
if filename.endswith(".zip"):
folder_path: Path = ALL_LEVELS_DIR / filename[:-4]
if not folder_path.exists():
with zipfile.ZipFile(ALL_LEVELS_DIR / filename) as zip_ref:
zip_ref.extractall(folder_path)
def validate_format() -> None:
count = 0
for filename in os.listdir(ALL_LEVELS_DIR):
dir_path = ALL_LEVELS_DIR / filename
if dir_path.is_dir() and "DUMMY" not in filename:
count += 1
if not (dir_path / "about.json").exists():
print("No json in", filename)
else:
# Check json for values
with (dir_path / "about.json").open() as f:
check_json(f, filename)
readme_path: Path | None = None
for possible_readme_filename in (
"readme.md",
"README.md",
"README.txt",
"readme.txt",
):
possible_readme_path = dir_path / possible_readme_filename
if possible_readme_path.exists():
readme_path = possible_readme_path
# Assume only one readme exists
break
if readme_path is None:
print("No readme in", filename)
if not (dir_path / "blurb.txt").exists():
print("No blurb in", filename)
# Check readme is bigger than blurb
if (dir_path / "blurb.txt").exists() and readme_path is not None:
blurb_size = (dir_path / "blurb.txt").stat().st_size
readme_size = readme_path.stat().st_size
if blurb_size > readme_size:
print("Blurb is bigger than readme for", filename)
images = [
dir_path / file
for file in os.listdir(dir_path)
if Path(file).suffix.lower() in CONTENT_TYPES
]
# Should find exactly the right number - check the file extensions if not.
if len(images) != 5:
print(f"Found {len(images)} images in {dir_path}")
else:
images.sort(key=lambda x: x.name.lower())
if not images[0].name.startswith("clue"):
print("No clue in", filename)
# Check the images aren't too big or bad things will happen to the
# upload. We don't want a repeat of the Wawrinka incident.
for image in images:
image_size = image.stat().st_size
if image_size > 3 * 1000 * 1000: # ~3 MB
print(
"Image",
image,
"is too big in",
filename,
"size = ",
f"{image_size:,}",
)
for i in range(1, 5):
if not images[i].name.startswith("hint"):
print("No hint", i, "in", filename)
print("Analyzed", count, "levels")
def check_coord(coord: str, coord_name: str, filename: str) -> None:
lat = float(coord)
if not lat:
print("No", coord_name, "for level", filename)
elif lat == 0.0:
print(" warning: 0", coord_name, "for level", filename)
numbers_and_dp_only = re.sub("[^0-9.]", "", coord)
a, b = numbers_and_dp_only.split(".") if "." in coord else (coord, "")
if len(b) > 5:
print("More than 5 dp for", coord_name, "for level", filename, ":", coord)
if len(a) + len(b) > 7:
print("More than 7 digits for", coord_name, "for level", filename, ":", coord)
def check_json(f: TextIO, filename: str) -> None:
json_data = json.load(f)
if not len(json_data["name"]) > 0:
print("No name for level", filename)
check_coord(json_data["latitude"], "lat", filename)
check_coord(json_data["longitude"], "long", filename)
tol = int(json_data["tolerance"])
if not tol:
print("No tolerance for level", filename)
elif tol < 1:
print("0 tolerance for level", filename)
elif tol < 20:
print("Too-low-resolution tolerance of", tol, "for level", filename)
elif tol <= 50:
print(" warning: Small tolerance of", tol, "for level", filename)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"input_directory",
help="Path to a directory containing the (possibly zipped) "
"levels to be examined",
)
args = argparser.parse_args()
ALL_LEVELS_DIR = Path(args.input_directory)
assert ALL_LEVELS_DIR.exists()
assert ALL_LEVELS_DIR.is_dir()
unzip_all()
validate_format()