index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const express = require('express');
  2. const cors = require('cors')
  3. var app = express()
  4. var http = require('http').Server(app);
  5. var io = require('socket.io')(http);
  6. app.use(express.static('public'))
  7. app.use(cors())
  8. const rnd = max => Math.floor(Math.random() * max)
  9. const getSocketById = id => {
  10. let namespace = null;
  11. let ns = io.of(namespace || "/");
  12. return ns.connected[id]
  13. }
  14. class Card {
  15. constructor(health, hit, price){
  16. this.health = health;
  17. this.hit = hit;
  18. this.price = price;
  19. this.hitHistory = []
  20. }
  21. get alive(){
  22. return this.health > 0
  23. }
  24. hitBy(enemy){ //hit to THIS from ENEMY
  25. this.hitHistory.push({enemy, health: this.health})
  26. this.health -= enemy.hit
  27. }
  28. static battle(card1, card2){
  29. card2.hitBy(card1) //card1 -> card2
  30. if (card2.alive)
  31. card1.hitBy(card2)
  32. }
  33. }
  34. class Deck {
  35. constructor(config={count: 100, maxHealth: 10, maxHit: 10, priceGet:(health, hit) => health + hit }){
  36. const { count, maxHealth, maxHit, priceGet } = config;
  37. this.deck = []
  38. for (let i=0;i<count;i++){
  39. let health = rnd(maxHealth)
  40. let hit = rnd(maxHit)
  41. this.deck.push(new Card(health, hit, priceGet(health, hit)))
  42. }
  43. }
  44. pop(){
  45. return this.deck.pop()
  46. }
  47. get left(){
  48. return this.deck.length
  49. }
  50. }
  51. class Player {
  52. constructor(id, deck, startMoney=10, turnMoney=1){
  53. this.id = id
  54. this.socket = getSocketById(id)
  55. this.deck = deck
  56. this.money = startMoney
  57. this.turnMoney = turnMoney
  58. this.hand = []
  59. }
  60. turn(){
  61. this.money += this.turnMoney
  62. this.turnMoney = this.turnMoney >= 10 ? 10 : this.turnMoney +1
  63. this.cardToHand()
  64. }
  65. cardToHand(){
  66. const card = this.deck.pop()
  67. if (card){
  68. this.hand.push(card)
  69. }
  70. }
  71. emitHand(){
  72. if (!this.socket) return;
  73. this.socket.emit('hand', this.hand)
  74. }
  75. }
  76. class Game {
  77. constructor(player1, player2, config={deck: {count: 100, maxHealth: 10, maxHit: 10}}){
  78. this.player1 = player1
  79. this.player2 = player2
  80. this.currentPlayer = Math.random() > 0.5 ? player1 : player2
  81. this.enemy = this.currentPlayer === player1 ? player2 : player1
  82. this.currentPlayer.cardToHand()
  83. this.currentPlayer.cardToHand()
  84. this.currentPlayer.turn()
  85. this.enemy.cardToHand()
  86. this.enemy.cardToHand()
  87. this.enemy.cardToHand()
  88. this.enemy.turn()
  89. }
  90. emitHands(){
  91. this.currentPlayer.emitHand()
  92. this.enemy.emitHand()
  93. }
  94. }
  95. const gamers = new Proxy({}, {
  96. get(obj, id){
  97. return obj[id]
  98. },
  99. set(obj, id, value){
  100. console.log('SET', id, value)
  101. if (!value) delete obj[id]
  102. else obj[id] = value
  103. console.log(obj)
  104. io.emit('gamers', obj) //broadcast
  105. }
  106. })
  107. io.on('connection', socket => {
  108. const id = socket.id
  109. let game;
  110. socket.emit('id', id)
  111. socket.on('hi', data => gamers[id] = {...gamers[id], nick: data.nick, ready: data.ready})
  112. socket.on('start', data => {
  113. if (game){
  114. socket.emit('error', {error: 'Game already in progress'})
  115. return;
  116. }
  117. console.log('START', data)
  118. game = new Game(new Player(id, new Deck), new Player(data.with, new Deck))
  119. game.emitHands()
  120. })
  121. socket.on('disconnect', () => {
  122. gamers[id] = null
  123. })
  124. })
  125. http.listen(4000, function(){
  126. console.log(`listening`);
  127. });