game related functions moved to briskin5
[brisk.git] / web / briskin5 / briskin5.js
1 /*
2  *  brisk - briskin5.js
3  *
4  *  Copyright (C) 2006-2008 Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it 
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program; if
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20  * Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * $Id$
23  *
24  */
25
26 function background_set()
27 {
28     $("bg").style.backgroundImage = 'url("img/brisk_table_sand'+table_pos+'.jpg")'; 
29 }
30
31 /* Stat: TABLE  Subst: ASTA */
32 function act_asta(card,pnt)
33 {
34     send_mesg("asta|"+card+"|"+pnt);
35 }
36
37 var asta_xarr = new Array(0,66,132);
38
39 /* TODO: impostare gli onclick */
40 function dispose_asta(idx, pnt, nopoint)
41 {
42     var i, btn, pass;
43     var btn;
44     var lng = langtolng(g_lang);
45
46     for (i = 0 ; i < 10 ; i++) {
47         btn = $("asta"+i);
48         if (i < idx) {
49             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
50             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
51             pass = -1;
52         }
53         else {
54             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+lng+".png";
55             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
56             pass = i;
57         }
58         if (i < 19)
59             btn.style.left = asta_xarr[i % 3];
60         else
61             btn.style.left = asta_xarr[(i+1) % 3];
62         
63         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
64
65         if (pnt >= 0) {
66             eval("btn.onclick = function () { act_asta("+pass+",61); }");
67             btn.style.cursor = "pointer";
68         }
69         else {
70             btn.onclick = null;
71             btn.style.cursor = "default";
72         }
73     }
74     
75     
76     btn = $("astaptdiv");
77     btn.style.left = asta_xarr[i % 3];
78     btn.style.top = parseInt(i / 3) * 50 - 2;
79     // btn.style.visibility  = "visible";
80     
81     btn = $("astapt");
82     var rpnt = (pnt < 0 ? -pnt : pnt);
83     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
84     
85     btn = $("astaptsub");
86     btn.style.left = asta_xarr[i % 3];
87     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
88     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+lng+".png";
89     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
90     if (pnt >= 0) {
91         btn.onclick = function () { act_asta(9,$("astapt").value); };
92         btn.style.cursor = "pointer";
93     }
94     else {
95         btn.onclick = null;
96         btn.style.cursor = "default";
97     }
98     
99     i+=1;
100     if (nopoint) {
101         btn = $("astapasso");
102         btn.style.left = asta_xarr[i % 3];
103         btn.style.top = parseInt(i / 3) * 50;
104         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+lng+".png";
105         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
106         if (pnt >= 0) {
107             btn.onclick = function () { act_asta(-1,0); };
108         }
109         else {          
110             btn.onclick = null;
111         }
112
113         btn = $("astalascio");
114         btn.style.left = asta_xarr[i % 3];
115         btn.style.top = parseInt(i / 3) * 50 + 24;
116         btn.src = "img/astalascio"+lng+".png";
117         btn.style.visibility = "visible";
118         btn.onclick = function () { safelascio(); };
119         }
120     else {
121         btn = $("astapasso");
122         btn.style.left = asta_xarr[i % 3];
123         btn.style.top = parseInt(i / 3) * 50;;
124         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
125         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
126         if (pnt >= 0) {
127             btn.onclick = function () { act_asta(-1,0); };
128         }
129         else {
130             btn.onclick = null;
131         }
132
133         btn = $("astalascio");
134         btn.style.visibility = "hidden";
135         btn.onclick = null;
136     }
137     // btn.style.visibility  = "visible";
138     $("asta").style.visibility = "visible";
139 }
140
141 function asta_pnt_set(pnt)
142 {
143     btn = $("astapt");
144     var rpnt = (pnt < 0 ? -pnt : pnt);
145     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
146 }
147
148 function hide_asta()
149 {
150     $("asta").style.visibility = "hidden"; 
151 }
152
153 function choose_seed(card)
154 {
155     var i;
156
157     $("asta").style.visibility = "hidden"; 
158     $("astalascio").style.visibility = "hidden"; 
159     $("chooseed").style.visibility = "visible";
160     for (i = 0 ; i < 4 ; i++) {
161         $("seed"+i).src = "img/"+i+""+card+".png";
162         seed=$("seed"+i);
163         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
164     }
165 }
166
167 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
168
169 function show_astat(zer,uno,due,tre,qua)
170 {
171     var astat = new Array(zer,uno,due,tre,qua);
172     var lng = langtolng(g_lang);
173
174     for (i = 0 ; i < PLAYERS_N ; i++) {
175         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
176
177         if (astat[i] == -2) {
178             $("public"+astat_suffix[idx]).style.visibility = "hidden";
179         }
180         else if (astat[i] == -1) {
181             $("public"+astat_suffix[idx]).style.visibility = "visible";
182             $("pubacard"+astat_suffix[idx]).src = "img/astapasso"+lng+".png";
183             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
184             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
185         }
186         else if (astat[i] <= 10) {
187             $("public"+astat_suffix[idx]).style.visibility = "visible";
188             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+lng+".png";
189             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
190         }
191         else if (astat[i] <= 120) {
192             $("public"+astat_suffix[idx]).style.visibility = "visible";
193             $("pubacard"+astat_suffix[idx]).src = "img/asta9"+lng+".png";
194             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
195             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
196         }
197     }
198 }
199
200
201 function table_init() {
202     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
203
204     // console.log("table_init");
205
206     remark_off();
207     $("asta").style.visibility = "hidden";
208     $("caller").style.visibility = "hidden";
209     show_astat(-2,-2,-2,-2,-2);
210     for (i=0 ; i < 8 ; i++) {
211         Drag.init($("card" + i), card_mouseup_cb);
212         for (e = 0 ; e < PLAYERS_N ; e++)
213             $("card"+sux[e]+i).style.visibility = "hidden";
214     }
215     for (i=0 ; i < PLAYERS_N ; i++) {
216         // console.log("shut: "+"takes"+sux[i]);
217         $("takes"+sux[i]).style.visibility = "hidden";
218         }
219
220     for (i = 0 ; i < 8 ; i++) {
221         cards_pos[i] = i;
222         cards_ea_pos[i] = i;
223         cards_ne_pos[i] = i;
224         cards_nw_pos[i] = i;
225         cards_we_pos[i] = i;
226     }
227
228 }
229   
230 function act_choose(card)
231 {
232     send_mesg("choose|"+card);
233 }
234
235 function act_play(card,x,y)
236 {
237     send_mesg("play|"+card+"|"+x+"|"+y);
238 }
239
240 function act_tableinfo()
241 {
242     send_mesg("tableinfo");
243 }
244
245 function act_exitlock()
246 {
247     send_mesg("exitlock");
248 }
249
250 function safelogout()
251 {
252     var res;
253     
254     if (g_exitlock < 2) 
255         res = window.confirm("Sei sicuro di volere abbandonare la partita?\nATTENZIONE: se esci adesso senza il consenso degli altri giocatori non potrai sederti ai tavoli per "+(Math.floor(EXIT_BAN_TIME/60))+" minuti.");    
256     else 
257         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
258     if (res)
259         act_logout(g_exitlock);
260 }
261
262 function act_reload()
263 {
264     window.onunload = null;
265     window.onbeforeunload = null;
266     document.location.reload();
267 }
268
269 function set_names(so,ea,ne,nw,we)
270 {
271 //    alert("EA: "+ea);
272     $("name").innerHTML = italizer(so);
273     $("name").title = unescapeHTML(so[1]); 
274     $("name_ea").innerHTML = italizer(ea);
275     $("name_ea").title = unescapeHTML(ea[1]);
276     $("name_ne").innerHTML = italizer(ne);
277     $("name_ne").title = unescapeHTML(ne[1]);
278     $("name_nw").innerHTML = italizer(nw);
279     $("name_nw").title = unescapeHTML(nw[1]);
280     $("name_we").innerHTML = italizer(we);
281     $("name_we").title = unescapeHTML(we[1]);
282
283     return;
284 }
285