together17: add points constraint on the suit when auction is finished
[brisk.git] / web / briskin5 / Obj / rules_together17.phh
1 <?php
2 /*
3  *  brisk - rules_together17.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_together17 extends Rules_no_draw {
29     function __construct($table)
30     {
31         parent::__construct($table);
32         $this->id = 4;
33     }
34
35     function tourn_points(&$user, $user_pos)
36     {
37         $card = $this->table->card;
38         unset($user->asta_tourn_pts);
39         $user->asta_tourn_pts = array(0, 0, 0, 0);
40         if (BIN5_CARD_HAND == 8)
41             $tourn_values = array(11, 10, 4,3,2, 1,1,1,1,1);
42         else
43             $tourn_values = array(33, 30, 12,9,6, 3,3,3,3,3);
44
45         for ($i = 0 ; $i < BIN5_PLAYERS_N * BIN5_CARD_HAND ; $i++) {
46
47             if ($card[$i]->owner == $user_pos) {
48                 $user->asta_tourn_pts[(int)($i / 10)] += $tourn_values[$i % 10];
49             }
50         }
51     }
52
53     function tourn_points_max($user)
54     {
55         $mx = 0;
56         for ($i = 0 ; $i < 4 ; $i++) {
57             if ($user->asta_tourn_pts[$i] > $mx) {
58                 $mx = $user->asta_tourn_pts[$i];
59             }
60         }
61
62         return $mx;
63     }
64
65     function min_tourn_points($a_pnt)
66     {
67         if ($a_pnt < 71)
68             $min_pts = 13;
69         else if ($a_pnt < 80)
70             $min_pts = 19;
71         else if ($a_pnt < 90)
72             $min_pts = 23;
73         else if ($a_pnt < 100)
74             $min_pts = 25;
75         else
76             $min_pts = 28;
77
78         return $min_pts;
79     }
80
81     function check_tourn_points($hand_pts, $a_pnt, $a_seed, &$ret_s) {
82         $min_pts = $this->min_tourn_points($a_pnt);
83
84         if ($hand_pts < $min_pts) {
85             // FIXME_LANG
86             $seeds = array('coppe', 'ori', 'spade', 'bastoni');
87             $ret_s = sprintf("Hai chiamato un due di %s a %d ma in mano per quel seme hai %d punti-torneo, non puoi.",
88                              $seeds[$a_seed], $a_pnt, $hand_pts);
89             return FALSE;
90         }
91         return TRUE;
92     }
93
94     function rules_asta(&$bri, $curtime, $action, $user, &$ret_s, $a_card, $a_pnt) {
95         /*
96 Per le chiamate da 62 a 70 bisognerà avere 13 punti ( esempi.: asso e fante oppure tre
97 e cavallo oppure re, cavallo, fante, sette, sei, cinque, quattro) ;
98 Per chiamate da 71 ad 80 bisognerà avere 19 punti ( esempi.: asso, re, cavallo e 6
99 oppure tre, re, fante, 7, 6 e 5 );
100 Per chiamate da 81 a 90 bisognerà avere 23 punti ( esempi.: asso, tre e fante oppure
101 asso, re, cavallo, fante, 6, 5 e 4);
102 Per chiamate da 91 a 100 bisognerà avere 25 punti ( esempi.: asso, tre, re e 7 oppure
103 asso, tre, cavallo e fante );
104 Per chiamate da 101 a 120 bisognerà avere 28 punti ( esempi.:asso, tre, re e cavallo
105 oppure asso, tre, cavallo, 7, 6, 5 e 4 ).
106         */
107
108         $index_cur = $this->table->gstart % BIN5_PLAYERS_N;
109
110         // Abbandono dell'asta
111         if ($a_card <= -1) {
112             log_wr("Abbandona l'asta.");
113             $this->table->asta_pla[$index_cur] = FALSE;
114             $user->asta_card  = -1;
115             $this->table->asta_pla_n--;
116
117             if ($this->table->asta_card == -1 && $this->table->asta_pla_n == 1) {
118                 // all abandon auction except one => move next bid to two and 71 points
119                 $this->table->asta_card = 9;
120                 $this->table->asta_pnt = 70;
121             }
122
123             return TRUE;
124         }
125         else if ($a_card <= 9) {
126             $ret_s = "";
127
128             do {
129                 if ($a_card >= 0 && $a_card < 9 && $a_card > $this->table->asta_card) {
130                     ;
131                 }
132                 else if ($a_card == 9 &&
133                          $a_pnt > ($this->table->asta_pnt >= 61 ? $this->table->asta_pnt : 60)
134                          && $a_pnt <= 120) {
135                     $min_pts = $this->min_tourn_points($a_pnt);
136
137                     $max_hand_pts = $this->tourn_points_max($user);
138                     if ($max_hand_pts < $min_pts) {
139                         // FIXME_LANG
140                         $ret_s = sprintf("Hai chiamato un due a %d ma in mano hai al massimo %d punti-torneo, non puoi.",
141                                          $a_pnt, $max_hand_pts);
142                         break;
143                     }
144                 }
145                 else {
146                     break;
147                 }
148
149                 $user->asta_card  = $a_card;
150                 $this->table->asta_card = $a_card;
151                 if ($a_card == 9) {
152                     $user->asta_pnt   = $a_pnt;
153                     $this->table->asta_pnt  = $a_pnt;
154                 }
155
156                 return TRUE;
157             } while (0);
158         }
159
160         return FALSE;
161     }
162
163     function rules_nextauct(&$bri, $curtime, $action, $user, $maxcard)
164     {
165         if (($this->table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
166             !($this->table->asta_card == 9 && $this->table->asta_pnt == 120)) {
167             return TRUE;
168         }
169         else {
170             return FALSE;
171         }
172
173     }
174
175     function rules_checkchoose(&$bri, $curtime, $action, $user, &$ret_s, $a_brisco)
176     {
177         if (! parent::rules_checkchoose(&$bri, $curtime, $action, $user, &$ret_s, $a_brisco)) {
178             return FALSE;
179         }
180
181         if ($this->table->asta_card == 9) {
182             $bri_seed = (int)($a_brisco / 10);
183             return $this->check_tourn_points($user->asta_tourn_pts[$bri_seed],
184                                              $this->table->asta_pnt, $bri_seed, $ret_s);
185         }
186
187         return TRUE;
188     }
189 } // class Rules_no_draw
190
191 ?>