Skip to content

Commit

Permalink
feat(is-my-room): add module is-my-room
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath authored and RiftLurker committed Jan 15, 2018
1 parent f03884e commit 897db27
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ yarn.lock
*.js
*.js.map
.nyc_output/
node_modules/
21 changes: 21 additions & 0 deletions src/is-my-room/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
49 changes: 49 additions & 0 deletions src/is-my-room/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
20 changes: 20 additions & 0 deletions src/is-my-room/readme.md
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions src/is-my-room/test.ts
Original file line number Diff line number Diff line change
@@ -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(''));
});
3 changes: 3 additions & 0 deletions src/is-my-room/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}

0 comments on commit 897db27

Please sign in to comment.