Browse Source

Improvements.

feature/split
Nero Ignis 5 years ago
parent
commit
9fae0d6a61
  1. 3
      index.htm
  2. 83
      js/Blackjack.js
  3. 38
      js/CardDeck.js
  4. 135
      js/Hand.js
  5. 54
      js/LunaBank.js
  6. 137
      js/app.js

3
index.htm

@ -182,6 +182,9 @@ @@ -182,6 +182,9 @@
<script src="https://cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/alertify.min.js"></script>
<script src="https://kit.fontawesome.com/b54a4cceff.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="js/LunaBank.js"></script>
<script src="js/Blackjack.js"></script>
<script src="js/Hand.js"></script>
<script src="js/CardDeck.js"></script>
<script src="js/Card.js"></script>
<script src="js/vue.js"></script>

83
js/Blackjack.js

@ -1,11 +1,86 @@ @@ -1,11 +1,86 @@
class Blackjack {
deck;
bank;
bet;
roundActive = false;
constructor() {
this.deck = new CardDeck();
this.bank = new LunaBank();
constructor(vue) {
this.vue = vue;
this.deck = CardDeck.createAndShuffle(vue);
this.bank = LunaBank.createEmpty();
// this.bank = new LunaBank(vue);
this.dealer = Hand.createDealerHand();
this.player = Hand.create();
}
startRound() {
if (this.bet > this.bank.money) {
alertify.notify('Not enough money for bet.');
this.endRound();
}
this.bank.money -= this.bet;
this.roundActive = true;
this.player.pickCard(this.deck.takeOneCard());
this.dealer.pickCard(this.deck.takeOneCard());
this.player.pickCard(this.deck.takeOneCard());
this.dealer.pickCard(this.deck.takeOneCard());
if (this.dealer.hasBlackjack()) {
this.dealer.win();
}
if (this.player.points === 21) {
this.player.pass();
}
console.log('Round has started');
return true;
}
pass() {
if (this.dealer.points === this.player.points) {
this.roundActive = false;
alertify.notify('Draw!');
this.draw();
} else if (this.dealer.points > 21) {
this.roundActive = false;
alertify.notify('Dealer is plus, you win');
this.win();
} else if (this.dealer.points > this.player.points) {
this.roundActive = false;
alertify.notify('Dealer Wins');
} else if (this.dealer.points < 17) {
this.roundActive = false;
this.dealer.pickCard(this.deck.takeOneCard());
this.pass();
} else if (this.player.points > this.dealer.points && this.player.points < 22) {
this.roundActive = false;
alertify.notify('You win ' + this.bet * 1.5 + '!');
this.win();
}
}
draw() {
this.money = (parseInt(this.money) + parseInt(this.bet));
}
win() {
this.money += (parseInt(this.bet) * 2);
}
endRound() {
this.roundActive = false;
this.lastCard = null;
this.status = null;
this.dealersHand = [];
this.dealer.points = 0;
this.usersHand = [];
this.player.points = 0;
this.deck = CardDeck.createAndShuffle();
}
}

38
js/CardDeck.js

@ -1,17 +1,17 @@ @@ -1,17 +1,17 @@
class CardDeck {
cardBack;
cards;
constructor(cardBack = 'img/cardBack_red5.png') {
constructor(vue) {
this.vue = vue;
this.generateDeck();
this.cardBack = cardBack;
}
create() {
return new CardDeck();
static create(vue) {
return new CardDeck(vue);
}
createAndShuffle() {
let cardDeck = new CardDeck();
static createAndShuffle(vue) {
let cardDeck = new CardDeck(vue);
cardDeck.shuffle();
return cardDeck;
@ -52,44 +52,44 @@ class CardDeck { @@ -52,44 +52,44 @@ class CardDeck {
cardDeck.push(card);
});
this.deck = cardDeck;
this.cards = cardDeck;
})
}
shuffle(runs = 1) {
let cardCount = this.deck.length, t, i;
let cardCount = this.cards.length, t, i;
for (let run = 0; run === runs; run++) {
while (cardCount) {
i = Math.floor(Math.random() * cardCount--);
t = this.deck[cardCount];
this.deck[cardCount] = this.deck[i];
this.deck[i] = t;
t = this.cards[cardCount];
this.cards[cardCount] = this.cards[i];
this.cards[i] = t;
}
}
}
cardsInStack() {
return this.deck.length;
return this.cards.length;
}
deckIsEmpty() {
return this.deck.length > 0 ? true : false;
return this.cards.length > 0;
}
takeOneCard(cardsToDraw = 1) {
if (this.cardsInStack() > 0) {
takeOneCard() {
if (this.cardsInStack() < 0) {
return false;
}
return this.deck.shift();
return this.cards.shift();
}
takeCards(cardsToDraw) {
let drawnCards = [];
for (let run = 0; run === cardsToDraw; run++) {
drawnCards.push(this.deck.shift());
drawnCards.push(this.cards.shift());
}
return drawnCards;

135
js/Hand.js

@ -1,41 +1,36 @@ @@ -1,41 +1,36 @@
class Hand {
drawCardToUser() {
let vue = this;
cards;
points;
let drawnCard = vue.cards.shift();
vue.usersHand.push(drawnCard);
if (drawnCard.ace && vue.handScore > 10) {
drawnCard.points = 1;
}
if ((vue.handScore + drawnCard.points) > 21) {
let aceCount = this.handIncludesAces(this.usersHand);
console.log(aceCount + ' aces should be decreased in value');
}
constructor() {
this.cards = [];
}
vue.handScore += drawnCard.points;
static create() {
return new Hand();
}
drawCardToDealer() {
let vue = this;
static createDealerHand() {
let dealerHand = new Hand();
dealerHand.isDealer = true;
let drawnCard = vue.cards.shift();
vue.dealersHand.push(drawnCard);
return dealerHand;
}
if (drawnCard.ace && vue.dealerScore > 10) {
/**
* Adds given Card to the hand
* @param {Card} drawnCard
*/
pickCard(drawnCard) {
if (drawnCard.ace && this.points > 10) {
drawnCard.points = 1;
}
if ((vue.dealerScore + drawnCard.points) > 21) {
let aceCount = this.handIncludesAces(this.dealersHand);
console.log(aceCount + ' aces should be decreased in value');
}
vue.dealerScore += drawnCard.points;
this.cards.push(drawnCard);
this.points = (this.points + drawnCard.points);
}
handIncludesAces(hand) {
includesAces(hand) {
let aceCount = 0;
hand.forEach((card) => {
@ -47,91 +42,11 @@ class Hand { @@ -47,91 +42,11 @@ class Hand {
return aceCount;
}
handHasBlackJack(score) {
return score === 21;
}
isPlus(score) {
return score > 21;
}
startRound() {
if (this.bet > this.money) {
alertify.notify('Not enough money for bet.');
return false;
}
this.money -= this.bet;
this.initiated = true;
this.endRound();
this.roundActive = true;
this.drawCardToUser();
this.drawCardToDealer();
this.drawCardToUser();
this.drawCardToDealer();
if (this.handHasBlackJack(this.dealerScore)) {
this.dealerWon();
}
if (this.handScore === 21) {
this.pass();
}
}
drawAnotherCard() {
this.drawCardToUser();
if (this.isPlus(this.handScore)) {
alertify.notify('Hand is plus, you lose.')
this.roundActive = false;
}
hasBlackjack() {
return this.points === 21;
}
pass() {
if (this.dealerScore === this.handScore) {
this.roundActive = false;
alertify.notify('Draw!');
this.draw();
} else if (this.dealerScore > 21) {
this.roundActive = false;
alertify.notify('Dealer is plus, you win');
this.win();
} else if (this.dealerScore > this.handScore) {
this.roundActive = false;
alertify.notify('Dealer Wins');
} else if (this.dealerScore < 17) {
this.roundActive = false;
this.drawCardToDealer();
this.pass();
} else if (this.handScore > this.dealerScore && this.handScore < 22) {
this.roundActive = false;
alertify.notify('You win ' + this.bet * 1.5 + '!');
this.win();
}
}
draw() {
this.money = (parseInt(this.money) + parseInt(this.bet));
}
win() {
this.money += (parseInt(this.bet) * 2);
}
endRound() {
this.roundActive = false;
this.lastCard = null;
this.status = null;
this.dealersHand = [];
this.dealerScore = 0;
this.usersHand = [];
this.handScore = 0;
this.cards = this.generateDeck();
this.shuffleDeck();
isPlus() {
return this.points > 21;
}
}

54
js/LunaBank.js

@ -1,37 +1,45 @@ @@ -1,37 +1,45 @@
class LunaBank {
constructor(application) {
this._application = application;
}
money;
account;
connect() {
// TODO: Implement method
return false;
constructor(vue, application) {
this.vue = vue;
this.application = application;
this.money = 0;
this.account = 0;
this.connect();
}
get application() {
return this._application;
static createEmpty() {
let bank = new LunaBank(null, 'Testing');
bank.money = 1000;
bank.account = 5000;
return bank;
}
set application(application) {
this._application = application;
connect() {
// TODO: Implement method
return false;
}
// TODO: re-write methods
//
// checkUserStatus() {
// let vue = this;
// let bank = this;
//
// axios.get('https://luna-development.net/bank/get')
// .then(function (response) {
// vue.bank = Number(response.data.value);
// vue.username = response.data.name;
// vue.avatar = '/storage/avatars/' + response.data.avatar;
// vue.bankIsActive = true;
// vue.$forceUpdate;
//
// if (Number(vue.bank) <= Number(0)) {
// vue.money = 5000;
// bank.account = Number(response.data.value);
// bank.username = response.data.name;
// bank.avatar = '/storage/avatars/' + response.data.avatar;
// bank.isOnline = true;
// bank.$forceUpdate;
//
// if (Number(bank.account) <= Number(0)) {
// bank.money = 5000;
// } else {
// alertify.notify('Welcome back, ' + vue.username + '!', 'custom')
// alertify.notify('Welcome back, ' + bank.username + '!', 'custom')
// }
// })
// .catch(function (error) {
@ -39,8 +47,8 @@ class LunaBank { @@ -39,8 +47,8 @@ class LunaBank {
// alertify.notify("Can't connect to Luna Bank, are you logged in?", 'custom');
//
// // Removed default money here if bank empty / dead
// vue.username = 'Gast';
// vue.bankIsActive = false;
// bank.username = 'Gast';
// bank.isOnline = false;
// });
// }
//

137
js/app.js

@ -211,6 +211,143 @@ let app = new Vue({ @@ -211,6 +211,143 @@ let app = new Vue({
this.cards = this.generateDeck();
this.shuffleDeck();
},
checkUserStatus() {
let vue = this;
axios.get('https://luna-development.net/bank/get')
.then(function (response) {
vue.bank = Number(response.data.value);
vue.username = response.data.name;
vue.avatar = '/storage/avatars/' + response.data.avatar;
vue.bankIsActive = true;
vue.$forceUpdate;
if (Number(vue.bank) <= Number(0)) {
vue.money = 5000;
} else {
alertify.notify('Welcome back, ' + vue.username + '!', 'custom')
}
})
.catch(function (error) {
console.log(error);
alertify.notify("Can't connect to Luna Bank, are you logged in?", 'custom');
// Removed default money here if bank empty / dead
vue.username = 'Gast';
vue.bankIsActive = false;
});
},
updateBankAccount() {
let vue = this;
if (vue.bankIsActive) {
axios.get('https://luna-development.net/bank/get')
.then(function (response) {
vue.bank = Number(response.data.value);
vue.username = response.data.name;
vue.avatar = '/storage/avatars/' + response.data.avatar;
vue.$forceUpdate;
})
.catch(function (error) {
console.log(error);
vue.username = 'Gast';
vue.bankIsActive = false;
vue.avatar = null;
});
}
},
transferToBank() {
let vue = this;
if (vue.toBank <= vue.money && vue.toBank > 0) {
let bankSave = vue.bank;
let moneySave = vue.money;
let toBankSave = vue.toBank;
vue.bank = Number(vue.bank) + Number(vue.toBank);
vue.money = Number(vue.money) - Number(vue.toBank);
axios.post('/bank/store', {
value: vue.bank,
app: 'BlackJack',
description: 'Deposit',
amount: toBankSave
}).then(function (response) {
// i'm the master of self-advertisement
// speaking of advertisement? check out the fresh servers at luna-development.net
alertify.notify('Successfully transfered ' + toBankSave + ' $ to your account.', 'custom');
}).catch(function (error) {
vue.bank = bankSave;
vue.money = moneySave;
alertify.notify('Error while process bank-transaction.', 'custom');
});
vue.toBank = undefined;
} else if (vue.toBank === undefined) {
return;
} else {
vue.toBank = undefined;
}
},
transferFromBank() {
let vue = this;
// Number() all over again
if (vue.toMoney <= vue.bank && vue.toMoney > 0) {
let bankSave = vue.bank;
let moneySave = vue.money;
let toMoneySave = vue.toMoney;
vue.bank = Number(vue.bank) - Number(vue.toMoney);
vue.money = Number(vue.money) + Number(vue.toMoney);
axios.post('/bank/store', {
value: vue.bank,
app: 'BlackJack',
description: 'Withdrawal',
amount: toMoneySave
}).then(function (response) {
// i'm the master of self-advertisement, speaking of advertisement?
// check out the fresh servers on luna-development.net
alertify.notify('Successfully got ' + toMoneySave + ' $ from your account', 'custom');
}).catch(function (error) {
vue.bank = bankSave;
vue.money = moneySave;
alertify.notify('Error while process bank-transaction.', 'custom');
});
vue.toMoney = undefined;
} else if (vue.toMoney === undefined || vue.toMoney <= 0) {
return;
} else {
vue.toMoney = undefined;
}
},
bankButtonEnter(button, direction) {
let vue = this;
console.log('protocol: ' + direction);
if (direction === 'from') {
vue.transferFromBank();
} else if (direction === 'to') {
vue.transferToBank();
} else {
alertify.notify('Error while process bank-transaction.', 'custom');
return;
}
document.getElementById(button).click();
},
isMoney(number) {
return new Intl.NumberFormat('de-DE', {style: 'decimal'}).format(number);
},
},
created() {
this.cardDeck = this.generateDeck();

Loading…
Cancel
Save