<commit_before>Game = require '../models/Game'
apns = require '../src/apns_helper'
util = require 'util'

#hardcoded device ids for now
deviceTokens = 
  p1: "<KEY>" #justin
  p2: "b" #jeremy

module.exports.newGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  game = new Game
    nextTurn: req.body.deviceToken

  console.log "Saving Game to DB: #{util.inspect(game)}"
  game.save (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
    else
      json_response = JSON.stringify {game}

    res.writeHead status_code, {"Content-Type": "application/json"}
    console.log "Response: #{json_response}"
    res.end json_response

module.exports.getGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  console.log "Searching for Game with ID: #{req.params.gameID}"
  Game.findOne {_id: req.params.gameID}, (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
    else
      json_response = JSON.stringify {game}

    res.writeHead status_code, {"Content-Type": "application/json"}
    console.log "Response: #{json_response}"
    res.end json_response

module.exports.updateGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  console.log "Searching for Game with ID: #{req.params.gameID}"
  Game.findOne {_id: req.params.gameID}, (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
      res.writeHead status_code, {"Content-Type": "application/json"}
      console.log "Response: #{json_response}"
      res.end json_response
    else
      if req.body.deviceToken isnt game.nextTurn
        status_code = 500
        json_response = JSON.stringify {error: "It is not your turn, no cheatin!!!"}
        res.writeHead status_code, {"Content-Type": "application/json"}
        console.log "Response: #{json_response}"
        res.end json_response
      else
        console.log "Updating game with update: #{req.body.lastUpdate}"
        game.lastUpdate = req.body.lastUpdate

        console.log "Was '#{game.nextTurn}' turn"
        game.nextTurn = if game.nextTurn is deviceTokens.p1 then deviceTokens.p2 else deviceTokens.p1
        console.log "Now its '#{game.nextTurn}' turn"

        game.isGameOver = req.body.isGameOver unless req.body.isGameOver? is false

        game.save (error, game) ->
          if error
            status_code = 500 # server error
            json_response = JSON.stringify {error}
          else
            json_response = JSON.stringify {status: "ok"}

          apns.SendAPN(game.nextTurn, "Your turn loser!", 1)

          res.writeHead status_code, {"Content-Type": "application/json"}
          console.log "Response: #{json_response}"
          res.end json_response<commit_msg>error message now nested inside error like nodejs style<commit_after>Game = require '../models/Game'
apns = require '../src/apns_helper'
util = require 'util'

#hardcoded device ids for now
deviceTokens = 
  p1: "073af1914b43e524976062ab82d537556b356e5efd5a10ca4e28cf7d3db6fbb3" #justin
  p2: "b" #jeremy

module.exports.newGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  game = new Game
    nextTurn: req.body.deviceToken

  console.log "Saving Game to DB: #{util.inspect(game)}"
  game.save (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
    else
      json_response = JSON.stringify {game}

    res.writeHead status_code, {"Content-Type": "application/json"}
    console.log "Response: #{json_response}"
    res.end json_response

module.exports.getGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  console.log "Searching for Game with ID: #{req.params.gameID}"
  Game.findOne {_id: req.params.gameID}, (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
    else
      json_response = JSON.stringify {game}

    res.writeHead status_code, {"Content-Type": "application/json"}
    console.log "Response: #{json_response}"
    res.end json_response

module.exports.updateGame = (req, res) -> 
  console.log "#{req.method} to '#{req.url}' with body: #{util.inspect(req.body)}'"

  console.log "Searching for Game with ID: #{req.params.gameID}"
  Game.findOne {_id: req.params.gameID}, (error, game) ->
    status_code = 200 # ok
    if error
      status_code = 500 # server error
      json_response = JSON.stringify {error}
      res.writeHead status_code, {"Content-Type": "application/json"}
      console.log "Response: #{json_response}"
      res.end json_response
    else
      if req.body.deviceToken isnt game.nextTurn
        status_code = 500
        json_response = JSON.stringify {error: {message: "It is not your turn, no cheatin!!!"} }
        res.writeHead status_code, {"Content-Type": "application/json"}
        console.log "Response: #{json_response}"
        res.end json_response
      else
        console.log "Updating game with update: #{req.body.lastUpdate}"
        game.lastUpdate = req.body.lastUpdate

        console.log "Was '#{game.nextTurn}' turn"
        game.nextTurn = if game.nextTurn is deviceTokens.p1 then deviceTokens.p2 else deviceTokens.p1
        console.log "Now its '#{game.nextTurn}' turn"

        game.isGameOver = req.body.isGameOver unless req.body.isGameOver? is false

        game.save (error, game) ->
          if error
            status_code = 500 # server error
            json_response = JSON.stringify {error}
          else
            json_response = JSON.stringify {status: "ok"}

          apns.SendAPN(game.nextTurn, "Your turn loser!", 1)

          res.writeHead status_code, {"Content-Type": "application/json"}
          console.log "Response: #{json_response}"
          res.end json_response
{'fraction_non_alphanumeric': 0.1279910069054119, 'fraction_numerical': 0.016059097478721696, 'mean_word_length': 2.9694072657743784, 'pattern_counts': {'":': 10, '<': 4, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300911', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1939, 'n_words': 508}