UNPKG

warriorjs-ja

Version:

Game written in JavaScript for learning JavaScript and artificial intelligence

253 lines (229 loc) 7.35 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _bluebird = require('bluebird'); var _bluebird2 = _interopRequireDefault(_bluebird); var _Config = require('./Config'); var _Config2 = _interopRequireDefault(_Config); var _Game = require('./Game'); var _Game2 = _interopRequireDefault(_Game); var _Tower = require('./Tower'); var _Tower2 = _interopRequireDefault(_Tower); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Profile = function () { function Profile() { _classCallCheck(this, Profile); this._towerPath = null; this._warriorName = null; this._score = 0; this._epic = false; this._epicScore = 0; this._currentEpicScore = 0; this._currentEpicGrades = {}; this._averageGrade = null; this._abilities = []; this._levelNumber = 0; this._lastLevelNumber = null; this._playerPath = null; } _createClass(Profile, [{ key: 'addAbilities', value: function addAbilities() { var _this = this; var abilities = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0]; abilities.forEach(function (newAbility) { if (_this.abilities.map(function (ability) { return ability.name; }).indexOf(newAbility.name) === -1) { _this.abilities.push(newAbility); } }); } }, { key: 'isEpic', value: function isEpic() { return this._epic; } }, { key: 'enableEpicMode', value: function enableEpicMode() { this._epic = true; this.epicScore = this.epicScore || 0; this.currentEpicScore = this.currentEpicScore || 0; this.lastLevelNumber = this.lastLevelNumber || this.levelNumber; } }, { key: 'enableNormalMode', value: function enableNormalMode() { this._epic = false; this.epicScore = 0; this.currentEpicScore = 0; this.currentEpicGrades = {}; this.averageGrade = null; this.levelNumber = this.lastLevelNumber; this.lastLevelNumber = null; } }, { key: 'calculateAverageGrade', value: function calculateAverageGrade() { var grades = Object.values(this.currentEpicGrades); return grades.length ? grades.reduce(function (sum, value) { return sum + value; }) / grades.length : null; } }, { key: 'updateEpicScore', value: function updateEpicScore() { if (this.currentEpicScore > this.epicScore) { this.epicScore = this.currentEpicScore; this.averageGrade = this.calculateAverageGrade(); } } }, { key: 'encode', value: function encode() { return new Buffer(JSON.stringify(this)).toString('base64'); } }, { key: 'save', value: function save() { this.updateEpicScore(); if (this.isEpic()) { this.levelNumber = 0; } return _fsExtra2.default.writeFileAsync(_path2.default.join(this.playerPath, '.profile'), this.encode()); } }, { key: 'toString', value: function toString() { return this.isEpic() ? this.warriorName + ' - ' + this.tower.name + ' - first score ' + this.score + ' - epic score ' + ('' + this.epicScoreWithGrade) : this.warriorName + ' - ' + this.tower.name + ' - level ' + this.levelNumber + ' - score ' + this.score; } }, { key: 'towerPath', get: function get() { return this._towerPath; }, set: function set(towerPath) { this._towerPath = towerPath; } }, { key: 'warriorName', get: function get() { return this._warriorName; }, set: function set(warriorName) { this._warriorName = warriorName; } }, { key: 'score', get: function get() { return this._score; }, set: function set(score) { this._score = score; } }, { key: 'epicScore', get: function get() { return this._epicScore; }, set: function set(score) { this._epicScore = score; } }, { key: 'currentEpicScore', get: function get() { return this._currentEpicScore; }, set: function set(score) { this._currentEpicScore = score; } }, { key: 'currentEpicGrades', get: function get() { return this._currentEpicGrades; }, set: function set(grades) { this._currentEpicGrades = grades; } }, { key: 'averageGrade', get: function get() { return this._averageGrade; }, set: function set(grade) { this._averageGrade = grade; } }, { key: 'epicScoreWithGrade', get: function get() { return this.averageGrade ? this.epicScore + ' (' + _Game2.default.getGradeLetter(this.averageGrade) + ')' : this.epicScore; } }, { key: 'abilities', get: function get() { return this._abilities; } }, { key: 'levelNumber', get: function get() { return this._levelNumber; }, set: function set(levelNumber) { this._levelNumber = levelNumber; } }, { key: 'lastLevelNumber', get: function get() { return this._lastLevelNumber; }, set: function set(levelNumber) { this._lastLevelNumber = levelNumber; } }, { key: 'playerPath', get: function get() { return this._playerPath || _path2.default.join(_Config2.default.pathPrefix, 'warriorjs', this.directoryName); }, set: function set(playerPath) { this._playerPath = playerPath; } }, { key: 'tower', get: function get() { return new _Tower2.default(this.towerPath); } }, { key: 'directoryName', get: function get() { return (this.warriorName + '-' + this.tower.name).toLowerCase().replace(/[^a-z0-9]+/, '-'); } }], [{ key: 'decode', value: function decode(encodedProfile) { try { return JSON.parse(new Buffer(encodedProfile, 'base64').toString()); } catch (err) { throw new Error('Invalid .profile file. Try changing the directory under which you are running warriorjs.'); } } }, { key: 'load', value: function load(profilePath) { return _fsExtra2.default.readFileAsync(profilePath, 'utf8').then(function (encodedProfile) { var profile = Object.assign(new Profile(), Profile.decode(encodedProfile)); profile.playerPath = _path2.default.dirname(profilePath); return _bluebird2.default.resolve(profile); }); } }]); return Profile; }(); exports.default = Profile;