Dateien nach "Backend" hochladen
This commit is contained in:
commit
28871900c6
34
Backend/characters.php
Normal file
34
Backend/characters.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
require "db.php";
|
||||||
|
$a = $_GET['action'] ?? '';
|
||||||
|
$b = body();
|
||||||
|
|
||||||
|
function find_user($name) {
|
||||||
|
$st=db()->prepare("SELECT user_id FROM users WHERE username=?");
|
||||||
|
$st->execute([$name]);
|
||||||
|
$r=$st->fetch();
|
||||||
|
return $r ? $r["user_id"] : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a==="create") {
|
||||||
|
$u=trim($b["username"]??"");
|
||||||
|
$c=trim($b["name"]??"");
|
||||||
|
if($u==""||$c=="") out(false,"invalid",[],400);
|
||||||
|
$uid=find_user($u);
|
||||||
|
if(!$uid) out(false,"user not found",[],404);
|
||||||
|
db()->prepare("INSERT INTO characters(user_id,name) VALUES(?,?)")->execute([$uid,$c]);
|
||||||
|
out(true,"created",["character_id"=>db()->lastInsertId()]);
|
||||||
|
}
|
||||||
|
if ($a==="list") {
|
||||||
|
$st=db()->query("SELECT c.character_id,c.name,u.username FROM characters c JOIN users u ON u.user_id=c.user_id");
|
||||||
|
out(true,"ok",["items"=>$st->fetchAll()]);
|
||||||
|
}
|
||||||
|
if ($a==="delete") {
|
||||||
|
$u=$_GET["username"]??""; $c=$_GET["name"]??"";
|
||||||
|
$uid=find_user($u);
|
||||||
|
if(!$uid) out(false,"user not found",[],404);
|
||||||
|
$d=db()->prepare("DELETE FROM characters WHERE user_id=? AND name=?");
|
||||||
|
$d->execute([$uid,$c]);
|
||||||
|
out(true,"deleted",["count"=>$d->rowCount()]);
|
||||||
|
}
|
||||||
|
out(false,"unknown action",[],404);
|
27
Backend/db.php
Normal file
27
Backend/db.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
function db()
|
||||||
|
{
|
||||||
|
static $pdo = null;
|
||||||
|
if ($pdo)
|
||||||
|
return $pdo;
|
||||||
|
$pdo = new PDO("mysql:host=127.0.0.1;dbname=vprmini_simple;charset=utf8mb4", "root", "root", [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||||||
|
]);
|
||||||
|
return $pdo;
|
||||||
|
}
|
||||||
|
function body()
|
||||||
|
{
|
||||||
|
$raw = file_get_contents("php://input");
|
||||||
|
$j = json_decode($raw, true);
|
||||||
|
return is_array($j) ? $j : $_POST;
|
||||||
|
}
|
||||||
|
function out($ok, $msg, $data = [], $status = 200)
|
||||||
|
{
|
||||||
|
http_response_code($status);
|
||||||
|
header("Content-Type: application/json");
|
||||||
|
echo json_encode(["ok" => $ok, "msg" => $msg] + $data);
|
||||||
|
exit;
|
||||||
|
}
|
15
Backend/participants.php
Normal file
15
Backend/participants.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
require "db.php";$action=$_GET['action']??'';$b=body();
|
||||||
|
if($action==="join"){
|
||||||
|
$sid=(int)($b["session_id"]??0);$uid=(int)($b["user_id"]??0);$role=$b["role"]??"Player";if(!in_array($role,["DM","Player"]))$role="Player";
|
||||||
|
$st=db()->prepare("INSERT INTO participants (session_id,user_id,role) VALUES (?,?,?)");$st->execute([$sid,$uid,$role]);
|
||||||
|
out(true,"joined",[],201);
|
||||||
|
}
|
||||||
|
if($action==="list"){ $sid=(int)($_GET["session_id"]??0);
|
||||||
|
$st=db()->prepare("SELECT p.user_id,u.username,p.role FROM participants p JOIN users u ON u.user_id=p.user_id WHERE p.session_id=?");$st->execute([$sid]);
|
||||||
|
out(true,"ok",["items"=>$st->fetchAll()]);
|
||||||
|
}
|
||||||
|
if($action==="leave"){ $sid=(int)($_GET["session_id"]??0);$uid=(int)($_GET["user_id"]??0);
|
||||||
|
$st=db()->prepare("DELETE FROM participants WHERE session_id=? AND user_id=?");$st->execute([$sid,$uid]);
|
||||||
|
out(true,"left",["count"=>$st->rowCount()]);}
|
||||||
|
out(false,"unknown action",[],404);
|
20
Backend/sessions.php
Normal file
20
Backend/sessions.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
require "db.php";
|
||||||
|
$action=$_GET['action']??'';$b=body();
|
||||||
|
if($action==="create"){
|
||||||
|
$owner=(int)($b["owner_id"]??0);$title=trim($b["title"]??"");
|
||||||
|
if($owner<1||$title==="") out(false,"invalid data",[],400);
|
||||||
|
$st=db()->prepare("INSERT INTO sessions (owner_id,title) VALUES (?,?)");$st->execute([$owner,$title]);
|
||||||
|
out(true,"created",["session_id"=>db()->lastInsertId()],201);
|
||||||
|
}
|
||||||
|
if($action==="list"){
|
||||||
|
$owner=isset($_GET["owner_id"])?(int)$_GET["owner_id"]:null;$q=trim($_GET["q"]??"");$sort=$_GET["sort"]??"created_at_desc";
|
||||||
|
$allowed=["created_at_asc"=>"created_at ASC","created_at_desc"=>"created_at DESC","title_asc"=>"title ASC","title_desc"=>"title DESC"];
|
||||||
|
$order=$allowed[$sort]??$allowed["created_at_desc"];
|
||||||
|
$sql="SELECT s.session_id,s.title,s.created_at,u.username FROM sessions s JOIN users u ON u.user_id=s.owner_id WHERE 1=1";$p=[];
|
||||||
|
if($owner){$sql.=" AND s.owner_id=?";$p[]=$owner;}if($q!==""){$sql.=" AND s.title LIKE ?";$p[]="%$q%";}$sql.=" ORDER BY $order";
|
||||||
|
$st=db()->prepare($sql);$st->execute($p);out(true,"ok",["items"=>$st->fetchAll()]);
|
||||||
|
}
|
||||||
|
if($action==="delete"){ $id=(int)($_GET["id"]??0);$st=db()->prepare("DELETE FROM sessions WHERE session_id=?");$st->execute([$id]);
|
||||||
|
out(true,"deleted",["count"=>$st->rowCount()]);}
|
||||||
|
out(false,"unknown action",[],404);
|
30
Backend/setup.sql
Normal file
30
Backend/setup.sql
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
CREATE DATABASE IF NOT EXISTS vprmini_simple DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
USE vprmini_simple;
|
||||||
|
|
||||||
|
--jakob
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS characters;
|
||||||
|
DROP TABLE IF EXISTS sessions;
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
|
|
||||||
|
-- user für login
|
||||||
|
CREATE TABLE users (
|
||||||
|
user_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
username VARCHAR(40) NOT NULL UNIQUE,
|
||||||
|
pass_hash VARCHAR(255) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- charaktere sind unique und gehören zu einem user
|
||||||
|
CREATE TABLE characters (
|
||||||
|
character_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
user_id INT NOT NULL,
|
||||||
|
name VARCHAR(50) NOT NULL,
|
||||||
|
CONSTRAINT fk_char_user FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT ux_user_char UNIQUE(user_id, name)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- session
|
||||||
|
CREATE TABLE sessions (
|
||||||
|
session_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
title VARCHAR(100) NOT NULL
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user