The following files exists in this folder. Click to view.
home.php1089 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
<?php
session_start();
require_once('databaseconnection.php');
include('check_login.php');
include('functions.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LBMLadders</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand ms-0 ms-lg-4" href="#">LBM Ladders</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link <?php if(!isset($_GET['action'])){echo'active" aria-current="page"';}else{echo'"';} ?> href="home.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link <?php if(isset($_GET['action']) && $_GET['action'] == 'joinLadder'){echo'active" aria-current="page"';}else{echo'"';} ?> href="?action=joinLadder">Join Ladder</a>
</li>
<li class="nav-item">
<a class="nav-link <?php if(isset($_GET['action']) && $_GET['action'] == 'createLadder'){echo'active" aria-current="page"';}else{echo'"';} ?> href="?action=createLadder">Create Ladder</a>
</li>
<li class="nav-item">
<a class="nav-link <?php if(isset($_GET['action']) && $_GET['action'] == 'editLadder'){echo'active" aria-current="page"';}else{echo'"';} ?> href="?action=editLadder">Edit Your Ladders</a>
</li>
</ul>
<ul class="navbar-nav me-4 mb-2 mb-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Menu
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><span class="dropdown-item-text">Logged in as: <?php if(isset($_SESSION['username'])){echo $_SESSION['username'];}?></span></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="logout.php?action=logout">Logout</a></li>
<?php
if(isset($_COOKIE['remember'])){
echo'<li><a class="dropdown-item" href="logout.php?action=cookie">Forget login</a></li>';
}
?>
<li><a class="dropdown-item" href="password_form.php">Change password</a></li>
<li><a class="dropdown-item" href="delete_user_form.php">Delete user</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<?php
echo'<div class="container-fluid">';
if(isset($_GET['action'])){
switch($_GET['action']){ //Different forms (except 'editLadder')
case 'joinLadder': //joinLadder
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Join a Ladder!</h1></div>
</div>';
echo'<div class="container py-4"></div>';
echo'</div>';
echo'<div class="row justify-content-center">
<form class="col-md-4 p-4 border border-success-subtle rounded" action="join_ladder.php" method="POST">
<label for="code" class="form-label">Enter your code:</label>
<div class="row mb-3">
<div class="col-9" id="code">
<input type="text" class="form-control" name="code" required>
</div>
<div class="col-3">
<button type="submit" class="btn btn-success">Join</button>
</div>
</div>
</form>
</div>
';
echo'</div>';
break;
case 'createLadder': //createLadder
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Create your own Ladder!</h1></div>
</div>';
echo'<div class="row justify-content-center py-5">
<form class="col-md-5 p-4 border border-success-subtle rounded" action="create_ladder.php" method="POST">
<div class="mb-3">
<label for="Laddername" class="form-label">Name</label>
<input type="text" class="form-control" id="Laddername" name="laddername" required>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" maxlength="1000" name="description"></textarea>
</div>
<hr class="hr m-4">
<div class="mb-3">
<label for="gameType" class="form-label">Game Type</label>
<div class="mb-3" id="gameType">
<input type="radio" class="btn-check" name="pointsystem" value="percantage" id="gameType5" autocomplete="off" disabled>
<label class="btn" for="gameType5">Percentage</label>
<input type="radio" class="btn-check" name="pointsystem" value="fixedSum" id="gameType7" autocomplete="off" checked>
<label class="btn" for="gameType7">Fixed Sum</label>
</div>
</div>
<div class="mb-3">
<label for="startingPoints" class="form-label">Starting Points</label>
<input type="number" class="form-control" id="startingPoints" name="startingpoints" value="1000" required>
</div>
<div class="row mb-3">
<div class="col-6">
<label for="minbet" class="form-label">Minimum Bet</label>
<input type="number" class="form-control" id="minbet" name="minbet" value="100" required>
</div>
<div class="col-6">
<label for="minbetPercent" class="form-label">Minimum Bet% Raise per time</label>
<div class="input-group">
<input type="number" class="form-control" id="minbetPercent" name="minbetPercent" value="50">
<span class="input-group-text">%</span>
</div>
</div>
</div>
<div class="md-3">
<label for="minbetTime" class="form-label">Time between raises</label>
<div class="input-group" id="minbetTime">
<span class="input-group-text">Time</span>
<input type="number" class="form-control" name="y" placeholder="y">
<input type="number" class="form-control" name="m" placeholder="m">
<input type="number" class="form-control" name="d" placeholder="d">
<input type="number" class="form-control" name="h" placeholder="h">
<input type="number" class="form-control" name="i" placeholder="i">
<input type="number" class="form-control" name="s" placeholder="s">
</div>
</div>
<hr class="hr m-4">
<div class="row mb-3">
<div class="col-6">
<label for="startDate">Start date</label>
<input id="startDate" class="form-control" type="datetime-local" name="startdate">
</div>
<div class="col-6">
<label for="enddate">End date (leave blank if infinite)</label>
<input id="enddate" class="form-control" type="datetime-local" name="enddate">
</div>
</div>
<div class="mb-3">
<label for="open" class="form-label">Can players join after starting date?</label>
<div class="mb-3" id="open">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="open" id="inlineRadio1" value="1" checked>
<label class="form-check-label" for="inlineRadio1">yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="open" id="inlineRadio2" value="0">
<label class="form-check-label" for="inlineRadio2">no</label>
</div>
</div>
</div>
<div class="mb-3">
<label for="playerroof" class="form-label">Player Roof (leave blank if infinite)</label>
<input type="number" class="form-control" id="playerroof" name="playerroof">
</div>
<button type="submit" class="btn btn-success mb-2">Create</button>
</form>
</div>
';
break;
case 'editLadder': //editLadder
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Your ladders</h1></div>
</div>';
echo'<div class="row text-center p-4">
</div>';
echo'<div class="row justify-content-center">
<div class="col-8 border rounded py-2">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Code</th>
<th scope="col">Players</th>
<th scope="col">Can join</th>
<th scope="col">Start datetime</th>
<th scope="col">End datetime</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>';
$sql = "SELECT * FROM Ladders WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$res = array_reverse($res);
foreach($res as $a){
$enddate = $a['enddate'] ?? "-";
$playercount = playercount($a['ladderId']);
if (isset($a['playerroof'])){
$playercount .= "/".$a['playerroof'];
}
$open = ($a['open'] == 1 or $a['startdate'] >= date('Y-m-d H:i:s')) ? "Yes" : "No";
echo"<tr class='position-relative'>
<td>".$a['laddername']."</td>
<td>".$a['code']."</td>
<td>".$playercount."</td>
<td>".$open."</td>
<td>".$a['startdate']."</td>
<td>".$enddate."</td>
<td><a class='btn btn-outline-success btn-sm disabled' href='home.php?editladder=".$a['ladderId']."'>edit</a></td>
<td><a class='btn btn-outline-success btn-sm' href='home.php?openladder=".$a['ladderId']."'>open</a></td>
</tr></a>";
}
echo' </tbody>
</table>
</div>
</div>';
break;
case 'challenge': //challenge
//Check that both players are in the selected ladder:
$sql = "SELECT playerId FROM Players WHERE userId = :userId AND ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId'], 'ladderId' => $_GET['ladder']));
$res = $stm->fetch(PDO::FETCH_ASSOC);
if ($res['playerId'] == $_GET['defender']){
header('location: home.php');
exit();
}
validateladdertime($res['playerId'], 'yes');
$challengerId = $res['playerId'];
$defenderId = $_GET['defender'];
if (isset($res) && $res != Null){
$sql = "SELECT ladderId FROM Players WHERE playerId = :defenderId AND ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(['defenderId' => $_GET['defender'], 'ladderId' => $_GET['ladder']]);
$res = $stm->fetch(PDO::FETCH_ASSOC);
}
if (!isset($res) or (isset($res) && $res == Null)){
header('location: home.php');
exit();
}
//Get ladder information
$sql = "SELECT * FROM Ladders WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $_GET['ladder']));
$ladderInf = $stm->fetch(PDO::FETCH_ASSOC);
$minbetPercent = $ladderInf['minbetPercent']/100;
$minbet = $ladderInf['minbet'];
if ((isset($ladderInf['minbetPercent']) && $ladderInf['minbetPercent'] != NULL) and (isset($ladderInf['minbetTime']) && $ladderInf['minbetTime'] != NULL)){
$startS = strtotime($ladderInf['startdate']);
$now = strtotime('now');
for ($x = 1; ; $x++) {
if (($startS + ($ladderInf['minbetTime'] * $x)) <= $now){
$minbet += ($ladderInf['minbet'] * $minbetPercent);
}
else{
break;
}
}
}
if ($minbet > playerpoints($challengerId) or $minbet > playerpoints($defenderId)){
$minbet = playerpoints($challengerId);
if (playerpoints($challengerId) > playerpoints($defenderId)){
$minbet = playerpoints($defenderId);
}
}
$maxbet = playerpoints($challengerId);
if (playerpoints($challengerId) > playerpoints($defenderId)){
$maxbet = playerpoints($defenderId);
}
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Challenge '.getplayername((int)$defenderId).'!</h1></div>
</div>';
echo'<div class="row justify-content-center py-5">
<form class="col-md-5 p-4 border border-success-subtle rounded" action="create_match.php?defenderId='.$defenderId.'&challengerId='.$challengerId.'&ladderId='.$_GET['ladder'].'" method="POST">
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" maxlength="1000" name="description"></textarea>
</div>
<hr class="hr m-4">
<div class="mb-3 row">
<label for="challenger" class="col-sm-4 col-form-label">Your points:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="challengers" value="'.playerpoints($challengerId).'">
</div>
<label for="defender" class="col-sm-4 col-form-label">'.getplayername((int)$defenderId).' points:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.playerpoints($defenderId).'">
</div>
</div>
<div class="mb-3 row">
<label for="challenger" class="col-sm-4 col-form-label">Minimum betlimit:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="challengers" value="'.$minbet.'">
</div>
<label for="defender" class="col-sm-4 col-form-label">Maximum betlimit:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$maxbet.'">
</div>
</div>
<div class="mb-3">
<label for="matchpoints" class="form-label">Challenge bet</label>
<input type="number" class="form-control" id="matchpoints" name="matchpoints" value="'.$minbet.'" min="'.$minbet.'" max="'.$maxbet.'" required>
</div>
<hr class="hr m-4">
<div class="mb-3">
<label for="startDate">Match start (not required)</label>
<input id="startDate" class="form-control" type="datetime-local" name="matchdate">
</div>
<button type="submit" class="btn btn-success mb-2">Challenge</button>
</form>
</div>';
break;
case 'answer': //answer
$sql = "SELECT * FROM Matches WHERE matchId = :matchId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('matchId' => $_GET['matchId']));
$match = $stm->fetch(PDO::FETCH_ASSOC);
//Validating player
$sql = "SELECT playerId FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$exit = TRUE;
foreach($res as $a){
if ((int)$match['defenderId'] == (int)$a['playerId']){
$exit = FALSE;
validateladdertime($match['defenderId'], 'yes');
}
}
if ($exit == TRUE){
header('location: home.php');
exit();
}
if (!Isset($match['matchdate'])){
$matchdate = 'right now';
}
else{
$matchdate = $match['matchdate'];
}
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Challenged by '.getplayername((int)$match['challengerId']).'!</h1></div>
</div>';
$url = 'answer_match.php?matchId=' . $match['matchId'];
if (isset($_GET['openladder'])) {
$url .= '&openladder=' . urlencode($_GET['openladder']);
}
echo'<div class="row justify-content-center py-5">
<form class="col-md-5 p-4 border border-success-subtle rounded" action="'.$url.'" method="POST">
<div class="mb-3 row justify-content-center">
<label for="staticEmail" class="col-sm-3 col-form-label">From '.getplayername((int)$match['challengerId']).':</label>
<div class="col-sm-9">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="'.$match['description'].'">
</div>
</div>
<div class="mb-3 row">
<label for="challenger" class="col-sm-3 col-form-label">Your points:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="challengers" value="'.playerpoints($match['defenderId']).'">
</div>
<label for="defender" class="col-sm-3 col-form-label">'.getplayername((int)$match['challengerId']).' points:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.playerpoints($match['challengerId']).'">
</div>
</div>
<div class="mb-3 row">
<label for="defender" class="col-sm-3 col-form-label">Match bet:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$match['defenderpoints'].'">
</div>
<label for="defender" class="col-sm-3 col-form-label">Match date:</label>
<div class="col-sm-4">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$matchdate.'">
</div>
</div>
<hr class="hr m-4">
<div class="mb-3 row">
<label for="gameType" class="col-sm-2 col-form-label">Answer:</label>
<div class="mb-3 col-sm-10" id="gameType">
<input type="radio" class="btn-check" name="answer" value="accepted" id="gameType5" autocomplete="off">
<label class="btn" for="gameType5">Bet '.$match['defenderpoints'].' and Accept</label>
<input type="radio" class="btn-check" name="answer" value="declined" id="gameType7" autocomplete="off" checked>
<label class="btn" for="gameType7">Decline</label>
</div>
</div>
<div class="row justify-content-center pt-2">
<button type="submit" class="btn btn-success mb-2 col-auto">Answer</button>
</div>
</form>
</div>';
break;
case 'endmatch': //select a winner
$sql = "SELECT * FROM Matches WHERE matchId = :matchId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('matchId' => $_GET['matchId']));
$match = $stm->fetch(PDO::FETCH_ASSOC);
//Validating player
$sql = "SELECT playerId FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$exit = TRUE;
foreach($res as $a){
if ((int)$match['defenderId'] == (int)$a['playerId'] or (int)$match['challengerId'] == (int)$a['playerId'] && $match['status'] == 'accepted'){
if ((int)$match['challengerId'] == (int)$a['playerId']){
$winnerId = $match['defenderId'];
}
else{
$winnerId = $match['challengerId'];
}
$exit = FALSE;
validateladdertime($match['defenderId'], 'yes');
}
}
if ($exit == TRUE){
header('location: home.php');
exit();
}
$matchdate = $match['matchdate'];
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Match against '.getplayername((int)$winnerId).'</h1></div>
</div>';
$url = 'end_match.php?matchId=' . $match['matchId'];
if (isset($_GET['openladder'])) {
$url .= '&openladder=' . urlencode($_GET['openladder']);
}
echo'<div class="row justify-content-center py-5">
<form class="col-md-5 p-4 border border-success-subtle rounded" action="'.$url.'" method="POST">
<div class="mb-3 row justify-content-center">
<label for="staticEmail" class="col-sm-3 col-form-label">Description:</label>
<div class="col-sm-9">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="'.$match['description'].'">
</div>
</div>
<div class="mb-3 row">
<label for="defender" class="col-sm-3 col-form-label">Match bet:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$match['defenderpoints'].'">
</div>
<label for="defender" class="col-sm-3 col-form-label">Match date:</label>
<div class="col-sm-4">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$matchdate.'">
</div>
</div>
<div class="mb-3 row">
<div class="col-sm-12">
<input type="text" readonly class="form-control-plaintext" id="defender" value="The player who did not win must answer this.">
</div>
</div>
<button type="submit" class="btn btn-success mb-2 col-auto">'.getplayername($winnerId).' won</button>
</form>
</div>';
break;
case 'stopmatch': //stop not answered match
$sql = "SELECT * FROM Matches WHERE matchId = :matchId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('matchId' => $_GET['matchId']));
$match = $stm->fetch(PDO::FETCH_ASSOC);
//Validating player and match
$sql = "SELECT playerId FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$exit = TRUE;
foreach($res as $a){
if ((int)$match['challengerId'] == (int)$a['playerId'] && $match['status'] == 'request'){
$exit = FALSE;
validateladdertime($match['challengerId'], 'yes');
}
}
if ($exit == TRUE){
header('location: home.php');
exit();
}
if (!Isset($match['matchdate'])){
$matchdate = 'right now';
}
else{
$matchdate = $match['matchdate'];
}
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Match against '.getplayername((int)$match['defenderId']).'</h1></div>
</div>';
$url = 'stop_match.php?matchId=' . $match['matchId'];
if (isset($_GET['openladder'])) {
$url .= '&openladder=' . urlencode($_GET['openladder']);
}
echo'<div class="row justify-content-center py-5">
<form class="col-md-5 p-4 border border-success-subtle rounded" action="'.$url.'" method="POST">
<div class="mb-3 row justify-content-center">
<label for="staticEmail" class="col-sm-3 col-form-label">Description:</label>
<div class="col-sm-9">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="'.$match['description'].'">
</div>
</div>
<div class="mb-3 row">
<label for="defender" class="col-sm-3 col-form-label">Match bet:</label>
<div class="col-sm-2">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$match['defenderpoints'].'">
</div>
<label for="defender" class="col-sm-3 col-form-label">Match date:</label>
<div class="col-sm-4">
<input type="text" readonly class="form-control-plaintext" id="defender" value="'.$matchdate.'">
</div>
</div>
<button type="submit" class="btn btn-success mb-2 col-auto">Stop match</button>
</form>
</div>';
break;
}
}
elseif(isset($_GET['openladder'])){ //Opened ladder
$sql = "SELECT * FROM Ladders WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $_GET['openladder']));
$ladderInf = $stm->fetch(PDO::FETCH_ASSOC);
$exit = TRUE;
//checking if the user is the ladder owner
if ((int)$_SESSION['userId'] === (int)$ladderInf['userId']) {
$exit = FALSE;
}
//If not then theck if the user is a player
if ($exit){
$sql = "SELECT userId FROM Players WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $_GET['openladder']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$exit = TRUE;
foreach($res as $a){
if ((int)$_SESSION['userId'] == (int)$a['userId'] || (int)$_SESSION['userId'] == (int)$ladderInf['userId']){
$exit = FALSE;
}
}
}
//force out user if not in the ladder or owner
if ($exit == TRUE){
header('location: home.php');
exit();
}
$playercount = playercount($ladderInf['ladderId']);
if (isset($ladderInf['playerroof'])){
$playercount .= "/".$ladderInf['playerroof'];
}
$header = '';
$td = '';
$th = '';
$notIn = True;
if(isset($_GET['oladder'])){ //If matches should be displayed
$th = '<th scope="col">Oponent</th>
<th scope="col">Winner</th>
<th scope="col">Bet</th>
<th scope="col">Matchdate</th>
<th scope="col">Status</th>
<th scope="col"></th>';
$sql = "SELECT playerId FROM Players WHERE ladderId = :ladderId AND userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $_GET['openladder'], 'userId' => $_SESSION['userId']));
$playerId = $stm->fetch(PDO::FETCH_ASSOC);
$notIn = False;
if(!empty($playerId)){
$playerId = $playerId['playerId'];
$playerpoints = playerpoints($playerId);
switch($_GET['oladder']){
case 'latestMatches': //latestMatches
$header = 'Latest Matches';
$sql = "SELECT * FROM Matches WHERE defenderId = :playerId OR challengerId = :playerId";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'openladder='.$_GET['openladder'].'&oladder=latestMatches';
break;
case 'activeMatches': //activeMatches
$header = 'Active Matches';
$sql = "SELECT * FROM Matches WHERE (defenderId = :playerId OR challengerId = :playerId) AND status IN ('request', 'accepted')";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'openladder='.$_GET['openladder'].'&oladder=activeMatches';
break;
case 'wonMatches': //wonMatches
$header = 'Won Matches';
$sql = "SELECT * FROM Matches WHERE winnerId = :playerId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'openladder='.$_GET['openladder'].'&oladder=wonMatches';
break;
case 'lostMatches': //lostMatches
$header = 'Lost Matches';
$sql = "SELECT * FROM Matches WHERE (defenderId = :playerId OR challengerId = :playerId) AND winnerId != :playerId AND status IN ('won')";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'openladder='.$_GET['openladder'].'&oladder=lostMatches';
break;
}
foreach($i as $a){ //Using the data from database and setting up the printing
if ($a['defenderId'] == $playerId){
$playername = getplayername((int)$a['challengerId']);
}
else{
$playername = getplayername((int)$a['defenderId']);
}
if (isset($a['winnerId'])){
$winnername = getplayername($a['winnerId']);
}
else{
$winnername = '-';
}
if (($a['status'] == 'accepted')){
$hl = 'action=endmatch&matchId='.$a['matchId'].'&openladder='.$_GET['openladder'].'';
}
elseif($a['status'] == 'request'){
$hl = 'action=stopmatch&matchId='.$a['matchId'].'&openladder='.$_GET['openladder'].'';
}
else{
$hl = $h;
}
if(($a['status'] == 'request' or $a['status'] == 'stopped') and $a['defenderId'] == $playerId){
continue;
}
$td .="<tr class='position-relative'>
<td>".$playername."</td>
<td>".$winnername."</td>
<td>".$a['defenderpoints']."</td>
<td>".$a['matchdate']."</td>
<td>".$a['status']."</td>
<td><a href='home.php?".$hl."' class='stretched-link'></a></td>
</tr></a>";
}
}
}
else{
$header = 'Players: '.$playercount.'';
$th = '<th scope="col">Name</th>
<th scope="col">Points</th>
<th scope="col">Points (without active matches)</th>
<th scope="col"></th>';
$sql = "SELECT * FROM Players WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $ladderInf['ladderId']));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($i as $a){
$playername = getplayername((int)$a['playerId']);
if ($a['userId'] == $_SESSION['userId']){
$playerId = $a['playerId'];
$playername .= ' (you)';
$playerpoints = playerpoints($a['playerId']);
$notIn = False;
}
$td .="<tr class='position-relative'>
<td>".$playername."</td>
<td>".playerpoints($a['playerId'])."</td>
<td>".playerpointsA($a['playerId'])."</td>
<td><a href='home.php?action=challenge&defender=".$a['playerId']."&ladder=".$_GET['openladder']."' class='stretched-link'></a></td>
</tr></a>";
}
}
$minbetPercent = $ladderInf['minbetPercent']/100;
$minbet = $ladderInf['minbet'];
if ((isset($ladderInf['minbetPercent']) && $ladderInf['minbetPercent'] != NULL) and (isset($ladderInf['minbetTime']) && $ladderInf['minbetTime'] != NULL)){
$startS = strtotime($ladderInf['startdate']);
$now = strtotime('now');
for ($x = 1; ; $x++) {
if (($startS + ($ladderInf['minbetTime'] * $x)) <= $now){
$minbet += ($ladderInf['minbet'] * $minbetPercent);
}
else{
$timetoraise = ($startS + $ladderInf['minbetTime'] * $x) - $now;
$timetoraise = sectotime($timetoraise);
$nextminbet = $minbet + ($ladderInf['minbet'] * $minbetPercent);
break;
}
}
}
if ($ladderInf['startdate'] < date('Y-m-d H:i:s')){
$date = "ends: ";
$date .= $ladderInf['enddate'] ?? "when there is only one left";
}else{
$date = "starts: ";
$date .= $ladderInf['startdate'];
}
$ubet = '';
if ($minbetPercent < 0.01){
$ubet = '<div class="col-auto pt-3">
<h3 class="fw-bold text-success">Minbet: '.$minbet.'</h3>
</div>';
}
else{
$ubet = '<div class="col-auto pt-3">
<h3 class="fw-bold text-success">Minbet: '.$minbet.'</h3>
</div>
<div class="col-auto pt-3">
<h3 class="fw-bold text-success">Next Minbet: '.$nextminbet.'</h3>
</div>
<div class="col-auto pt-3">
<h3 class="fw-bold text-success">Time until raise: '.$timetoraise.'</h3>
</div>';
}
if (!$notIn){
$upoints = '<h2 class="fw-bold text-success">Points: '.$playerpoints.'</h2>';
}
else{
$upoints = '';
}
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Welcome to '.$ladderInf['laddername'].'!</h1></div>
</div>';
echo'<div class="row text-center pt-5">
<div class="col"><p class="fw-bold text-success">'.$ladderInf['description'].'</p></div>
</div>';
echo'<div class="row text-center justify-content-center pt-5">
<div class="col-2">
</div>
<div class="col-6">
<h2 class="fw-bold text-success">Ladder '.$date.'</h2>
</div>
<div class="col-2">
<h2 class="fw-bold text-success">Code: '.$ladderInf['code'].'</h2>
</div>
</div>';
echo'<div class="row justify-content-center pb-2 pt-3">
<div class="col-2 pt-3">
'.$upoints.'
</div>
<div class="row col-8 justify-content-end">
'.$ubet.'
</div>
</div>';
echo'<div class="row justify-content-center pb-2 pt-5">
<div class="col-2 pt-3">
<h2 class="fw-bold text-success">'.$header.'</h2>
</div>
<div class="row col-6 pt-3">
<div class="col-auto text-success">
<a class="btn btn-success" href="home.php?openladder='.$_GET['openladder'].'" role="button">Ladder</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="home.php?openladder='.$_GET['openladder'].'&oladder=latestMatches" role="button">Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="home.php?openladder='.$_GET['openladder'].'&oladder=activeMatches" role="button">Active Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="home.php?openladder='.$_GET['openladder'].'&oladder=wonMatches" role="button">Won Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="home.php?openladder='.$_GET['openladder'].'&oladder=lostMatches" role="button">Lost Matches</a>
</div>
</div>
<div class="col-2 pt-3">
<h2 class="fw-bold text-success">Requests</h2>
</div>
</div>';
echo'<div class="row justify-content-center">
<div class="col-8 border rounded py-2">
<table class="table table-hover">
<thead>
<tr>
'.$th.'
</tr>
</thead>
<tbody>
'.$td.'
</tbody>
</table>
</div>'; //Requests
$td = '';
$th = '<th scope="col">From</th>
<th scope="col">Bet</th>
<th scope="col"></th>';
if(isset($playerId)){
$sql = "SELECT * FROM Matches WHERE defenderId = :playerId AND status IN ('request')";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($i as $a){
$td .="<tr class='position-relative'>
<td>".getplayername((int)$a['challengerId'])."</td>
<td>".$a['defenderpoints']."</td>
<td><a href='home.php?action=answer&matchId=".$a['matchId']."&openladder=".$_GET['openladder']."' class='stretched-link'></a></td>
</tr></a>";
}
}
echo'<div class="col-2 border rounded py-2">
<table class="table table-hover">
<thead>
<tr>
'.$th.'
</tr>
</thead>
<tbody>
'.$td.'
</tbody>
</table>
</div>';
echo'</div>';
}
else{ //Hemskärmen man kommer till som inloggad
echo'<div class="row text-center pt-5">
<div class="col"><h1 class="fw-bold text-success">Welcome!</h1></div>
</div>';
$td = "";
$th = "";
$header = "";
if(isset($_GET['home'])){
$th = '<th scope="col">Ladder</th>
<th scope="col">Oponent</th>
<th scope="col">Winner</th>
<th scope="col">Bet</th>
<th scope="col">Matchdate</th>
<th scope="col">Status</th>
<th scope="col"></th>';
$sql = "SELECT playerId FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$playerId = $stm->fetchAll(PDO::FETCH_COLUMN); //Fetching COLUMN instead of ASSOC to get an one level array.
$placeholders = implode(',', array_fill(0, count($playerId), '?'));
if(!empty($playerId)){
switch($_GET['home']){
case 'latestMatches': //latestMatches
$header = 'Latest Matches';
$sql = "SELECT * FROM Matches WHERE defenderId IN ($placeholders) OR challengerId IN ($placeholders)";
$stm = $pdo->prepare($sql);
$stm->execute(array_merge($playerId, $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'home=latestMatches';
break;
case 'activeMatches': //activeMatches
$header = 'Active Matches';
$sql = "SELECT * FROM Matches WHERE (defenderId IN ($placeholders) OR challengerId IN ($placeholders)) AND status IN ('request', 'accepted')";
$stm = $pdo->prepare($sql);
$stm->execute(array_merge($playerId, $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'home=activeMatches';
break;
case 'wonMatches': //wonMatches
$header = 'Won Matches';
$sql = "SELECT * FROM Matches WHERE winnerId IN ($placeholders)";
$stm = $pdo->prepare($sql);
$stm->execute($playerId);
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'home=wonMatches';
break;
case 'lostMatches': //lostMatches
$header = 'Lost Matches';
$sql = "SELECT * FROM Matches WHERE (defenderId IN ($placeholders) OR challengerId IN ($placeholders)) AND winnerId NOT IN ($placeholders) AND status IN ('won')";
$stm = $pdo->prepare($sql);
$stm->execute(array_merge($playerId, $playerId, $playerId));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
$i = array_reverse($i);
$h = 'home=lostMatches';
break;
}
foreach($i as $a){ //Using the data from database and setting up the printing
if (in_array($a['defenderId'], $playerId)){
$playername = getplayername((int)$a['challengerId']);
}
else{
$playername = getplayername((int)$a['defenderId']);
}
if (isset($a['winnerId'])){
$winnername = getplayername($a['winnerId']);
}
else{
$winnername = '-';
}
if ($a['status'] == 'accepted'){
$hl = 'action=endmatch&matchId='.$a['matchId'].'';
}
elseif($a['status'] == 'request'){
$hl = 'action=stopmatch&matchId='.$a['matchId'].'';
}
else{
$hl = $h;
}
if(($a['status'] == 'request' or $a['status'] == 'stopped') and in_array($a['defenderId'], $playerId)){
continue;
}
$sql = "SELECT ladderId FROM Players WHERE playerId = :defenderId";
$stm = $pdo->prepare($sql);
$stm->execute(array('defenderId' => $a['defenderId']));
$l = $stm->fetch(PDO::FETCH_ASSOC);
$sql = "SELECT laddername FROM Ladders WHERE ladderId = :ladderId";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $l['ladderId']));
$l = $stm->fetch(PDO::FETCH_ASSOC);
$td .="<tr class='position-relative'>
<td>".$l['laddername']."</td>
<td>".$playername."</td>
<td>".$winnername."</td>
<td>".$a['defenderpoints']."</td>
<td>".$a['matchdate']."</td>
<td>".$a['status']."</td>
<td><a href='home.php?".$hl."' class='stretched-link'></a></td>
</tr></a>";
}
}
}
else{ //Ladders
$header = 'Ladders';
$th = '<th scope="col">Name</th>
<th scope="col">Players</th>
<th scope="col">Can join</th>
<th scope="col">Start/End date</th>
<th scope="col"></th>';
$sql = "SELECT ladderId FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($res as $a){
$sql = "SELECT * FROM Ladders WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $a['ladderId']));
$res = $stm->fetch(PDO::FETCH_ASSOC);
$i[] = $res;
}
if(isset($i)){
$i = array_reverse($i);
foreach($i as $a){
if ($a['startdate'] < date('Y-m-d H:i:s')){
$date = "End: ";
$date .= $a['enddate'] ?? "-";
}else{
$date = "Start: ";
$date .= $a['startdate'];
}
$playercount = playercount($a['ladderId']);
if (isset($a['playerroof'])){
$playercount .= "/".$a['playerroof'];
}
$open = ($a['open'] == 1 or $a['startdate'] > date('Y-m-d H:i:s')) ? "Yes" : "No";
$td .="<tr class='position-relative'>
<td>".$a['laddername']."</td>
<td>".$playercount."</td>
<td>".$open."</td>
<td>".$date."</td>
<td><a href='home.php?openladder=".($a['ladderId'])."' class='stretched-link'></a></td>
</tr></a>";
}
}
}
echo'<div class="row justify-content-center pb-2 pt-5">
<div class="col-2 pt-3">
<h2 class="fw-bold text-success">'.$header.'</h2>
</div>
<div class="row col-6 pt-3">
<div class="col-auto text-success">
<a class="btn btn-success" href="?" role="button">Ladders</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="?home=latestMatches" role="button">Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="?home=activeMatches" role="button">Active Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="?home=wonMatches" role="button">Won Matches</a>
</div>
<div class="col-auto text-success">
<a class="btn btn-success" href="?home=lostMatches" role="button">Lost Matches</a>
</div>
</div>
<div class="col-2 pt-3">
<h2 class="fw-bold text-success">Requests</h2>
</div>
</div>';
echo'<div class="row justify-content-center">
<div class="col-8 border rounded py-2">
<table class="table table-hover">
<thead>
<tr>
'.$th.'
</tr>
</thead>
<tbody>
'.$td.'
</tbody>
</table>
</div>'; //Requests
$td = '';
$th = '<th scope="col">From</th>
<th scope="col">Ladder</th>
<th scope="col"></th>';
$sql = "SELECT * FROM Players WHERE userId = :userId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('userId' => $_SESSION['userId']));
$i = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($i as $a){
$sql = "SELECT laddername FROM Ladders WHERE ladderId = :ladderId;";
$stm = $pdo->prepare($sql);
$stm->execute(array('ladderId' => $a['ladderId']));
$laddername = $stm->fetch(PDO::FETCH_ASSOC);
$sql = "SELECT * FROM Matches WHERE defenderId = :playerId AND status IN ('request')";
$stm = $pdo->prepare($sql);
$stm->execute(array('playerId' => $a['playerId']));
$j = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($j as $b){
$td .="<tr class='position-relative'>
<td>".getplayername((int)$b['challengerId'])."</td>
<td>".$laddername['laddername']."</td>
<td><a href='home.php?action=answer&matchId=".$b['matchId']."' class='stretched-link'></a></td>
</tr></a>";
}
}
echo'<div class="col-2 border rounded py-2">
<table class="table table-hover">
<thead>
<tr>
'.$th.'
</tr>
</thead>
<tbody>
'.$td.'
</tbody>
</table>
</div>';
echo'</div>';
echo'</div>';
}
echo'</div>';
?>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.min.js" integrity="sha384-G/EV+4j2dNv+tEPo3++6LCgdCROaejBqfUeNjuKAiuXbjrxilcCdDz6ZAVfHWe1Y" crossorigin="anonymous"></script>
</body>
</html>