removed proxy check from _wr and _rd
[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 ($user->the_end == TRUE) {
126         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
127         Room::unlock_data($sem);
128         exit;
129       }
130       /* TODO: refact to a function */
131       if ($user->bantime > $user->laccwr) {
132         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
133         $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);
134         
135         $user->step_inc();
136         Room::save_data($room);
137         Room::unlock_data($sem);
138         exit;
139       }
140     
141       // Take parameters
142       $table_idx = $argz[1];
143       $table = &$room->table[$table_idx];
144     
145       if ($table->player_n == PLAYERS_N) {
146         log_wr("WARN:FSM: Sitdown unreachable, table full.");
147         Room::unlock_data($sem);
148         exit;
149       } 
150       
151       // set new status
152       $user->subst = "sitdown";
153       $user->table = $table_idx;
154       $user->table_pos = $table->user_add($idx);
155       
156       log_wr("MOP before");
157
158       if ($table->player_n == PLAYERS_N) {
159         log_wr("MOP inall");
160
161         // Start game for this table.
162         log_wr("Start game!");
163         
164         //
165         //  START THE SPAWN HERE!!!!
166         //
167
168         if (TRUE) { // WITH SPAWN
169           $curtime = time();
170           // Create new spawned table
171           $bri_sem = Briskin5::lock_data($table_idx);
172           $table_token = uniqid("");
173           $room->table[$table_idx]->table_token = $table_token;
174           $room->table[$table_idx]->table_start = $curtime;
175           
176           if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
177             log_wr("bri create: FALSE");
178           else
179             log_wr("bri create: ".serialize($bri));
180         
181           // init table
182           $bri_table =& $bri->table[0];
183           $bri_table->init(&$bri->user);
184           $bri_table->game_init(&$bri->user);
185           //
186           // Init spawned users.
187           //
188           for ($i = 0 ; $i < $table->player_n ; $i++) {
189             $bri_user_cur = &$bri->user[$i];
190             $user_cur = &$room->user[$table->player[$i]];
191             
192             $bri_user_cur->stat_set('table');
193             $bri_user_cur->subst = 'asta';
194             $bri_user_cur->laccwr = $curtime;
195
196             $bri_user_cur->trans_step = $user_cur->step + 1;
197             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
198             $bri_user_cur->step_inc();
199             $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
200
201             $bri_user_cur->step_inc();
202
203             log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
204
205             log_wr("Pre if!");
206             
207 //          ARRAY_POP DISABLED
208 //          // CHECK
209             while (array_pop($user_cur->comm) != NULL);
210
211             $ret = "";
212             $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);
213             
214             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
215             $user_cur->trans_step = $user_cur->step + 1;
216             log_wr("TRANS ATTIVATO");
217             
218             
219             $user_cur->stat_set('table');
220             $user_cur->subst = 'asta';
221             $user_cur->laccwr = $curtime;
222             $user_cur->step_inc();
223           }
224           log_wr("presave bri");
225           Briskin5::save_data($bri);
226           Briskin5::unlock_data($bri_sem);
227           log_wr("postsave bri");
228         }
229         else { // BEFORE SPAWN
230           // init table
231           $table->init(&$room->user);
232           $table->game_init(&$room->user);
233           $curtime = time();
234           
235           // init users
236           for ($i = 0 ; $i < $table->player_n ; $i++) {
237             $user_cur = &$room->user[$table->player[$i]];
238             log_wr("Pre if!");
239             
240             $ret = "";
241             $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
242             
243             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
244             $user_cur->trans_step = $user_cur->step + 1;
245             log_wr("TRANS ATTIVATO");
246             
247             
248             $user_cur->stat_set('table');
249             $user_cur->subst = 'asta';
250             $user_cur->laccwr = $curtime;
251             $user_cur->step_inc();
252             
253             $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
254             $user_cur->step_inc();
255           }
256         } // end else {  BEFORE SPAWN
257         
258         log_wr("MOP after");
259
260       }
261       // change room
262       $room->room_sitdown(&$user, $table_idx);
263
264       log_wr("MOP finish");
265
266       
267     }
268     else if ($argz[0] == 'logout') {
269       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
270       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
271       $user->the_end = TRUE;
272       $user->step_inc();
273     }
274   }
275   /**********************
276    *                    *
277    *   SUBST: sitdown   *
278    *                    *
279    **********************/
280   else if ($user->subst == 'sitdown') {
281     if ($argz[0] == 'wakeup') {
282       $room->room_wakeup(&$user);      
283     }
284     else if ($argz[0] == 'logout') {
285       $room->room_wakeup(&$user);      
286       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
287       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
288       $user->the_end = TRUE;
289       $user->step_inc();
290     }
291   }
292 }
293 log_wr("before save data");
294 Room::save_data($room);
295
296 Room::unlock_data($sem);
297 exit;
298 ?>