-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.php
More file actions
187 lines (167 loc) · 5.11 KB
/
stats.php
File metadata and controls
187 lines (167 loc) · 5.11 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
function htmlStart(){
echo '<html>
<head>
<title>Foosball Stats</title>
<link rel="stylesheet" type="text/css" href="/foosballStats/style.css"/>
</head>
<body>';
}
function scoreForms(){
echo '<div id="loadstats">
<form id="simple" action="" method="post" enctype="multipart/form-data">
<h4>Enter Score:</h4>
<label for="personOne">Person: </label>
<input type="text" name="personOne" id="personOne"/>
<label for="scoreOne">Score: </label>
<input type="text" name="scoreOne" id="scoreOne"/>
<h5>VS</h5>
<label for="personTwo">Person: </label>
<input type="text" name="personTwo" id="personTwo"/>
<label for="scoreTwo">Score: </label>
<input type="text" name="scoreTwo" id="scoreTwo"/>
<br/><br/>
<input type="submit" name="singleEntry" value="Submit"/>
</form>
<form id="multi" action="" method="post" enctype="multipart/form-data">
<h4>Or upload a .csv file with the format Person,Score,Person,Score:</h4>
<input type="file" name="file" id="file" />
<input type="submit" name="multiEntry" value="Submit" />
</form>
<!--
<form id="searchGames" action="playerList.php" enctype="multipart/form-data">
<h4>Look-up game history by player:</h4>
<input type="text" name="pSearch" id="pSearch" />
<input type="submit" name="search" value="Search" />
</form>
-->
</div>';
}
function htmlEnd(){
echo "</body></html>";
}
function dbOpen(){
$con=mysql_connect("127.0.0.1","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('foosball_stats', $con);
if (!$db_selected) {
die ('Can\'t use foos : ' . mysql_error());
}
return $con;
}
function dbClose($con){
mysql_close($con);
}
function reloadRankings(){
echo "<div id='rankings'>";
echo "<table><tr><th>Rank</th><th>Player</th><th>GF/G</th></tr>";
$con=dbOpen();
$result = mysql_query('SELECT * FROM players ORDER BY GF_G DESC, PID ASC',$con);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$i=1;
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>".$i."</td><td>".$row['PID']."</td><td>".$row['GF_G']."</td></tr>";
$i++;
}
dbClose($con);
echo "</table></div>";
}
function uploadInsert($line){
list($playerOne, $scoreOne, $playerTwo, $scoreTwo) = split(",", $line);
$playerOne=ucfirst(strtolower($playerOne));
$playerTwo=ucfirst(strtolower($playerTwo));
if($playerOne==null||$scoreOne==null||$playerTwo==null||$scoreTwo==null){
echo "<span class='error'>Null value: ".$line."</span><br/>";
return "null value";
}
elseif(preg_match("/\D/",$scoreOne)||preg_match("/\D/",$scoreTwo)){
echo "<span class='error'>Invalid score: ".$line."</span><br />";
return "score not a number";
}
elseif(strcmp($playerOne, $playerTwo)==0){
echo "<span class='error'>You can't play yourself silly!: ".$line."</span><br />";
return "pone equals ptwo";
}
elseif($scoreOne==$scoreTwo){
echo "<span class='error'>Can't have a tie in foosball!: ".$line."</span><br />";
return "tie game";
}
else{
$con=dbOpen();
if($scoreOne>$scoreTwo){
$winner=$playerOne;
$winnerScore=$scoreOne;
$loser=$playerTwo;
$loserScore=$scoreTwo;
}
else{
$winner=$playerTwo;
$winnerScore=$scoreTwo;
$loser=$playerOne;
$loserScore=$scoreOne;
}
//echo $winner." ".$wScore." ".$loser." ".$lScore."<br/>";
$loserResult = mysql_query("INSERT INTO players (PID,W,L,GP,G,GA,GF_G) VALUES('".$loser."','0','1','1','".$loserScore."','".$winnerScore."','".$loserScore."') ON DUPLICATE KEY UPDATE L=L+1,GP=GP+1,G=G+".$loserScore.",GA=GA+".$winnerScore.",GF_G=G/GP",$con);
if (!$loserResult) {
die('Invalid loser query: ' . mysql_error());
}
$winnerResult = mysql_query("INSERT INTO players (PID,W,L,GP,G,GA,GF_G) VALUES('".$winner."','1','0','1','".$winnerScore."','".$loserScore."','".$winnerScore."') ON DUPLICATE KEY UPDATE W=W+1,GP=GP+1,G=G+".$winnerScore.",GA=GA+".$loserScore.",GF_G=G/GP",$con);
if (!$winnerResult) {
die('Invalid winner query: ' . mysql_error());
}
$gameResult = mysql_query("INSERT INTO games (PID_winner, w_score, PID_loser, l_score) VALUES ('".$winner."','".$winnerScore."','".$loser."','".$loserScore."')",$con);
if(!$gameResult){
die('Invalid game query: ' . mysql_error());
}
dbClose($con);
}
}
function processFile(){
if ($_FILES["file"]["error"] > 0){
echo "<span class='error'>Error: " . $_FILES["file"]["error"] . "</span><br />";
return "null file";
}
elseif ($_FILES["file"]["type"]!="text/csv"){
echo "<span class='error'>File type must be csv</span><br />";
return "not csv";
}
else{
$file=fopen($_FILES["file"]["tmp_name"],'r');
$line=fgets($file);
if(preg_match("/\d/",$line)){
uploadInsert(rtrim($line));
}
else{
echo "<span class='error'>Ignoring header line: ".$line."</span><br />";
$error="header line";
}
while (!feof($file)){
$line=fgets($file);
uploadInsert(rtrim($line));
}
fclose($file);
return $error;
}
}
htmlStart();
?>
<div class='errorContainer'>
<?php
if ($_POST['singleEntry']) {
uploadInsert($_POST['personOne'].','.$_POST['scoreOne'].','.$_POST['personTwo'].','.$_POST['scoreTwo']);
}
if ($_POST['multiEntry']) {
processFile();
}
?>
</div>
<?php
reloadRankings();
scoreForms();
htmlEnd();
?>