-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcards.js
79 lines (59 loc) · 1.51 KB
/
cards.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var cards = (function() {
var $cardsD = $('.dealer-cards');
var $cardsP = $('.player-cards');
function getCardURL(hand, position){
var suit = hand[position].suit;
var value = hand[position].value;
var face_up = hand[position].extra.face_up;
var cardURL = mapToCardImg(face_up, value, suit);
return cardURL
}
function createCard(cardURL){
var $newCard = $('<img src="'+cardURL+'">');
return $newCard
}
// attached a back card image to respective hand
function showBack(actor){
var cardURL = 'img/back_dark.png';
var $newCard = createCard(cardURL);
appendCard($newCard, actor);
}
function openUp(actor, hand, position){
var cardURL = getCardURL(hand, position);
function flipUpDealer(){
$('.dealer-cards > img').eq(position).attr("src", cardURL);
}
function flipUpPlayer(){
$('.player-cards > img').eq(position).attr("src", cardURL);
}
if (actor === "dealer"){
setTimeout(flipUpDealer, 500);
} else {
setTimeout(flipUpPlayer, 500);
}
}
/// HELPERS ///
function mapToCardImg(face_up, value, suit){
if (face_up === true){
var cardName = suit+'_'+value+'.png';
var cardURL = 'img/' + cardName;
} else {
var cardURL = 'img/back_dark.png';
}
return cardURL
}
function appendCard(newCard, actor){
if (actor === "player"){
$cardsP.append(newCard);
} else {
$cardsD.append(newCard);
}
}
var module = {
"getCardURL": getCardURL,
"createCard": createCard,
"showBack": showBack,
"openUp": openUp
}
return module
})();