diff --git a/.gitignore b/.gitignore index 7c4441c..a9b7e78 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ yarn.lock *.js *.js.map .nyc_output/ +node_modules/ \ No newline at end of file diff --git a/src/is-my-room/index.ts b/src/is-my-room/index.ts new file mode 100644 index 0000000..59de9da --- /dev/null +++ b/src/is-my-room/index.ts @@ -0,0 +1,21 @@ +import { isRoomVisible } from '@open-screeps/is-room-visible'; + +export function isMyRoom(roomName: string | Room): boolean { + let room: Room; + + if (typeof roomName === 'string') { + if (!isRoomVisible(roomName)) { + return false; + } + + room = Game.rooms[roomName]; + } else { + room = roomName; + } + + if (room.controller === undefined) { + return false; + } + + return room.controller.my; +} diff --git a/src/is-my-room/package.json b/src/is-my-room/package.json new file mode 100644 index 0000000..64ceb61 --- /dev/null +++ b/src/is-my-room/package.json @@ -0,0 +1,49 @@ +{ + "name": "@open-screeps/is-my-room", + "version": "0.0.0-development", + "description": "Check wether a room is yours", + "main": "index.js", + "scripts": { + "lint": "tslint -p ./tsconfig.json", + "test": "tsc && nyc ava", + "prepare": "npm test", + "release": "semantic-release -e semantic-release-monorepo" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/postcrafter/open-screeps.git" + }, + "files": [ + "index.{d.ts,js,js.map}" + ], + "keywords": [ + "screeps", + "open-screeps" + ], + "author": "Adam Laycock", + "license": "MIT", + "bugs": { + "url": "https://github.com/postcrafter/open-screeps/issues" + }, + "homepage": "https://github.com/postcrafter/open-screeps/src/is-my-room#readme", + "dependencies": { + "@types/screeps": "^0.0.0", + "@open-screeps/is-room-visible": "^1.0.0" + }, + "devDependencies": { + "ava": "^0.24.0", + "condition-circle": "^2.0.1", + "nyc": "^11.3.0", + "semantic-release": "^11.0.2", + "semantic-release-monorepo": "^4.0.0", + "tslint": "^5.9.1", + "tslint-config-airbnb": "^5.4.2", + "typescript": "^2.6.2" + }, + "publishConfig": { + "access": "public" + }, + "release": { + "verifyConditions": "condition-circle" + } +} diff --git a/src/is-my-room/readme.md b/src/is-my-room/readme.md new file mode 100644 index 0000000..d3168ca --- /dev/null +++ b/src/is-my-room/readme.md @@ -0,0 +1,20 @@ +# is-my-room +> Check wether a room is yours + +## Install +```sh +$ npm install @open-screeps/is-my-room +``` + +## Usage +```typescript +import { isMyRoom } from '@open-screeps/is-my-room'; + +const myRooms = _.filter(Game.rooms, room => isMyRoom(room)) +``` + +## 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-my-room/test.ts b/src/is-my-room/test.ts new file mode 100644 index 0000000..55445f2 --- /dev/null +++ b/src/is-my-room/test.ts @@ -0,0 +1,35 @@ +import ava from 'ava'; + +import { isMyRoom } from './index'; + +declare const global: any; + +function stubGame() { + global.Game = { + rooms: { + W1N1: { + controller: { + my: true, + }, + }, + W2N1: { + controller: { + my: false, + }, + }, + W3N1: {}, + }, + }; +} + +ava('Should only return true for owned rooms', (t) => { + stubGame(); + + t.true(isMyRoom('W1N1')); + t.false(isMyRoom('W2N1')); + t.false(isMyRoom('W3N1')); + t.false(isMyRoom('E1N1')); + t.true(isMyRoom(Game.rooms['W1N1'])); + t.false(isMyRoom(Game.rooms['W3N1'])); + t.false(isMyRoom('')); +}); diff --git a/src/is-my-room/tsconfig.json b/src/is-my-room/tsconfig.json new file mode 100644 index 0000000..9536a0f --- /dev/null +++ b/src/is-my-room/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.base.json" +}