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