-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.php
102 lines (78 loc) · 2.08 KB
/
stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
function stats()
{
include $_SERVER["DOCUMENT_ROOT"] . '/rps/db.inc.php';
include $_SERVER["DOCUMENT_ROOT"] . '/rps/entry.php';
session_start();
$array = $pdo->query("SELECT * FROM rpsentries")->fetchAll();
$totalGames = count($array);
$totalWins = 0;
$userGames = 0;
$userWins = 0;
$currentGames = 0;
$currentWins = 0;
foreach ($array as $entry)
{
if($entry['session_id'] == session_id())
{
if($entry['winner'] == 1)
{
$userWins = $userWins + 1;
$totalWins = $totalWins + 1;
}
$userGames = $userGames + 1;
}
else
{
if($entry['winner'] == 1)
{
$totalWins = $totalWins + 1;
}
}
}
//current games calculation
$curr_array = $_SESSION['entrylist'];
foreach($curr_array as $entry)
{
if($entry->winner == 1)
{
$currentWins = $currentWins + 1;
}
$currentGames = $currentGames + 1;
}
//formatting percentages
$winrate = ($totalGames > 0) ? floatval($totalWins / $totalGames) : 0;
$userWinrate = ($userGames > 0) ? floatval($userWins / $userGames) : 0;
$currentWinrate = ($currentGames > 0) ? floatval($currentWins / $currentGames) : 0;
$winrate = sprintf("%0.2f",$winrate);
$userWinrate = sprintf("%0.2f",$userWinrate);
$currentWinrate = sprintf("%0.2f",$currentWinrate);
//Total games
echo "<div style='background-color: #EEE0EE;'>";
echo "<h2>Total:</h2>";
echo "<p>games: $totalGames</p>";
echo "<p>total won games: $totalWins</p>";
echo "<p>total winrate: $winrate</p>";
echo "</div>";
//Your games
echo "<div style='background-color: #EEEEE0;'>";
echo "<h2>Yours:</h2>";
echo "<p>games: $userGames</p>";
echo "<p>your won games: $userWins</p>";
echo "<p>your winrate: $userWinrate</p>";
echo "</div>";
//This session
echo "<div style='background-color: #DADDE0;'>";
echo "<h2>Currently:</h2>";
echo "<p>games: $currentGames</p>";
echo "<p>your won games: $currentWins</p>";
echo "<p>your winrate: $currentWinrate</p>";
echo "</div>";
}
if(isset($_GET['action']) && !empty($_GET['action'])) {
$action = $_GET['action'];
switch($action) {
case 'stats' : stats();break;
}
}
?>