Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no more sql-injections; fixed dungeon exp rewarding; refactoring. #2

Merged
merged 20 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
Makefile
Makefile
vendor
74 changes: 74 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

function sendApiResponse(array $responseData, bool $success = true): void {
header('Content-Type: application/json');
echo json_encode(['success' => $success, 'data' => $responseData]);
}

function getCurrentPlayer(): \Game\Player {
return \Game\Game::instance()->findPlayer($_SESSION['username']);
}

session_start();

if (!isset($_SESSION['username'])) {
header('Location: /login.php');
exit();
}

$action = $_GET['action'] ?? '';
if (!is_string($action)) {
throw new RuntimeException('Tried to perform invalid action');
}

switch ($action) {
case 'addChatMessage':
$message = (string)$_POST['message'] ?? '';
if ($message === '') {
sendApiResponse(['error' => 'Message can not be empty'], false);
break;
}

\Game\Game::instance()->chat->addMessage(getCurrentPlayer(), $message);

sendApiResponse([]);

break;
case 'getChatMessages':
$maxMessagesToShow = 10;
$messages = \Game\Game::instance()->chat->getLastMessages($maxMessagesToShow);
$responseData = [];
foreach ($messages as $message) {
$responseData[] = [
'isFromAdmin' => $message->isFromAdmin,
'sender' => $message->sender,
'message' => $message->content,
'sentAt' => $message->sentAt->format(DATE_ATOM),
];
}
sendApiResponse($responseData);
break;
case 'ban':
$userToBan = $_POST['username'] ?? '';
if ($userToBan === '') {
sendApiResponse(['error' => 'You need to pass the username to ban him'], false);
break;
}

$currentUser = getCurrentPlayer();
if (!$currentUser->isAdmin()) {
sendApiResponse(['error' => 'Sorry, you\'re not admin.'], false);
break;
}

\Game\Game::instance()->banPlayer($userToBan);

sendApiResponse([]);

break;
default:
throw new RuntimeException('Unknown ');
}
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "crilleaz/eloth_online",
"description": "Web based, web browser MMORPG",
"autoload": {
"psr-4": {
"Game\\": "src/"
}
},
"require": {
"php": "^8.2",
"ext-mysqli": "*",
"doctrine/dbal": "^3.6"
}
}
Loading