-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
191 lines (161 loc) · 4.91 KB
/
ajax.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
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
<?php
include('redis.php');
//check if user is logged in
if(!isLoggedIn()) return 'notLoggedIn';
loadUserInfo($User['id']);
//check for the function name, then call it
if (!gt('function') === false){
$function = gt('function');
if($function == 'getCards' || $function == 'cashOut' || $function == 'markCard' || $function == 'addValue'){
$function($User, $redis);
exit();
}
}
$return = array('error'=>'bad params');
echo json_encode($return);
exit();
/**
* addValue
* adds 50 credits to the user's account
* @param user array
* @param redis object
* @return echos json array with user's current total
**/
function addValue($User, $redis){
$value = addCurrentValue($User['id'], 50);
$return = array('total'=>$value);
echo json_encode($return);
}
/**
* getCards
* makes a new game and new shuffled deck
* @param user array
* @param redis object
* @return echos json array with user's current total
* and user's unique game id
**/
function getCards($User, $redis){
$value = getCurrentValue($User['id']);
if($value < 50){
$return = array('error'=>'not enough credit');
echo json_encode($return);
exit();
}
//all possible cards
$suits = array('c', 'd', 'h', 's');
$values = array(2,3,4,5,6,7,8,9,10,11,12,13,20);
$deck= array();
foreach($suits as $suit){
foreach($values as $value){
$deck[] = "$suit"."_"."$value";
}
}
//add jokers
$deck[52] = "99";
$deck[53] = "99";
//make game board here: 9x6
$x = range(0,8);
$y = range(0,5);
$board = array();
foreach($x as $xpos){
foreach($y as $ypos){
$board[] = $xpos."_".$ypos;
}
}
//get game id here
$gameid = setGameId($User['id']);
//shuffle deck
shuffle($deck);
//deal cards: assign a card in the deck for each space on the board
$allcards = array();
foreach($deck as $position => $card){
$pos = $board[$position];
$redis->set("card:$pos:gameid:$gameid", $card);
$allcards[$pos] = $card;
}
//take away 50 coins from user for this game
$total = subCurrentValue($User['id'], 50);
//return their current running total
$return = array( 'gameid'=>$gameid, 'total'=>$total );
echo json_encode($return);
}
/**
* markCard
* gets the card from the data base
* @param user array
* @param redis object
* @post params: x and y position of the requested card
* @return echos json array with the card
* and user's unique game id
**/
function markCard($User, $redis){
if (gt('positionx') === false|| gt('positiony') === false|| !gt('gameid')){
$return = array('error'=>'bad params');
echo json_encode($return);
exit();
}
//get position of requested card
$positionx = gt('positionx');
$positiony = gt('positiony');
$gameid = gt('gameid');
//is game id associated with user?
if(!isValidGameId($User['id'], $gameid)){
$return = array('error'=>'not a valid gameid');
echo json_encode($return);
}
//get the card from the db
$card_value = $redis->get("card:$positionx"."_"."$positiony:gameid:$gameid");
//is it a joker?
if($card_value == 99){
//destroy the current tally of card values
$length = $redis->llen("gameid:$gameid:cards");
for($i=0; $i<$length; $i++){
$redis->lpop("gameid:$gameid:cards");
}
$redis->set("uid:".$User['id'].":latest", '');
$return = array( 'gameid'=>null, 'card_value'=>$card_value);
echo json_encode($return);
exit();
}
//add this card to the total for this game
$redis->lpush("gameid:$gameid:cards", $card_value);
//return the card value
$return = array( 'gameid'=>$gameid, 'card_value'=>$card_value);
echo json_encode($return);
}
/**
* cashOut
* ends the user's game
* @param user array
* @param redis object
* @return echos json array with the user's score for this game
* and user's unique game id
**/
function cashOut($User, $redis){
if (!gt('gameid')){
$return = array('error'=>'bad params');
echo json_encode($return);
exit();
}
$gameid = gt('gameid');
//is game id associated with user?
if(!isValidGameId($User['id'], $gameid)){
$return = array('error'=>'not a valid gameid');
echo json_encode($return);
}
//tally the total for this game
$value = 0;
//get number of cards (in list)
$length = $redis->llen("gameid:$gameid:cards");
for($i=0; $i<$length; $i++){
//then add them together one at a time
$card = $redis->lpop("gameid:$gameid:cards");
$temp = explode("_", $card);
$value += $temp[1];
}
//add this value to the user's overall total
$total_value = addCurrentValue($User['id'], $value);
//return their overall total
$return = array( 'gameid'=>$gameid, 'total'=>$total_value);
echo json_encode($return);
}