78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
import os
|
|
|
|
from ..connection import connect
|
|
from ..game_dir import game_dir
|
|
from ..slugify import slugify
|
|
|
|
|
|
def create_game(name):
|
|
"""Feature 1 : crée le dossier du jeu, ses fichiers index.html/css/js
|
|
reliés entre eux, et sa base de données dédiée (nom du jeu en méta)."""
|
|
slug = slugify(name)
|
|
base = slug
|
|
n = 2
|
|
while os.path.isdir(game_dir(base)):
|
|
base = f"{slug}-{n}"
|
|
n += 1
|
|
slug = base
|
|
|
|
os.makedirs(game_dir(slug), exist_ok=True)
|
|
|
|
index_html = f"""<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>{name}</title>
|
|
<link rel="stylesheet" href="index.css">
|
|
</head>
|
|
<body>
|
|
<h1>{name}</h1>
|
|
<p>Squelette généré par le moteur Forge — à remplir au fur et à mesure
|
|
des fonctionnalités du jeu.</p>
|
|
<script src="index.js"></script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
index_css = """body {
|
|
font-family: system-ui, sans-serif;
|
|
margin: 40px;
|
|
color: #222;
|
|
}
|
|
"""
|
|
index_js = """// index.js — point d'entrée du jeu, généré par le moteur Forge.
|
|
console.log("Jeu chargé.");
|
|
"""
|
|
with open(os.path.join(game_dir(slug), "index.html"), "w", encoding="utf-8") as f:
|
|
f.write(index_html)
|
|
with open(os.path.join(game_dir(slug), "index.css"), "w", encoding="utf-8") as f:
|
|
f.write(index_css)
|
|
with open(os.path.join(game_dir(slug), "index.js"), "w", encoding="utf-8") as f:
|
|
f.write(index_js)
|
|
|
|
conn = connect(slug)
|
|
conn.executescript(
|
|
"""
|
|
CREATE TABLE _meta (key TEXT PRIMARY KEY, value TEXT);
|
|
CREATE TABLE _definitions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
table_name TEXT NOT NULL UNIQUE,
|
|
created_at TEXT DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
CREATE TABLE _fields (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
definition_id INTEGER NOT NULL REFERENCES _definitions(id) ON DELETE CASCADE,
|
|
name TEXT NOT NULL,
|
|
type TEXT NOT NULL,
|
|
relation_definition_id INTEGER REFERENCES _definitions(id),
|
|
required INTEGER NOT NULL DEFAULT 0,
|
|
position INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
"""
|
|
)
|
|
conn.execute("INSERT INTO _meta (key, value) VALUES ('name', ?)", (name,))
|
|
conn.commit()
|
|
conn.close()
|
|
return slug
|