diff --git a/src/is-room-visible/readme.md b/src/is-room-visible/readme.md index 5bb2d3d..c220197 100644 --- a/src/is-room-visible/readme.md +++ b/src/is-room-visible/readme.md @@ -13,5 +13,8 @@ import { isRoomVisible } from '@open-screeps/is-room-visible'; const observerTargets = Object.keys(Memory.rooms).filter(r => isRoomVisible(r)); ``` +## Related +- [is-simulation](https://github.com/PostCrafter/open-screeps/tree/master/src/is-simulation) + ## License [MIT](../../license.md) diff --git a/src/is-simulation/index.ts b/src/is-simulation/index.ts new file mode 100644 index 0000000..78fe0cb --- /dev/null +++ b/src/is-simulation/index.ts @@ -0,0 +1,7 @@ +import { isRoomVisible } from '@open-screeps/is-room-visible'; + +export const ROOM_SIMULATION = 'sim'; + +export function isSimulation() { + return isRoomVisible(ROOM_SIMULATION); +} diff --git a/src/is-simulation/package.json b/src/is-simulation/package.json new file mode 100644 index 0000000..4c26299 --- /dev/null +++ b/src/is-simulation/package.json @@ -0,0 +1,56 @@ +{ + "name": "@open-screeps/is-simulation", + "version": "0.0.0", + "description": "Check whether the code is running in simulation", + "main": "index.js", + "scripts": { + "test": "tsc && nyc ava", + "prepare": "npm test", + "release": "semantic-release" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/postcrafter/open-screeps.git" + }, + "files": [ + "index.{d.ts,js,js.map}" + ], + "keywords": [ + "screeps", + "open-screeps" + ], + "author": "Leo "PostCrafter" Friedrichs <dev@postcrafter.de>", + "license": "MIT", + "bugs": { + "url": "https://github.com/postcrafter/open-screeps/issues" + }, + "homepage": "https://github.com/postcrafter/open-screeps/src/is-simulation#readme", + "dependencies": { + "@open-screeps/is-room-visible": "*", + "@types/screeps": "^0.0.0" + }, + "devDependencies": { + "@semantic-release/git": "^2.0.1", + "ava": "^0.24.0", + "condition-circle": "^2.0.1", + "coveralls": "^3.0.0", + "nyc": "^11.3.0", + "semantic-release": "^11.0.2", + "semantic-release-monorepo": "^3.1.0", + "typescript": "^2.6.2" + }, + "publishConfig": { + "access": "public" + }, + "release": { + "verifyConditions": "condition-circle", + "analyzeCommits": "semantic-release-monorepo", + "generateNotes": "semantic-release-monorepo", + "getLastRelease": "semantic-release-monorepo", + "publish": [ + "@semantic-release/npm", + "@semantic-reelase/git", + "semantic-release-monorepo/github" + ] + } +} diff --git a/src/is-simulation/readme.md b/src/is-simulation/readme.md new file mode 100644 index 0000000..d1f30c4 --- /dev/null +++ b/src/is-simulation/readme.md @@ -0,0 +1,22 @@ +# is-simulation +> Check whether the code is running in simulation + +## Install +```sh +$ npm install @open-screeps/is-simulation +``` + +## Usage +```typescript +import { isSimulation } from '@open-screeps/is-simulation'; + +if (isSimulation()) { + console.log('Code loaded in simulation.'); +} +``` + +## Related +- [is-room-visible](https://github.com/PostCrafter/open-screeps/tree/master/src/is-room-visible) + +## License +[MIT](../../license.md) diff --git a/src/is-simulation/test.ts b/src/is-simulation/test.ts new file mode 100644 index 0000000..b2c47e0 --- /dev/null +++ b/src/is-simulation/test.ts @@ -0,0 +1,28 @@ +import test from 'ava'; + +import { isSimulation, ROOM_SIMULATION } from './index'; + +declare const global: any; + +function stubGame(...rooms: string[]) { + type RoomsStub = { + [roomName: string]: boolean; + }; + + global.Game = rooms.reduce((game, roomName) => { + game.rooms[roomName] = true; + return game; + }, { + rooms: {} as RoomsStub + }); +} + +test('Should return true when the room `ROOM_SIMULATION` is visible.', t => { + stubGame(ROOM_SIMULATION); + t.true(isSimulation()); +}); + +test('Should return false when the room `ROOM_SIMULATION` is not visible.', t => { + stubGame('tutorial'); + t.false(isSimulation()); +}); diff --git a/src/is-simulation/tsconfig.json b/src/is-simulation/tsconfig.json new file mode 100644 index 0000000..9536a0f --- /dev/null +++ b/src/is-simulation/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.base.json" +}