renamed to briskin5.css
[brisk.git] / web / briskin5 / Obj / briskin5.phh
1 <?php
2 define(BRISKIN5_PLAYERS_N, 3);
3 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
4 define(BRISKIN5_SHM_DIMS, (50000 * BRISKIN5_MAX_PLAYERS));
5
6 class Briskin5 {
7   var $user;
8   var $table;
9   var $table_idx;
10   var $table_token;
11
12   var $comm; // commands for many people
13   var $step; // current step of the comm array
14   var $garbage_timeout;
15
16   var $the_end;
17
18   var $tok;
19
20   function Briskin5 (&$room, $table_idx, $table_token) {
21     $this->user = array();
22     $this->table = array();
23
24     $this->the_end = FALSE;
25
26     if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
27       echo "FTOK FAILED";
28       exit;
29     }
30
31     $user  =& $room->user;
32     $table =& $room->table[$table_idx];
33
34     log_wr("Briskin5 constructor");
35
36     for ($i = 0 ; $i < $table->player_n ; $i++) {
37       $user[$table->player[$i]]->table_token = $table_token;
38       $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
39     }
40     $this->table[0] =& Table::spawn(&$table);
41     $this->table_idx = $table_idx;
42     $this->table_token = $table_token;
43     $this->garbage_timeout = 0;
44     
45     log_wr("Briskin5 constructor end");
46   }
47
48
49   function &get_user($sess, &$idx)
50   {
51     GLOBAL $PHP_SELF, $G_false;
52
53     if (validate_sess($sess)) {
54       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
55         if (strcmp($sess, $this->user[$i]->sess) == 0) {
56           // find it
57           $idx = $i;
58           $ret = &$this->user[$i];
59           return ($ret);
60         }
61       }
62       log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
63       // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) 
64       // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
65     }
66     else {
67       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
68     }
69
70     return ($G_false);
71   }
72
73
74   function garbage_manager($force)
75   {
76     
77     /* Garbage collector degli utenti in timeout */
78     $ismod = FALSE;
79     $curtime = time();
80     if ($force || $this->garbage_timeout < $curtime) {
81       
82       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
83         $user_cur = &$this->user[$i];
84         if ($user_cur->sess == "" || 
85             ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
86           continue;
87         
88         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
89           log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
90
91           if ($user_cur->stat == 'table') {
92             log_auth($user_cur->sess," bin5 Autologout session.");
93
94             /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE 
95
96             $tmp_sess = $user_cur->sess;
97             $user_cur->sess = "";
98             step_unproxy($tmp_sess);
99             $user_cur->name = "";
100             $user_cur->the_end = FALSE;
101             
102             */
103
104             $this->table_wakeup(&$user_cur);
105           }
106         }
107       }
108       log_rd2($user_cur->sess." GARBAGE UPDATED!");
109       
110       $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
111       $ismod = TRUE;
112     }
113
114     return ($ismod);
115   }
116
117
118
119
120   //
121   //  static functions
122   //
123   function &load_data($table_idx, $table_token = "") 
124   {
125     GLOBAL $G_false, $sess;
126     $shm = FALSE;
127
128     log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
129     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
130       echo "FTOK FAILED";
131       exit;
132     }
133     
134     do {
135       //      if (shmchk_exists($tok) == FALSE)
136       if (locshm_exists($tok) == FALSE)
137         break;
138
139       if (($shm = shm_attach($tok, BRISKIN5_SHM_DIMS)) == FALSE)
140         break;
141
142       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
143         break;
144
145       if ($table_token != "" && $bri->table_token != $table_token) {
146         log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
147         
148         break;
149       }
150       $bri->tok = $tok;
151
152       shm_detach($shm);
153         
154       $ret = &$bri;
155       return ($ret); 
156     } while (FALSE);
157
158     if ($shm != FALSE)
159       shm_detach($shm);
160
161     log_wr("briskin5 load_data failed");
162     
163     return ($G_false);
164   }
165   
166
167   function save_data(&$bri) 
168   {
169     GLOBAL $sess;
170     
171     $ret =   FALSE;
172     $shm =   FALSE;
173     $isacq = FALSE;
174     
175     log_main("SAVE BRISKIN5 DATA");
176     // var_dump($bri);
177     
178     if (!isset($bri->tok))
179       return (FALSE);
180     
181     do {
182       $isacq = TRUE;
183       
184       if (($shm = shm_attach($bri->tok, BRISKIN5_SHM_DIMS)) == FALSE)
185         break;
186       
187       // log_only("PUT_VAR DI ".strlen(serialize($bri)));
188       if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
189         log_only("PUT_VAR FALLITA ".strlen(serialize($bri)));
190         log_only(serialize($bri));
191         break;
192       }
193       // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
194       $ret = TRUE;
195     } while (0);
196     
197     if ($shm)
198       shm_detach($shm);
199     
200     return ($ret);
201   }
202
203   function destroy_data($table_idx) 
204   {
205     GLOBAL $sess;
206
207     $ret =   FALSE;
208     $shm =   FALSE;
209     $isacq = FALSE;
210
211     log_main("DESTROY BRISKIN5 DATA");
212
213     
214     do {
215       $isacq = TRUE;
216       
217       log_main("DESTROY2 BRISKIN5 DATA");
218
219       if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
220         break;
221
222       if (($shm = shm_attach($tok, BRISKIN5_SHM_DIMS)) === FALSE)
223         break;
224       
225       if (shm_remove($shm) === FALSE) {
226         log_only("REMOVE FALLITA ".strlen(serialize($bri)));
227         log_only(serialize($bri));
228         break;
229       }
230    
231       $shm = FALSE;
232       log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
233       
234       // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
235       $ret = TRUE;
236     } while (0);
237     
238     if ($shm)
239       shm_detach($shm);
240     
241     return ($ret);
242   }
243
244   function lock_data($table_idx)
245   {
246     GLOBAL $sess; 
247     
248     log_wr("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
249     //  echo "LOCK: ".FTOK_PATH."/main";
250     //  exit;
251     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
252       echo "FTOK FAILED";
253       exit;
254     }
255     // echo "FTOK ".$tok."<br>";
256     if (($res = sem_get($tok)) == FALSE) {
257       echo "SEM_GET FAILED";
258       exit;
259     }
260     if (sem_acquire($res)) {   
261       log_lock("LOCK table ".$table_idx."[res: ".$res."]");
262       return ($res);
263     }
264     else {
265       log_lock("LOCK table ".$table_idx.":FAILED");
266       return (FALSE);
267     }
268   }
269   
270   function unlock_data($res)
271   {
272     GLOBAL $sess; 
273     
274     log_lock("UNLOCK table [res: ".$res."]");
275
276     return (sem_release($res));
277   }
278
279
280   function chatt_send(&$user, $mesg)
281   {
282     if ($user->stat == 'table') {
283       $table = &$this->table[$user->table];
284     }
285     
286     $user_mesg = substr($mesg,6);
287     
288     $timecur = time();
289     
290     $dt = date("H:i ", $timecur);
291     if (strncmp($user_mesg, "/nick ", 6) == 0) {
292       log_main($user->sess." chatt_send BEGIN");
293
294       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
295           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
296           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
297           $user->step_inc();
298
299           return;
300       }
301       $user_mesg = "COMMAND ".$user_mesg;
302       // Search dup name
303       // change
304       // update local graph
305       // update remote graphs
306       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
307         $user_cur = &$this->user[$i];
308         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
309         if ($user_cur->sess == '')
310           continue;
311         if ($user_cur->name == $name_new) {
312           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
313           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
314           $user->step_inc();
315           break;
316         }
317       }
318       if ($i == BRISKIN5_MAX_PLAYERS) {
319         $user->name = $name_new;
320
321       log_main($user->sess." chatt_send start set");
322         
323
324         for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
325           log_main($user->sess." chatt_send set loop");
326           
327           $user_cur = &$this->user[$i];
328           if ($user_cur->sess == '')
329             continue;
330           if ($user_cur->stat == 'room') {
331             if ($user->stat == 'room' && $user->subst == 'standup') {
332               $this->standup_update(&$user);
333             }
334             else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
335                      $user->stat == 'table') {
336               log_main($user->sess." chatt_send pre table update");
337
338               $this->table_update(&$user);
339
340               log_main($user->sess." chatt_send post table update");
341             }
342           }
343           else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
344             $table = &$this->table[$user->table];
345             
346             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
347             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
348                 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
349                 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
350                 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
351                 (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
352                 (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
353             if ($user_cur == $user)
354               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
355                                                                    xcape($user->name,ENT_COMPAT,"UTF-8"));
356             $user_cur->step_inc();
357           }
358         }
359       }
360     }
361     else {
362       for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
363         if ($user->stat == 'room') {
364           $user_cur = &$this->user[$i];
365           if ($user_cur->sess == '' || $user_cur->stat != 'room')
366             continue;
367         }
368         else {
369           $user_cur = &$this->user[$table->player[$i]];
370         }
371         
372         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
373         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
374                                                              $dt.xcape($user->name), xcape($user_mesg));
375         $user_cur->step_inc();
376       }
377       log_legal($timecur, $user->sess, $user->name, 
378                 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
379     }
380   }
381
382   function table_wakeup(&$user)
383   {
384     $table = &$this->table[0];
385
386     log_main("BRISKIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
387
388     $curtime = time();
389
390     log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
391     
392     for ($i = 0 ; $i < $table->player_n ; $i++) {
393       $user_cur = &$this->user[$i];
394       log_main("PREIMPOST INLOOP name: ".$user_cur->name);
395       
396       if ($user_cur == $user)
397         $user_cur->subst = "shutdowner";
398       else
399         $user_cur->subst = "shutdowned";
400       $user_cur->laccwr = $curtime;
401
402       $ret = "gst.st = ".($user_cur->step+1)."; ";
403       $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
404
405       log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
406       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
407       $user_cur->step_inc();
408     }
409
410     $this->the_end = TRUE;
411   }
412   
413   /*
414    *  If all players are freezed the room garbage_manager clean up table and users.
415    */ 
416   function is_abandoned() 
417   {
418     $is_ab = TRUE;
419     $curtime = time();
420
421     $table = &$this->table[0];
422
423     for ($i = 0 ; $i < $table->player_n ; $i++) {
424       $user_cur = &$this->user[$i];
425
426       if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) { 
427         $is_ab = FALSE;
428         break;
429       }
430     }
431
432     return ($is_ab);
433   }
434 } // end class Briskin5
435
436 function locshm_exists($tok)
437 {
438   // return (TRUE);
439
440   if (($id = shmop_open($tok,"a", 0, 0)) == FALSE) {
441     log_main($tok." SHM NOT exists");
442
443     return (FALSE);
444   }
445   else {
446     shmop_close($id);
447     log_main($tok." SHM exists");
448
449     return (TRUE);
450   }
451     
452 }
453
454
455 ?>