index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const jwtSecret = 'AjnjLhjxM'
  7. const { verify } = require('jsonwebtoken')
  8. app.use(express.static('public'))
  9. app.use(cors())
  10. let gamers = []
  11. let cleanSocket = gamer => {
  12. let {socket, ...copy} = gamer
  13. return copy
  14. }
  15. let emitGamers = () => io.emit('gamers', gamers.map(cleanSocket))
  16. let thisGamer = s => gamers.filter(g => g.socket === s) [0]
  17. let gamerById = id => gamers.filter(g => g.id === id)[0]
  18. const dice = () => Math.ceil(Math.random()*6)
  19. const startBoard = () => [
  20. {color: 'white', count: 15},
  21. {color: 'white', count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0},
  22. {color: 'black', count: 15},
  23. { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0},
  24. ]
  25. const turnValidate = (before, dice, after) => {
  26. dice.sort()
  27. const from = []
  28. const to = []
  29. //count check:
  30. const moveCalc = (column, dice) => (column + dice) % 24;
  31. const countChecker = (arr) => {
  32. const stats = arr.reduce((a, b) => ((b.color && (a[b.color]+=b.count)), a), {white: 0, black: 0})
  33. console.log(stats)
  34. return 15 === stats.black && stats.white === 15;
  35. }
  36. if (!countChecker(before) || !countChecker(after)) return {error: '15'}
  37. let fromCount = 0;
  38. let toCount = 0;
  39. before.forEach( (column, i) => (column.count > after[i].count) && from.push(i) && (fromCount+=column.count - after[i].count))
  40. before.forEach( (column, i) => column.count < after[i].count && to.push(i) && (toCount-=column.count - after[i].count))
  41. console.log(fromCount, toCount, from, to)
  42. if (before[from[0]].color !== after[to[0]].color) return {error: 'colors wrong'}
  43. if (from.length === 2 && before[from[0]].color !== before[from[1]].color) return {error: 'colors wrong'}
  44. if (to.length === 2 && after[to[0]].color !== after[to[1]].color) return {error: 'colors wrong'}
  45. if (before[to[0]].color && before[to[0]].color !== after[to[0]].color) return {error: 'forbidden'}
  46. if (to[1] && before[to[1]].color && before[to[1]].color !== after[to[1]].color) return {error: 'forbidden'}
  47. if (fromCount === 2){
  48. if (from.length === 1 && to.length === 1){
  49. if (dice[0] !== dice[1] || moveCalc(from[0], dice[0]) !== to[0]) {return {error: 'error move two 1 to another 1'}}
  50. }
  51. if (from.length === 1 && to.length === 2){
  52. if (dice[0] === dice[1] || moveCalc(from[0], dice[0]) !== to[0] || moveCalc(from[0], dice[1]) !== to[1]) return {error: 'error move two 1 to another 2'}
  53. }
  54. if (from.length === 2 && to.length === 1){
  55. if (dice[0] === dice[1] || moveCalc(from[0], dice[1]) !== to[0] || moveCalc(from[1], dice[0]) !== to[0]) return {error: 'error move two 2 to another 1'}
  56. }
  57. if (from.length === 2 && to.length === 2){
  58. if (moveCalc(from[0], dice[0]) === to[0] && moveCalc(from[1], dice[1]) === to[1]) return {ok: 'ok'}
  59. if (moveCalc(from[0], dice[1]) === to[1] && moveCalc(from[1], dice[0]) === to[0]) return {ok: 'ok'}
  60. return {error: 'hz'}
  61. }
  62. return {ok: 'ok'}
  63. }
  64. if (fromCount === 1){
  65. if (moveCalc(from[0], dice[0] + dice[1]) !== to[0]) return {error: 'error dice sum'}
  66. let passThrough = [before[moveCalc(from[0], dice[0])], before[moveCalc(from[0], dice[1])]]
  67. if ((passThrough[0].color && passThrough[0].count && passThrough[0].color !== before[from[0]].color) ||
  68. (passThrough[1].color && passThrough[1].count && passThrough[1].color !== before[from[0]].color))
  69. return {error: 'forbidden passThrough'}
  70. return {ok: 'ok'}
  71. }
  72. return {error: 'count wrong'}
  73. //dice check
  74. }
  75. //console.log(turnValidate(startBoard(), [5,4], [
  76. //{color: 'white', count: 14},
  77. //{color: 'white', count: 0}, {color: 'white', count: 0}, { count: 0}, {color: 'black' ,count: 1}, {color: 'black', count: 1}, {color: 'white', count: 0}, { count: 0}, { count: 0}, {color:'white', count: 1}, { count: 0}, { count: 0},
  78. //{color: 'black', count: 13},
  79. //{ count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0}, { count: 0},
  80. //]))
  81. io.on('connection', socket => {
  82. socket.emit('hi', {id: socket.id})
  83. socket.on('disconnect', () => {
  84. gamers = gamers.filter(n => n.socket !== socket)
  85. emitGamers()
  86. });
  87. socket.on('conn', token => {
  88. const decoded = verify(token, jwtSecret)
  89. if (decoded){
  90. gamers.push({nick: decoded.sub.login, socket: socket, id: socket.id})
  91. }
  92. else {
  93. socket.emit("error", 'jwt failed')
  94. }
  95. emitGamers()
  96. })
  97. socket.on('newGame', msg => {
  98. let gamer = thisGamer(socket)
  99. gamer.newGame = msg.newGame
  100. emitGamers()
  101. })
  102. socket.on('startGame', ({id}) => {
  103. let gamer = thisGamer(socket)
  104. let enemy = gamerById(id)
  105. //console.log(id, gamers)
  106. if (enemy && enemy.newGame){
  107. gamer.inGameWith = enemy.id
  108. enemy.inGameWith = gamer.id
  109. gamer.turn = Math.random() > 0.5
  110. enemy.turn = !gamer.turn
  111. }
  112. gamer.socket.emit('startGame', cleanSocket(gamer))
  113. enemy.socket.emit('startGame', cleanSocket(enemy))
  114. const whoTurn = gamer.turn ? gamer : enemy
  115. whoTurn.socket.emit('turn', {board: startBoard(), dice: [dice(), dice()]})
  116. whoTurn.lastBoard = {board: startBoard(), dice: [dice(), dice()]}
  117. emitGamers()
  118. })
  119. socket.on('turn', data => {
  120. let gamer = thisGamer(socket)
  121. let enemy = gamerById(gamer.inGameWith)
  122. if (gamer && enemy &&
  123. gamer.inGameWith === enemy.id &&
  124. enemy.inGameWith === gamer.id &&
  125. gamer.turn){
  126. gamer.turn = !gamer.turn
  127. enemy.turn = !enemy.turn
  128. enemy.lastBoard = data
  129. data = {...data, dice: [dice(), dice()]}
  130. gamer.socket.emit('dice', data.dice)
  131. enemy.socket.emit('turn', data)
  132. }
  133. })
  134. socket.on('validate', data => {
  135. socket.emit('validate',{data, result: turnValidate(...data)})
  136. })
  137. });
  138. http.listen(4000, function(){
  139. console.log(`listening`);
  140. });