4 * Copyright (C) 2006-2012 Matteo Nastasi
5 * mailto: nastasi@alternativeoutput.it
6 * matteo.nastasi@milug.org
7 * web: http://www.alternativeoutput.it
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.
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.
23 /* number of players */
25 /* initial number of cards in hand */
32 var is_my_time = false;
33 /* number of takes cards */
49 var cards_width = 55 + 2;
50 var cards_width_d2 = 27;
52 /* height of images */
53 var cards_height = 101 + 2;
54 var cards_height_d2 = 51;
56 /* width of hands area */
57 var hands_width = 400;
59 /* width of the border */
60 var border_width = 10;
62 /* time to send a card to the player 10 or 250 */
63 var G_send_time = 250;
64 var G_play_time = 500;
65 var G_take_time = 500;
67 /* suffix to add to images name */
68 var sux = new Array( "", "_ea", "", "", "_we");
70 var cards_pos = new Array (CARD_HAND);
71 var cards_ea_pos = new Array (CARD_HAND);
72 var cards_ne_pos = new Array (CARD_HAND);
73 var cards_nw_pos = new Array (CARD_HAND);
74 var cards_we_pos = new Array (CARD_HAND);
76 var take_x = new Array(562, 745, 452, 30, 52);
77 var take_y = new Array(545, 177, 70, 62,155);
79 for (i = 0 ; i < CARD_HAND ; i++) {
86 function $(id) { return document.getElementById(id); }
88 function cards_dispose_so(car_n, tak_n)
92 $("takes").style.left = 200 + 400 - 90 + ((90 - cards_width) / 2);
93 $("takes").style.top = 475 + (125 - cards_height)/2;
94 $("takes").style.zIndex = 1;
95 $("takes").style.visibility = "visible";
101 disp = 400 - delta - (2 * border_width);
103 inter = parseInt((disp - cards_width) / (car_n - 1));
104 if (inter > cards_width)
109 wcard = cards_width + inter * (car_n - 1);
110 start = 200 + border_width + (400 - border_width - border_width - wcard - delta) / 2;
112 for (i = 0 ; i < car_n ; i++) {
113 $("card" + cards_pos[i]).style.left = start + (i * inter);
114 $("card" + cards_pos[i]).style.top = 475 + (125 - cards_height)/2;
115 $("card" + cards_pos[i]).style.zIndex = CARD_HAND - i;
116 $("card" + cards_pos[i]).style.visibility = "visible";
125 function cards_dispose_ne(car_n, tak_n) {
130 $("takes_ne").style.left = 800 - cards_width - 400 + 90 - ((90 - cards_width) / 2);
131 $("takes_ne").style.top = (125 - cards_height)/2;
132 $("takes_ne").style.zIndex = 1;
133 $("takes_ne").style.visibility = "visible";
139 disp = 400 - delta - (2 * border_width);
141 inter = parseInt((disp - cards_width) / (car_n - 1));
142 if (inter > cards_width)
147 wcard = cards_width + inter * (car_n - 1);
148 start = 800 - cards_width - border_width - (400 - border_width - border_width - wcard - delta) / 2;
151 for (i = 0 ; i < car_n ; i++) {
152 $("card_ne" + cards_ne_pos[i]).style.left = start - ((car_n - i -1) * inter);
153 $("card_ne" + cards_ne_pos[i]).style.top = (125 - cards_height)/2;
154 $("card_ne" + cards_ne_pos[i]).style.zIndex = 8-i;
155 $("card_ne" + cards_ne_pos[i]).style.visibility = "visible";
164 function cards_dispose_nw(car_n, tak_n) {
169 $("takes_nw").style.left = 400 - cards_width - 400 + 90 - ((90 - cards_width) / 2);
170 $("takes_nw").style.top = (125 - cards_height)/2;
171 $("takes_nw").style.zIndex = 1;
172 $("takes_nw").style.visibility = "visible";
178 disp = 400 - delta - (2 * border_width);
180 inter = parseInt((disp - cards_width) / (car_n - 1));
181 if (inter > cards_width)
186 wcard = cards_width + inter * (car_n - 1);
187 // start = 0 + delta + border_width + (400 - border_width - border_width - wcard - delta) / 2;
188 start = 400 - cards_width - border_width - (400 - border_width - border_width - wcard - delta) / 2;
191 for (i = 0 ; i < car_n ; i++) {
192 $("card_nw" + cards_nw_pos[i]).style.left = start - ((car_n-i-1) * inter);
193 $("card_nw" + cards_nw_pos[i]).style.top = (125 - cards_height)/2;
194 $("card_nw" + cards_nw_pos[i]).style.zIndex = (8-i);
195 $("card_nw" + cards_nw_pos[i]).style.visibility = "visible";
196 // alert("xx "+start + (i * inter)+" yy " + (125 - cards_height)/2);
205 function cards_dispose_ea(car_n, tak_n) {
208 $("takes_ea").style.left = 675 + (125 - cards_height)/2;
209 $("takes_ea").style.top = 125 + ((90 - cards_width) / 2);
210 $("takes_ea").style.zIndex = 1;
211 $("takes_ea").style.visibility = "visible";
216 disp = 400 - delta - (2 * border_width);
218 inter = parseInt((disp - cards_width) / (car_n - 1));
219 if (inter > cards_width)
224 wcard = cards_width + inter * (car_n - 1);
225 start = 125 + delta + border_width + (400 - border_width - border_width - wcard - delta) / 2;
228 // console.log(car_n);
229 for (i = 0 ; i < car_n ; i++) {
230 $("card_ea" + cards_ea_pos[i]).style.left = 675 + (125 - cards_height)/2;
231 $("card_ea" + cards_ea_pos[i]).style.top = start + (i * inter);
232 $("card_ea" + cards_ea_pos[i]).style.zIndex = CARD_HAND - i;
233 $("card_ea" + cards_ea_pos[i]).style.visibility = "visible";
234 // alert("xx "+ (675 + (125 - cards_height)/2) +" yy " + start + (i * inter));
243 function cards_dispose_we(car_n, tak_n)
247 $("takes_we").style.left = (125 - cards_height)/2;
248 // $("takes_we").style.top = 125 + 400 - 90 + ((90 - cards_width) / 2);
249 $("takes_we").style.top = 525 - cards_width - 400 + 90 - ((90 - cards_width) / 2);
250 $("takes_we").style.zIndex = 1;
251 $("takes_we").style.visibility = "visible";
256 /* pixel a disposizione per mettere le carte: 400 - delta - 2 bordi */
257 disp = 400 - delta - (2 * border_width);
258 /* se c'e' piu' di una carta calcola di quanti pixel devono rimanere scoperte le carte dopo la prima */
260 inter = parseInt((disp - cards_width) / (car_n - 1));
261 if (inter > cards_width)
267 /* dopo avere fatto tutti i conti ricalcola quanti pixel effettivamente verranno occupati dalle carte */
268 wcard = cards_width + inter * (car_n - 1);
270 /* calcola il punto d'inizio da dove disporre le carte: DELTAY + lo spessore del bordo + la meta' di quello
271 che resta della larghezza totale meno tutti gli altri ingombri */
272 // start = 125 + border_width + (400 - border_width - border_width - wcard - delta) / 2;
273 start = 525 - cards_width - border_width - (400 - border_width - border_width - wcard - delta) / 2;
275 for (i = 0 ; i < car_n ; i++) {
276 $("card_we" + cards_we_pos[i]).style.left = (125 - cards_height)/2;
277 $("card_we" + cards_we_pos[i]).style.top = start - (i * inter);
278 $("card_we" + cards_we_pos[i]).style.zIndex = CARD_HAND - i;
279 $("card_we" + cards_we_pos[i]).style.visibility = "visible";
288 var cards_dispose_arr = new Array(cards_dispose_so, cards_dispose_ea,
289 cards_dispose_ne, cards_dispose_nw,
292 function cards_dispose(player_pos, cards, takes)
294 var idx = (player_pos - table_pos + PLAYERS_N) % PLAYERS_N;
296 return (cards_dispose_arr[idx](cards,takes));
302 function card_mouseup_cb(o) {
303 var idx = o.id.substring(4);
304 var briskid = o.briskid;
310 /* case swap in the group */
313 if (parseInt(o.style.top) > 475 &&
314 parseInt(o.style.left) >= 200 && parseInt(o.style.left) < 600) {
315 /* Rearrange cards */
317 // $("sandbox3").innerHTML = "REARRANGE: "+idx;
324 /* found the associated index of the current card. */
325 for (i = 0 ; i < cards_n ; i++)
326 if (cards_pos[i] == idx)
330 disp = 400 - delta - (2 * border_width);
332 inter = parseInt((disp - cards_width) / (cards_n - 1));
333 if (inter > cards_width)
338 wcard = cards_width + inter * (cards_n - 1);
339 start = 200 + border_width + (400 - border_width - border_width - wcard - delta) / 2;
341 for (i = 0 ; i < cards_n ; i++) {
342 /* $("sandbox").innerHTML = */
343 // alert( "LEFT: " + o.style.left + "VALUE " + (cards_width + start + (i * (cards_width / 2))));
344 // $("sandbox3").innerHTML += "<br>LEFT: "+parseInt(o.style.left)+" START["+i+"]: "+(start + ((i+1) * inter));
346 tst = (parseInt(o.style.left) < start + ((i+1) * inter));
348 tst = (parseInt(o.style.left) > start + (i * inter));
350 // $("sandbox2").innerHTML = "old: " +old_idx+ " i: " +i+ "left: " + parseInt(o.style.left) + "comp: " + (start + (cards_width / 2) + (i * (cards_width / 2)));
356 for (e = old_idx ; e < i ; e++)
357 cards_pos[e] = cards_pos[e+1];
361 for (e = old_idx ; e > i ; e--)
362 cards_pos[e] = cards_pos[e-1];
369 cards_dispose_so(cards_n,takes_n);
373 else if (is_my_time &&
374 parseInt(o.style.top) >= 250 &&
375 parseInt(o.style.top) + cards_height < 450 &&
376 parseInt(o.style.left) >= 300 &&
377 (parseInt(o.style.left) + cards_width) < 500) {
380 $("sandbox2").innerHTML = "PLAYED";
382 for (i = 0 ; i < cards_n ; i++) {
383 if (cards_pos[i] == idx) {
384 /* $("sandbox").innerHTML = "Pippo: "+ i; */
385 for (e = i ; e < cards_n-1 ; e++) {
386 cards_pos[e] = cards_pos[e+1];
388 cards_pos[cards_n-1] = idx;
390 cards_dispose_so(cards_n, takes_n);
393 act_play(briskid,o.style.left,o.style.top);
397 cards_dispose_so(cards_n, takes_n);
402 $("sandbox2").innerHTML = "TO ORIGINAL";
403 /* alert("out card " + parseInt(o.style.top)); */
404 /* return to the original position */
405 cards_dispose_so(cards_n, takes_n);
412 function card_send_so(id,card,free,ct)
414 var img = $("card"+id);
415 img.src = getcard(-1,0);
418 img.style.left = 400 - cards_width_d2;
419 img.style.top = 300 - cards_height_d2;
420 img.style.zIndex = 100;
422 var movimg = new slowimg(img,400 - cards_width / 2,475 + (125 - cards_height)/2,25,free,"cards_dispose_so("+ct+", 0)",getcard(card,0));
423 movimg.settime(G_send_time);
427 function card_send_ea(id,card,free,ct)
429 var img = $("card_ea"+id);
430 img.src = getcard(card,1);
433 img.style.left = 400 - cards_height_d2;
434 img.style.top = 300 - cards_width_d2;
435 img.style.zIndex = 100;
437 var movimg = new slowimg(img,686,296,25,free,"cards_dispose_ea("+ct+", 0);",getcard(card,1));
438 movimg.settime(G_send_time);
442 function card_send_ne(id,card,free,ct)
444 var img = $("card_ne"+id);
445 img.src = getcard(card,2);
448 img.style.left = 400 - cards_width_d2;
449 img.style.top = 300 - cards_height_d2;
450 img.style.zIndex = 100;
452 var movimg = new slowimg(img,571,11,25,free,"cards_dispose_ne("+ct+", 0);",getcard(card,2));
453 movimg.settime(G_send_time);
457 function card_send_nw(id,card,free,ct)
459 var img = $("card_nw"+id);
460 img.src = getcard(card,3);
463 img.style.left = 400 - cards_width_d2;
464 img.style.top = 300 - cards_height_d2;
465 img.style.zIndex = 100;
467 var movimg = new slowimg(img,171,11,25,free,"cards_dispose_nw("+ct+", 0);",getcard(card,3));
468 movimg.settime(G_send_time);
472 function card_send_we(id,card,free,ct)
474 var img = $("card_we"+id);
475 img.src = getcard(card,4);
478 if (id < 0 || id > 39)
479 alert("ID ERRATO"+id);
481 img.style.left = 400 - cards_height_d2;
482 img.style.top = 300 - cards_width_d2;
483 img.style.zIndex = 100;
484 var movimg = new slowimg(img,11,296,25,free,"cards_dispose_we("+ct+", 0);",getcard(card,4));
485 movimg.settime(G_send_time);
489 var card_send_arr = new Array(card_send_so, card_send_ea,
490 card_send_ne, card_send_nw,
493 function card_send(player_pos,id,card,free,ct)
495 var idx = (player_pos - table_pos + PLAYERS_N) % PLAYERS_N;
497 card_send_arr[idx](id,card,free,ct);
500 function getcard(card,pos_id)
503 return ("img/cover"+sux[pos_id]+".png");
505 return ("img/0"+card+sux[pos_id]+".png");
507 return ("img/"+card+sux[pos_id]+".png");
510 function card_setours(zer,uno,due,tre,qua,cin,sei,set)
513 var arg = new Array(zer,uno,due,tre,qua,cin,sei,set);
515 for (i = 0 ; i < CARD_HAND ; i++) {
516 $("card"+i).src = getcard(arg[i], 0);
517 $("card"+i).briskid = arg[i];
523 function card_play_so(card_idx, x, y)
525 alert("card_play_so: unreachable function.");
529 function card_postplay_ea(card_pos)
531 var img = $("card_ea"+card_pos);
534 for (i = 0 ; i < cards_ea_n ; i++) {
535 if (cards_ea_pos[i] == card_pos) {
536 for (e = i ; e < cards_ea_n-1 ; e++) {
537 cards_ea_pos[e] = cards_ea_pos[e+1];
539 cards_ea_pos[cards_ea_n-1] = card_pos;
541 cards_dispose_ea(cards_ea_n, takes_ea_n);
547 function card_play_ea(card_idx, x, y)
549 // var card_pos = RANGE 0 <= x < cards_ea_n
550 var card_pos = rnd_int(0,cards_ea_n-1);
551 var img = $("card_ea"+cards_ea_pos[card_pos]);
552 // alert("IMMO CON "+cards_ea_pos[card_pos]);
553 var newname = getcard(card_idx,1);
556 x1 = 500 + ((y-250) * (125 - cards_height) / (200 - cards_height));
557 y1 = 450 - cards_width - (x - 300);
559 var movimg = new slowimg(img, x1, y1, 25, 1, "card_postplay_ea("+cards_ea_pos[card_pos]+");", newname);
560 movimg.settime(G_play_time);
565 function card_postplay_ne(obj,card_pos)
567 var img = $("card_ne"+card_pos);
571 for (i = 0 ; i < cards_ne_n ; i++) {
572 if (cards_ne_pos[i] == card_pos) {
573 for (e = i ; e < cards_ne_n-1 ; e++) {
574 cards_ne_pos[e] = cards_ne_pos[e+1];
576 cards_ne_pos[cards_ne_n-1] = card_pos;
578 cards_dispose_ne(cards_ne_n, takes_ne_n);
584 function card_play_ne(card_idx, x, y)
586 var card_pos = rnd_int(0,cards_ne_n-1);
587 var img = $("card_ne"+cards_ne_pos[card_pos]);
588 var newname = getcard(card_idx,2);
591 x1 = 600 - cards_width - (x - 300);
592 y1 = 250 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
594 var movimg = new slowimg(img, x1, y1, 25, 1, "card_postplay_ne(this,"+cards_ne_pos[card_pos]+");", newname);
595 movimg.settime(G_play_time);
600 function card_postplay_nw(card_pos)
602 var img = $("card_nw"+card_pos);
605 for (i = 0 ; i < cards_nw_n ; i++) {
606 if (cards_nw_pos[i] == card_pos) {
607 for (e = i ; e < cards_nw_n-1 ; e++) {
608 cards_nw_pos[e] = cards_nw_pos[e+1];
610 cards_nw_pos[cards_nw_n-1] = card_pos;
612 cards_dispose_nw(cards_nw_n, takes_nw_n);
618 function card_play_nw(card_idx, x, y)
620 var card_pos = rnd_int(0,cards_nw_n-1);
621 var img = $("card_nw"+cards_nw_pos[card_pos]);
622 var newname = getcard(card_idx,3);
625 x1 = 400 - cards_width - (x - 300);
626 y1 = 250 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
628 var movimg = new slowimg(img, x1, y1, 25, 1, "card_postplay_nw("+cards_nw_pos[card_pos]+");", newname);
629 movimg.settime(G_play_time);
634 function card_postplay_we(card_pos)
636 var img = $("card_we"+card_pos);
639 for (i = 0 ; i < cards_we_n ; i++) {
640 if (cards_we_pos[i] == card_pos) {
641 for (e = i ; e < cards_we_n-1 ; e++) {
642 cards_we_pos[e] = cards_we_pos[e+1];
644 cards_we_pos[cards_we_n-1] = card_pos;
646 cards_dispose_we(cards_we_n, takes_we_n);
652 function card_play_we(card_idx, x, y)
654 var card_pos = rnd_int(0,cards_we_n-1);
655 var img = $("card_we"+cards_we_pos[card_pos]);
656 var newname = getcard(card_idx,4);
659 x1 = 300 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
662 var movimg = new slowimg(img, x1, y1, 25, 1, "card_postplay_we("+cards_we_pos[card_pos]+");", newname);
663 movimg.settime(G_play_time);
667 var card_play_arr = new Array( card_play_so, card_play_ea, card_play_ne, card_play_nw, card_play_we);
669 /* card_play(player_pos, card_pos, card_idx, x, y)
670 player_pos - position of the player on the table
671 card_pos - position of the card in the hand of the player
672 card_idx - id of the card (to show it after the move)
673 x, y - coordinates of the card on the original table
675 orig 200x200 dest 200x125
677 function card_play(player_pos, card_idx, x, y)
679 var idx = (player_pos - table_pos + PLAYERS_N) % PLAYERS_N;
681 card_play_arr[idx](card_idx, x, y);
686 function card_place_so(card_pos, card_idx, x, y)
688 var img = $("card"+card_pos);
690 // alert("card_place_so"+card_pos);
694 img.style.visibility = "visible";
695 img.src = getcard(card_idx,0);
699 function card_place_ea(card_pos, card_idx, x, y)
701 var img = $("card_ea"+card_pos);
703 // alert("card_place_ea");
705 img.style.left = 500 + ((y-250) * (125 - cards_height) / (200 - cards_height));
706 img.style.top = 450 - cards_width - (x - 300);
707 img.style.visibility = "visible";
708 img.src = getcard(card_idx,1);
712 function card_place_ne(card_pos, card_idx, x, y)
714 var img = $("card_ne"+card_pos);
716 // alert("card_place_ne");
718 img.style.left = 600 - cards_width - (x - 300);
719 img.style.top = 250 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
720 img.style.visibility = "visible";
721 img.src = getcard(card_idx,2);
725 function card_place_nw(card_pos, card_idx, x, y)
727 var img = $("card_nw"+card_pos);
729 // alert("card_place_nw");
731 img.style.left = 400 - cards_width - (x - 300);
732 img.style.top = 250 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
733 img.style.visibility = "visible";
734 img.src = getcard(card_idx,3);
738 function card_place_we(card_pos, card_idx, x, y)
740 var img = $("card_we"+card_pos);
742 // alert("card_place_we");
744 img.style.left = 300 - cards_height - ((y-250) * (125 - cards_height) / (200 - cards_height));
745 img.style.top = 250 + x - 300;
746 img.style.visibility = "visible";
747 img.src = getcard(card_idx,4);
750 var card_place_arr = new Array( card_place_so, card_place_ea, card_place_ne, card_place_nw, card_place_we );
753 function card_place(player_pos, card_pos, card_idx, x, y)
755 var idx = (player_pos - table_pos + PLAYERS_N) % PLAYERS_N;
757 // alert("card_place"+idx);
759 card_place_arr[idx](card_pos, card_idx, x, y);
764 function card_post_take(card)
766 var img = $("card"+card);
767 img.style.visibility = "hidden";
768 cards_dispose_so(cards_n, takes_n);
771 function card_ea_post_take(card)
773 var img = $("card_ea"+card);
774 img.style.visibility = "hidden";
775 cards_dispose_ea(cards_ea_n, takes_ea_n);
778 function card_ne_post_take(card)
780 var img = $("card_ne"+card);
781 img.style.visibility = "hidden";
782 cards_dispose_ne(cards_ne_n, takes_ne_n);
785 function card_nw_post_take(card)
787 var img = $("card_nw"+card);
788 img.style.visibility = "hidden";
789 cards_dispose_nw(cards_nw_n, takes_nw_n);
792 function card_we_post_take(card)
794 var img = $("card_we"+card);
795 img.style.visibility = "hidden";
796 cards_dispose_we(cards_we_n, takes_we_n);
800 function cards_take(win)
802 var taker = (win - table_pos + PLAYERS_N) % PLAYERS_N;
804 // alert("cards_n: "+cards_n+"card: "+cards_pos[cards_n]+"cards_ea_n: "+cards_ea_n+"card_ea: "+cards_ea_pos[cards_ea_n]+"cards_ne_n: "+cards_ne_n+"card_ne: "+cards_ne_pos[cards_ne_n] + "taker:"+taker);
808 takes_n += PLAYERS_N; break;
810 takes_ea_n += PLAYERS_N; break;
812 takes_ne_n += PLAYERS_N; break;
814 takes_nw_n += PLAYERS_N; break;
816 takes_we_n += PLAYERS_N; break;
821 var img = $("card"+cards_pos[cards_n]);
822 var movimg = new slowimg(img,
823 take_x[taker] - cards_width_d2,
824 take_y[taker] - cards_height_d2,
825 25, 0, "card_post_take("+cards_pos[cards_n]+");", null);
826 movimg.settime(G_take_time);
829 var img = $("card_ea"+cards_ea_pos[cards_ea_n]);
830 var movimg = new slowimg(img,
831 take_x[taker] - cards_height_d2,
832 take_y[taker] - cards_width_d2,
833 25, 0, "card_ea_post_take("+cards_ea_pos[cards_ea_n]+");", null);
834 movimg.settime(G_take_time);
837 var img = $("card_ne"+cards_ne_pos[cards_ne_n]);
838 var movimg = new slowimg(img,
839 take_x[taker] - cards_width_d2,
840 take_y[taker] - cards_height_d2,
841 25, (PLAYERS_N == 3 ? 1 : 0), "card_ne_post_take("+cards_ne_pos[cards_ne_n]+");", null);
842 movimg.settime(G_take_time);
845 var img = $("card_nw"+cards_nw_pos[cards_nw_n]);
846 var movimg = new slowimg(img,
847 take_x[taker] - cards_width_d2,
848 take_y[taker] - cards_height_d2,
849 25, 0, "card_nw_post_take("+cards_nw_pos[cards_nw_n]+");", null);
850 movimg.settime(G_take_time);
853 var img = $("card_we"+cards_we_pos[cards_we_n]);
854 var movimg = new slowimg(img,
855 take_x[taker] - cards_height_d2,
856 take_y[taker] - cards_width_d2,
857 25, 1, "card_we_post_take("+cards_we_pos[cards_we_n]+");", null);
858 movimg.settime(G_take_time);