makes the checks for submitting klumpy result much stricter

This commit is contained in:
2023-06-03 22:51:50 -04:00
parent 238baed47b
commit b8c58dd50f
7 changed files with 81 additions and 13 deletions

View File

@@ -1,14 +1,45 @@
<?php
// Makes sure that this was a valid game
function valid_moves($board, $history)
{
if (count($board) != 4 && count($history) != 16) {
return false;
}
foreach ($board as $row) {
if (count($row) != 4) {
return false;
}
}
$rounds = pow(2, 16) - 1;
foreach ($history as $round) {
if (count($round["hand"]) != 3) {
return false;
}
if (count($round["board"]) != 2) {
return false;
}
$rounds -= pow(2, $round["board"][0] + $round["board"][1] * 4);
if ($rounds < 0) {
return false;
}
$board_card = $board[$round["board"][0]][$round["board"][1]];
$hand_card1_match = $round["hand"][0]["number"] == $board_card["number"] && $round["hand"][0]["color"] == $board_card["color"];
$hand_card2_match = $round["hand"][1]["number"] == $board_card["number"] && $round["hand"][1]["color"] == $board_card["color"];
$hand_card3_match = $round["hand"][2]["number"] == $board_card["number"] && $round["hand"][2]["color"] == $board_card["color"];
if (!$hand_card1_match && !$hand_card2_match && !$hand_card3_match) {
return false;
}
}
return $rounds == 0;
}
require_once "api.php";
// Used to create an entry in the leaderboard
if (array_key_exists("board", $_POST) && array_key_exists("history", $_POST) && array_key_exists("points", $_POST) && array_key_exists("type", $_POST)) {
exec("node ../klumpy/score.js " . escapeshellarg($OGPOST["board"]), $points);
$points = array_sum(json_decode(join("", $points)));
if ($points > 0 && $points == intval($_POST["points"]) && $_POST["type"] == "main") {
if ($points > 0 && $points == intval($_POST["points"]) && $_POST["type"] == "main" && valid_moves(json_decode($OGPOST["board"], true), json_decode($OGPOST["history"], true))) {
if ($USERNAME) {
$ID = random_int(0, 2147483600);
dbCommand("DELETE FROM klumpy WHERE gameID = ?", [$ID]);
dbCommand("INSERT INTO klumpy (gameID, `type`, username, score, board, history) VALUES (?, ?, ?, ?, ?, ?)", [$ID, $_POST["type"], $USERNAME, $points, $OGPOST["board"], $OGPOST["history"]]);
dbCommand("INSERT INTO klumpy `type`, username, score, board, history) VALUES (?, ?, ?, ?, ?, ?)", [$_POST["type"], $USERNAME, $points, $OGPOST["board"], $OGPOST["history"]]);
}
http_response_code(200);
// Gets the leaderboard position
@@ -23,7 +54,8 @@ if (array_key_exists("board", $_POST) && array_key_exists("history", $_POST) &&
"message" => "You are not logged in",
]);
} else {
writeLog("klumpy", "$USERNAME scored $points on leaderboard for position $position");
$ID = dbRequest2("SELECT gameID FROM klumpy WHERE username=? AND board=? AND history=?", "gameID", [$USERNAME, $OGPOST["board"], $OGPOST["history"]])[0];
writeLog(31, "$USERNAME scored $points on leaderboard for position $position");
echo json_encode([
"position" => $position,
"points" => $points,

View File

@@ -9,7 +9,7 @@ include 'functions.php';
<link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<link rel="stylesheet" type="text/css" href="/css/website.css?v=1.0.3" />
<link rel="stylesheet" type="text/css" href="/css/website.css?v=1.0.4" />
<script type="text/javascript" src="/javascript/jquery.js"></script>
<script defer="true" type="text/javascript" src="/javascript/jquery-ui.min.js"></script>
<link defer="true" rel="stylesheet" href="/css/jquery-ui.min.css">

View File

@@ -3,7 +3,7 @@
<html dir="ltr" lang="en">
<?php
if (!file_exists("config.json")) {
echo '<link rel="stylesheet" type="text/css" href="/css/website.css?v=1.0.3" />';
echo '<link rel="stylesheet" type="text/css" href="/css/website.css?v=1.0.4" />';
echo "<h2>Missing configuration please input configuration</h2>";
}
?>

0
html/klumpy/leaderboard.js Normal file → Executable file
View File

0
html/klumpy/leaderboard.php Normal file → Executable file
View File

0
html/klumpy/render.js Normal file → Executable file
View File