Skip to content

Instantly share code, notes, and snippets.

@rodel77
Last active November 20, 2016 02:30
Show Gist options
  • Save rodel77/76c631fa27ee9044370fbaeedf6cadff to your computer and use it in GitHub Desktop.
Save rodel77/76c631fa27ee9044370fbaeedf6cadff to your computer and use it in GitHub Desktop.
MBG WebStats
<?php
## CONFIGURATION ###
# MySQL Connection #
$sql_host = "";
$sql_username = "";
$sql_password = "";
$sql_dbname = "";
# Strings #
$select_game = "Select Game";
$stat = "{game} stats!";
$selectdisplay = "Select a Game to display stats!";
# Strings - Games #
$games = array();
$games["connect4"] = "Connect4";
$games["fabulousfred"] = "FabolousFred";
$games["hearthstone"] = "HearthStone";
$games["mastermind"] = "MasterMind";
$games["rockpaperscissors"] = "RockPaperScissors";
$games["tictactoe"] = "TicTacToe";
$games["uno"] = "UNO";
$games["whackamole"] = "WhackAMole";
# Strings - Columns #
$columns = array();
$columns["discsPlaced"] = "Discs Placed";
$columns["purplePlaced"] = "Purple Placed";
$columns["minionsDamage"] = "Minions Damage";
$columns["lose"] = "Lose";
$columns["GhastPlaced"] = "Ghast Placed";
$columns["SilverfishPlaced"] = "Silverfish Placed";
$columns["mobsSmashed"] = "Mobs Smashed";
$columns["VillagerPlaced"] = "Villager Placed";
$columns["WitherBossPlaced"] = "Wither Boss Placed";
$columns["horizontalWins"] = "Horizontal Wins";
$columns["rock"] = "Rock";
$columns["coins"] = "Coins";
$columns["timesPlayed"] = "Times Played";
$columns["diagonalWins"] = "Diagonal Wins";
$columns["CreeperPlaced"] = "Creeper Placed";
$columns["songs"] = "Songs";
$columns["bluePlaced"] = "Blue Placed";
$columns["ZombieVillagerPlaced"] = "Zombie Villager Placed";
$columns["linesCompleted"] = "Lines Completed";
$columns["EnderDragonPlaced"] = "Ender Dragon Placed";
$columns["paper"] = "Paper";
$columns["IronGolemPlaced"] = "Iron Golem Placed";
$columns["SnowmanPlaced"] = "Snowman Placed";
$columns["EndermanPlaced"] = "Enderman Placed";
$columns["ChickenPlaced"] = "Chicken Placed";
$columns["SpiderPlaced"] = "Spider Placed";
$columns["greenPlaced"] = "Green Placed";
$columns["wins"] = "Wins";
$columns["verticalWins"] = "Vertical Wins";
$columns["SquidPlaced"] = "Squid Placed";
$columns["skullsPlaced"] = "Skulls Placed";
$columns["redPlaced"] = "Red Placed";
$columns["player"] = "Player";
$columns["green"] = "Green";
$columns["totalDamage"] = "Total Damage";
$columns["SkeletonPlaced"] = "Skeleton Placed";
$columns["GuardianPlaced"] = "Guardian Placed";
$columns["scissors"] = "Scissors";
$columns["blue"] = "Blue";
$columns["yellow"] = "Yellow";
$columns["red"] = "Red";
$columns["fails"] = "Fails";
$columns["goodNotes"] = "Good Notes";
$columns["playersDamage"] = "Players Damage";
$columns["cardsPlaced"] = "Cards Placed";
$columns["BlazePlaced"] = "Blaze Placed";
$columns["yellowPlaced"] = "Yellow Placed";
?>
<html>
<head>
<title>MiniBoardGames Web-Stats</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="panel panel-default">
<?php
# Config
require_once("./config.php");
# MySQL Connection
$connection = mysqli_connect($sql_host, $sql_username, $sql_password, $sql_dbname);
if(!$connection){
die("Error connecting to database: ".$connection->error);
}
# Get url values
$game = empty($_GET["game"]) ? "" : $_GET["game"];
$order = empty($_GET["order"]) ? "timesPlayed" : $_GET["order"];
if(empty($order) || empty($columns[$order])){
$order = "timesPlayed";
}
if(!empty($game) && empty($games[$game])){
$game = null;
}
# Header
echo '<div class="panel-heading">';
if(empty($game)){
echo $selectdisplay;
}else{
echo str_replace("{game}", $games[$game], $stat);
}
echo "</div>";
# Body
echo '<div class="panel-body">';
# Button
echo '<div class="btn-group" style="margin:10px;">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
'.$select_game.' <span class="caret"></span>
</button>
<ul class="dropdown-menu">';
$gameSlider = $connection->query("SHOW TABLES");
while ($slide = $gameSlider->fetch_row()){
$displayName = $slide[0];
foreach ($games as $key => $value){
if($key == $slide[0]){
$displayName = $value;
}
}
echo '<li><a href="?game='.$slide[0].'">'.$displayName.'</a></li>';
}
echo '</ul></div>';
if(!empty($game)){
echo '<table class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>';
$result = $connection -> query("SHOW COLUMNS FROM ".$game);
while($c = $result->fetch_array()){
$displayName = $c[0];
if(!empty($columns[$c[0]])){
$displayName = $columns[$c[0]];
}
echo '<th><a href="?game='.$game.'&order='.$c[0].'">'.$displayName.'</a></th>';
}
echo '</tr>
</thead>
<tbody>';
$sql = $connection->query("SELECT * FROM ".$game." ORDER BY ".$order." DESC");
$i2 = 1;
while($p = $sql->fetch_array()){
echo '<tr>';
echo "<td>".$i2."</td>";
for ($i=0; $i < $sql->field_count; $i++) {
if($i==0){
echo '<td><img src="http://cravatar.eu/avatar/'.$p[$i].'/32.png"> '.json_decode(file_get_contents(str_replace("-", "", "https://api.mojang.com/user/profiles/".$p[$i]."/names")),true)[0]["name"].'</td>';
}else{
echo "<td>".$p[$i]."</td>";
}
}
$i2+=1;
echo "</tr>";
}
$result->close();
}
?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment