const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const path = require('path')
const ip = require('ip')

const Game = require('../common/Game.js')
const C = require('../common/constants.js')

app.use(express.static('public'))
app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, '..', 'public', 'index.html'))
})

const map = [
  '111111111111111',
  '100001110000001',
  '101000000111001',
  '101111111111101',
  '101111111111001',
  '100000000000001',
  '111111111111111'
].map((row) => row.split('').map(Number))

const game = new Game(map, true)
function tickAndSchedule () {
  game.tick()
  setTimeout(tickAndSchedule, Date.now() + C.TIME_STEP - game.lastTick)
}
tickAndSchedule()

io.on('connection', function (socket) {
  console.log(`${socket.client.id} connected`)

  socket.on('game:join', (debug) => game.onPlayerJoin(socket, debug))

  socket.on('player:events', (events, turnIndex) => {
    const shipId = game.getShipIdForSocket(socket)
    if (shipId == null) return // TO-DO: some kind of error sent to the client
    try {
      game.onPlayerEvents(shipId, events, turnIndex)
    } catch (e) {
      if (e instanceof C.InvalidTurnError) {
        console.log(`${socket.client.id} got lost`)
        game.bootstrapSocket(socket)
      }
    }
  })

  socket.on('player:lost', () => {
    console.log(`${socket.client.id} requesting bootstrap`)
    game.bootstrapSocket(socket)
  })

  socket.on('game:ping', () => socket.emit('game:pong', Date.now()))
})

const PORT = process.env.PORT || 3000
http.listen(PORT, function () {
  console.log(`listening on ${ip.address()}:${PORT}`)
})

const { TELEGRAM_TOKEN, TELEGRAM_CHAT_ID } = process.env
if (TELEGRAM_TOKEN) {
  const TelegramBot = require('node-telegram-bot-api')
  const bot = new TelegramBot(TELEGRAM_TOKEN)
  bot.sendMessage(TELEGRAM_CHAT_ID, 'server up! beep boop')
  process.on('exit', () => { bot.sendMessage(TELEGRAM_CHAT_ID, 'reb00ting') })
}

process.on('SIGTERM', () => {
  http.close(() => { process.exit(0) })
})

const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const path = require('path')
const ip = require('ip')

const Game = require('../common/Game.js')
const C = require('../common/constants.js')

app.use(express.static('public'))
app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, '..', 'public', 'index.html'))
})

const map = [
  '111111111111111',
  '100001110000001',
  '101000000111001',
  '101111111111101',
  '101111111111001',
  '100000000000001',
  '111111111111111'
].map((row) => row.split('').map(Number))

const game = new Game(map, true)
function tickAndSchedule () {
  game.tick()
  setTimeout(tickAndSchedule, Date.now() + C.TIME_STEP - game.lastTick)
}
tickAndSchedule()

io.on('connection', function (socket) {
  console.log(`${socket.client.id} connected`)

  socket.on('game:join', (debug) => game.onPlayerJoin(socket, debug))

  socket.on('player:events', (events, turnIndex) => {
    const shipId = game.getShipIdForSocket(socket)
    if (shipId == null) return // TO-DO: some kind of error sent to the client
    try {
      game.onPlayerEvents(shipId, events, turnIndex)
    } catch (e) {
      if (e instanceof C.InvalidTurnError) {
        console.log(`${socket.client.id} got lost`)
        game.bootstrapSocket(socket)
      }
    }
  })

  socket.on('player:lost', () => {
    console.log(`${socket.client.id} requesting bootstrap`)
    game.bootstrapSocket(socket)
  })

  socket.on('game:ping', () => socket.emit('game:pong', Date.now()))
})

const PORT = process.env.PORT || 3000
http.listen(PORT, function () {
  console.log(`listening on ${ip.address()}:${PORT}`)
})

const { TELEGRAM_TOKEN, TELEGRAM_CHAT_ID } = process.env
if (TELEGRAM_TOKEN) {
  const TelegramBot = require('node-telegram-bot-api')
  const bot = new TelegramBot(TELEGRAM_TOKEN)
  bot.sendMessage(TELEGRAM_CHAT_ID, 'server up! beep boop')
  process.on('beforeExit', () => {
    console.log('sending reb00t notification via telegram')
    bot.sendMessage(TELEGRAM_CHAT_ID, 'reb00ting')
  })
}

process.on('SIGTERM', () => {
  console.log('got SIGTERM')
  http.close(() => {
    console.log('gracefully exiting')
    process.exit(0)
  })
})

Add logs for graceful exit and use beforeExit