|
@@ -10,6 +10,12 @@ app.use(cors())
|
|
|
|
|
|
const rnd = max => Math.floor(Math.random() * max)
|
|
|
|
|
|
+const getSocketById = id => {
|
|
|
+ let namespace = null;
|
|
|
+ let ns = io.of(namespace || "/");
|
|
|
+ return ns.connected[id]
|
|
|
+}
|
|
|
+
|
|
|
class Card {
|
|
|
constructor(health, hit, price){
|
|
|
this.health = health;
|
|
@@ -35,7 +41,7 @@ class Card {
|
|
|
}
|
|
|
|
|
|
class Deck {
|
|
|
- constructor(config={count: 100, maxHealth: 10, maxHit: 10, priceGet=(health, hit) => health + hit}){
|
|
|
+ constructor(config={count: 100, maxHealth: 10, maxHit: 10, priceGet:(health, hit) => health + hit }){
|
|
|
const { count, maxHealth, maxHit, priceGet } = config;
|
|
|
|
|
|
this.deck = []
|
|
@@ -56,7 +62,9 @@ class Deck {
|
|
|
}
|
|
|
|
|
|
class Player {
|
|
|
- constructor(deck, startMoney=10, turnMoney=1){
|
|
|
+ constructor(id, deck, startMoney=10, turnMoney=1){
|
|
|
+ this.id = id
|
|
|
+ this.socket = getSocketById(id)
|
|
|
this.deck = deck
|
|
|
this.money = startMoney
|
|
|
this.turnMoney = turnMoney
|
|
@@ -65,11 +73,22 @@ class Player {
|
|
|
|
|
|
turn(){
|
|
|
this.money += this.turnMoney
|
|
|
+ this.turnMoney = this.turnMoney >= 10 ? 10 : this.turnMoney +1
|
|
|
+ this.cardToHand()
|
|
|
+ }
|
|
|
+
|
|
|
+ cardToHand(){
|
|
|
const card = this.deck.pop()
|
|
|
if (card){
|
|
|
this.hand.push(card)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ emitHand(){
|
|
|
+ if (!this.socket) return;
|
|
|
+
|
|
|
+ this.socket.emit('hand', this.hand)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
class Game {
|
|
@@ -77,7 +96,22 @@ class Game {
|
|
|
this.player1 = player1
|
|
|
this.player2 = player2
|
|
|
|
|
|
- this.turn = Math.random() > 0.5
|
|
|
+ this.currentPlayer = Math.random() > 0.5 ? player1 : player2
|
|
|
+ this.enemy = this.currentPlayer === player1 ? player2 : player1
|
|
|
+
|
|
|
+ this.currentPlayer.cardToHand()
|
|
|
+ this.currentPlayer.cardToHand()
|
|
|
+ this.currentPlayer.turn()
|
|
|
+
|
|
|
+ this.enemy.cardToHand()
|
|
|
+ this.enemy.cardToHand()
|
|
|
+ this.enemy.cardToHand()
|
|
|
+ this.enemy.turn()
|
|
|
+ }
|
|
|
+
|
|
|
+ emitHands(){
|
|
|
+ this.currentPlayer.emitHand()
|
|
|
+ this.enemy.emitHand()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -98,13 +132,21 @@ const gamers = new Proxy({}, {
|
|
|
|
|
|
io.on('connection', socket => {
|
|
|
const id = socket.id
|
|
|
-// gamers[id] = {};
|
|
|
+ let game;
|
|
|
|
|
|
socket.emit('id', id)
|
|
|
|
|
|
socket.on('hi', data => gamers[id] = {...gamers[id], nick: data.nick, ready: data.ready})
|
|
|
|
|
|
- socket.on('start', data => console.log('START', data))
|
|
|
+ socket.on('start', data => {
|
|
|
+ if (game){
|
|
|
+ socket.emit('error', {error: 'Game already in progress'})
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ console.log('START', data)
|
|
|
+ game = new Game(new Player(id, new Deck), new Player(data.with, new Deck))
|
|
|
+ game.emitHands()
|
|
|
+ })
|
|
|
|
|
|
|
|
|
socket.on('disconnect', () => {
|