first commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Cycle de vie d'un jeu (création, méta, suppression)."""
|
||||
@@ -0,0 +1,77 @@
|
||||
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
|
||||
@@ -0,0 +1,7 @@
|
||||
import shutil
|
||||
|
||||
from ..game_dir import game_dir
|
||||
|
||||
|
||||
def delete_game(slug):
|
||||
shutil.rmtree(game_dir(slug))
|
||||
@@ -0,0 +1,8 @@
|
||||
from ..connection import connect
|
||||
|
||||
|
||||
def game_meta(slug):
|
||||
conn = connect(slug)
|
||||
row = conn.execute("SELECT value FROM _meta WHERE key = 'name'").fetchone()
|
||||
conn.close()
|
||||
return {"slug": slug, "name": row["value"] if row else slug}
|
||||
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
from ..constants import PROJECTS_DIR
|
||||
from ..connection import connect
|
||||
from ..db_path import db_path
|
||||
|
||||
|
||||
def list_games():
|
||||
"""Scanne le dossier projects/ : chaque sous-dossier avec un game.db est un jeu."""
|
||||
games = []
|
||||
if not os.path.isdir(PROJECTS_DIR):
|
||||
return games
|
||||
for slug in sorted(os.listdir(PROJECTS_DIR)):
|
||||
path = db_path(slug)
|
||||
if os.path.isfile(path):
|
||||
conn = connect(slug)
|
||||
row = conn.execute("SELECT value FROM _meta WHERE key = 'name'").fetchone()
|
||||
conn.close()
|
||||
games.append({"slug": slug, "name": row["value"] if row else slug})
|
||||
return games
|
||||
@@ -0,0 +1,8 @@
|
||||
from ..connection import connect
|
||||
|
||||
|
||||
def update_game_name(slug, new_name):
|
||||
conn = connect(slug)
|
||||
conn.execute("UPDATE _meta SET value = ? WHERE key = 'name'", (new_name,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user