Skip to content

Commit

Permalink
guess10: added current WIP version
Browse files Browse the repository at this point in the history
  • Loading branch information
gheja committed Aug 1, 2019
1 parent 916561a commit 14cde67
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/website/api/guess10/v1/.htaccess
@@ -0,0 +1,3 @@
RewriteEngine On

RewriteRule .* index.php [L]
110 changes: 110 additions & 0 deletions src/website/api/guess10/v1/index.php
@@ -0,0 +1,110 @@
<?php
define("VERSION", "v1");
define("BASE_URL", "/api/guess10/" . VERSION);

require_once("../../../config.php");
// define("BASE_PATH",

$url = substr($_SERVER["REQUEST_URI"], strlen(BASE_URL));

function return_json($obj)
{
echo json_encode($obj);
die();
}

function return_bad_request()
{
header("HTTP/1.1 400 Bad Request");
return_json(array("result" => "error", "result_code" => 400, "result_text" => "Bad request"));
}

function return_unauthorized()
{
header("HTTP/1.1 401 Unauthorized");
return_json(array("result" => "error", "result_code" => 401, "result_text" => "Unauthorized"));
}

function db_connect()
{
global $db;

$db = new PDO("mysql:host=" . DB_HOSTNAME . ";dbname=" . DB_DATABASE, DB_USERNAME, DB_PASSWORD);

if (!$db)
{
echo "Cannot reach database.";
die();
}
}

function bot_auth()
{
global $db, $bot;

// TODO: syntax check on parameters

$statement = $db->prepare("SELECT * FROM bot WHERE bot_client_id = ? AND bot_secret = ? AND is_deleted != 1");
$statement->bindValue(1, 123);
$statement->bindValue(2, 123);
$statement->execute();

$bot = $statement->fetch(PDO::FETCH_ASSOC);

print_r($bot);

if (!$bot)
{
return_unauthorized();
}
}

function match_auth()
{
global $db, $match;

// TODO: syntax check on parameters

if ($bot["current_match"] != "xxx")
{
return_unauthorized();
}

$statement = $db->prepare("SELECT * FROM match WHERE match_id = ? AND is_deleted != 1");
$statement->bindValue(1, 123);
$statement->execute();

$match = $statement->fetch(PDO::FETCH_ASSOC);

print_r($match);

if (!$match)
{
return_unauthorized();
}
}

$db = null;
$bot = null;
$match = null;

switch ($url)
{
case "/start":
db_connect();
bot_auth();
break;

case "/join":
return_bad_request();
break;

case "/guess":
db_connect();
bot_auth();
match_auth();
break;
}

return_bad_request();
?>

0 comments on commit 14cde67

Please sign in to comment.