<commit_before>//////////////////////////////////////////////////
//
//  grouple - devil.game.js
//
//  Purpose: Game logic for devil
//  Created: 2014.02.11
//
//////////////////////////////////////////////////

// Module dependencies
var async = require('async');

// Model
var Hero = require('../lib/model').Hero;
var Devil = require('../lib/model').Devil;
var City = require('../lib/model').City;
var Player = require('../lib/model').Player;
var Monster = require('../lib/model').Monster;

// Model Prototype
var ProtoHero = require('../lib/model').ProtoHero;
var ProtoSoldier = require('../lib/model').ProtoSoldier;
var ProtoMonster = require('../lib/model').ProtoMonster;

// Library
var errorHandler = require('../lib/errorHandler');
var common = require('../lib/common');

var getBattleResult = common.getBattleResult;
var getIntrudeResult = common.getIntrudeResult;
var getPointToUpdate = common.getPointToUpdate;
var getDamage = common.getDamage;

exports.purchase = function (req, res) {
  async.waterfall([
    function getPlayer (callback) {
      Player.findById(req.session.current_player_id, function (err, player) {
        if (err) throw err;

        if ( !player ) {
          callback('NO_PLAYER_FOUND');
          return;
        }

        callback(null, player);
        return;
      });
    },

    function getDevil (player, callback) {
      Devil.findById(player.devil_id, function (err, devil) {
        if (err) throw err;

        if ( !devil ) {
          callback('NO_DEVIL_FOUND');
          return;
        }

        callback(null, player, devil);
        return;
      });
    },

    function checkMonsterLimit (player, devil, callback) {
      Monster.find({ 'player_id': player._id }, function (err, monsters) {
        if (err) throw err;

        if ( devil.monster_limit <= monsters.length - 1 ) {
          callback('MONSTER_LIMIT_EXCEEDED');
          return;
        }

        callback(null, player, devil);
        return;
      });
    },

    function getProtoMonster (player, devil, callback) {
      ProtoMonster.findById(req.body.monster_id, function (err, protomonster) {
        if (err) throw err;

        if ( !protomonster ) {
          callback('NO_MONSTER_PROTOTYPE_FOUND');
          return;
        }

        callback(null, player, devil, protomonster);
        return;
      });
    },

    function payActionPoint (player, devil, protomonster, callback) {
      Devil.findByIdAndUpdate(devil._id, { $inc: { 'current_action_point': -1 } }, function (err, devil) {
        if (err) throw err;

        if ( !devil ) {
          callback('NO_DEVIL_FOUND');
          return;
        }

        callback(null, player, devil, protomonster);
        return;
      });
    },

    function payMonsterTraining (player, devil, protomonster, callback) {
      var update = {
        $inc: {
          'money': -protomonster.price
        }
      };

      if ( player.money < protomonster.price ) {
        callback('NOT_ENOUGH_MONEY');
        return;
      }

      Player.findByIdAndUpdate(player._id, update, function (err, player) {
        if (err) throw err;

        if ( !player ) {
          callback('NO_PLAYER_FOUND');
          return;
        }

        callback(null, player, devil, protomonster);
        return;
      });
    },

    function addToMonster (player, devil, protomonster, callback) {
      var monster = JSON.parse(JSON.stringify(protomonster));<commit_msg>Money and action point check routine added.<commit_after>//////////////////////////////////////////////////
//
//  grouple - devil.game.js
//
//  Purpose: Game logic for devil
//  Created: 2014.02.11
//
//////////////////////////////////////////////////

// Module dependencies
var async = require('async');

// Model
var Hero = require('../lib/model').Hero;
var Devil = require('../lib/model').Devil;
var City = require('../lib/model').City;
var Player = require('../lib/model').Player;
var Monster = require('../lib/model').Monster;

// Model Prototype
var ProtoHero = require('../lib/model').ProtoHero;
var ProtoSoldier = require('../lib/model').ProtoSoldier;
var ProtoMonster = require('../lib/model').ProtoMonster;

// Library
var errorHandler = require('../lib/errorHandler');
var common = require('../lib/common');

var getBattleResult = common.getBattleResult;
var getIntrudeResult = common.getIntrudeResult;
var getPointToUpdate = common.getPointToUpdate;
var getDamage = common.getDamage;

exports.purchase = function (req, res) {
  async.waterfall([
    function getPlayer (callback) {
      Player.findById(req.session.current_player_id, function (err, player) {
        if (err) throw err;

        if ( !player ) {
          callback('NO_PLAYER_FOUND');
          return;
        }

        callback(null, player);
        return;
      });
    },

    function getDevil (player, callback) {
      Devil.findById(player.devil_id, function (err, devil) {
        if (err) throw err;

        if ( !devil ) {
          callback('NO_DEVIL_FOUND');
          return;
        }

        callback(null, player, devil);
        return;
      });
    },

    function checkMonsterLimit (player, devil, callback) {
      Monster.find({ 'player_id': player._id }, function (err, monsters) {
        if (err) throw err;

        if ( devil.monster_limit <= monsters.length - 1 ) {
          callback('MONSTER_LIMIT_EXCEEDED');
          return;
        }

        callback(null, player, devil);
        return;
      });
    },

    function getProtoMonster (player, devil, callback) {
      ProtoMonster.findById(req.body.monster_id, function (err, protomonster) {
        if (err) throw err;

        if ( !protomonster ) {
          callback('NO_MONSTER_PROTOTYPE_FOUND');
          return;
        }

        callback(null, player, devil, protomonster);
        return;
      });
    },

    function payMonsterTraining (player, devil, protomonster, callback) {
      if ( player.money < protomonster.price ) {
        callback('NOT_ENOUGH_MONEY');
        return;
      }

      if ( devil.current_action_point < 1 ) {
        callback('NOT_ENOUGH_ACTION_POINT');
        return;
      }

      async.parallel([
        function payActionPoint (done) {
          Devil.findByIdAndUpdate(devil._id, { $inc: { 'current_action_point': -1 } }, function (err, devil) {
            if (err) throw err;

            if ( !devil ) {
              done('NO_DEVIL_FOUND');
              return;
            }

            done(null);
            return;
          });
        },

        function payMoney (done) {
          var update = {
            $inc: {
              'money': -protomonster.price
            }
          };

          Player.findByIdAndUpdate(player._id, update, function (err, player) {
            if (err) throw err;

            if ( !player ) {
              done('NO_PLAYER_FOUND');
              return;
            }

            done(null);
            return;
          });
        }
      ], function (err, result) {
        if (err) {
          callback(err);
          return;
        }

        callback(null, player, devil, protomonster);
        return;
      });
    },

    function addToMonster (player, devil, protomonster, callback) {
      var monster = JSON.parse(JSON.stringify(protomonster));
{'fraction_non_alphanumeric': 0.15776465742139228, 'fraction_numerical': 0.002883427159137718, 'mean_word_length': 1.9609756097560975, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, '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': '5413512', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2106, 'n_words': 457}