100 lines
4.8 KiB
Python
100 lines
4.8 KiB
Python
import itertools, os, sys
|
|
try: from tqdm import tqdm
|
|
except: pass
|
|
|
|
args = sys.argv
|
|
help_mode = "help" in args
|
|
file_mode = "file" in args
|
|
server_mode = "server" in args
|
|
path = list(filter(lambda x: x.endswith("/") or x.endswith("\\"), args))
|
|
|
|
if help_mode:
|
|
print(f'Usage: python generate.py [args]')
|
|
print(f'Args:')
|
|
print(f'\thelp - Show this message')
|
|
print(f'\tfile - File mode with .html extension')
|
|
print(f'\tserver - Server mode without .html extension for use folder as static files')
|
|
print(f'\t[path]/ - Directory for save files')
|
|
exit(0)
|
|
else:
|
|
if file_mode and server_mode:
|
|
print("args 'file' and 'server' is incompatible")
|
|
exit(1)
|
|
elif not (file_mode or server_mode):
|
|
print("args 'file' or 'server' is required")
|
|
exit(1)
|
|
elif len(path) > 1:
|
|
print("args must contain only one path (optional)")
|
|
exit(1)
|
|
|
|
if len(path) == 0: path = "static/"
|
|
else: path = path[0]
|
|
|
|
def check_win(board) -> str:
|
|
lines = [
|
|
[0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
|
|
[0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
|
|
[0, 4, 8], [2, 4, 6] # diagonals
|
|
]
|
|
return next((board[line[0]] for line in lines if board[line[0]] == board[line[1]] == board[line[2]] != '□'), '□')
|
|
|
|
minify = lambda s: s.replace('\n', '').replace('\t', '').replace(' ', '')
|
|
files = {
|
|
'index':
|
|
minify('''<meta property="og:type" content="website"><meta property="og:url" content="ToDo"><title>oxTicTacToe</title>
|
|
<h1>oxTicTacToe.html</h1><p>Select team for start [ <a href="o□□□□□□□□□.html">O</a> | <a href="x□□□□□□□□□.html">X</a> ]</p>
|
|
<a href="https://git.0x174.su/wowlikon/oxTTT">source code</a><script src="s.js"></script>''')
|
|
}
|
|
|
|
try: iterable = tqdm(itertools.product('□xo', repeat=10))
|
|
except: iterable = itertools.product('□xo', repeat=10)
|
|
for field_tuple in iterable:
|
|
field = ''.join(field_tuple)
|
|
if abs(field.count('x') - field.count('o')) > 1 or field[0] == '□': continue
|
|
win = check_win(list(field[1:]))
|
|
np = "x" if field.startswith("o") else "o"
|
|
line = lambda n: (np + field[1:])[:n] + field[0] + (np + field[1:])[n+1:] + '.html'
|
|
if win != '□':
|
|
line = lambda n: "index.html"
|
|
title = f'WIN {win}'
|
|
elif '□' not in field:
|
|
line = lambda n: "index.html"
|
|
title = f'Draft'
|
|
else: title = field[0]
|
|
content = minify(f"""<meta property="og:type" content="website"><meta property="og:url" content="ToDo">
|
|
<title>oxTTT:{title}</title><h1><a href="{line(0)}">{title}</a></h1>
|
|
<table>
|
|
<tr>
|
|
<td>{'' if field[1] != '□' else f'<a href="{line(1)}">'}{field[1]}{'' if field[1] != '□' else '</a>'}</td>
|
|
<td>{'' if field[2] != '□' else f'<a href="{line(2)}">'}{field[2]}{'' if field[2] != '□' else '</a>'}</td>
|
|
<td>{'' if field[3] != '□' else f'<a href="{line(3)}">'}{field[3]}{'' if field[3] != '□' else '</a>'}</td>
|
|
</tr>
|
|
<tr>
|
|
<td>{'' if field[4] != '□' else f'<a href="{line(4)}">'}{field[4]}{'' if field[4] != '□' else '</a>'}</td>
|
|
<td>{'' if field[5] != '□' else f'<a href="{line(5)}">'}{field[5]}{'' if field[5] != '□' else '</a>'}</td>
|
|
<td>{'' if field[6] != '□' else f'<a href="{line(6)}">'}{field[6]}{'' if field[6] != '□' else '</a>'}</td>
|
|
</tr>
|
|
<tr>
|
|
<td>{'' if field[7] != '□' else f'<a href="{line(7)}">'}{field[7]}{'' if field[7] != '□' else '</a>'}</td>
|
|
<td>{'' if field[8] != '□' else f'<a href="{line(8)}">'}{field[8]}{'' if field[8] != '□' else '</a>'}</td>
|
|
<td>{'' if field[9] != '□' else f'<a href="{line(9)}">'}{field[9]}{'' if field[9] != '□' else '</a>'}</td>
|
|
</tr>
|
|
</table><script src="s.js"></script>""")
|
|
files[field] = f"{'<a href="../index.html">' if win != '□' else ''}{content}{'</a>' if win != '□' else ''}"
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
for filename, content in files.items():
|
|
with open(f"{path}{filename}{'.html' if file_mode else ''}", 'w+', encoding='utf-8') as f:
|
|
f.write(content.replace('.html') if server_mode else content)
|
|
|
|
with open(f'{path}s.js', 'w+', encoding='utf-8') as f:
|
|
f.write(minify("""
|
|
const style=document.createElement('style');
|
|
style.textContent='
|
|
body{margin:0;padding:20px;font-family:Arial,sans-serif;text-align:center}
|
|
h1{font-size:24px;margin-bottom:20px}a{text-decoration:none;color:#007bff}
|
|
h1 a{color:#007bff}table{margin:0 auto;border-collapse:collapse}
|
|
td{border:1px solid #ccc;padding:10px;width:50px;height:50px;
|
|
text-align:center;font-size:18px;line-height:50px;vertical-align:middle}
|
|
td a{display:block;width:100%;height:100%;line-height:50px;color:#007bff}
|
|
td:hover{background:#f0f0f0}';document.head.appendChild(style);""")) |