9db417c7154f9607d55303dce416c95f2d9a7bdf
[brisk.git] / web / brisk.phh
1 <?php
2 /*
3  *  brisk - brisk.phh
4  *
5  *  Copyright (C) 2006 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 define( FTOK_PATH, "/var/lib/brisk");
23 define(TABLES_N, 8);
24 define(PLAYERS_N, 3);
25 define(MAX_POINTS, 5);
26 define(MAX_PLAYERS, (PLAYERS_N * TABLES_N));
27 define(COMM_N, 12);
28 define(COMM_GEN_N, 50);
29 define(SESS_LEN, 13);
30 define(STREAM_TIMEOUT, 20);
31 define(EXPIRE_TIME, 180);
32 define(GARBAGE_TIMEOUT, 10);
33 define(NICKSERV, "<i>SERVER</i>");
34 define(BRISK_DEBUG, FALSE);
35 // define(DEBUGGING, "local");
36
37 /*
38 $root_wellarr = Array ( '<a href="http://www.google.com">GOOGLE</a>',
39                         'pinollo lanollo',
40                         'rorella liolla' );
41 */
42 $root_wellarr = Array ( );
43 $table_wellarr = Array ( );
44
45 $G_brisk_version = "0.4.2";
46
47 /*$G_room_help = '
48 Brisk - Ver. '.$G_brisk_version.'<br><br>
49 Copyright 2006 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a><br><br>';
50 */
51 $G_room_help = '<b>Brisk - Ver. '.$G_brisk_version.'</b><br><br>Copyright 2006 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a><br><br>';
52
53
54 function xcape($s)
55 {
56   $from = array (   '\\',     '@',        '|' );
57   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
58
59   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
60 }
61
62
63 class Card {
64   var $value; /* 0 - 39 card value */
65   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
66   var $owner; /* (table position 0-4) */
67   // var $pos;   /* Pos in hand. */
68   var $x;     /* When played the X position on the table of the owner. */
69   var $y;     /* When played the Y position on the table of the owner. */
70
71   function Card($value, $stat, $owner)
72   {
73     $this->value = $value;
74     $this->stat  = $stat;
75     $this->owner = $owner;
76   }
77
78   function assign($stat,$owner)
79   {
80     $this->stat  = $stat;
81     $this->owner = $owner;
82   }
83
84   function setpos($pos)
85   {
86     $this->pos   = $pos;
87   }
88
89   function play($x,$y)
90   {
91     $this->stat = 'table';
92     $this->x = $x;
93     $this->y = $y;
94   }
95
96   function take($newown)
97   {
98     $this->stat = 'take';
99     $this->owner = $newown;
100   }
101 }
102
103 class Table {
104   var $player;
105   var $player_n;
106   var $card;
107   var $mazzo;
108   var $gstart;
109   var $turn;
110
111   var $asta_pla;
112   var $asta_pla_n;
113   var $asta_card;
114   var $asta_pnt;
115   
116   var $mult;
117   var $points;    // points array
118   var $points_n;  // number of row of points
119   var $total;
120
121   var $asta_win;
122   var $briscola;
123   var $friend;
124   
125   var $old_asta_pnt;
126   var $old_pnt;
127   var $old_win;
128   var $old_friend;
129
130   function Table() 
131   {
132     $this->player    =   array();
133     $this->player_n  =   0;
134     $this->card      =  &$this->bunch_create();
135     $this->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
136     $this->asta_pla_n=  -1;
137     $this->asta_card =  -1;
138     $this->asta_pnt  =  -1;
139     $this->mult      =   1;
140     $this->points    =   array( );
141     $this->points_n  =   0;
142     $this->total     =   array( 0, 0, 0, 0, 0);
143     $this->asta_win  =  -1;
144     $this->briscola  =  -1;
145     $this->friend    =  -1;
146     $this->turn      =   0;
147     $this->old_asta_pnt = -1;
148     $this->old_pnt      = -1;
149     $this->old_win   =  -1;
150     $this->old_friend=  -1;
151
152   }
153
154   function &bunch_create()
155   {
156     $ret = array();
157
158     for ($i = 0 ; $i < 40 ; $i++) {
159       $ret[$i] = new Card($i, 'bunch', 'no_owner');
160     }
161
162     return ($ret);
163   }
164
165   function bunch_make()
166   {
167     $ct = array(0,0,0,0,0);
168     
169     mt_srand(make_seed());
170     
171     for ($i = 39 ; $i >= 0 ; $i--) 
172       $rest[$i] = $i;
173
174     for ($i = 39 ; $i >= 0 ; $i--) {
175       $rn = rand(0, $i);
176
177       if ($rn == 0)
178         log_main("RND ZERO", "");
179       
180       $id = $rest[$rn];
181
182       $owner = $i % 5;
183       $this->card[$id]->assign('hand', $owner);
184
185       $rest[$rn] = $rest[$i];
186       $pubbpos[$rn2] = $pubbpos[$i];
187     }
188   }
189
190   function init()
191   {
192     $this->mazzo    = rand(0,PLAYERS_N-1);
193     $this->points_n = 0;
194     $this->mult     = 1;
195     $this->old_win  =-1;
196     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
197       $this->total[$i] = 0;
198     }
199
200     log_main("table::init","ci siamo");
201   }
202
203   function game_init(&$bri)
204   {
205     log_rd2($sess,"GSTART 4");
206
207     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
208     $this->bunch_make();
209     
210     
211     $this->asta_pla_n = PLAYERS_N;
212     $this->asta_card = -1;
213     $this->asta_pnt  = 60;
214     $this->asta_win  = -1;
215     $this->briscola  = -1;
216     $this->friend    = -1;
217     $this->turn      =  0;
218     
219     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
220       $this->asta_pla[$i] = TRUE;
221       $user_cur = &$bri->user[$this->player[$i]];
222       $user_cur->subst = 'asta';
223       $user_cur->asta_card = -2;
224       $user_cur->asta_pnt  = -1;
225     }
226   }
227
228   function game_next()
229   {
230     $this->mazzo  = ($this->mazzo + 1) % PLAYERS_N;
231   }
232
233   function getPlayer($idx)
234   {
235     return ($this->player[$idx]);
236   }
237
238   function setPlayer($idx, $player)
239   {
240     $this->player[$idx] = $player;
241   }
242
243   function user_add($idx)
244   {
245     $this->player[$this->player_n] = $idx;
246     $this->player_n++;
247     
248     return ($this->player_n - 1);
249   }
250   
251   function user_rem(&$bri, &$user)
252   {
253     $tabpos = $user->table_pos;
254     
255     /* verifico la consistenza dei dati */
256     if ($bri->user[$this->player[$tabpos]] == $user) {
257       
258       /* aggiorna l'array dei giocatori al tavolo. */
259       for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
260         $this->player[$i] = $this->player[$i+1];
261         $user_cur = &$bri->user[$this->player[$i]];
262         $user_cur->table_pos = $i;
263       }
264       $this->player_n--;
265     }
266     else {
267       log_main($user->sess, "INCONSISTENCY ON TABLE.");
268     }
269   }
270
271 } // End class Table
272   
273 class User {
274   var $name;       // name of the user
275   var $sess;       // session of the user
276   var $lacc;       // last access (for the cleanup)
277   var $stat;       // status (outdoor, room, table, game, ...)
278   var $subst;      // substatus for each status   
279   var $step;       // step of the current status
280   var $trans_step; // step to enable transition between pages (disable == -1)
281   var $comm;       // commands array
282   var $asta_card;  // 
283   var $asta_pnt;   //
284
285   var $table;      // id of the current table (if in table state)
286   var $table_pos;  // idx on the table
287
288   function User($name, $sess, $stat = "", $subst = "", $table = -1) {
289     $this->name  = $name;
290     $this->sess  = $sess;
291     $this->lacc  = time();
292     $this->stat  = $stat;
293     $this->subst  = $subst;
294     $this->step  = 1;
295     $this->trans_step  = -1;
296     $this->comm  = array();
297     $this->asta_card = -2;
298     $this->asta_pnt  = -1;
299
300     $this->table = $table;
301   }
302 }
303
304 class brisco {
305   var $user;
306   var $table;
307   var $comm; // commands for many people
308   var $step; // current step of the comm array
309   var $garbage_timeout;
310
311   function brisco () {
312     $this->user = array();
313
314     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
315       $this->user[$i] = new User("", "");
316     }
317     for ($i = 0 ; $i < TABLES_N ; $i++) 
318       $this->table[$i] = new Table();
319     $this->garbage_timeout = 0;
320   }
321
322   function garbage_manager($force)
323   {
324     
325     /* Garbage collector degli utenti in timeout */
326     $curtime = time();
327     if ($force || $this->garbage_timeout < $curtime) {
328       
329       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
330         $user_cur = &$this->user[$i];
331         if ($user_cur->sess == "")
332           continue;
333         
334         if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
335           log_rd2($user_cur->sess, "AUTO LOGOUT.");
336           /*
337           if ($user_cur->stat == 'table') {
338               log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
339               continue;
340             }
341           else
342           */
343           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
344             log_auth($user_cur->sess, "Autologout session.");
345             
346             $user_cur->sess = "";
347             $user_cur->name = "";
348             $user_cur->the_end = FALSE;
349             
350             log_rd2($user_cur->sess, "AUTO LOGOUT.");
351             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
352               $this->room_wakeup(&$user_cur);
353             else if ($user_cur->subst == 'standup')
354               $this->room_outstandup(&$user_cur);
355             else
356               log_rd2($sess, "LOGOUT FROM WHAT ???");
357           }
358           //    } // if (0 == 1) 
359           
360         }
361       }
362       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
363       
364       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
365     }
366   }
367
368
369   function room_wakeup(&$user)
370   {
371     $table_idx = $user->table;
372     $table = &$this->table[$table_idx];
373
374     log_main("WAKEUP", "begin function table:".$table_idx);
375
376     $from_table = ($user->stat == "table");
377     if ($from_table) {
378       log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
379
380       for ($i = 0 ; $i < $table->player_n ; $i++) {
381         $user_cur = &$this->user[$table->player[$i]];
382         log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
383
384         if ($user_cur != $user) {
385           $user_cur->stat = "room";
386           $user_cur->subst = "sitdown";
387         }
388         else if ($user->sess != "") {
389           $user_cur->stat = "room";
390           $user_cur->subst = "standup";
391           $user_cur->table = -1;
392         }
393       }
394     }
395
396     /* aggiorna l'array dei giocatori al tavolo. */
397     $table->user_rem(&$this, &$user);
398
399     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
400       $user_cur = &$this->user[$i];
401       if ($user_cur->sess == '' || $user_cur->stat != 'room')
402         continue;
403       
404       log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
405
406       // function show_room(&$bri, &$user)
407
408       $ret = "gst.st = ".($user_cur->step+1)."; ";
409       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
410         $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
411         // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
412         log_main("DOCUMENT.index.php", "from table");
413       }
414       else if ($user_cur->stat == "room") {
415         $ret .= table_content($this, $user_cur, $table_idx);
416         $ret .= standup_content($this, $user_cur);
417         
418         $act_content = table_act_content(FALSE, 0, $e, $user->table);
419         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
420         
421         
422         if ($user_cur == $user) {
423           // set the new status 
424           $ret .=  'subst = "standup"; ';
425           // clean the action buttons in other tables
426           for ($e = 0 ; $e < TABLES_N ; $e++) {
427             if ($this->table[$e]->player_n < PLAYERS_N)
428               $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
429           }
430         }
431         else {
432           $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
433           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
434         }
435       }
436       log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
437       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
438       $user_cur->step++;
439     }
440   }
441   
442
443
444
445   function room_outstandup(&$user)
446   {
447     $this->room_sitdown(&$user, -1);
448   }
449   
450   function table_update(&$user)
451   {
452     log_main("table_update", "pre - USER: ".$user->name);
453
454     $table_idx = $user->table;
455
456     if ($table_idx > -1) 
457       $table = &$this->table[$table_idx];
458     
459     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
460       $ret = "";
461       $user_cur = &$this->user[$i];
462       if ($user_cur->sess == '' || $user_cur->stat != 'room')
463       continue;
464       
465       $ret = "gst.st = ".($user_cur->step+1)."; ";
466       if ($table_idx > -1)
467         $ret .= table_content($this, $user_cur, $table_idx);
468       
469       if ($user_cur == $user) {
470         $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
471       }
472       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
473       $user_cur->step++;
474     }
475
476     log_main("table_update", "post");
477   }
478
479   function room_sitdown(&$user, $table_idx)
480   {
481     log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
482
483     if ($table_idx > -1) 
484       $table = &$this->table[$table_idx];
485     
486     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
487       $ret = "";
488       $user_cur = &$this->user[$i];
489       if ($user_cur->sess == '' || $user_cur->stat != 'room')
490       continue;
491       
492       $ret = "gst.st = ".($user_cur->step+1)."; ";
493       if ($table_idx > -1)
494       $ret .= table_content($this, $user_cur, $table_idx);
495       $ret .= standup_content($this, $user_cur);
496       
497       if ($user_cur == $user) {
498         $ret .=  'subst = "sitdown"; ';
499         // clean the action buttons in other tables
500         for ($e = 0 ; $e < TABLES_N ; $e++) {
501           $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
502           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
503         }
504       }
505       else if ($table_idx > -1) {
506         if ($table->player_n == PLAYERS_N) {
507           $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
508           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
509         }
510       }
511       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
512       $user_cur->step++;
513     }
514   }
515
516   function chatt_send(&$user, $mesg)
517   {
518     if ($user->stat == 'table') {
519       $table = &$this->table[$user->table];
520     }
521     
522     $user_mesg = substr($mesg,6);
523     
524     $dt = date("H:i ",time());
525     if (strncmp($user_mesg, "/nick ", 6) == 0) {
526       log_main($user->sess, "chatt_send BEGIN");
527
528       $name_new = str_replace(' ', '_', substr(trim(substr($user_mesg, 6)),0,12));
529       $user_mesg = "COMMAND ".$user_mesg;
530       // Search dup name
531       // change
532       // update local graph
533       // update remote graphs
534       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
535         $user_cur = &$this->user[$i];
536         if ($user_cur->sess == '' || $user_cur->stat != 'room')
537           continue;
538         if ($user_cur->name == $name_new) {
539           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
540           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
541           $user->step++;
542           break;
543         }
544       }
545       if ($i == MAX_PLAYERS) {
546         $user->name = $name_new;
547
548       log_main($user->sess, "chatt_send start set");
549         
550
551         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
552           log_main($user->sess, "chatt_send set loop");
553           
554           $user_cur = &$this->user[$i];
555           if ($user_cur->sess == '')
556             continue;
557           if ($user_cur->stat == 'room') {
558             if ($user->stat == 'room' && $user->subst == 'standup') {
559               $this->standup_update(&$user);
560             }
561             else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
562                      $user->stat == 'table') {
563               log_main($user->sess, "chatt_send pre table update");
564
565               $this->table_update(&$user);
566
567               log_main($user->sess, "chatt_send post table update");
568             }
569           }
570           else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
571             $table = &$this->table[$user->table];
572             
573             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
574             $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
575                 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
576                 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
577                 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
578                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
579                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
580             if ($user_cur == $user)
581               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
582                                                                    xcape($user->name,ENT_COMPAT,"UTF-8"));
583             $user_cur->step++;
584           }
585         }
586       }
587     }
588     else {
589       for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
590         if ($user->stat == 'room') {
591           $user_cur = &$this->user[$i];
592           if ($user_cur->sess == '' || $user_cur->stat != 'room')
593             continue;
594         }
595         else {
596           $user_cur = &$this->user[$table->player[$i]];
597         }
598         
599         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
600         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
601                                                              $dt.xcape($user->name), xcape($user_mesg));
602         $user_cur->step++;
603       }
604     }
605   }
606
607   function &get_user($sess, &$idx)
608   {
609     GLOBAL $PHP_SELF;
610     
611     if (strlen($sess) == SESS_LEN) {
612       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
613         if (strcmp($sess, $this->user[$i]->sess) == 0) {
614           // find it
615           $idx = $i;
616           return ($this->user[$i]);
617         }
618       }
619       log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
620       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
621       // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
622     }
623     else {
624       log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
625     }
626     return (FALSE);
627   }
628
629   /*
630    * function &add_user(&$bri, &$sess, &$idx, $name)
631    *
632    * RETURN VALUE:
633    *   if ($idx != -1 && ret == FALSE)  =>  duplicated nick
634    *   if ($idx == -1 && ret == FALSE)  =>  no space left
635    *   if (ret == TRUE)                 =>  SUCCESS
636    */
637   function &add_user(&$sess, &$idx, $name)
638   {
639     $idx = -1;
640     $idfree = -1;
641     
642     log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
643     if (validate_sess($sess) == FALSE) 
644       $sess = "";
645
646     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
647       /* free user ? */
648       if (strcmp($sess, $this->user[$i]->sess) == 0) {
649         if ($idx == -1)
650           $idx = $i;
651       }
652       if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
653         $idfree = $i;
654       }
655       if (strcmp($this->user[$i]->name, $name) == 0) {
656         $idx = $i;
657         break;
658       }
659     }
660     if ($idx == -1)
661       $idx = $idfree;
662
663     log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
664
665     if ($idx != -1 && $i == MAX_PLAYERS) {
666       /* SUCCESS */
667       if ($sess == "") {
668         $this->user[$idx]->sess = uniqid("");
669         $sess = $this->user[$idx]->sess;
670         
671       }
672       else {
673         $this->user[$idx]->sess = $sess;
674       }
675       $this->user[$idx]->name = $name;
676       $this->user[$idx]->stat = "room";
677       $this->user[$idx]->subst = "standup";
678       
679       log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
680       
681       return ($this->user[$idx]);
682     }
683
684     return (FALSE);
685   }
686   
687   function standup_update(&$user)
688   {
689     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
690       $user_cur = &$this->user[$i];
691       if ($user_cur->sess == '')
692         continue;
693
694       log_main("STANDUP START", $user_cur->stat);
695       
696       if ($user_cur->stat == 'room') {
697         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
698         if ($user_cur == $user)
699           $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
700         
701         log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
702         
703         $user_cur->step++;
704       }
705     }
706   }
707
708
709 } // end class brisco
710
711 function make_seed()
712 {
713   list($usec, $sec) = explode(' ', microtime());
714   return (float) $sec + ((float) $usec * 100000);
715 }
716
717 function log_main($sess, $log) {
718   if (BRISK_DEBUG != TRUE)
719     return;
720
721   $fp = fopen("/tmp/brisk_main.log", 'a');
722   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
723   fclose($fp);
724 }
725
726 function log_rd($sess, $log) {
727   if (BRISK_DEBUG != TRUE)
728     return;
729
730   $fp = fopen("/tmp/brisk_rd.log", 'a');
731   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
732   fclose($fp);
733 }
734
735 function log_rd2($sess, $log) {
736   if (BRISK_DEBUG != TRUE)
737     return;
738
739   $fp = fopen("/tmp/brisk_rd2.log", 'a');
740   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
741   fclose($fp);
742 }
743
744 function log_send($sess, $log) {
745   if (BRISK_DEBUG != TRUE)
746     return;
747
748   $fp = fopen("/tmp/brisk_send.log", 'a');
749   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
750   fclose($fp);
751 }
752
753 function log_auth($sess, $log) {
754   if (BRISK_DEBUG != TRUE)
755     return;
756
757   $fp = fopen("/tmp/brisk_auth.log", 'a');
758   fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
759   fclose($fp);
760 }
761
762 function log_wr($sess, $log) {
763   if (BRISK_DEBUG != TRUE)
764     return;
765
766   $fp = fopen("/tmp/brisk_wr.log", 'a');
767   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
768   fclose($fp);
769 }
770
771 function log_load($sess, $log) {
772   if (BRISK_DEBUG != TRUE)
773     return;
774
775   $fp = fopen("/tmp/brisk_load.log", 'a');
776   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
777   fclose($fp);
778 }
779
780 function init_data()
781 {
782   $brisco = new brisco();
783
784   return $brisco;
785 }
786
787 function lock_data()
788 {
789         //  echo "LOCK: ".FTOK_PATH."/main";
790         //  exit;
791   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
792     echo "FTOK FAILED";
793     exit;
794   }
795   // echo "FTOK ".$tok."<br>";
796   if (($res = sem_get($tok)) == FALSE) {
797     echo "SEM_GET FAILED";
798     exit;
799   }
800   if (sem_acquire($res)) 
801     return ($res);
802   else
803     return (false);
804 }
805
806 function unlock_data($res)
807 {
808   return (sem_release($res));
809 }
810
811
812 function &load_data() 
813 {
814   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
815     echo "FTOK FAILED";
816     exit;
817   }
818
819   if ($shm = shm_attach($tok,100000 * TABLES_N)) {
820     if(($bri = @shm_get_var($shm, $tok)) == false) {
821       log_main("XXX", "INIT MAIN DATA");
822
823       $bri = init_data();
824       shm_put_var($shm, $tok, $bri);
825     }
826     
827     shm_detach($shm);
828
829     return ($bri);
830   }
831
832   return (NULL);
833 }
834
835
836 function save_data(&$bri) 
837 {
838   $ret =   FALSE;
839   $shm =   FALSE;
840   $isacq = FALSE;
841
842   // var_dump($bri);
843
844   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
845     return (FALSE);
846
847   do {
848     $isacq = TRUE;
849     
850     if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
851       break;
852     
853     if (shm_put_var($shm, $tok, $bri) == FALSE)
854       break;
855     // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
856     $ret = TRUE;
857   } while (0);
858   
859   if ($shm)
860     shm_detach($shm);
861      
862   return ($ret);
863 }
864
865 function table_act_content($isstanding, $sitted, $table, $cur_table)
866 {
867   $ret = "";
868
869   if ($isstanding) {
870     if ($sitted < PLAYERS_N) {
871       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
872     }
873   }
874   else {
875     if ($table == $cur_table)
876       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
877     else
878       $ret = "";
879   }
880   return ($ret);
881 }
882
883 function table_content($bri, $user, $table_idx)
884 {
885   $content = "";
886
887   // TODO
888   //
889   //   Si possono usare i dati nella classe table
890   //
891
892   $sess = $user->sess;
893   $table = &$bri->table[$table_idx];
894
895   if ($user->stat != 'room')
896     return;
897
898   for ($i = 0 ; $i < $table->player_n ; $i++) {
899     $user_cur = &$bri->user[$table->player[$i]];
900
901     if ($user_cur == $user) 
902         { $hilion = "<b>"; $hilioff = "</b>"; }
903       else
904         { $hilion = ""; $hilioff = ""; }
905
906     log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
907     
908     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
909   }
910   /*
911   for ( ; $i < PLAYERS_N ; $i++)
912     $content .= "<br>";
913   */
914
915   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
916   
917   return ($ret);
918 }
919
920 function standup_content(&$bri, $user)
921 {
922   $ret = "";
923   $content = "";
924
925   if ($user->stat != 'room')
926     return;
927
928   for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
929     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
930       continue;
931     $ct++;
932   }
933
934   $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
935
936   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
937     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
938       continue;
939
940
941     if ($bri->user[$e]->subst == "standup") {
942       if (($ct % 4) == 0) {
943         $content .= '<tr>';
944       }
945       if ($bri->user[$e] == $user) 
946         { $hilion = "<b>"; $hilioff = "</b>"; }
947       else
948         { $hilion = ""; $hilioff = ""; }
949
950       $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
951       if (($ct % 4) == 3) {
952         $content .= '</tr>';
953       }
954       $ct++;
955     }
956   }
957   $content .= '</table>';
958         
959   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
960   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
961                   $content, $content2);
962
963   return ($ret);
964 }
965
966
967
968
969
970 function show_notify($anc, $text, $tout, $butt)
971 {
972   log_main("SHOW_NOTIFY", $text);
973   return sprintf('var noti = new notify(gst,$("%s"),"%s",%d,"%s");', $anc, $text, $tout,$butt);
974 }
975
976
977
978
979 function briscola_show($bri, $table, $user)
980 {
981   $ptnadd = "";
982   $ret = "";
983
984   if ($table->asta_card == 9) 
985     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
986   
987   /* text of caller cell */
988   if ($user->table_pos == $table->asta_win) 
989     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
990   else 
991     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
992                     xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
993
994   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
995                   $table->asta_win);
996   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
997   $ret .= sprintf('$("caller").style.visibility = "visible";');
998   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
999   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1000   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1001   
1002   return ($ret);
1003 }
1004
1005
1006 function game_result($asta_pnt, $pnt)
1007 {
1008   if ($asta_pnt == 61) {
1009     if ($pnt > 60)
1010       return (1);
1011     else if ($pnt == 60)
1012       return (0);
1013     else
1014       return (-1);
1015   }
1016   else {
1017     if ($pnt >= $asta_pnt)
1018       return (1);
1019     else
1020       return (-1);
1021   }
1022 }
1023
1024 function multoval($mult)
1025 {
1026   if ($mult == 2)
1027     return ("doppio");
1028   else if ($mult == 4)
1029     return ("quadruplo");
1030   else
1031     return (sprintf("%d-plo", $mult));
1032 }
1033
1034 function show_table_info(&$bri, &$table, $table_pos)
1035 {
1036   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1037   $noty = sprintf('<table class=\"points\"><tr><th></th>');
1038   
1039   // Names.
1040   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1041     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1042   $noty .= sprintf("</tr>");
1043
1044   // Points.
1045   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1046
1047   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1048     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1049     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1050       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1051     $noty .= "</tr>";
1052   }
1053
1054   // Total points.
1055   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1056   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1057     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1058   $noty .= "</tr></table>";
1059
1060   if ($table->old_win != -1) {
1061     $win = $table->player[$table->old_win];
1062     $fri = $table->player[$table->old_friend];
1063
1064     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1065
1066     if ($win != $fri) {
1067       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
1068                        xcape($bri->user[$win]->name),
1069                        xcape($bri->user[$fri]->name));
1070       if ($table->old_pnt == 120) {
1071         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1072       }
1073       else {
1074         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1075                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1076                           'pi&ugrave; di 60'), $table->old_pnt,
1077                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1078       }
1079     }
1080     else {
1081       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1082                        xcape($bri->user[$win]->name));
1083       if ($table->old_pnt == 120) {
1084         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1085       }
1086       else {
1087         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1088                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1089                           'pi&ugrave; di 60'), $table->old_pnt,
1090                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1091       }
1092     }
1093   }
1094   if ($table->mazzo == $table_pos) 
1095     $noty .= "Fai <b>tu</b> il mazzo.";
1096   else {
1097     $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1098     $noty .= "Il mazzo a <b>$unam</b>.";
1099   }
1100
1101   if ($table->mult > 1) {
1102     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1103   }
1104   $noty .= "<hr><br>";
1105
1106   $ret .= show_notify("bg", $noty, 3000, "torna alla partita");
1107   
1108   return ($ret);
1109 }
1110
1111 function root_wellcome($user)
1112 {
1113   GLOBAL $root_wellarr;
1114
1115   for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1116     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1117
1118   return ($ret);
1119 }
1120
1121 function table_wellcome($user)
1122 {
1123   GLOBAL $table_wellarr;
1124
1125   for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1126     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1127
1128   return ($ret);
1129 }
1130
1131 function show_room(&$bri, &$user)
1132 {
1133   $ret .= sprintf('gst.st = %d;',  $user->step);
1134   $ret .= sprintf('stat = "%s";',  $user->stat);
1135
1136   $ret .= root_wellcome($user);
1137   $ret .= sprintf('subst = "%s";', $user->subst);
1138   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1139   for ($i = 0 ; $i < TABLES_N ; $i++) {
1140     $ret .= table_content($bri, $user, $i);
1141     $act_content = table_act_content(($user->subst == 'standup'), 
1142                                      $bri->table[$i]->player_n, $i, $user->table);
1143     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1144   }
1145   $ret .= standup_content($bri, $user);
1146   
1147   return ($ret);
1148 }
1149
1150
1151
1152 /* show table 
1153 is_transition (is from room to table ?)
1154 is_again      (is another game)
1155  */
1156 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1157 {
1158   $table_idx = $user->table;
1159   $table = &$bri->table[$table_idx];
1160
1161   /****************
1162    FOR RELOAD:
1163      DONE - user names
1164      handed cards
1165      tabled cards
1166      taked  cards
1167      remark on/off
1168      cards dnd (and gameable card if its turn)
1169      who call and what
1170   ****************/
1171  
1172
1173   $ret = "table_init();";
1174   
1175   if (!$is_again) {
1176     /* GENERAL STATUS */
1177     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1178                      $sendstep, $user->stat, $user->subst, $user->table_pos);
1179     /* BACKGROUND */
1180     $ret .= "background_set();";
1181     
1182     /* USERS INFO */
1183     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1184     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1185                     xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1186                     xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1187                     xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1188                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1189                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1190   }
1191   /* NOTIFY FOR THE CARD MAKER */
1192   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1193     $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1194     $ret .= table_wellcome($user);
1195   }
1196
1197   /* CARDS */
1198   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1199     $ret .= "|";
1200     
1201     for ($i = 0 ; $i < 8 ; $i++) {
1202       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1203         $ct = 0;
1204         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1205           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1206             $ct++;
1207             if ($ct == $i+1)
1208               break;
1209           }
1210         }
1211         log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1212         
1213         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1214                          $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1215                               $table->card[$o]->value : -1), 
1216                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1217       }
1218     }    
1219   }
1220   else {
1221     $taked  = array(0,0,0,0,0);
1222     $inhand = array(0,0,0,0,0);
1223     $ontabl  = array(-1,-1,-1,-1,-1);
1224     $cards  = array();
1225
1226     for ($i = 0 ; $i < 40 ; $i++) {
1227       if ($table->card[$i]->stat == 'hand') {
1228         if ($table->card[$i]->owner == $user->table_pos) {
1229           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1230         }
1231         $inhand[$table->card[$i]->owner]++;
1232       }
1233       else if ($table->card[$i]->stat == 'take') {
1234         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1235         $taked[$table->card[$i]->owner]++;
1236       }
1237       else if ($table->card[$i]->stat == 'table') {
1238         $ontabl[$table->card[$i]->owner] = $i;
1239       }
1240     }
1241     $logg = "\n";
1242     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1243       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1244     }
1245     log_main("Stat table:", $logg);
1246
1247     /* Set ours cards. */
1248     $oursarg = "";
1249     for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++) 
1250       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1251     for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++) 
1252       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1253     $ret .= sprintf('card_setours(%s);', $oursarg);
1254
1255     /* Dispose all cards */
1256     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1257       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1258       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1259                       $inhand[$i], $taked[$i]);
1260
1261       if ($ontabl[$i] != -1) {
1262         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1263                         $table->card[$ontabl[$i]]->value, 
1264                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1265       }
1266     }
1267   }
1268
1269   /* Show auction */
1270   if ($user->subst == 'asta') {
1271
1272     /* show users auction status */
1273     $showst = "";
1274     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1275       $user_cur = &$bri->user[$table->player[$i]];
1276       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1277                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1278     }
1279     if (PLAYERS_N == 3)
1280         $showst .= ",-2,-2";
1281     $ret .= sprintf('show_astat(%s);', $showst);
1282
1283     if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1284       /* show card chooser */
1285       $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1286                       $table->asta_card);
1287     }
1288     else {
1289       /* show auction */
1290       if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1291           $table->asta_win == -1) 
1292         $ret .= sprintf('dispose_asta(%d,%d);', 
1293                         $table->asta_card + 1, $table->asta_pnt+1);
1294       else
1295         $ret .= sprintf('dispose_asta(%d,%d);',
1296                         $table->asta_card + 1, -($table->asta_pnt+1));
1297     }
1298
1299     /* Remark */
1300     if ($table->asta_win == -1) { // auction case
1301       if ($user->table_pos == ($table->gstart % PLAYERS_N)) 
1302         $ret .= "remark_on();";
1303       else
1304         $ret .= "remark_off();";
1305     }
1306     else { // chooseed case
1307       if ($user->table_pos == $table->asta_win) 
1308         $ret .= "remark_on();";
1309       else
1310         $ret .= "remark_off();";
1311     }
1312   }
1313   else if ($user->subst == 'game') {
1314     /* HIGHLIGHT */
1315     if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos) 
1316       $ret .= "is_my_time = true; remark_on();";
1317     else
1318       $ret .= "remark_off();";
1319     
1320     /* WHO CALL AND WATH */
1321     $ret .= briscola_show($bri, $table, $user);
1322     
1323   }
1324   return ($ret);
1325   }
1326
1327 function calculate_winner(&$table)
1328 {
1329   $briontab = FALSE;
1330   $ontab = array();
1331   $ontid = array();
1332   $cur_win  =  -1;
1333   $cur_val  = 100;
1334   $cur_seed = $table->briscola - ($table->briscola % 10);
1335
1336   for ($i = 0 ; $i < 40 ; $i++) {
1337     if ($table->card[$i]->stat != "table")
1338       continue;
1339
1340     log_wr($sess, sprintf("Card On table: [%d]", $i));
1341
1342     $v = $table->card[$i]->value; 
1343     $ontab[$table->card[$i]->owner] = $v;
1344     $ontid[$table->card[$i]->owner] = $i;
1345     /* se briscola setto il flag */
1346     if (($v - ($v % 10)) == $cur_seed)
1347       $briontab = TRUE;
1348   }
1349
1350   if ($briontab == FALSE) {
1351     $cur_win  = $table->gstart;
1352     $cur_val  = $ontab[$cur_win];
1353     $cur_seed = $cur_val - ($cur_val % 10);
1354   }
1355
1356   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1357     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1358       if ($ontab[$i] < $cur_val) {
1359         $cur_val = $ontab[$i];
1360         $cur_win = $i;
1361       }
1362     }
1363   }
1364
1365   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1366     $table->card[$ontid[$i]]->owner = $cur_win;
1367     $table->card[$ontid[$i]]->stat =  "take";
1368   }
1369   return ($cur_win);
1370 }
1371
1372 function calculate_points(&$table)
1373 {
1374   $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1375
1376   $pro = 0;
1377
1378   if ($table->asta_pnt == 60)
1379     $table->asta_pnt = 61;
1380
1381   $table->old_win = $table->asta_win;
1382   $table->old_friend = $table->friend;
1383   $table->old_asta_pnt = $table->asta_pnt;
1384
1385   for ($i = 0 ; $i < 40 ; $i++) {
1386     $ctt = $table->card[$i]->value % 10;
1387     $own = $table->card[$i]->owner;
1388     if ($own == $table->asta_win || $own == $table->friend) 
1389       $pro += $all_points[$ctt];
1390   }
1391
1392   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1393
1394   
1395   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1396     $table->points[$table->points_n % MAX_POINTS] = array();
1397     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1398       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1399     $table->points_n++;
1400     $table->old_pnt = $pro;
1401     $table->mult *= 2;
1402
1403     return;
1404   }
1405
1406   if ($pro >= $table->asta_pnt) 
1407     $sig = 1;
1408   else
1409     $sig = -1;
1410
1411   $table->points[$table->points_n % MAX_POINTS] = array();
1412   for ($i = 0 ; $i < 5 ; $i++) {
1413     if ($i == $table->asta_win) 
1414       $pt = ($i == $table->friend ? 4 : 2);
1415     else if ($i == $table->friend) 
1416       $pt = 1;
1417     else
1418       $pt = -1;
1419
1420     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1421
1422     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1423
1424     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1425     
1426     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1427     $table->total[$i] += $pt;
1428   }
1429   $table->points_n++;
1430   $table->old_pnt = $pro;
1431   $table->mult = 1;
1432 }
1433
1434 function validate_sess($sess) {
1435   if (strlen($sess) == SESS_LEN) 
1436     return (TRUE);
1437   else
1438     return (FALSE);
1439 }
1440 ?>