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