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