417cdf3675246125c708ac2e1dd1db705ae7fcbf
[brisk.git] / web / briskin5 / Obj / rules_old_rules.phh
1 <?php
2 /*
3  *  brisk - rules_old_rules.phh
4  *
5  *  Copyright (C) 2017 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  */
24
25 require_once("briskin5.phh");
26 require_once("rules_base.phh");
27
28 class Rules_old_rules extends Rules {
29     function __construct($table)
30     {
31         parent::__construct($table);
32         $this->id = 1;
33     }
34
35     static function asta2mult($asta_pnt)
36     {
37         if ($asta_pnt > 110)
38             return (6);
39         else if ($asta_pnt > 100)
40             return (5);
41         else if ($asta_pnt > 90)
42             return (4);
43         else if ($asta_pnt > 80)
44             return (3);
45         else if ($asta_pnt > 70)
46             return (2);
47         else
48             return (1);
49     }
50     static function s_multer($mult, $pnt)
51     {
52         return (pow(2, $mult) * static::asta2mult($pnt));
53     }
54
55     static function s_point_calc($pnt_done, $mult, $pnt_req, $is_allpoints)
56     {
57         return ($pnt_done * static::s_multer($mult, $pnt_req) * ($is_allpoints ? 2 : 1));
58     }
59
60     function multer($is_new)
61     {
62         if ($is_new) {
63             return (static::s_multer($this->table->mult, $this->table->asta_pnt));
64         }
65         else {
66             return (static::s_multer($this->table->old_mult, $this->table->old_asta_pnt));
67         }
68     }
69
70     static function game_result($asta_pnt, $pnt)
71     {
72         $sixty = (BIN5_PLAYERS_N == 3 ? 30 : 60);
73         if ($asta_pnt == 61) {
74             if ($pnt > $sixty)
75                 return (1);
76             else if ($pnt == $sixty)
77                 return (0);
78             else
79                 return (-1);
80         }
81         else {
82             if ($pnt >= $asta_pnt)
83                 return (1);
84             else
85                 return (-1);
86         }
87     }
88
89     function rules_asta(&$bri, $curtime, $action, $user, &$ret_s, $a_card, $a_pnt)
90     {
91         // Abbandono dell'asta
92         if ($a_card <= -1) {
93             log_wr("Abbandona l'asta.");
94             $this->table->asta_pla[$index_cur] = FALSE;
95             $user->asta_card  = -1;
96             $this->table->asta_pla_n--;
97
98             return TRUE;
99         }
100         else if ($a_card <= 9) {
101             $ret_s = "";
102
103             do {
104                 if ($a_card >= 0 && $a_card < 9 && $a_card > $this->table->asta_card)
105                     ;
106                 else if ($a_card == 9 && $a_pnt > ($this->table->asta_pnt >= 61 ? $this->table->asta_pnt : 60)
107                          && $a_pnt <= 120)
108                     ;
109                 else
110                     break;
111
112                 $user->asta_card  = $a_card;
113                 $this->table->asta_card = $a_card;
114                 if ($a_card == 9) {
115                     $user->asta_pnt   = $a_pnt;
116                     $this->table->asta_pnt  = $a_pnt;
117                 }
118
119                 return TRUE;
120             } while (0);
121         }
122
123         return FALSE;
124     }
125
126     function engine(&$bri, $curtime, $action, $user, &$opt1 = NULL, &$opt2 = NULL, &$opt3 = NULL)
127     {
128         GLOBAL $G_all_points, $G_dbasetype;
129
130         $table = $this->table;
131         $pnts_sav = array();
132
133         if ($action == BIN5_RULES_ASTA) {
134             return ($this->rules_asta($bri, $curtime, $action, $user, &$opt1, $opt2, $opt3));
135         }
136         if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
137             $table->old_act = $action;
138             $table->old_asta_win = -1;
139             $table->old_pnt = 0;
140             $table->mult_inc(1);
141             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
142                 $pnts_sav[$i] = 0;
143             }
144
145             $game_delta = 1;
146             // $table->game_next(1);
147             $table->game_init(&$bri->user);
148         }
149         else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
150             if (!($user->handpt <= 2)) {
151                 return (FALSE);
152             }
153             $table->old_act = $action;
154             log_wr(sprintf("GIOCO FINITO !!!"));
155             $table->old_asta_win = $user->table_pos;
156             $table->old_pnt = 0;
157             $table->mult_inc(1);
158
159             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
160                 $pnts_sav[$i] = 0;
161             }
162
163             // Non si cambia mazzo se si abbandona la partita
164             $game_delta = 0;
165             // $table->game_next(0);
166             $table->game_init(&$bri->user);
167         }
168         else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
169             $table->old_act = $action;
170             do {
171                 $pro = 0;
172
173                 if ($table->asta_pnt == 60)
174                     $table->asta_pnt = 61;
175
176                 $table->old_reason = "";
177
178                 // count points for the temporary 2 teams
179                 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
180                     $ctt = $table->card[$i]->value % 10;
181                     $own = $table->card[$i]->owner;
182                     if ($own == $table->asta_win || $own == $table->friend)
183                         $pro += $G_all_points[$ctt];
184                 }
185
186                 log_wr(sprintf("PRO: [%d]", $pro));
187
188                 // PATTA case !
189                 if (static::game_result($table->asta_pnt, $pro) == 0) {
190                     $table->points[$table->points_n % MAX_POINTS] = array();
191                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
192                         $table->points[$table->points_n % MAX_POINTS][$i] = 0;
193                         $pnts_sav[$i] = 0;
194                     }
195                     $table->points_n++;
196                     $table->old_pnt = $pro;
197                     $table->old_asta_win = $table->asta_win;
198                     $table->mult_inc(1);
199
200                     break;
201                     }
202
203                 if (static::game_result($table->asta_pnt, $pro) == 1)
204                     $sig = 1;
205                 else
206                     $sig = -1;
207
208                 // TAG: POINTS_MANAGEMENT
209                 $table->points[$table->points_n % MAX_POINTS] = array();
210                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
211                     if ($i == $table->asta_win)
212                         $pnt = ($i == $table->friend ? 4 : 2);
213                     else if ($i == $table->friend)
214                         $pnt = 1;
215                     else
216                         $pnt = -1;
217
218                     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pnt));
219
220                     $pnt_sav = static::s_point_calc($pnt * $sig,           0, $table->asta_pnt, ($pro == 120));
221                     $pnt_tab = static::s_point_calc($pnt * $sig, $table->mult, $table->asta_pnt, ($pro == 120));
222
223                     $table->points[$table->points_n % MAX_POINTS][$i] = $pnt_tab;
224                     $table->total[$i] += $pnt_tab;
225                     $pnts_sav[$i] = $pnt_sav;
226                 }
227                 $table->points_n++;
228                 $table->old_pnt = $pro;
229                 $table->old_asta_win = $table->asta_win;
230                 $table->mult_set(0);
231             } while (0);
232             $game_delta = 1;
233         }
234         else {
235             return (FALSE);
236         }
237         $table->game_next($game_delta);
238
239         $plist = "$table->table_token|$user->table_orig|$table->player_n";
240         $ucodes = array();
241         $codes = "";
242         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
243             $user_cur = &$bri->user[$table->player[$i]];
244
245             /* pro db */
246             $ucodes[$i] = $user_cur->code_get();
247
248             /* pro log */
249             $plist .= '|'.xcapelt($user_cur->name).'|'.$pnts_sav[$i];
250             $codes .= '|'.xcapelt($user_cur->code_get());
251         }
252         $plist .= $codes;
253         log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
254
255         $table->old_asta_pnt = $table->asta_pnt;
256         // $table->old_mazzo is managed by ->game_next();
257         // $table->old_mult, $table->old_pnt, $table->old_reason and $table->old_asta_win are specific
258
259         $table->old_friend = $table->friend;
260         $table->old_tourn_pts = $table->tourn_pts;
261
262         $table->old_reason = static::game_description($action, 'html', $table->old_mult,
263                                              $table->old_asta_win,
264                                              ($table->old_asta_win != -1 ?
265                                               $bri->user[$table->player[$table->old_asta_win]]->name : ""),
266                                              $table->old_friend,
267                                              ($table->old_friend != -1 ?
268                                               $bri->user[$table->player[$table->old_friend]]->name : ""),
269                                              $table->old_pnt, $table->old_asta_pnt, $table->old_tourn_pts);
270
271
272         if ($user->table_orig < TABLES_AUTH_N) {
273             require_once("../Obj/dbase_".$G_dbasetype.".phh");
274
275             if (($bdb = BriskDB::create()) != FALSE) {
276                 $bdb->bin5_points_save($curtime, $table, $user->table_orig, $action, $ucodes, $pnts_sav);
277                 unset($bdb);
278             }
279             else {
280                 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
281             }
282             log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
283         }
284
285         $table->game_init(&$bri->user);
286
287         return (TRUE);
288     }
289
290     static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
291                               $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
292     {
293         GLOBAL $G_lang, $mlang_bin5_rules;
294
295         if ($form == 'html') {
296             $tg_br = "<br>";
297             $tg_hr = "<hr>";
298             $tg_bo = "<b>";
299             $tg_bc = "</b>";
300             $win_name = xcape($win_name);
301             $fri_name = xcape($fri_name);
302         }
303         else {
304             $tg_br = " ";
305             $tg_hr = " ";
306             $tg_bo = "";
307             $tg_bc = "";
308         }
309
310         if ($act == BIN5_RULES_OLDSCHEMA) {
311             return ("");
312         }
313         else if ($act == BIN5_RULES_ALLPASS) {
314             return (sprintf($mlang_bin5_rules['info_alpa'][$G_lang],
315                             $tg_br, $tg_hr, $tg_bo, $tg_bc));
316         }
317         else if ($act == BIN5_RULES_ABANDON) {
318             return (sprintf($mlang_bin5_rules['info_aban'][$G_lang],
319                             $tg_br, $tg_hr, $tg_bo, $tg_bc,
320                             $win_name));
321         }
322         else {
323             $wol = static::game_result($old_asta_pnt, $old_pnt);
324
325             $noty = "";
326
327             if ($win != $fri) { // not alone case
328                 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'pi&ugrave; di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
329
330                 $noty .= sprintf($mlang_bin5_rules['info_part'][$G_lang],
331                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
332                                  $win_name,
333                                  $fri_name,
334                                  $old_tourn_pts);
335                 if ($old_pnt == 120) {
336                     $noty .= sprintf($mlang_bin5_rules['info_capp'][$G_lang],
337                                      $tg_br, $tg_hr, $tg_bo, $tg_bc );
338                 }
339                 else {
340                     if ($old_asta_pnt > 61) {
341                         $noty .= sprintf($mlang_bin5_rules['info_alea'][$G_lang],
342                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
343                                          $old_asta_pnt, $old_pnt,
344                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
345                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
346                                            $mlang_bin5_rules['info_lost'][$G_lang])));
347                     }
348                     else {
349                         $noty .= sprintf($mlang_bin5_rules['info_more'][$G_lang],
350                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
351                                          $old_pnt,
352                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
353                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
354                                            $mlang_bin5_rules['info_lost'][$G_lang])));
355                     } // else of if ($old_asta_pnt > 61) {
356                 } // else of if ($old_pnt == 120) {
357             } // if ($win != $fri) { // not alone case
358             else {
359                 $noty .= sprintf($mlang_bin5_rules['info_alon'][$G_lang],
360                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
361                                  $win_name,
362                                  $old_tourn_pts);
363                 if ($old_pnt == 120) {
364                     $noty .= sprintf($mlang_bin5_rules['info_acap'][$G_lang],
365                                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
366                 }
367                 else {
368                     if ($old_asta_pnt > 61) {
369                         $noty .= sprintf($mlang_bin5_rules['info_aleaa'][$G_lang],
370                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
371                                          $old_asta_pnt, $old_pnt,
372                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
373                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
374                                            $mlang_bin5_rules['info_lost'][$G_lang])));
375                     }
376                     else {
377                         $noty .= sprintf($mlang_bin5_rules['info_morea'][$G_lang],
378                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
379                                          $old_pnt,
380                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
381                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
382                                            $mlang_bin5_rules['info_lost'][$G_lang])));
383                     }
384                 }
385             }
386         }
387
388         $old_multer = static::s_multer($old_mult, $old_asta_pnt);
389         if ($old_multer > 1) {
390             $noty .= sprintf($mlang_bin5_rules['info_omul'][$G_lang],
391                              $tg_br, $tg_hr, $tg_bo, $tg_bc,
392                              multoval($old_multer));
393         }
394         $noty .= sprintf('%2$s', $tg_br, $tg_hr);
395
396         return ($noty);
397     }
398 } // class Rules_old_rules
399
400 ?>