<commit_before>'use strict'
/**
 * Dependencies
 */
const cwd = process.cwd()
const path = require('path')
const firebase = require('firebase')
const BaseHandler = require(path.join(cwd, 'handlers', 'BaseHandler'))
const User = require(path.join(cwd, 'models', 'User'))
const Game = require(path.join(cwd, 'models', 'Game'))
const Player = require(path.join(cwd, 'models', 'Player'))
const Secret = require(path.join(cwd, 'models', 'Secret'))

/**
 * Scope
 */
const scope = 'RegisterHandler'

/**
 * RegisterHandler
 */
class RegisterHandler extends BaseHandler {

  static handle (event) {
    let handler = new RegisterHandler(event)

    Promise
      .resolve({handler})
      .then(handler.validate)
      .then(handler.registerPlayer)
      .then(handler.mapGamePlayer)
      .then(handler.done)
      .catch(handler.internalServerError({
        scope
      }))
  }
    
  validate (opts) {
    let {handler} = opts

    if (!handler.event || !handler.event.subject || !handler.event.game) {
      return handler.error({
        scope,
        message: 'registration event must have subject and game fields'
      })
    }

    return new Promise ((resolve, reject) => {
      return Promise.all([
        Game.get(handler.event.game).loaded,
        User.get(handler.event.subject).loaded
      ]).then((game, user) => {
        if (!game || !game.val) {
          return handler.error({
            scope,
            message: `${handler.event.game} is an invalid game`
          })
        }

        if (!user || !user.val) {
          return handler.error({
            scope,
            message: `${handler.event.subject} is an invalid user`
          })
        }

        if (game.val.status !== 'registration') {
          return handler.error({
            scope,
            message: `${game.name} is not open for registration`
          })
        }

        resolve({
          handler,
          game,
          user
        })
      })
    })
  }

  registerPlayer (opts) {
    let {handler, game, user} = opts

    return new Promise((resolve, reject) => {
      return Player.get(handler.game.name, handler.user.uid).loaded
        .then((player) => {
          return new Promise((resolve, reject) => {
            Secret.create(player.uid)
              .then((secret) => {
                resolve({
                  secret,
                  player
                })
              })
          })
        })
        .then((opts) => {
          let {secret, player} = opts
          return player.create({
            game: game_name,
            uid: subject,
            display_name: user.val.username,
            picture: user.val.picture,
            game_state: 'human',
            secret: secret.val
          })
        }).then(() => {
          resolve({
            handler,
            game,
            user,
            player
          })
        }).catch((err) => {<commit_msg>fix(RegisterHandler): resolve registerPlayer promise with correct values<commit_after>'use strict'
/**
 * Dependencies
 */
const cwd = process.cwd()
const path = require('path')
const firebase = require('firebase')
const BaseHandler = require(path.join(cwd, 'handlers', 'BaseHandler'))
const User = require(path.join(cwd, 'models', 'User'))
const Game = require(path.join(cwd, 'models', 'Game'))
const Player = require(path.join(cwd, 'models', 'Player'))
const Secret = require(path.join(cwd, 'models', 'Secret'))

/**
 * Scope
 */
const scope = 'RegisterHandler'

/**
 * RegisterHandler
 */
class RegisterHandler extends BaseHandler {

  static handle (event) {
    let handler = new RegisterHandler(event)

    Promise
      .resolve({handler})
      .then(handler.validate)
      .then(handler.registerPlayer)
      .then(handler.mapGamePlayer)
      .then(handler.done)
      .catch(handler.internalServerError({
        scope
      }))
  }
    
  validate (opts) {
    let {handler} = opts

    if (!handler.event || !handler.event.subject || !handler.event.game) {
      return handler.error({
        scope,
        message: 'registration event must have subject and game fields'
      })
    }

    return new Promise ((resolve, reject) => {
      return Promise.all([
        Game.get(handler.event.game).loaded,
        User.get(handler.event.subject).loaded
      ]).then((game, user) => {
        if (!game || !game.val) {
          return handler.error({
            scope,
            message: `${handler.event.game} is an invalid game`
          })
        }

        if (!user || !user.val) {
          return handler.error({
            scope,
            message: `${handler.event.subject} is an invalid user`
          })
        }

        if (game.val.status !== 'registration') {
          return handler.error({
            scope,
            message: `${game.name} is not open for registration`
          })
        }

        resolve({
          handler,
          game,
          user
        })
      })
    })
  }

  registerPlayer (opts) {
    let {handler, game, user} = opts
    let _player
    let _secret

    return new Promise((resolve, reject) => {
      return Player.get(handler.game.name, handler.user.uid).loaded
        .then((player) => {
          return new Promise((resolve, reject) => {
            Secret.create(player.uid)
              .then((secret) => {
                _player = player
                _secret = secret
                resolve()
              })
          })
        })
        .then(() => {
          return _player.create({
            game: game_name,
            uid: subject,
            display_name: user.val.username,
            picture: user.val.picture,
            game_state: 'human',
            secret: _secret.val
          })
        }).then(() => {
          resolve({
            handler,
            game,
            user,
            player: _player,
            secret: _secret
          })
        }).catch((err) => {
{'fraction_non_alphanumeric': 0.13534468228552166, 'fraction_numerical': 0.0, 'mean_word_length': 1.7548746518105849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '330481', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1613, 'n_words': 379}