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