Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to

## [Unreleased]

## [0.14.0] - 2018-10-17

### Changed

- Simplify `@warriorjs/helper-get-level-score` ([@olistic][] in [#247][])
- Rename `@warriorjs/helper-get-play-score` package to
`@warriorjs/helper-get-level-score` ([@olistic][] in [#246][])

## [0.13.0] - 2018-10-09

### Added
Expand Down Expand Up @@ -233,7 +241,8 @@ whose logic can be reused by different flavors of the game.

Initial version.

[unreleased]: https://github.com/olistic/warriorjs/compare/v0.13.0...HEAD
[unreleased]: https://github.com/olistic/warriorjs/compare/v0.14.0...HEAD
[0.14.0]: https://github.com/olistic/warriorjs/compare/v0.13.0...v0.14.0
[0.13.0]: https://github.com/olistic/warriorjs/compare/v0.12.3...v0.13.0
[0.12.3]: https://github.com/olistic/warriorjs/compare/v0.12.2...v0.12.3
[0.12.2]: https://github.com/olistic/warriorjs/compare/v0.12.1...v0.12.2
Expand Down Expand Up @@ -322,3 +331,5 @@ Initial version.
[#236]: https://github.com/olistic/warriorjs/pull/236
[#238]: https://github.com/olistic/warriorjs/pull/238
[#240]: https://github.com/olistic/warriorjs/pull/240
[#246]: https://github.com/olistic/warriorjs/pull/246
[#247]: https://github.com/olistic/warriorjs/pull/247
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lerna": "2.9.0",
"version": "0.13.0",
"version": "0.14.0",
"command": {
"bootstrap": {
"ignore": "warriorjs-website"
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.6.1",
"eslint": "^5.9.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^3.1.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-prettier": "^3.0.0",
"eslint-plugin-react": "^7.11.1",
"husky": "^1.1.0",
"husky": "^1.2.0",
"jest": "^23.6.0",
"lerna": "^2.11.0",
"lint-staged": "^7.3.0",
"prettier": "1.14.3"
"lint-staged": "^8.1.0",
"prettier": "1.15.3"
},
"husky": {
"hooks": {
Expand Down
16 changes: 8 additions & 8 deletions packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Here is the list of packages with a brief description:
| ------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| [`@warriorjs/core`][warriorjs-core] | [![npm][warriorjs-core-badge]][warriorjs-core-npm] |
| [`@warriorjs/helper-get-level-config`][warriorjs-helper-get-level-config] | [![npm][warriorjs-helper-get-level-config-badge]][warriorjs-helper-get-level-config-npm] |
| [`@warriorjs/helper-get-play-score`][warriorjs-helper-get-play-score] | [![npm][warriorjs-helper-get-play-score-badge]][warriorjs-helper-get-play-score-npm] |
| [`@warriorjs/helper-get-level-score`][warriorjs-helper-get-level-score] | [![npm][warriorjs-helper-get-level-score-badge]][warriorjs-helper-get-level-score-npm] |
| [`@warriorjs/helper-get-grade-letter`][warriorjs-helper-get-grade-letter] | [![npm][warriorjs-helper-get-grade-letter-badge]][warriorjs-helper-get-grade-letter-npm] |
| [`@warriorjs/geography`][warriorjs-geography] | [![npm][warriorjs-geography-badge]][warriorjs-geography-npm] |
| [`@warriorjs/abilities`][warriorjs-abilities] | [![npm][warriorjs-abilities-badge]][warriorjs-abilities-npm] |
Expand All @@ -24,8 +24,8 @@ Here is the list of packages with a brief description:
- [`@warriorjs/helper-get-level-config`][warriorjs-helper-get-level-config]
exposes a helper function to get the config for a level defined in a tower, to
use as input for the functions exposed by the core.
- [`@warriorjs/helper-get-play-score`][warriorjs-helper-get-play-score] exposes
a helper function to get the score for a play.
- [`@warriorjs/helper-get-level-score`][warriorjs-helper-get-level-score]
exposes a helper function to get a level's score.
- [`@warriorjs/helper-get-grade-letter`][warriorjs-helper-get-grade-letter]
exposes a helper function to get the letter for a numeric grade.
- [`@warriorjs/helper-get-level-config`][warriorjs-helper-get-level-config]
Expand Down Expand Up @@ -69,11 +69,11 @@ universes (levels, abilities and units) to the game.
https://img.shields.io/npm/v/@warriorjs/helper-get-level-config.svg?style=flat-square
[warriorjs-helper-get-level-config-npm]:
https://www.npmjs.com/package/@warriorjs/helper-get-level-config
[warriorjs-helper-get-play-score]: /packages/warriorjs-helper-get-play-score
[warriorjs-helper-get-play-score-badge]:
https://img.shields.io/npm/v/@warriorjs/helper-get-play-score.svg?style=flat-square
[warriorjs-helper-get-play-score-npm]:
https://www.npmjs.com/package/@warriorjs/helper-get-play-score
[warriorjs-helper-get-level-score]: /packages/warriorjs-helper-get-level-score
[warriorjs-helper-get-level-score-badge]:
https://img.shields.io/npm/v/@warriorjs/helper-get-level-score.svg?style=flat-square
[warriorjs-helper-get-level-score-npm]:
https://www.npmjs.com/package/@warriorjs/helper-get-level-score
[warriorjs-helper-get-grade-letter]: /packages/warriorjs-helper-get-grade-letter
[warriorjs-helper-get-grade-letter-badge]:
https://img.shields.io/npm/v/@warriorjs/helper-get-grade-letter.svg?style=flat-square
Expand Down
12 changes: 6 additions & 6 deletions packages/warriorjs-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@warriorjs/cli",
"version": "0.13.0",
"version": "0.14.0",
"description": "WarriorJS command line",
"author": "Matias Olivera <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -47,24 +47,24 @@
"mock-fs": "^4.7.0"
},
"dependencies": {
"@warriorjs/core": "^0.13.0",
"@warriorjs/core": "^0.14.0",
"@warriorjs/helper-get-grade-letter": "^0.11.1",
"@warriorjs/helper-get-level-config": "^0.12.3",
"@warriorjs/helper-get-play-score": "^0.11.1",
"@warriorjs/helper-get-level-score": "^0.14.0",
"@warriorjs/tower-baby-steps": "^0.13.0",
"ansi-escapes": "^3.0.0",
"array-shuffle": "^1.0.1",
"chalk": "^2.4.1",
"delay": "^4.0.1",
"delay": "^4.1.0",
"ejs": "^2.6.1",
"find-up": "^3.0.0",
"globby": "^8.0.1",
"inquirer": "^6.2.0",
"inquirer": "^6.2.1",
"inquirer-prompt-suggest": "^0.1.0",
"lodash.uniqby": "^4.7.0",
"resolve": "^1.8.1",
"string-width": "^2.1.1",
"superheroes": "^2.0.0",
"yargs": "^12.0.2"
"yargs": "^12.0.5"
}
}
19 changes: 12 additions & 7 deletions packages/warriorjs-cli/src/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';

import globby from 'globby';
import getLevelConfig from '@warriorjs/helper-get-level-config';
import getPlayScore from '@warriorjs/helper-get-play-score';
import getLevelScore from '@warriorjs/helper-get-level-score';
import { getLevel, runLevel } from '@warriorjs/core';

import GameError from './GameError';
Expand Down Expand Up @@ -275,15 +275,15 @@ class Game {
printLevel(level);

const playerCode = this.profile.readPlayerCode();
const playResult = runLevel(levelConfig, playerCode);
const levelResult = runLevel(levelConfig, playerCode);

if (!this.silencePlay) {
await printPlay(playResult.events, this.delay);
await printPlay(levelResult.events, this.delay);
}

printSeparator();

if (!playResult.passed) {
if (!levelResult.passed) {
printFailureLine(
`Sorry, you failed level ${levelNumber}. Change your script and try again.`,
);
Expand Down Expand Up @@ -316,9 +316,14 @@ class Game {
);
}

const score = getPlayScore(playResult, levelConfig);
printLevelReport(this.profile, score);
this.profile.tallyPoints(levelNumber, score);
const scoreParts = getLevelScore(levelResult, levelConfig);
const totalScore = Object.values(scoreParts).reduce(
(sum, value) => sum + value,
0,
);
const grade = (totalScore * 1.0) / levelConfig.aceScore;
printLevelReport(this.profile, scoreParts, totalScore, grade);
this.profile.tallyPoints(levelNumber, totalScore, grade);

if (this.profile.isEpic()) {
if (!hasNextLevel && !this.practiceLevel) {
Expand Down
2 changes: 1 addition & 1 deletion packages/warriorjs-cli/src/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Profile {
* @param {number} levelNumber The number of the level.
* @param {Object} score The score of the play.
*/
tallyPoints(levelNumber, { total: totalScore, grade }) {
tallyPoints(levelNumber, totalScore, grade) {
if (this.isEpic()) {
this.currentEpicGrades[levelNumber] = grade;
this.currentEpicScore += totalScore;
Expand Down
4 changes: 2 additions & 2 deletions packages/warriorjs-cli/src/Profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('Profile', () => {

test('tallies the points by adding to the score', () => {
profile.score = 0;
profile.tallyPoints(1, { total: 123 });
profile.tallyPoints(1, 123);
expect(profile.score).toBe(123);
});

Expand Down Expand Up @@ -342,7 +342,7 @@ describe('Profile', () => {
test('tallies the points by adding to the current epic score and grades', () => {
profile.epicScore = 0;
profile.currentEpicGrades = {};
profile.tallyPoints(1, { total: 124, grade: 0.8 });
profile.tallyPoints(1, 124, 0.8);
expect(profile.currentEpicScore).toBe(124);
expect(profile.currentEpicGrades['1']).toBe(0.8);
});
Expand Down
19 changes: 9 additions & 10 deletions packages/warriorjs-cli/src/ui/printLevelReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import printTotalScore from './printTotalScore';
* Prints the level report.
*
* @param {Profile} profile The profile.
* @param {Object} score The score of the play.
* @param {number} score.warrior The points earned by the warrior.
* @param {number} score.timeBonus The time bonus.
* @param {number} score.clearBonus The clear bonus.
* @param {number} score.total The total score.
* @param {Object} scoreParts The score components.
* @param {number} scoreParts.warrior The points earned by the warrior.
* @param {number} scoreParts.timeBonus The time bonus.
* @param {number} scoreParts.clearBonus The clear bonus.
* @param {number} totalScore The total score.
* @param {number} grade The grade.
*/
function printLevelReport(
profile,
{
parts: { warrior: warriorScore, timeBonus, clearBonus },
total: totalScore,
grade,
},
{ warrior: warriorScore, timeBonus, clearBonus },
totalScore,
grade,
) {
printLine(`Warrior Score: ${warriorScore}`);
printLine(`Time Bonus: ${timeBonus}`);
Expand Down
20 changes: 9 additions & 11 deletions packages/warriorjs-cli/src/ui/printLevelReport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ const profile = {
};

const score = {
parts: {
warrior: 3,
timeBonus: 2,
clearBonus: 0,
},
total: 5,
grade: 0.9,
warrior: 3,
timeBonus: 2,
clearBonus: 0,
};
const totalScore = 5;
const grade = 0.9;

test('prints level score', () => {
printLevelReport(profile, score);
printLevelReport(profile, score, totalScore, grade);
expect(printLine).toHaveBeenCalledWith('Warrior Score: 3');
expect(printLine).toHaveBeenCalledWith('Time Bonus: 2');
expect(printLine).toHaveBeenCalledWith('Clear Bonus: 0');
});

test('prints regular score if not epic', () => {
printLevelReport(profile, score);
printLevelReport(profile, score, totalScore, grade);
expect(printTotalScore).toHaveBeenCalledWith(3, 5);
});

Expand All @@ -42,13 +40,13 @@ describe('epic', () => {
});

test('prints epic score', () => {
printLevelReport(profile, score);
printLevelReport(profile, score, totalScore, grade);
expect(printTotalScore).toHaveBeenCalledWith(2, 5);
});

test('prints level grade', () => {
getGradeLetter.mockReturnValue('A');
printLevelReport(profile, score);
printLevelReport(profile, score, totalScore, grade);
expect(printLine).toHaveBeenCalledWith('Level Grade: A');
expect(getGradeLetter).toHaveBeenCalledWith(0.9);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/warriorjs-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@warriorjs/core",
"version": "0.13.0",
"version": "0.14.0",
"description": "WarriorJS core",
"author": "Matias Olivera <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/warriorjs-core/src/runLevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import loadLevel from './loadLevel';
* @param {Object} levelConfig The config of the level.
* @param {string} playerCode The code of the player.
*
* @returns {Object} The result of the play.
* @returns {Object} The result of the level.
*/
function runLevel(levelConfig, playerCode) {
const level = loadLevel(levelConfig, playerCode);
Expand Down
16 changes: 16 additions & 0 deletions packages/warriorjs-helper-get-level-score/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# @warriorjs/helper-get-level-score

> WarriorJS helper to get the level score from the result of running player code
> through that level.

## Install

```sh
npm install @warriorjs/helper-get-level-score
```

## Usage

```js
import getLevelScore from '@warriorjs/helper-get-level-score';
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@warriorjs/helper-get-play-score",
"version": "0.11.1",
"name": "@warriorjs/helper-get-level-score",
"version": "0.14.0",
"description": "WarriorJS helper to get the score for a play",
"author": "Matias Olivera <[email protected]>",
"license": "MIT",
"homepage": "https://warrior.js.org",
"repository": "https://github.com/olistic/warriorjs/tree/master/packages/warriorjs-helper-get-play-score",
"repository": "https://github.com/olistic/warriorjs/tree/master/packages/warriorjs-helper-get-level-score",
"main": "lib/index.js",
"files": [
"lib"
Expand Down
31 changes: 31 additions & 0 deletions packages/warriorjs-helper-get-level-score/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import getClearBonus from './getClearBonus';
import getRemainingTimeBonus from './getRemainingTimeBonus';
import getWarriorScore from './getWarriorScore';

/**
* Returns the score for the given level.
*
* @param {Object} result The level result.
* @param {boolean} result.passed Whether the level was passed or not.
* @param {Object[][]} result.events The events of the level.
* @param {Object} levelConfig The level config.
* @param {number} levelConfig.timeBonus The bonus for passing the level in time.
*
* @returns {Object} The score of the level, broken down into its components.
*/
function getLevelScore({ passed, events }, { timeBonus }) {
if (!passed) {
return null;
}

const warriorScore = getWarriorScore(events);
const remainingTimeBonus = getRemainingTimeBonus(events, timeBonus);
const clearBonus = getClearBonus(events, warriorScore, remainingTimeBonus);
return {
clearBonus,
timeBonus: remainingTimeBonus,
warrior: warriorScore,
};
}

export default getLevelScore;
Loading