123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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)
- const getSocketById = id => {
- let namespace = null;
- let ns = io.of(namespace || "/");
- return ns.connected[id]
- }
- 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(id, deck, startMoney=10, turnMoney=1){
- this.id = id
- this.socket = getSocketById(id)
- this.deck = deck
- this.money = startMoney
- this.turnMoney = turnMoney
- this.hand = []
- }
- 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 {
- constructor(player1, player2, config={deck: {count: 100, maxHealth: 10, maxHit: 10}}){
- this.player1 = player1
- this.player2 = player2
- 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()
- }
- }
- 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
- let game;
-
- socket.emit('id', id)
- socket.on('hi', data => gamers[id] = {...gamers[id], nick: data.nick, ready: data.ready})
- 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', () => {
- gamers[id] = null
- })
- })
- http.listen(4000, function(){
- console.log(`listening`);
- });
|