Browse Source

Improvements.

feature/split
Oliver Stingl 5 years ago
parent
commit
c25f32dd3a
  1. 3
      js/Blackjack.js
  2. 8
      js/Card.js
  3. 11
      js/CardDeck.js
  4. 137
      js/Hand.js
  5. 152
      js/LunaBank.js
  6. 137
      js/app.js

3
js/Blackjack.js

@ -2,7 +2,10 @@ class Blackjack { @@ -2,7 +2,10 @@ class Blackjack {
deck;
bank;
roundActive = false;
constructor() {
this.deck = new CardDeck();
this.bank = new LunaBank();
}
}

8
js/Card.js

@ -2,18 +2,20 @@ class Card { @@ -2,18 +2,20 @@ class Card {
color;
symbol;
points;
ace = false;
assetUrl;
constructor(color, symbol, points, ace, assetBaseUrl = 'img/') {
constructor(color, symbol, points, assetBaseUrl = 'img/') {
this.color = color;
this.symbol = symbol;
this.points = points;
this.ace = ace;
this.assetUrl = this.createAssetPath(assetBaseUrl);
}
createAssetPath(assetBaseUrl) {
return assetBaseUrl + this.color + this.symbol + '.png'
}
cardIsAnAce() {
return this.symbol === 'a';
}
}

11
js/CardDeck.js

@ -1,6 +1,9 @@ @@ -1,6 +1,9 @@
class CardDeck {
cardBack;
constructor() {
this.generateDeck();
this.cardBack = 'img/cardBack_red5.png';
}
create() {
@ -24,19 +27,16 @@ class CardDeck { @@ -24,19 +27,16 @@ class CardDeck {
for (let cardPoints = 1; cardPoints <= 10; cardPoints++) {
let symbol = cardPoints;
let points = cardPoints;
let isAnAce = false;
if (cardPoints === 1) {
points = 11;
symbol = 'a';
isAnAce = true;
}
let card = new Card(
color,
symbol,
points,
isAnAce
points
)
cardDeck.push(card);
@ -46,8 +46,7 @@ class CardDeck { @@ -46,8 +46,7 @@ class CardDeck {
let card = new Card(
color,
highCard,
10,
false
10
)
cardDeck.push(card);

137
js/Hand.js

@ -0,0 +1,137 @@ @@ -0,0 +1,137 @@
class Hand {
drawCardToUser() {
let vue = this;
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');
}
vue.handScore += drawnCard.points;
}
drawCardToDealer() {
let vue = this;
let drawnCard = vue.cards.shift();
vue.dealersHand.push(drawnCard);
if (drawnCard.ace && vue.dealerScore > 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;
}
handIncludesAces(hand) {
let aceCount = 0;
hand.forEach((card) => {
if (card.ace === true) {
aceCount++;
}
});
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;
}
}
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();
}
}

152
js/LunaBank.js

@ -1,5 +1,157 @@ @@ -1,5 +1,157 @@
class LunaBank {
constructor(application) {
this._application = application;
}
connect() {
// TODO: Implement method
return false;
}
get application() {
return this._application;
}
set application(application) {
this._application = application;
}
// TODO: re-write methods
//
// 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);
// },
}

137
js/app.js

@ -211,143 +211,6 @@ let app = new Vue({ @@ -211,143 +211,6 @@ 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