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