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