aggiunto *_wellcome per le chat, create user_add e user_rem nella classe table, aggiu...
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
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  * $Id$
21  *
22  */
23
24 require_once("brisk.phh");
25 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
26   echo "Debugging time!";
27   exit;
28 }
29
30 log_load($sess, "LOAD: index_wr.php");
31
32 /*
33  *  MAIN
34  */
35 log_wr($sess, 'COMM: '.$mesg);
36
37 $sem = lock_data();
38 $bri = &load_data();
39 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
40   echo "Get User Error";
41   log_wr($sess, "Get User Error");
42   unlock_data($sem);
43   exit;
44 }
45 $argz = explode('|', $mesg);
46
47 if ($argz[0] == 'shutdown') {
48   log_auth($user_cur->sess, "Shutdown session.");
49   
50   $user->sess = "";
51   $user->name = "";
52   $user->the_end = FALSE;
53   
54   log_rd2($user->sess, "AUTO LOGOUT.");
55   if ($user->subst == 'sitdown' || $user->stat == 'table')
56     $bri->room_wakeup(&$user);
57   else if ($user->subst == 'standup')
58     $bri->room_outstandup(&$user);
59   else
60     log_rd2($sess, "SHUTDOWN FROM WHAT ???");
61 }
62 else if ($user->stat == 'room') {
63   if ($argz[0] == 'logout') {
64     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
65     $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
66     $user->the_end = TRUE;
67     $user->step++;
68   }
69   else if ($argz[0] == 'chatt') {
70     $bri->chatt_send(&$user,$mesg);
71   }
72   else if ($argz[0] == 'sitdown') {
73     if ($user->stat != 'room' || $user->subst != 'standup') {
74       log_wr($sess, "Warning ! sitdown out fsm");
75       unlock_data($sem);
76       exit;
77     }
78                 
79     // Take parameters
80     $table_idx = $argz[1];
81     $table = &$bri->table[$table_idx];
82                 
83     if ($table->player_n == PLAYERS_N) {
84       log_wr($sess, "Warning ! unreachable, table full.");
85       unlock_data($sem);
86       exit;
87     } 
88
89     // set new status
90     $user->subst = "sitdown";
91     $user->table = $table_idx;
92     $user->table_pos = $table->user_add($idx);
93                 
94     if ($table->player_n == PLAYERS_N) {
95       // Start game for this table.
96       log_wr($sess, "Start game!");
97       
98       $table->init();
99       $table->game_init(&$bri);
100       
101       for ($i = 0 ; $i < $table->player_n ; $i++) {
102         $user_cur = &$bri->user[$table->player[$i]];
103         log_wr($sess, "Pre if!");
104         
105         $ret = "";
106         $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
107         
108         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
109         $user_cur->trans_step = $user_cur->step + 1;
110         log_wr($sess, "TRANS ATTIVATO");
111         
112         $user_cur->stat =  'table';
113         $user_cur->subst = 'asta';
114         $user_cur->step++;
115         
116         $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
117         $user_cur->step++;
118       }
119     }
120                 
121     $bri->room_sitdown(&$user, $table_idx);
122   }
123   else if ($argz[0] == 'wakeup') {
124     if ($user->stat != 'room' || $user->subst != 'sitdown') {
125       log_wr($sess, "Warning ! wakeup out fsm.");
126       unlock_data($sem);
127       exit;
128     }
129                 
130     // set new status
131
132     $user->subst = "standup";
133
134     $bri->room_wakeup(&$user);
135   }
136 }
137 /***************
138  *             *
139  *    TABLE    *
140  *             *
141  ***************/
142 else if ($user->stat == 'table' && $user->subst == 'asta') {
143   $table = &$bri->table[$user->table];
144
145   if ($argz[0] == 'logout') {
146
147     //   document.location.assign("index.php");
148
149     $bri->room_wakeup(&$user);
150   }
151   else if ($argz[0] == 'tableinfo') {
152     log_wr($sess, "PER DI TABLEINFO");
153     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
154     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
155
156     log_wr($sess, $user->comm[$user->step % COMM_N]);
157
158
159     $user->step++;
160   }
161   else if ($argz[0] == 'chatt') {
162     $bri->chatt_send(&$user,$mesg);
163   }
164   else if ($argz[0] == 'asta') {
165     $again = TRUE;
166     
167     $index_cur = $table->gstart % PLAYERS_N;
168     if ($user->table_pos == $index_cur &&
169         $table->asta_pla[$index_cur]) {
170       $a_card = $argz[1];
171       $a_pnt  = $argz[2];
172       
173       log_wr($sess, "CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
174       
175       // Abbandono dell'asta
176       if ($a_card <= -1) {
177         log_wr($sess, "Abbandona l'asta.");
178         $table->asta_pla[$index_cur] = FALSE;
179         $user->asta_card  = -1;
180         $table->asta_pla_n--;
181         $again = FALSE;
182       }
183       else if ($a_card <= 9) {
184         if ($table->asta_card == 9) {
185           if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
186             $again = FALSE;
187         }
188         else {
189           if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
190             $again = FALSE;
191         }
192
193         if ($again == FALSE) {
194           log_wr($sess, "NUOVI ORZI.");
195           $user->asta_card  = $a_card;
196           $table->asta_card = $a_card;
197           if ($a_card == 9) {
198             $user->asta_pnt   = $a_pnt;
199             $table->asta_pnt  = $a_pnt;
200           }
201         }
202       }
203       
204       
205       
206       if ($again) { // Qualcosa non andato bene, rifare
207         log_wr($sess, "Ripetere.");
208       }
209       else {
210         /* next step */
211         $showst = "show_astat("; 
212         for ($i = 0 ; $i < PLAYERS_N ; $i++) {
213           $user_cur = &$bri->user[$table->player[$i]];
214           $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
215                              ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
216         }
217         if (PLAYERS_N == 3)
218           $showst .= ",-2,-2";
219         $showst .= ");";
220
221         $maxcard = -2;
222         for ($i = 0 ; $i < PLAYERS_N ; $i++) {
223           $user_cur = &$bri->user[$table->player[$i]];
224           if ($maxcard < $user_cur->asta_card)
225             $maxcard = $user_cur->asta_card;
226         }
227
228         if ($table->asta_pla_n > ($maxcard > -1 ? 1 : 0) &&
229             !($table->asta_card == 9 && $table->asta_pnt == 120)) {
230           for ($i = 1 ; $i < PLAYERS_N ; $i++) {
231             $index_next = ($table->gstart + $i) % PLAYERS_N;
232             if ($table->asta_pla[$index_next]) {
233               log_wr($sess,"GSTART 1");
234               $table->gstart += $i;
235               break;
236             }
237           }
238           
239           
240           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
241             $user_cur = &$bri->user[$table->player[$i]];
242             $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
243             if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
244               $ret .= sprintf('dispose_asta(%d,%d); remark_on();', 
245                               $table->asta_card + 1, $table->asta_pnt+1);
246             else
247               $ret .= sprintf('dispose_asta(%d,%d); remark_off();',
248                               $table->asta_card + 1, -($table->asta_pnt+1));
249             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
250             $user_cur->step++;
251           }
252         }
253         else if ($table->asta_pla_n == 0) {
254           log_wr($sess, "MOLLANO TUTTI!");
255
256           log_wr($sess, sprintf("GIOCO FINITO !!!"));
257           
258           $table->mult *= 2; 
259
260           $table->game_next();
261           $table->game_init(&$bri);
262           
263           for ($i = 0 ; $i < PLAYERS_N ; $i++) {        
264             $user_cur = &$bri->user[$table->player[$i]];
265
266             $ret = sprintf('gst.st = %d;', $user_cur->step+1);
267             $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
268             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
269             $user_cur->step++;      
270           }
271         }
272         else {
273           log_wr($sess, "FINITA !");
274           // if a_pnt == 120 supergame ! else abbandono
275           if ($a_pnt == 120) {
276             $chooser = $index_cur;
277             for ($i = 1 ; $i < PLAYERS_N ; $i++) 
278               if ($i != $chooser)
279                 $table->asta_pla[$i] = FALSE;
280           }
281           else {
282             $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt)."); remark_off();";
283             $user->step++;
284             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
285               $chooser = ($table->gstart + $i) % PLAYERS_N;
286               if ($table->asta_pla[$chooser]) {
287                 break;
288               }
289             }
290           }
291           $table->asta_win = $chooser;
292
293           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
294             $user_cur = &$bri->user[$table->player[$i]];
295             $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
296
297             if ($i == $chooser) {
298               $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
299             }
300             else {
301               $ret .= "remark_off();";
302             }
303
304             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
305             $user_cur->step++;      
306           }
307         }
308       }
309     }
310     else {
311       log_wr($sess, "NON CI SIAMO");
312     }
313   }
314   /*  asta::choose */
315   else if ($argz[0] == 'choose') {
316     if ($table->asta_win > -1 && 
317         $user->table_pos == $table->asta_win) {
318       $a_brisco = $argz[1];
319       if ($a_brisco >= 0 && $a_brisco < 40) {
320         $table->briscola = $a_brisco;
321         $table->friend   = $table->card[$a_brisco]->owner;
322         log_wr($sess,"GSTART 2");
323         $table->gstart = ($table->mazzo+1) % PLAYERS_N;
324         log_wr($sess, "Setta la briscola a ".$a_brisco);
325
326         $chooser = $table->asta_win;
327         $user_chooser = &$bri->user[$table->player[$chooser]];
328         for ($i = 0 ; $i < PLAYERS_N ; $i++) {
329           $user_cur = &$bri->user[$table->player[$i]];
330           $user_cur->subst = 'game';
331           $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
332           
333
334           /* bg of caller cell */
335           $ret .= briscola_show($bri, $table, $user_cur);
336
337           /* first gamer */
338           if ($i == ($table->gstart % PLAYERS_N))
339             $ret .= "is_my_time = true; remark_on();";
340           else
341             $ret .= "is_my_time = false; remark_off();";
342
343           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
344           $user_cur->step++;        
345         }
346         /*
347             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
348             VAR PER PASSARE ALLA FASE DI GIOCO
349         */
350         
351       }
352     }
353   }
354 }
355 else if ($user->stat == 'table' && $user->subst == 'game') {
356   $table = &$bri->table[$user->table];
357
358   $retar = array();
359
360   log_wr($sess, "state: table::game".$argz[0]);
361
362   if ($argz[0] == 'logout') {
363     $bri->room_wakeup(&$user);
364   }
365   else if ($argz[0] == 'tableinfo') {
366     log_wr($sess, "PER DI TABLEINFO");
367     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
368     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
369
370     log_wr($sess, $user->comm[$user->step % COMM_N]);
371
372
373     $user->step++;
374   }
375   else if ($argz[0] == 'chatt') {
376     $bri->chatt_send(&$user,$mesg);
377   }
378   else if ($argz[0] == 'play') {
379     $a_play = $argz[1];
380     $a_x =    $argz[2];
381     $a_y =    $argz[3];
382
383     if (strpos($a_x, "px") != FALSE)
384       $a_x = substr($a_x,0,-2);
385     if (strpos($a_y, "px") != FALSE)
386       $a_y = substr($a_y,0,-2);
387
388     $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
389                      $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
390                      $table->mazzo, $table->gstart,
391                      $table->card[$a_play]->stat, $table->card[$a_play]->owner);
392     log_wr($sess, "CIC".$loggo);
393                           
394     /* se era il suo turno e la carta era sua ed era in mano */
395     if ($a_play >=0 && $a_play < 40 &&
396         ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
397         $table->card[$a_play]->stat == 'hand' &&
398         $table->card[$a_play]->owner == $user->table_pos) {
399       log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
400
401       /* Change the card status. */
402       $table->card[$a_play]->play($a_x, $a_y);
403
404       /*
405        *  !!!! TURN INCREMENTED BEFORE !!!!
406        */
407       $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
408       $table->turn++;
409
410       $card_play = sprintf("card_play(%d,%d,%d,%d);|",
411                            $user->table_pos, $a_play, $a_x, $a_y);
412       if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
413         $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
414
415         $player_cur = "remark_off();";
416         $player_nex = $card_play . "is_my_time = true; remark_on();";
417         $player_oth = $card_play;
418       }
419       else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
420         $winner = calculate_winner($table);
421         log_wr($sess,"GSTART 3");
422         $table->gstart = $winner;
423         $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
424
425         log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
426         $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
427                                                           $winner, $winner);
428         $player_cur = "remark_off();" . $card_take . "|"; 
429         if ($turn_cur != $turn_nex)
430           $player_nex = $card_play . $card_take . "|";
431         else
432           $player_nex = "";
433         if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
434           $player_nex .= "is_my_time = true; remark_on();";
435         $player_oth = $card_play . $card_take;
436       }
437
438       log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
439       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
440         $user_cur = &$bri->user[$table->player[$i]];
441
442         $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
443
444         
445         if ($i == $turn_cur) {
446           $ret .= $player_cur;    
447         }
448         if ($i == $turn_nex) {
449           $ret .= $player_nex;    
450         }
451         if ($i != $turn_cur && $i != $turn_nex) {
452           $ret .= $player_oth;
453         }
454
455         $retar[$i] = $ret;
456       }
457
458
459
460
461       if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
462         log_wr($sess, sprintf("GIOCO FINITO !!!"));
463
464         /* ************************************************ */
465         /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
466         /* ************************************************ */
467         calculate_points(&$table);
468
469         $table->game_next();
470         $table->game_init(&$bri);
471           
472         for ($i = 0 ; $i < PLAYERS_N ; $i++) {
473           $user_cur = &$bri->user[$table->player[$i]];
474           $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
475         }
476       }
477
478
479       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
480         $user_cur = &$bri->user[$table->player[$i]];
481         
482         $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
483         $user_cur->step++;          
484       }
485
486       log_wr($sess, sprintf("TURN: %d",$table->turn));
487       /* Have played all the players ? */
488       /* NO:  switch the focus and enable the next player to play. */
489       
490       /* YES: calculate who win and go to the next turn. */
491     }
492   }
493   else
494       log_wr($sess, "NOSENSE");
495
496 }
497 log_wr($sess, "before save data");
498 save_data($bri);
499
500 unlock_data($sem);
501 exit;
502 ?>