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