Ivan Asmer 5 éve
szülő
commit
0b20b8b28b
1 módosított fájl, 117 hozzáadás és 0 törlés
  1. 117 0
      index.js

+ 117 - 0
index.js

@@ -0,0 +1,117 @@
+const express = require('express');
+const cors    = require('cors')
+
+var app  = express()
+var http = require('http').Server(app);
+var io = require('socket.io')(http);
+
+app.use(express.static('public'))
+app.use(cors())
+
+const rnd = max => Math.floor(Math.random() * max)
+
+class Card {
+    constructor(health, hit, price){
+        this.health = health;
+        this.hit    = hit;
+        this.price  = price;
+        this.hitHistory = []
+    }
+
+    get alive(){
+        return this.health > 0
+    }
+
+    hitBy(enemy){ //hit to THIS from ENEMY
+        this.hitHistory.push({enemy, health: this.health})
+        this.health -= enemy.hit
+    }
+
+    static battle(card1, card2){
+        card2.hitBy(card1) //card1 -> card2
+        if (card2.alive) 
+            card1.hitBy(card2)
+    }
+}
+
+class Deck {
+    constructor(config={count: 100, maxHealth: 10, maxHit: 10, priceGet=(health, hit) => health + hit}){
+        const { count, maxHealth, maxHit, priceGet } = config;
+
+        this.deck = []
+        for (let i=0;i<count;i++){
+            let health = rnd(maxHealth)
+            let hit    = rnd(maxHit)
+            this.deck.push(new Card(health, hit, priceGet(health, hit)))
+        }
+    }
+
+    pop(){
+        return this.deck.pop()
+    }
+
+    get left(){
+        return this.deck.length
+    }
+}
+
+class Player {
+    constructor(deck, startMoney=10, turnMoney=1){
+        this.deck = deck
+        this.money = startMoney
+        this.turnMoney = turnMoney
+        this.hand  = []
+    }
+
+    turn(){
+        this.money += this.turnMoney
+        const card  = this.deck.pop()
+        if (card){
+            this.hand.push(card)
+        }
+    }
+}
+
+class Game {
+    constructor(player1, player2, config={deck: {count: 100, maxHealth: 10, maxHit: 10}}){
+        this.player1 = player1
+        this.player2 = player2
+
+        this.turn    = Math.random() > 0.5
+    }
+}
+
+const gamers = new Proxy({}, {
+    get(obj, id){
+        return obj[id]
+    },
+    set(obj, id, value){
+        console.log('SET', id, value)
+        if (!value) delete obj[id] 
+        else        obj[id] = value
+
+        console.log(obj)
+
+        io.emit('gamers', obj) //broadcast
+    }
+})
+
+io.on('connection', socket => {
+    const id = socket.id
+//    gamers[id] = {};
+    
+    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('disconnect', () => {
+        gamers[id] = null
+    })
+})
+
+http.listen(4000, function(){
+      console.log(`listening`);
+});