Оптимизация страниц. Начало реализации мультиплеера
This commit is contained in:
69
generate.py
69
generate.py
@@ -13,8 +13,8 @@ if help_mode:
|
||||
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')
|
||||
print(f'\tserver - Server mode without .html extension for use folder as static pages')
|
||||
print(f'\t[path]/ - Directory for save pages')
|
||||
exit(0)
|
||||
else:
|
||||
if file_mode and server_mode:
|
||||
@@ -39,12 +39,39 @@ def check_win(board) -> str:
|
||||
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(f"""{'<meta property="og:url" content="ToDo">' if server_mode else ''}<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>""")
|
||||
style = minify("""
|
||||
body{margin:0;padding:20px;font-family:Arial,sans-serif;text-align:center;background:#fff}h1{font-size:24px;margin-bottom:20px}
|
||||
a{text-decoration:none}img{width:10vw}a,h1 a{color:#007bff}table{margin:0 auto;border-collapse:collapse;max-width:100%;overflow-x:auto}
|
||||
td,td a{line-height:50px}td{border:1px solid #ccc;padding:10px;width:50px;height:50px;text-align:center;font-size:18px;vertical-align:middle}
|
||||
td a{display:block;width:100%;height:100%;color:#007bff}td:hover{background:#eee}@media (max-width:600px){body{padding:10px}img{width:20vw}
|
||||
h1{font-size:20px;margin-bottom:10px}td{padding:5px;width:40px;height:40px;font-size:16px}td,td a{line-height:40px}}""")
|
||||
apply_css = minify(f"const style=document.createElement('style');style.textContent='{style}';document.head.appendChild(style);")
|
||||
preload = minify("""
|
||||
document.addEventListener('mouseover', e => {
|
||||
const a = e.target.closest('a[href]');
|
||||
if (!a) return;
|
||||
if (!a.dataset.prefetched) {
|
||||
a.dataset.prefetched = true;
|
||||
fetch(a.href, {mode: 'no-cors'});
|
||||
}
|
||||
});
|
||||
""")
|
||||
pages = {
|
||||
'index':
|
||||
minify(f"""<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta property="og:url" content="http://0x174.su/tic/tac/toe/index.html">
|
||||
<title>oxTicTacToe</title><img src="/favicon.ico"><h1>oxTicTacToe.html</h1>
|
||||
<p>Select team for start [ <a href="o---------.html">O</a> | <a href="x---------.html">X</a> ]
|
||||
{'<br/><a href="../../../multiplayer">multiplayer mode</a>' if server_mode else ''}</p>
|
||||
<a href="https://git.0x174.su/wowlikon/oxTTT">source code</a>
|
||||
<script src="{'s' if file_mode else 'a'}.js"></script>""")
|
||||
}
|
||||
|
||||
scripts = {
|
||||
's': apply_css,
|
||||
'p': preload,
|
||||
'a': (apply_css+preload)
|
||||
}
|
||||
|
||||
try: iterable = tqdm(itertools.product('-xo', repeat=10))
|
||||
except: iterable = itertools.product('-xo', repeat=10)
|
||||
@@ -61,8 +88,8 @@ for field_tuple in iterable:
|
||||
line = lambda n: "index.html"
|
||||
title = f'Draft'
|
||||
else: title = field[0]
|
||||
content = minify(f"""{'<meta property="og:url" content="ToDo">' if server_mode else ''}
|
||||
<title>oxTTT:{title}</title><h1><a href="{line(0)}">{title}</a></h1>
|
||||
content = minify(f"""<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta property="og:url" content="http://0x174.su/tic/tac/toe/{field}.html"><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].replace('-', '□')}{'' if field[1] != '-' else '</a>'}</td>
|
||||
@@ -79,22 +106,18 @@ for field_tuple in iterable:
|
||||
<td>{'' if field[8] != '-' else f'<a href="{line(8)}">'}{field[8].replace('-', '□')}{'' if field[8] != '-' else '</a>'}</td>
|
||||
<td>{'' if field[9] != '-' else f'<a href="{line(9)}">'}{field[9].replace('-', '□')}{'' 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 ''}"
|
||||
</table><script src="{'s' if file_mode else 'a'}.js"></script>""")
|
||||
pages[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():
|
||||
for filename, content in pages.items():
|
||||
with open(f'{path}{filename}{".html" if file_mode else ""}', 'w+', encoding='utf-8') as f:
|
||||
f.write(('<meta property="og:type" content="website">' if server_mode else '')+(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);"""))
|
||||
|
||||
for filename, content in scripts.items():
|
||||
with open(f'{path}{filename}.js', 'w+', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
|
||||
with open(f'{path}s.css', 'w+', encoding='utf-8') as f:
|
||||
f.write(style)
|
||||
|
||||
Reference in New Issue
Block a user