tttgame_multiplayer /php/tttgame2_0.php

Language PHP Lines 281
MD5 Hash 37e0d732a2a4575ce094c419dbdafefd Estimated Cost $6,196 (why?)
Repository https://bitbucket.org/vitkozin/tttgame_multiplayer.git View Raw File View Project SPDX
  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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
 <?
$host='host';
$database='database';
$user='user';
$pswd='pswd';
$requestType = $_GET['requestType'];

if($requestType == 'nick'){
	$nick = $_GET['nick'];
	$error = 0;
	$id = 1;
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "DELETE FROM `players` WHERE statusTime <".(time()-3).";";
	mysql_query($query);
	$query = "SELECT idPlayer, nick FROM players;";
	$res = mysql_query($query);

	while($row = mysql_fetch_array($res)){
		if ($nick == $row['nick']){
	 		$error = 1;
	 		echo $error;
		}
		if($row['idPlayer'] == $id){
			$id++;
		}
	}

	if ($error == 0){
		$query = "INSERT INTO `players` (idPlayer, nick, statusTime) VALUES ('".$id."', '".$nick."', '".time()."');";
		mysql_query($query);
		echo $error;
	}

	mysql_close($dbh);
}

if($requestType == 'status'){
	$nick = $_GET['nick'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "UPDATE `tttgame`.`players` SET `statusTime` =  '".time()."' WHERE `players`.`nick` = '".$nick."';";
	mysql_query($query);
	mysql_close($dbh);
	
}

if($requestType == 'playersList'){
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "Select nick from `players` Where statusTime >=".(time()-3)." And inGame = 0;";
	$res = mysql_query($query);
	$id = 1;
	$playersList = '{"playersList":["';

	while ($row = mysql_fetch_array($res)){
	$playersList = $playersList.$row['nick'].'","';
	}

	mysql_close($dbh);
	$playersList = substr($playersList, 0, strlen($playersList)-2);
	$playersList = $playersList.']}';
	echo $playersList;
}

if($requestType == 'startGame'){
	$opponent = $_GET['opponent'];
	$nick = $_GET['nick'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "Select inGame from `players` Where nick ='".$opponent."';";
	$res = mysql_query($query);
	$inGame = mysql_result($res,0);

	if ($inGame == 0){
		$query = "Select idPlayer from `players` Where nick ='".$opponent."';";
		$res = mysql_query($query);
		$opponentId = mysql_result($res,0);
		$query = "Select idPlayer from `players` Where nick ='".$nick."';";
		$res = mysql_query($query);
		$nickId = mysql_result($res,0);
		$query = "Select * from `games`";
		$res = mysql_query($query);
		$id = 1;
		$query = "Select idGame from `games`";
		$res = mysql_query($query);

		while($row = mysql_fetch_array($res)){
			if($row['idGame'] == $id){
				$id++;
			}
		}

		$query = "INSERT INTO `tttgame`.`games` (idGame, player1, player2, flag) VALUES ('".$id."', '".$nickId."', '".$opponentId."', 1);";
		mysql_query($query);
		$query = "UPDATE `tttgame`.`players` SET `inGame` =  '".$id."' WHERE `players`.`idPlayer` = '".$nickId."';";
		mysql_query($query);
		$query = "UPDATE `tttgame`.`players` SET `inGame` =  '".$id."' WHERE `players`.`idPlayer` = '".$opponentId."';";
		mysql_query($query);
		$query = "Select idGame from games";	
		$res = mysql_query($query);

		for($i=0;$i<20;$i++){
			sleep(1);
			$query = "Select accept from `games` Where idGame ='".$id."';";
			$res = mysql_query($query);
			$accept = mysql_result($res,0);

		 	if($accept == 1 || $accept == -1){
			 	$i=20;
			}
					
		}

		if($accept == 0 || $accept == -1){
			$query = "UPDATE `tttgame`.`players` SET `inGame` =  '0' WHERE `players`.`idPlayer` = '".$nickId."';";
			mysql_query($query);
			$query = "UPDATE `tttgame`.`players` SET `inGame` =  '0' WHERE `players`.`idPlayer` = '".$opponentId."';";
			mysql_query($query);	
			$query = "DELETE FROM `tttgame`.`games` WHERE `games`.`idGame` = '".$id."';";
			mysql_query($query);
		}

		echo $accept;
	}else{
		echo 0;
	}

	mysql_close($dbh);
}

if ($requestType == 'check'){
	$nick = $_GET['nick'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "SELECT idPlayer FROM `players` WHERE nick = '".$nick."';";
	$res = mysql_query($query);
	$idPlayer = @mysql_result($res,0); 
	$query = "SELECT accept FROM `games` WHERE player2 = '".$idPlayer."' and accept = 0;";
	$res = mysql_query($query);
	$accept = @mysql_result($res,0);

	if ($accept == 1 || $accept == "" || $accept == -1){
		echo 0;
	}else{
		echo 1;
	}

	mysql_close($dbh);
}

if ($requestType == 'accept'){
	$nick = $_GET['nick'];
	$acceptType = $_GET['acceptType'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "SELECT idPlayer FROM `players` WHERE nick = '".$nick."';";
	$res = mysql_query($query);
	$idPlayer = mysql_result($res,0); 
	$query = "UPDATE `tttgame`.`games` SET accept = '".$acceptType."' WHERE player2 = '".$idPlayer."';";
	mysql_query($query); 
	mysql_close($dbh);
}

if ($requestType == 'move'){
	$nick = $_GET['nick'];
	$cell = $_GET['cell'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "SELECT idPlayer FROM `players` WHERE nick = '".$nick."';";
	$res = mysql_query($query);
	$idPlayer = mysql_result($res,0);
	$query = "SELECT flag, ".$cell.",count FROM `games` WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
	$res = mysql_query($query);
	$error = 1;

	while ($row = mysql_fetch_array($res)){
		if($row[$cell] == 0){
			if($row['flag'] == 1){
				$query = "UPDATE `tttgame`.`games` SET ".$cell." = 1, flag = 0, lastMove = '".$cell."', count = '".($row['count'] + 1)."' WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
				mysql_query($query);
				$error = 0;
			}else{
				if($row['flag'] == 0){
					$query = "UPDATE `tttgame`.`games` SET ".$cell." = 2, flag = 1, lastMove = '".$cell."', count = '".($row['count'] + 1)."' WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
					mysql_query($query);
					$error = 0;
				}
			}
		}
	}

	echo $error;
	mysql_close($dbh);
}

if ($requestType == 'checkMove'){
	$nick = $_GET['nick'];
	$lastMove = $_GET['lastMove'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "SELECT idPlayer FROM `players` WHERE nick = '".$nick."';";
	$res = mysql_query($query);
	$idPlayer = mysql_result($res,0);
	$query = "SELECT lastMove FROM `games` WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
	$res = mysql_query($query);
	$move = mysql_result($res,0);
	mysql_close($dbh);

	if($lastMove == $move){
		echo 0;
	}else{
		echo $move;
	}
}

if ($requestType == 'checkWin'){
	$nick = $_GET['nick'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "SELECT idPlayer FROM `players` WHERE nick = '".$nick."';";
	$res = mysql_query($query);
	$idPlayer = mysql_result($res,0);
	$query = "SELECT * FROM `games` WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
	$res = mysql_query($query);
	$row = mysql_fetch_array($res);

	if(($row['cell1']  == 1 && $row['cell2']  == 1 && $row['cell3']  == 1)||
	   ($row['cell1']  == 2 && $row['cell2']  == 2 && $row['cell3']  == 2)||
	   ($row['cell4']  == 1 && $row['cell5']  == 1 && $row['cell6']  == 1)||
	   ($row['cell4']  == 2 && $row['cell5']  == 2 && $row['cell6']  == 2)||
	   ($row['cell7']  == 1 && $row['cell8']  == 1 && $row['cell9']  == 1)||
	   ($row['cell7']  == 2 && $row['cell8']  == 2 && $row['cell9']  == 2)||
	   ($row['cell1']  == 1 && $row['cell4']  == 1 && $row['cell7']  == 1)||
	   ($row['cell1']  == 2 && $row['cell4']  == 2 && $row['cell7']  == 2)||
	   ($row['cell2']  == 1 && $row['cell5']  == 1 && $row['cell8']  == 1)||
	   ($row['cell2']  == 2 && $row['cell5']  == 2 && $row['cell8']  == 2)||
	   ($row['cell3']  == 1 && $row['cell6']  == 1 && $row['cell9']  == 1)||
	   ($row['cell3']  == 2 && $row['cell6']  == 2 && $row['cell9']  == 2)||
	   ($row['cell1']  == 1 && $row['cell5']  == 1 && $row['cell9']  == 1)||
	   ($row['cell1']  == 2 && $row['cell5']  == 2 && $row['cell9']  == 2)||
	   ($row['cell3']  == 1 && $row['cell5']  == 1 && $row['cell7']  == 1)||
	   ($row['cell3']  == 2 && $row['cell5']  == 2 && $row['cell7']  == 2))
		{
	   	if($row['flag'] == 1){
		   	echo 1;
		}else{
			echo 0;
		}
		}else{
			
			if($row['count'] == 9){
				echo 2;
			}else{
				echo -1;
			}
		}

	mysql_close($dbh);
}

if ($requestType == 'clear'){
	$opponent = $_GET['opponent'];
	$nick = $_GET['nick'];
	$dbh = mysql_connect($host, $user, $pswd) or die("Не могу подключиться к базе.");
	mysql_select_db($database) or die("Не могу подключиться к базе.");
	$query = "Select idPlayer from `players` Where nick ='".$nick."';";
	$res = mysql_query($query);
	$idPlayer = mysql_result($res,0);
	$query = "Select player1 from `games` Where player2 ='".$idPlayer."';";
	$res = mysql_query($query);
	$player1 = mysql_result($res,0);
	$query = "UPDATE `tttgame`.`players` SET `inGame` =  '0' WHERE `players`.`idPlayer` = '".$player1."';";
	mysql_query($query);
	$query = "UPDATE `tttgame`.`players` SET `inGame` =  '0' WHERE `players`.`nick` = '".$nick."';";
	mysql_query($query);
	$query = "DELETE FROM `tttgame`.`games` WHERE player2 = '".$idPlayer."' OR player1 = '".$idPlayer."';";
	mysql_query($query);
	mysql_close($dbh);
}
 ?>
Back to Top