-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.php
More file actions
320 lines (269 loc) · 8.19 KB
/
game.php
File metadata and controls
320 lines (269 loc) · 8.19 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
header("Content-Type:application/json;charset=utf-8");
session_start();
include "answer.php";
$db = new PDO("mysql:host=localhost;dbname=hard2016", "wechat", "wechat@)!#");
$db -> query("SET NAMES 'UTF8'");
$db -> query("SET CHARACTER SET UTF8");
$db -> query("SET CHARACTER_SET_RESULTS=UTF8");
$db -> setattribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$db -> setattribute(PDO::ATTR_EMULATE_PREPARES,false);
$wechat_id = $_SESSION['wechat_id'];
if ($_SESSION['location'] == 'north') {
$location = 'n';
} elseif ($_SESSION['location'] == 'south') {
$location = 's';
} else {
$array['msg'] = 'request error';
$array['state'] = false;
$json = json_encode($array);
echo $json;
exit();
}
// $wechat_id = "123456";//测试用
// $location = 'n';//测试用
$game = array(
'step' => 'start',
'question' => null,
'score' => 0
);
$answer = $_POST['answer'];
if($answer == 'start')
{
if($location == 'n')
{
$q_num = mt_rand(0,85); //共86道
}else
{
$q_num = mt_rand(0,136); //共137道
}
$rs = $db -> prepare("select id from step where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$result = $rs -> fetch();
$rs = $db -> prepare("select id from question_num where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$result_q = $rs -> fetch();
if(!empty($result) || !empty($result_q)) //状况:用户强行退出,导致数据未被delete
{
//重新初始化数据
$db -> beginTransaction();
try{
$exec_update = $db -> exec("update step set location = '$location', q_num = '$q_num', chance = '2', score = '0', time = NOW() where wechat_id = '$wechat_id'");
$db -> commit();
}catch(Exception $e)
{
$db -> rollBack();
}
$rs = $db -> prepare("delete from question_num where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
}else
{
$db -> beginTransaction();
try{
if($location == 'n')
{
$insert = $db -> exec("insert into step (id,wechat_id,location,q_num,time) values ('','$wechat_id','n','$q_num',NOW())");
}else
{
$insert = $db -> exec("insert into step (id,wechat_id,location,q_num,time) values ('','$wechat_id','s','$q_num',NOW())");
}
$db -> commit();
}
catch(Exception $e)
{
$db -> rollBack();
}
}
$db -> beginTransaction();
try{
$insert = $db -> exec("insert into question_num (id,wechat_id,q_sql) values ('','$wechat_id','$q_num')");
$db -> commit();
}
catch(Exception $e)
{
$db -> rollBack();
}
$game['question'] = $q_num; //题号传给前端
$json = json_encode($game);
echo $json;
}else
{
$rs = $db -> prepare("select score from step where wechat_id = ? order by time desc limit 1");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$score = $rs -> fetch();
$rs = $db -> prepare("select chance from step where wechat_id = ? order by time desc limit 1");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$chance = $rs->fetch();
if(($score['score'] <= 15) && ($chance['chance'] >= 0)) //不大于12个答题点且答题机会大于0
{
$rs = $db -> prepare("select q_num from step where wechat_id = ? order by time desc limit 1");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$q_num = $rs->fetch();
$q_num = $q_num['q_num'];
if($location == 'n')
{
if($answer == $answer_n[$q_num])
{
$isright = 1;
}else
{
$isright = 0;
}
}else
{
if($answer == $answer_s[$q_num])
{
$isright = 1;
}else
{
$isright = 0;
}
}
//避免重复抽题操作
$rs = $db -> prepare("select q_sql from question_num where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$q_num = $rs->fetchAll();
$row = $rs -> rowCount();
for($i=0;$i<$row;$i++)
{
$q_num[$i] = $q_num[$i]['q_sql'];
}
if($location == 'n')
{
$numbers = range(0,84);
}else
{
$numbers = range(0,136);
}
$num = array_diff($numbers,$q_num);
shuffle($num);
$q_num = array_slice($num,0,1);
$q_num = $q_num[0];
$overtime = $_POST['overtime'];
if(($overtime == 0) || ($isright == 0)) //超时或答错,前端传来超时信息与用户所选答案
{
if($chance['chance'] == 0) //机会为零而结束
{
$game['step'] = "over"; //游戏结束状态码
$game['score'] = $score['score']; //最终成绩传给前端
$json = json_encode($game);
echo $json;
$rs = $db -> prepare("delete from step where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$rs = $db -> prepare("delete from question_num where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
}else
{
$db -> beginTransaction();
try{
$exec_update = $db -> exec("update step set chance = chance-1 where wechat_id = '$wechat_id'");
$db -> commit();
}catch(Exception $e)
{
$db -> rollBack();
}
$db -> beginTransaction();
try{
$exec_update = $db -> exec("update step set q_num = '$q_num' where wechat_id = '$wechat_id'");
$db -> commit();
}catch(Exception $e)
{
$db -> rollBack();
}
$db -> beginTransaction();
try{
$insert = $db -> exec("insert into question_num (id,wechat_id,q_sql) values ('','$wechat_id','$q_num')");
$db -> commit();
}
catch(Exception $e)
{
$db -> rollBack();
}
$game['question'] = $q_num; //题号传给前端
$game['score'] = $score['score'];
$game['step'] = "stay"; //停留在原答题点再答另一题状态码
$json = json_encode($game);
echo $json;
}
}else
{
if($score['score'] == 15) //到达终点而结束
{
$game['step'] = "over"; //游戏结束状态码
$game['score'] = $score['score']; //最终成绩传给前端
$json = json_encode($game);
echo $json;
$rs = $db -> prepare("delete from step where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
$rs = $db -> prepare("delete from question_num where wechat_id = ?");
$rs -> setFetchMode(PDO::FETCH_ASSOC);
$db -> setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
$rs -> bindParam(1,$wechat_id);
$rs -> execute();
}else
{
$db -> beginTransaction();
try{
$exec_update = $db -> exec("update step set score = score+1 where wechat_id = '$wechat_id'");
$db -> commit();
}catch(Exception $e)
{
$db -> rollBack();
}
$db -> beginTransaction();
try{
$exec_update = $db -> exec("update step set q_num = '$q_num' where wechat_id = '$wechat_id'");
$db -> commit();
}catch(Exception $e)
{
$db -> rollBack();
}
$db -> beginTransaction();
try{
$insert = $db -> exec("insert into question_num (id,wechat_id,q_sql) values ('','$wechat_id','$q_num')");
$db -> commit();
}
catch(Exception $e)
{
$db -> rollBack();
}
$game['question'] = $q_num; //题号传给前端
$game['score'] = $score['score'];
$game['step'] = "move"; //继续前进状态码
$json = json_encode($game);
echo $json;
}
}
}
}
?>