d7ccac2d88a892f8447d6baa61de40422b08dbdc
[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("briskin5/Obj/briskin5.phh");
26
27 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
28   echo "Debugging time!";
29   exit;
30 }
31
32 log_load("index_wr.php");
33
34 /*
35  *  MAIN
36  */
37 $is_spawn = FALSE;
38
39 log_wr('COMM: '.$mesg);
40
41 $sem = Room::lock_data();
42 if (($room = &Room::load_data()) == FALSE) {
43   echo "Load data error";
44   log_wr("Load data error");
45   Room::unlock_data($sem);
46   exit;
47 }
48 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
49   echo "Get User Error";
50   log_wr("Get User Error");
51   Room::unlock_data($sem);
52   exit;
53 }
54 $argz = explode('|', $mesg);
55
56 log_wr('POSTSPLIT: '.$argz[0]);
57
58 if ($argz[0] == 'shutdown') {
59   log_auth($user->sess, "Shutdown session.");
60
61   $user->reset();
62   /* factorized with ->reset()
63   $tmp_sess = $user->sess;
64   $user->sess = "";
65   step_unproxy($tmp_sess);
66   $user->name = "";
67   while (array_pop($user->comm) != NULL);
68   $user->step = 0;
69   $user->the_end = FALSE;
70   */
71
72   log_rd2("AUTO LOGOUT.");
73   if ($user->subst == 'sitdown' || $user->stat == 'table')
74     $room->room_wakeup(&$user);
75   else if ($user->subst == 'standup')
76     $room->room_outstandup(&$user);
77   else
78     log_rd2("SHUTDOWN FROM WHAT ???");
79 }
80 /******************
81  *                *
82  *   STAT: room   *
83  *                *
84  ******************/
85 else if ($user->stat == 'room') {
86   $user->laccwr = time();
87
88   if ($argz[0] == 'help') {
89     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
90     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
91
92     log_wr($user->comm[$user->step % COMM_N]);
93     $user->step_inc();
94     
95   }
96   else if ($argz[0] == 'about') {
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_about), 0, "torna ai tavoli", 400, 200);
99
100     log_wr($user->comm[$user->step % COMM_N]);
101     $user->step_inc();
102     
103   }
104   else if ($argz[0] == 'chatt') {
105     $room->chatt_send(&$user,$mesg);
106   }
107   /**********************
108    *                    *
109    *   SUBST: standup   *
110    *                    *
111    **********************/
112   else if ($user->subst == 'standup') {
113    
114     if ($argz[0] == 'sitdown') {
115       log_wr("SITDOWN command");
116
117       if ($user->the_end == TRUE) {
118         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
119         Room::unlock_data($sem);
120         exit;
121       }
122       /* TODO: refact to a function */
123       if ($user->bantime > $user->laccwr) {
124         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
125         $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);
126         
127         $user->step_inc();
128         Room::save_data($room);
129         Room::unlock_data($sem);
130         exit;
131       }
132     
133       // Take parameters
134       $table_idx = $argz[1];
135       $table = &$room->table[$table_idx];
136     
137       if ($table->player_n == PLAYERS_N) {
138         log_wr("WARN:FSM: Sitdown unreachable, table full.");
139         Room::unlock_data($sem);
140         exit;
141       } 
142       
143       // set new status
144       $user->subst = "sitdown";
145       $user->table = $table_idx;
146       $user->table_pos = $table->user_add($idx);
147       
148       log_wr("MOP before");
149
150       if ($table->player_n == PLAYERS_N) {
151         log_wr("MOP inall");
152
153         // Start game for this table.
154         log_wr("Start game!");
155         
156         //
157         //  START THE SPAWN HERE!!!!
158         //
159
160         if (TRUE) { // WITH SPAWN
161           $curtime = time();
162           // Create new spawned table
163           $bri_sem = Briskin5::lock_data($table_idx);
164           $table_token = uniqid("");
165           $room->table[$table_idx]->table_token = $table_token;
166           $room->table[$table_idx]->table_start = $curtime;
167           
168           if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
169             log_wr("bri create: FALSE");
170           else
171             log_wr("bri create: ".serialize($bri));
172         
173           // init table
174           $bri_table =& $bri->table[0];
175           $bri_table->init(&$bri->user);
176           $bri_table->game_init(&$bri->user);
177           //
178           // Init spawned users.
179           //
180           for ($i = 0 ; $i < $table->player_n ; $i++) {
181             $bri_user_cur = &$bri->user[$i];
182             $user_cur = &$room->user[$table->player[$i]];
183             
184             $bri_user_cur->stat_set('table');
185             $bri_user_cur->subst = 'asta';
186             $bri_user_cur->laccwr = $curtime;
187
188             $bri_user_cur->trans_step = $user_cur->step + 1;
189             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
190             $bri_user_cur->step_inc();
191             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
192
193             $bri_user_cur->step_inc();
194
195             log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
196
197             log_wr("Pre if!");
198             
199 //          ARRAY_POP DISABLED
200 //          // CHECK
201             while (array_pop($user_cur->comm) != NULL);
202
203             $ret = "";
204             $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);
205             
206             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
207             $user_cur->trans_step = $user_cur->step + 1;
208             log_wr("TRANS ATTIVATO");
209             
210             
211             $user_cur->stat_set('table');
212             $user_cur->subst = 'asta';
213             $user_cur->laccwr = $curtime;
214             $user_cur->step_inc();
215           }
216           log_wr("presave bri");
217           Briskin5::save_data($bri);
218           Briskin5::unlock_data($bri_sem);
219           log_wr("postsave bri");
220         }
221         else { // BEFORE SPAWN
222           // init table
223           $table->init(&$room->user);
224           $table->game_init(&$room->user);
225           $curtime = time();
226           
227           // init users
228           for ($i = 0 ; $i < $table->player_n ; $i++) {
229             $user_cur = &$room->user[$table->player[$i]];
230             log_wr("Pre if!");
231             
232             $ret = "";
233             $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
234             
235             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
236             $user_cur->trans_step = $user_cur->step + 1;
237             log_wr("TRANS ATTIVATO");
238             
239             
240             $user_cur->stat_set('table');
241             $user_cur->subst = 'asta';
242             $user_cur->laccwr = $curtime;
243             $user_cur->step_inc();
244             
245             $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
246             $user_cur->step_inc();
247           }
248         } // end else {  BEFORE SPAWN
249         
250         log_wr("MOP after");
251
252       }
253       // change room
254       $room->room_sitdown(&$user, $table_idx);
255
256       log_wr("MOP finish");
257
258       
259     }
260     else if ($argz[0] == 'logout') {
261       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
262       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
263       $user->the_end = TRUE;
264       $user->step_inc();
265     }
266   }
267   /**********************
268    *                    *
269    *   SUBST: sitdown   *
270    *                    *
271    **********************/
272   else if ($user->subst == 'sitdown') {
273     if ($argz[0] == 'wakeup') {
274       $room->room_wakeup(&$user);      
275     }
276     else if ($argz[0] == 'logout') {
277       $room->room_wakeup(&$user);      
278       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
279       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
280       $user->the_end = TRUE;
281       $user->step_inc();
282     }
283   }
284 }
285 log_wr("before save data");
286 Room::save_data($room);
287
288 Room::unlock_data($sem);
289 exit;
290 ?>