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