-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestmatch.php
More file actions
81 lines (62 loc) · 2.39 KB
/
requestmatch.php
File metadata and controls
81 lines (62 loc) · 2.39 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
<?php
// a client requests a new match. look for open matches or create a new one.
//get a random string for player secret tokens (like a password). player tokens will be given to each new player only once.
function createPlayerToken()
{
$arr = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'); // get all the characters into an array
shuffle($arr); // randomize the array
$arr = array_slice($arr, 0, 10); // get the first ten (random) characters out
$str = implode('', $arr); // smush them back into a string
return $str;
}
if( !isset($_GET['playername']) )
{
die("playername not set");
}
include_once("cleanUpMatches.php");
include_once("connect.php");
$playername = mysql_real_escape_string($_GET['playername']);
$result = mysql_query("SELECT * FROM $matchesTableName WHERE status='$waitingForPlayersString' AND player1!='$playername'");
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) //no match is wating for a second player
{
// create new match and register as player1
$p1token = createPlayerToken();
mysql_query("INSERT INTO $matchesTableName (player1,p1token,status) VALUES ('$playername', '$p1token', '$waitingForPlayersString')");
$result = mysql_query("SELECT * FROM $matchesTableName WHERE status='$waitingForPlayersString' AND player1='$playername' AND p1token='$p1token'");
//get id of new match and return it
$num_rows = mysql_num_rows($result);
if( $num_rows != 1 )
{
die("error! created new match but could not find it!");
}
$row = mysql_fetch_array($result);
$matchID = $row['ID'];
echo $matchID.";".$p1token;
}
else //found an open game. register as second player and return game ID
{
$row = mysql_fetch_array($result);
$matchID = $row['ID'];
//check for same player name
$p1name = $row['player1'];
if( $p1name == $playername )
{
die("error! player names are equal!");
}
//register as second player
$p2token = createPlayerToken();
mysql_query("UPDATE $matchesTableName SET player2='$playername' WHERE ID=$matchID");
mysql_query("UPDATE $matchesTableName SET p2token='$p2token' WHERE ID=$matchID");
//set new match status: pick a player to start
$startPlayerIndex = mt_rand(0,1);
$startPlayerName = $p1name;
if( $startPlayerIndex == 1 )
{
$startPlayerName = $playername;
}
mysql_query("UPDATE $matchesTableName SET status='$startPlayerName' WHERE ID=$matchID");
echo $matchID.";".$p2token;
}
mysql_close($con);
?>