first pass at /api/me endpoint, adds auth redirect to play

This commit is contained in:
Sundog Jones 2024-07-01 11:10:33 -04:00
parent 40b6f01b2a
commit b516b526ba
3 changed files with 106 additions and 3 deletions

90
src/webserver/api/me.php Normal file
View File

@ -0,0 +1,90 @@
<?php
session_start();
if (!$_SESSION || !$_SESSION['nickname'] || trim($_SESSION['nickname']) == "") {
header('Location: https://wander.reclaim.technology/webserver/auth/login.php');
exit();
}
$db_path = realpath(dirname(__FILE__) . '/../config/db.php');
require_once($db_path);
// initialize some variables to populate from the database
$player_record = [];
$items = [];
$npcs = [];
$encounters = [];
$quests = [];
if (isset($_POST['id'])) {
// update player info
// TODO
} else {
// get player info
$player_query = $conn->prepare("SELECT * FROM Players p WHERE p.id = :player_id AND p.is_active = 1");
$player_query->bindValue(':player_id', $_SESSION['player_id']);
$player_result = $player_query->execute();
if ($player_result) {
$player_record = $player_result->fetchArray(SQLITE3_ASSOC);
}
// get player items
$items_query = $conn->prepare("SELECT * FROM PlayerItems pi JOIN Items i ON pi.item_id = i.id WHERE pi.player_id = :player_id");
$items_query->bindValue(':player_id', $_SESSION['player_id']);
$items_result = $items_query->execute();
if ($items_result) {
while ($items_record = $items_result->fetchArray(SQLITE3_ASSOC)) {
array_push($items, $items_record);
}
}
// get player NPCs
$npcs_query = $conn->prepare("SELECT * FROM PlayerNPCs pn WHERE pn.player_id = :player_id AND pn.is_active = 1");
$npcs_query->bindValue(':player_id', $_SESSION['player_id']);
$npcs_result = $npcs_query->execute();
if ($npcs_result) {
while ($npcs_record = $npcs_result->fetchArray(SQLITE3_ASSOC)) {
array_push($npcs, $npcs_record);
}
}
// get player NPC encounters
$encounters_query = $conn->prepare("SELECT * FROM PlayerEncounters pe WHERE pe.player_id = :player_id");
$encounters_query->bindValue(':player_id', $_SESSION['player_id']);
$encounters_result = $encounters_query->execute();
if ($encounters_result) {
while ($encounters_record = $encounters_result->fetchArray(SQLITE3_ASSOC)) {
array_push($encounters, $encounters_record);
}
}
// get player quests
$quests_query = $conn->prepare("SELECT * FROM PlayerQuests pq JOIN Quests q ON pq.quest_id = q.id WHERE pq.player_id = :player_id");
$quests_query->bindValue(':player_id', $_SESSION['player_id']);
$quests_result = $quests_query->execute();
if ($quests_result) {
while ($quests_record = $quests_result->fetchArray(SQLITE3_ASSOC)) {
array_push($quests, $quests_record);
}
}
// put it all together and return as JSON
$player = (object) [
'id' => $player_record['id'],
'nickname' => $player_record['nickname'],
'items' => $items,
'npcs' => $npcs,
'encounters' => $encounters,
'quests' => $quests
];
header('Content-Type: application/json; charset=utf-8');
echo json_encode($player);
exit();
}
?>

View File

@ -46,9 +46,17 @@
echo "<p><a href='register.php'>Register a new account</a>, <a href='forgot_pw.php'>reset your password</a>, or try again.</p>"; echo "<p><a href='register.php'>Register a new account</a>, <a href='forgot_pw.php'>reset your password</a>, or try again.</p>";
loginForm(); loginForm();
} else { } else {
echo "<p>You are now logged in as <strong>" . $_SESSION['nickname'] . "</strong></p>\n"; if (isset($_GET['redirect']) && $_GET['redirect'] == "play") {
echo "<p><a href='/../play/index.php'>Play now</a></p>\n"; ?>
echo "<p><a href='logout.php'>Log out</a></p>\n"; <script>
window.location.href = "https://wander.reclaim.technology/webserver/play/";
</script>
<?php
} else {
echo "<p>You are now logged in as <strong>" . $_SESSION['nickname'] . "</strong></p>\n";
echo "<p><a href='../play/index.php'>Play now</a></p>\n";
echo "<p><a href='logout.php'>Log out</a></p>\n";
}
} }
} }

View File

@ -1,5 +1,10 @@
<?php <?php
session_start(); session_start();
if (!$_SESSION || !$_SESSION['nickname'] || trim($_SESSION['nickname']) == "") {
header('Location: https://wander.reclaim.technology/webserver/auth/login.php?redirect=play');
exit();
}
$db_path = realpath(dirname(__FILE__) . '/../config/db.php'); $db_path = realpath(dirname(__FILE__) . '/../config/db.php');
require_once($db_path); require_once($db_path);