diff --git a/CHANGELOG.md b/CHANGELOG.md index c255fc3..d869b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,17 +1,30 @@ -# Change Log -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](http://keepachangelog.com/) -and this project adheres to [Semantic Versioning](http://semver.org/). - -## \[1.0.2\]: 2017-02-05 -### Added -- Added `toBuffer` method for Replay class. - -## \[1.0.1\]: 2017-01-31 -### Changed -- NPM package now only contains the `src\` directory. - -## \[1.0.0\]: 2017-01-30 -### Added -- First package release. +# Change Log + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## \[1.0.3\]: 2018-10-29 + +### Fixed + +- Fix critical bug with missing integrity checks making all levels invalid. + +## \[1.0.2\]: 2017-02-05 + +### Added + +- Added `toBuffer` method for Replay class. + +## \[1.0.1\]: 2017-01-31 + +### Changed + +- NPM package now only contains the `src\` directory. + +## \[1.0.0\]: 2017-01-30 + +### Added + +- First package release. diff --git a/package.json b/package.json index 21c4dae..f0dfc8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-elma", - "version": "1.0.2", + "version": "1.0.3", "description": "Elasto Mania NPM package to read, modify and write Elma files", "main": "src/index.js", "scripts": { diff --git a/src/lev.js b/src/lev.js index 040917b..03be2a6 100644 --- a/src/lev.js +++ b/src/lev.js @@ -282,12 +282,52 @@ class Level { return output } + _calculateIntegrity() { + const pol_sum = this.polygons.reduce((accumulator, current) => { + return ( + accumulator + + current.vertices.reduce((accumulator, current) => { + return accumulator + current.x + current.y; + }, 0) + ); + }, 0); + + const obj_sum = this.objects.reduce((acc, val) => { + let obj_val = 0; + if (val.type === "exit") obj_val = 1; + else if (val.type === "apple") obj_val = 2; + else if (val.type === "killer") obj_val = 3; + else if (val.type === "start") obj_val = 4; + return acc + val.x + val.y + obj_val; + }, 0); + + const pic_sum = this.pictures.reduce((acc, val) => { + return acc + val.x + val.y; + }, 0); + + const sum = (pol_sum + obj_sum + pic_sum) * 3247.764325643; + + this.integrity = [ + sum, + this._getRandomInt(0, 5871) + 11877 - sum, + this._getRandomInt(0, 5871) + 11877 - sum, + this._getRandomInt(0, 6102) + 12112 - sum + ]; + } + + _getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min)) + min; + } + /** * Internal convinience method. * @private * @returns {Promise} */ _update () { + this._calculateIntegrity() return new Promise((resolve, reject) => { // figure out how big of a buffer to create since dynamic allocation is not a thing... let bufferSize = 850 // all known level attributes' size