sitdown disable when shutdowning
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("Obj/brisk.phh");
25 // require_once("Obj/proxyscan.phh");
26 require_once("briskin5/Obj/briskin5.phh");
27
28 // Use of proxies isn't allowed.
29 // if (is_proxy()) {
30 //   sleep(5);
31 //   exit;
32 // }
33 log_load("index_rd.php");
34
35 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
36   echo "Debugging time!";
37   exit;
38 }
39
40 log_load("index_wr.php");
41
42 /*
43  *  MAIN
44  */
45 $is_spawn = FALSE;
46
47 log_wr('COMM: '.$mesg);
48
49 $sem = Room::lock_data();
50 if (($room = &Room::load_data()) == FALSE) {
51   echo "Load data error";
52   log_wr("Load data error");
53   Room::unlock_data($sem);
54   exit;
55 }
56 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
57   echo "Get User Error";
58   log_wr("Get User Error");
59   Room::unlock_data($sem);
60   exit;
61 }
62 $argz = explode('|', $mesg);
63
64 log_wr('POSTSPLIT: '.$argz[0]);
65
66 if ($argz[0] == 'shutdown') {
67   log_auth($user->sess, "Shutdown session.");
68
69   $user->reset();
70   /* factorized with ->reset()
71   $tmp_sess = $user->sess;
72   $user->sess = "";
73   step_unproxy($tmp_sess);
74   $user->name = "";
75   while (array_pop($user->comm) != NULL);
76   $user->step = 0;
77   $user->the_end = FALSE;
78   */
79
80   log_rd2("AUTO LOGOUT.");
81   if ($user->subst == 'sitdown' || $user->stat == 'table')
82     $room->room_wakeup(&$user);
83   else if ($user->subst == 'standup')
84     $room->room_outstandup(&$user);
85   else
86     log_rd2("SHUTDOWN FROM WHAT ???");
87 }
88 /******************
89  *                *
90  *   STAT: room   *
91  *                *
92  ******************/
93 else if ($user->stat == 'room') {
94   $user->laccwr = time();
95
96   if ($argz[0] == 'help') {
97     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
98     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
99
100     log_wr($user->comm[$user->step % COMM_N]);
101     $user->step_inc();
102     
103   }
104   else if ($argz[0] == 'about') {
105     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
106     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
107
108     log_wr($user->comm[$user->step % COMM_N]);
109     $user->step_inc();
110     
111   }
112   else if ($argz[0] == 'chatt') {
113     $room->chatt_send(&$user,$mesg);
114   }
115   /**********************
116    *                    *
117    *   SUBST: standup   *
118    *                    *
119    **********************/
120   else if ($user->subst == 'standup') {
121    
122     if ($argz[0] == 'sitdown') {
123       log_wr("SITDOWN command");
124
125       if ($G_shutdown) {
126         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
127
128         $timecur = time();
129         $dt = date("H:i ", $timecur);
130         $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);
131
132         $user->step_inc();
133         Room::save_data($room);
134         Room::unlock_data($sem);
135         exit;
136       }
137
138       if ($user->the_end == TRUE) {
139         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
140         Room::unlock_data($sem);
141         exit;
142       }
143       /* TODO: refact to a function */
144       if ($user->bantime > $user->laccwr) {
145         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
146         $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);
147         
148         $user->step_inc();
149         Room::save_data($room);
150         Room::unlock_data($sem);
151         exit;
152       }
153     
154       // Take parameters
155       $table_idx = $argz[1];
156       $table = &$room->table[$table_idx];
157     
158       if ($table->player_n == PLAYERS_N) {
159         log_wr("WARN:FSM: Sitdown unreachable, table full.");
160         Room::unlock_data($sem);
161         exit;
162       } 
163       
164       // set new status
165       $user->subst = "sitdown";
166       $user->table = $table_idx;
167       $user->table_pos = $table->user_add($idx);
168       
169       log_wr("MOP before");
170
171       if ($table->player_n == PLAYERS_N) {
172         log_wr("MOP inall");
173
174         // Start game for this table.
175         log_wr("Start game!");
176         
177         //
178         //  START THE SPAWN HERE!!!!
179         //
180
181         if (TRUE) { // WITH SPAWN
182           $curtime = time();
183           // Create new spawned table
184           $bri_sem = Briskin5::lock_data($table_idx);
185           $table_token = uniqid("");
186           $room->table[$table_idx]->table_token = $table_token;
187           $room->table[$table_idx]->table_start = $curtime;
188           
189           if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
190             log_wr("bri create: FALSE");
191           else
192             log_wr("bri create: ".serialize($bri));
193         
194           // init table
195           $bri_table =& $bri->table[0];
196           $bri_table->init(&$bri->user);
197           $bri_table->game_init(&$bri->user);
198           //
199           // Init spawned users.
200           //
201           for ($i = 0 ; $i < $table->player_n ; $i++) {
202             $bri_user_cur = &$bri->user[$i];
203             $user_cur = &$room->user[$table->player[$i]];
204             
205             $bri_user_cur->stat_set('table');
206             $bri_user_cur->subst = 'asta';
207             $bri_user_cur->laccwr = $curtime;
208
209             $bri_user_cur->trans_step = $user_cur->step + 1;
210             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
211             $bri_user_cur->step_inc();
212             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
213
214             $bri_user_cur->step_inc();
215
216             log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
217
218             log_wr("Pre if!");
219             
220 //          ARRAY_POP DISABLED
221 //          // CHECK
222             while (array_pop($user_cur->comm) != NULL);
223
224             $ret = "";
225             $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);
226             
227             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
228             $user_cur->trans_step = $user_cur->step + 1;
229             log_wr("TRANS ATTIVATO");
230             
231             
232             $user_cur->stat_set('table');
233             $user_cur->subst = 'asta';
234             $user_cur->laccwr = $curtime;
235             $user_cur->step_inc();
236           }
237           log_wr("presave bri");
238           Briskin5::save_data($bri);
239           Briskin5::unlock_data($bri_sem);
240           log_wr("postsave bri");
241         }
242         else { // BEFORE SPAWN
243           // init table
244           $table->init(&$room->user);
245           $table->game_init(&$room->user);
246           $curtime = time();
247           
248           // init users
249           for ($i = 0 ; $i < $table->player_n ; $i++) {
250             $user_cur = &$room->user[$table->player[$i]];
251             log_wr("Pre if!");
252             
253             $ret = "";
254             $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
255             
256             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
257             $user_cur->trans_step = $user_cur->step + 1;
258             log_wr("TRANS ATTIVATO");
259             
260             
261             $user_cur->stat_set('table');
262             $user_cur->subst = 'asta';
263             $user_cur->laccwr = $curtime;
264             $user_cur->step_inc();
265             
266             $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
267             $user_cur->step_inc();
268           }
269         } // end else {  BEFORE SPAWN
270         
271         log_wr("MOP after");
272
273       }
274       // change room
275       $room->room_sitdown(&$user, $table_idx);
276
277       log_wr("MOP finish");
278
279       
280     }
281     else if ($argz[0] == 'logout') {
282       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
283       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
284       $user->the_end = TRUE;
285       $user->step_inc();
286     }
287   }
288   /**********************
289    *                    *
290    *   SUBST: sitdown   *
291    *                    *
292    **********************/
293   else if ($user->subst == 'sitdown') {
294     if ($argz[0] == 'wakeup') {
295       $room->room_wakeup(&$user);      
296     }
297     else if ($argz[0] == 'logout') {
298       $room->room_wakeup(&$user);      
299       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
300       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
301       $user->the_end = TRUE;
302       $user->step_inc();
303     }
304   }
305 }
306 log_wr("before save data");
307 Room::save_data($room);
308
309 Room::unlock_data($sem);
310 exit;
311 ?>