Skip to content

Commit

Permalink
fix: integrity bug when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
hexjelly committed Oct 29, 2018
1 parent fa7cee6 commit 10d4bc1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
47 changes: 30 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
40 changes: 40 additions & 0 deletions src/lev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 10d4bc1

Please sign in to comment.