fdk
Coder
Hi all,
I'm a super new coder and I've set myself a project of building a small text-based game played in the browser to learn skills. I've built some basic features of my game and one of these is a gym, which I've been struggling with for a few days now without success.
Essentially, as you will see from my code, I am attempting to set a system whereby a player can train a stat once every 30 minutes. However, no matter what I do, I can't seem to enforce the cooldown. I'm using a database column last_train_time which I have verified is being correctly updated upon a train, as is the stat, however, I cannot seem to force the cooldown - a player is able to bypass the cooldown by just refreshing and resubmitting, or is able to train again without my error message showing and completely bypassing the cooldown.
I've tried multiple versions of code and have recoded the page three times so far to no avail, I just cannot fix this problem. I've read the guidance about not posting a wall of code, but seeing as I have no idea which part I am making an error in, here is the full 130 or so line code for the file - I'd appreciate any help!
I'm a super new coder and I've set myself a project of building a small text-based game played in the browser to learn skills. I've built some basic features of my game and one of these is a gym, which I've been struggling with for a few days now without success.
Essentially, as you will see from my code, I am attempting to set a system whereby a player can train a stat once every 30 minutes. However, no matter what I do, I can't seem to enforce the cooldown. I'm using a database column last_train_time which I have verified is being correctly updated upon a train, as is the stat, however, I cannot seem to force the cooldown - a player is able to bypass the cooldown by just refreshing and resubmitting, or is able to train again without my error message showing and completely bypassing the cooldown.
I've tried multiple versions of code and have recoded the page three times so far to no avail, I just cannot fix this problem. I've read the guidance about not posting a wall of code, but seeing as I have no idea which part I am making an error in, here is the full 130 or so line code for the file - I'd appreciate any help!
PHP:
<?php
session_start();
require_once 'db_connect.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
// Fetch user details
$sql = "SELECT strength, defense, speed, agility, health, gold, in_hospital, last_train_time, last_super_train_time
FROM users WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$now = time();
$train_cooldown = 1800; // 30 minutes in seconds
$super_train_cooldown = 43200; // 12 hours in seconds
$last_train_time = strtotime($user['last_train_time']);
$last_super_train_time = strtotime($user['last_super_train_time']);
$feedback = "";
// Check if the player is in the hospital
if ($user['in_hospital']) {
echo "You cannot use the gym whilst in hospital.";
echo "<br><a href='hospital.php'>Go to Hospital</a>";
exit();
}
// Check if the player has at least 50 health
if ($user['health'] < 50) {
echo "You are currently too injured to use the gym, please wait until you have more health.";
echo "<br><a href='home.php'>Home</a>";
echo " | <a href='hospital.php'>Go to Hospital</a>";
echo " | <a href='inventory.php'>Go to Inventory</a>";
exit();
}
// Handle training requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['train_type']) && isset($_POST['stat'])) {
$train_type = $_POST['train_type'];
$stat = $_POST['stat'];
if (!in_array($stat, ['strength', 'defense', 'speed', 'agility'])) {
$feedback = "Invalid stat selection.";
} else {
if ($train_type === 'train') {
if (($now - $last_train_time) < $train_cooldown) {
$remaining_time = ceil(($train_cooldown - ($now - $last_train_time)) / 60);
$feedback = "You have recently trained, and must wait 30 minutes between training sessions. Cooldown remaining: $remaining_time minutes.";
} else {
$increase = $user[$stat] * 0.0001;
$user[$stat] += $increase;
// Update stat and last_train_time in the database
$stmt = $conn->prepare("UPDATE users SET $stat = ?, last_train_time = NOW() WHERE id = ?");
$stmt->bind_param("di", $user[$stat], $user_id);
$stmt->execute();
$feedback = "Your $stat increased by " . number_format($increase, 8) . "!";
}
} elseif ($train_type === 'super_train') {
if (($now - $last_super_train_time) < $super_train_cooldown) {
$remaining_time = ceil(($super_train_cooldown - ($now - $last_super_train_time)) / 3600);
$feedback = "You have recently used super train and must wait 12 hours between sessions. Cooldown remaining: $remaining_time hours.";
} else {
$increase = $user[$stat] * 0.001;
$user[$stat] += $increase;
// Update stat and last_super_train_time in the database
$stmt = $conn->prepare("UPDATE users SET $stat = ?, last_super_train_time = NOW() WHERE id = ?");
$stmt->bind_param("di", $user[$stat], $user_id);
$stmt->execute();
$feedback = "Your $stat super increased by " . number_format($increase, 8) . "!";
}
} else {
$feedback = "Invalid training type.";
}
}
} else {
$feedback = "Please select a training type and stat.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gym</title>
</head>
<body>
<h2>Welcome to the Gym</h2>
<p>Your current stats:</p>
<ul>
<li>Strength: <?= number_format($user['strength'], 8) ?></li>
<li>Defense: <?= number_format($user['defense'], 8) ?></li>
<li>Speed: <?= number_format($user['speed'], 8) ?></li>
<li>Agility: <?= number_format($user['agility'], 8) ?></li>
<li>Health: <?= $user['health'] ?> / 5000</li>
<li>Gold: <?= $user['gold'] ?></li>
</ul>
<form method="POST">
<h3>Select a training type:</h3>
<input type="radio" name="train_type" value="train" required> Regular Train<br>
<input type="radio" name="train_type" value="super_train" required> Super Train<br>
<h3>Select a stat to train:</h3>
<input type="radio" name="stat" value="strength" required> Strength<br>
<input type="radio" name="stat" value="defense" required> Defense<br>
<input type="radio" name="stat" value="speed" required> Speed<br>
<input type="radio" name="stat" value="agility" required> Agility<br><br>
<button type="submit">Train</button>
</form>
<p><?= $feedback ?></p>
<a href="home.php">Home</a>
<a href="battle.php">Battle</a>
<a href="hospital.php">Hospital</a>
<a href="gym_c.php">Cooldowns</a>
</body>
</html>