5 * Copyright (C) 2006-2008 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
27 require_once("Obj/brisk.phh");
28 require_once("Obj/auth.phh");
29 // require_once("Obj/proxyscan.phh");
30 require_once("briskin5/Obj/briskin5.phh");
32 // Use of proxies isn't allowed.
37 log_load("index_wr.php");
39 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
40 echo "Debugging time!";
48 /* if the IP is banned, exit without do nothing */
49 if (array_search($_SERVER['REMOTE_ADDR'], $G_black_list) !== FALSE) {
56 log_wr('COMM: '.$mesg);
58 $sem = Room::lock_data();
59 if (($room = &Room::load_data()) == FALSE) {
60 echo "Load data error";
61 log_wr("Load data error");
62 Room::unlock_data($sem);
65 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
66 Room::unlock_data($sem);
67 $argz = explode('|', $mesg);
69 if ($argz[0] == 'getchallenge') {
70 if (($a_sem = Challenges::lock_data()) != FALSE) {
71 log_main("chal lock data success");
73 if (($chal = &Challenges::load_data()) != FALSE) {
77 $chal_save |= $chal->garbage_manager();
79 // echo '2|'.$argz[1].'|'.$token.'|'.$_SERVER['REMOTE_ADDR'].'|'.$curtime.'|';
82 if ($chal->add($argz[1], $token, $_SERVER['REMOTE_ADDR'], $curtime) != FALSE) {
90 Challenges::save_data(&$chal);
95 Challenges::unlock_data($a_sem);
98 else if ($argz[0] == 'auth') {
99 printf("challenge|ok");
101 else if ($argz[0] == 'help') {
102 echo show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
104 else if ($argz[0] == 'about') {
105 echo show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
107 else if ($argz[0] == 'roadmap') {
108 echo show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
110 else if ($argz[0] == 'whysupport') {
111 echo show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
114 log_wr("Get User Error");
115 echo "Get User Error:" + $argz[0];
119 $argz = explode('|', $mesg);
121 log_wr('POSTSPLIT: '.$argz[0]);
123 if ($argz[0] == 'shutdown') {
124 log_auth($user->sess, "Shutdown session.");
128 log_rd2("AUTO LOGOUT.");
129 if ($user->subst == 'sitdown' || $user->stat == 'table')
130 $room->room_wakeup(&$user);
131 else if ($user->subst == 'standup')
132 $room->room_outstandup(&$user);
134 log_rd2("SHUTDOWN FROM WHAT ???");
141 else if ($user->stat == 'room') {
142 $user->laccwr = time();
144 if ($argz[0] == 'help') {
145 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
146 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
148 log_wr($user->comm[$user->step % COMM_N]);
152 else if ($argz[0] == 'about') {
153 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
154 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
156 log_wr($user->comm[$user->step % COMM_N]);
160 else if ($argz[0] == 'roadmap') {
161 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
162 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
164 log_wr($user->comm[$user->step % COMM_N]);
168 else if ($argz[0] == 'whysupport') {
169 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
170 $user->comm[$user->step % COMM_N] .= show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
172 log_wr($user->comm[$user->step % COMM_N]);
176 else if ($argz[0] == 'chatt') {
177 $room->chatt_send(&$user,$mesg);
179 /**********************
183 **********************/
184 else if ($user->subst == 'standup') {
186 if ($argz[0] == 'sitdown') {
187 log_wr("SITDOWN command");
189 if ($user->the_end == TRUE) {
190 log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
191 Room::unlock_data($sem);
196 $table_idx = $argz[1];
197 $table = &$room->table[$table_idx];
201 if ($G_shutdown || $table->wakeup_time > $curtime) {
202 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
204 $dt = date("H:i ", $curtime);
206 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>");', $dt.NICKSERV);
208 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","<b>Il tavolo si è appena liberato, ci si potrà sedere tra %d secondi.</b>");', $dt.NICKSERV, $table->wakeup_time - $curtime);
211 Room::save_data($room);
212 Room::unlock_data($sem);
216 /* TODO: refact to a function */
217 if ($user->bantime > $user->laccwr) {
218 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
219 $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "resta in piedi.", 400, 100);
222 Room::save_data($room);
223 Room::unlock_data($sem);
227 if ($table->player_n == PLAYERS_N) {
228 log_wr("WARN:FSM: Sitdown unreachable, table full.");
229 Room::unlock_data($sem);
234 $user->subst = "sitdown";
235 $user->table = $table_idx;
236 $user->table_pos = $table->user_add($idx);
238 log_wr("MOP before");
240 if ($table->player_n == PLAYERS_N) {
243 // Start game for this table.
244 log_wr("Start game!");
247 // START THE SPAWN HERE!!!!
252 // Create new spawned table
253 $bri_sem = Briskin5::lock_data($table_idx);
254 $table_token = uniqid("");
255 $room->table[$table_idx]->table_token = $table_token;
256 $room->table[$table_idx]->table_start = $curtime;
258 $plist = "$table_token|$user->table|$table->player_n";
259 for ($i = 0 ; $i < $table->player_n ; $i++) {
260 $plist .= '|'.$room->user[$table->player[$i]]->sess;
262 log_legal($curtime, $user->sess, $user->name, "STAT:CREATE_GAME", $plist);
264 if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
265 log_wr("bri create: FALSE");
267 log_wr("bri create: ".serialize($bri));
270 $bri_table =& $bri->table[0];
271 $bri_table->init(&$bri->user);
272 $bri_table->game_init(&$bri->user);
274 // Init spawned users.
276 for ($i = 0 ; $i < $table->player_n ; $i++) {
277 $bri_user_cur = &$bri->user[$i];
278 $user_cur = &$room->user[$table->player[$i]];
280 $bri_user_cur->stat_set('table');
281 $bri_user_cur->subst = 'asta';
282 $bri_user_cur->laccwr = $curtime;
284 $bri_user_cur->trans_step = $user_cur->step + 1;
285 $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
286 $bri_user_cur->step_inc();
287 $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
289 $bri_user_cur->step_inc();
291 log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
295 // ARRAY_POP DISABLED
297 while (array_pop($user_cur->comm) != NULL);
300 $ret .= sprintf('gst.st_loc++; gst.st=%d; createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null ; document.location.assign("briskin5/index.php");|', $user_cur->step+1, $table_idx, $table_token);
302 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
303 $user_cur->trans_step = $user_cur->step + 1;
304 log_wr("TRANS ATTIVATO");
307 $user_cur->stat_set('table');
308 $user_cur->subst = 'asta';
309 $user_cur->laccwr = $curtime;
310 $user_cur->step_inc();
312 log_wr("presave bri");
313 Briskin5::save_data($bri);
314 Briskin5::unlock_data($bri_sem);
315 log_wr("postsave bri");
318 $room->room_sitdown(&$user, $table_idx);
320 log_wr("MOP finish");
324 else if ($argz[0] == 'logout') {
325 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
326 $user->comm[$user->step % COMM_N] .= 'postact_logout();';
327 $user->the_end = TRUE;
331 /**********************
335 **********************/
336 else if ($user->subst == 'sitdown') {
337 if ($argz[0] == 'wakeup') {
338 $room->room_wakeup(&$user);
340 else if ($argz[0] == 'logout') {
341 $room->room_wakeup(&$user);
342 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
343 $user->comm[$user->step % COMM_N] .= 'postact_logout();';
344 $user->the_end = TRUE;
349 log_wr("before save data");
350 Room::save_data($room);
352 Room::unlock_data($sem);