Skip to content

Commit 64a21c6

Browse files
committed
Merge branch 'v1.0'
2 parents a6828b2 + 99d16de commit 64a21c6

20 files changed

+3957
-282
lines changed

.babelrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
["env",{
4+
"modules":false,
5+
"targets":{
6+
node: 6
7+
}
8+
}]
9+
],
10+
"plugins": [
11+
"transform-async-to-generator",
12+
"external-helpers"
13+
]
14+
}

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
*.sublime-*
12
node_modules
23
auth.js
3-
credentials.js
4-
*.sublime-*
54
dist
5+
npm-debug.log

.npmignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
src
1+
*.sublime-*
22
node_modules
33
auth.js
4-
*.sublime-*
4+
src
5+
dist

README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,68 @@ const api = new ScreepsAPI({
1313
password: 'notMyPass'
1414
});
1515

16-
Promise.resolve()
17-
.then(() => api.connect())
18-
.then(() => api.memory.get())
16+
// Dump Memory
17+
api.memory.get()
1918
.then(memory => {
2019
fs.writeFileSync('memory.json', JSON.stringify(memory))
2120
})
2221
.catch(err => console.error(err));
22+
23+
24+
// Dump Memory Path
25+
api.memory.get('rooms.W0N0')
26+
.then(memory => {
27+
fs.writeFileSync('memory.rooms.W0N0.json', JSON.stringify(memory))
28+
})
29+
.catch(err => console.error(err));
30+
31+
// Get user info
32+
api.me().then((user)=>console.log(user))
33+
34+
// Socket API
35+
36+
api.socket.connect()
37+
// Events have the structure of:
38+
// {
39+
// channel: 'room',
40+
// id: 'E3N3', // Only on certain events
41+
// data: { ... }
42+
// }
43+
api.socket.on('connected',()=>{
44+
// Do stuff after conntected
45+
})
46+
api.socket.on('auth',(event)=>{
47+
event.data.status contains either 'ok' or 'failed'
48+
// Do stuff after auth
49+
})
50+
51+
// Events: (Not a complete list)
52+
// connected disconnected message auth time protocol package subscribe unsubscribe console
53+
54+
// Subscribtions can be queued even before the socket connects or auths,
55+
// although you may want to subscribe from the connected or auth callback to better handle reconnects
56+
57+
api.socket.subscribe('console')
58+
api.socket.on('console',(event)=>{
59+
event.data.messages.log // List of console.log output for tick
60+
})
61+
62+
63+
// Starting in 1.0, you can also pass a handler straight to subscribe!
64+
api.socket.subscribe('console', (event)=>{
65+
event.data.messages.log // List of console.log output for tick
66+
})
67+
68+
// More common examples
69+
api.socket.subscribe('cpu',(event)=>console.log('cpu',event.data))
70+
api.code.get('default').then(data=>console.log('code',data))
71+
api.code.set('default',{
72+
main: 'module.exports.loop = function(){ ... }'
73+
})
74+
api.socket.subscribe('memory/stats',(event)=>{
75+
console.log('stats',event.data)
76+
})
77+
api.socket.subscribe('memory/rooms.E0N0',(event)=>{
78+
console.log('E0N0 Memory',event.data)
79+
})
2380
```

docs/Endpoints.md

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
Copied from [python-screeps](https://github.com/screepers/python-screeps/blob/master/docs/Endpoints.md)
2+
3+
Start by sending a request to `auth/signin` with your e-mail address and
4+
password in a JSON object in the POST data. The response JSON object contains a
5+
token (a hex string); remember that value. Each time you make a request that
6+
requires authentication (the leaderboard and terrain ones, at least, don't),
7+
send the most recent token value as both the `X-Token` and `X-Username`
8+
headers. The response will contain a new token value in the `X-Token` header
9+
with which you should replace your saved token. (You can send the token on every
10+
request and just check for a new one in the response, so you don't have to know
11+
exactly which calls require authentication.)
12+
13+
Example request parameters are given below, along with schemata for the server's
14+
responses.
15+
16+
Memory and console endpoints are from
17+
[bzy-xyz](https://gist.github.com/bzy-xyz/9c4d8c9f9498a2d7983d).
18+
19+
You can access the PTR by changing `screeps.com` to `screeps.com/ptr` in all URLs.
20+
21+
# Enumeration values
22+
When an endpoint takes `interval` or `statName` as an argument, the valid values
23+
are the ones listed below.
24+
25+
- interval: 8, 180, 1440
26+
- statName: creepsLost, creepsProduced, energyConstruction, energyControl, energyCreeps, energyHarvested
27+
28+
# Authentication
29+
- [POST] `https://screeps.com/api/auth/signin`
30+
- post data: `{ email, password }`
31+
- response: `{ ok, token }`
32+
33+
# Room information
34+
- `https://screeps.com/api/game/room-overview?interval=8&room=E1N8`
35+
- `{ ok, owner: { username, badge: { type, color1, color2, color3, param, flip } }, stats: { energyHarvested: [ { value, endTime } ], energyConstruction: [ { value, endTime } ], energyCreeps: [ { value, endTime } ], energyControl: [ { value, endTime } ], creepsProduced: [ { value, endTime } ], creepsLost: [ { value, endTime } ] }, statsMax: { energy1440, energyCreeps1440, energy8, energyControl8, creepsLost180, energyHarvested8, energy180, energyConstruction180, creepsProduced8, energyControl1440, energyCreeps8, energyHarvested1440, creepsLost1440, energyConstruction1440, energyHarvested180, creepsProduced180, creepsProduced1440, energyCreeps180, energyControl180, energyConstruction8, creepsLost8 } }`
36+
37+
- `https://screeps.com/api/game/room-terrain?room=E1N8`
38+
- `{ ok, terrain: [ { room, x, y, type } ] }`
39+
- `type` in each element of `terrain` can be "wall" or "swamp"
40+
41+
- `https://screeps.com/api/game/room-terrain?room=E1N8&encoded=1`
42+
- `{ ok, terrain: [ { _id, room, terrain, type } ] }`
43+
- `terrain` is a string of digits, giving the terrain left-to-right and top-to-bottom
44+
- 0: plain, 1: wall, 2: swamp, 3: also wall
45+
- encoded argument can be anything non-empty
46+
47+
- `https://screeps.com/api/game/room-status?room=E1N8`
48+
- `{ _id, status, novice }`
49+
- `status` can at least be "normal" or "out of borders"
50+
- if the room is in a novice area, `novice` will contain the Unix timestamp of the end of the protection (otherwise it is absent)
51+
52+
- `https://screeps.com/api/experimental/pvp?interval=50`
53+
- `{ ok, time, rooms: [ { _id, lastPvpTime } ] }`
54+
- `time` is the current server tick
55+
- `_id` contains the room name for each room, and `lastPvpTime` contains the last tick pvp occurred
56+
- if neither a valid `interval` nor a valid `start` argument is provided, the result of the call is still `ok`, but with an empty rooms array.
57+
58+
- `https://screeps.com/api/experimental/pvp?start=14787157`
59+
- `{ ok, time, rooms: [ { _id, lastPvpTime } ] }`
60+
61+
# Market information
62+
63+
- `https://screeps.com/api/game/market/orders-index`
64+
- `{"ok":1,"list":[{"_id":"XUHO2","count":2}]}`
65+
- `_id` is the resource type, and there will only be one of each type.
66+
- `count` is the number of orders.
67+
68+
- `https://screeps.com/api/game/market/my-orders`
69+
- `{ ok, list: [ { _id, created, user, active, type, amount, remainingAmount, resourceType, price, totalAmount, roomName } ] }`
70+
71+
- `https://screeps.com/api/game/market/orders?resourceType=Z`
72+
- `{ ok, list: [ { _id, created, user, active, type, amount, remainingAmount, resourceType, price, totalAmount, roomName } ] }`
73+
- `resourceType` is one of the RESOURCE_* constants.
74+
75+
- `https://screeps.com/api/user/money-history`
76+
- `{"ok":1,"page":0,"list":[ { _id, date, tick, user, type, balance, change, market: {} } ] }`
77+
- `page` used for pagination.
78+
- `hasMore` is true if there are more pages to view.
79+
- `market`
80+
- New Order- `{ order: { type, resourceType, price, totalAmount, roomName } }`
81+
- Extended Order- `{ extendOrder: { orderId, addAmount } }`
82+
- Fulfilled Order- `{ resourceType, roomName, targetRoomName, price, npc, amount }`
83+
- Price Change - `{ changeOrderPrice: { orderId, oldPrice, newPrice } }`
84+
85+
# Leaderboard
86+
- `https://screeps.com/api/leaderboard/seasons`
87+
- `{ ok, seasons: [ { _id, name, date } ] }`
88+
- the `_id` returned here is used for the season name in the other leaderboard calls
89+
90+
- `https://screeps.com/api/leaderboard/find?mode=world&season=2015-09&username=danny`
91+
- `{ ok, _id, season, user, score, rank }`
92+
- `user` (not `_id`) is the user's _id, as returned by `me` and `user/find`
93+
- `rank` is 0-based
94+
95+
- `https://screeps.com/api/leaderboard/find?mode=world&username=danny`
96+
- `{ ok, list: [ _id, season, user, score, rank ] }`
97+
- lists ranks in all seasons
98+
99+
- `https://screeps.com/api/leaderboard/list?limit=10&mode=world&offset=10&season=2015-09`
100+
- `{ ok, list: [ { _id, season, user, score, rank } ], count, users: { <user's _id>: { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } } }`
101+
102+
# Messages
103+
- `https://screeps.com/api/user/messages/index`
104+
- `{ ok, messages: [ { _id, message: { _id, user, respondent, date, type, text, unread } } ], users: { <user's _id>: { _id, username, badge: { type, color1, color2, color3, param, flip } } } }`
105+
106+
- `https://screeps.com/api/user/messages/list?respondent=55605a6654db1fa952de8d90`
107+
- `{ ok, messages: [ { _id, date, type, text, unread } ] }`
108+
109+
- [POST] `https://screeps.com/api/user/messages/send`
110+
- post data: `{ respondent, text }`
111+
- `respondent` is the long _id of the user, not the username
112+
- response: `{ ok }`
113+
114+
- `https://screeps.com/api/user/messages/unread-count`
115+
- `{ ok, count }`
116+
117+
# User information
118+
- `https://screeps.com/api/auth/me`
119+
- `{ ok, _id, email, username, cpu, badge: { type, color1, color2, color3, param, flip }, password, notifyPrefs: { sendOnline, errorsInterval, disabledOnMessages, disabled, interval }, gcl, credits, lastChargeTime, lastTweetTime, github: { id, username }, twitter: { username, followers_count } }`
120+
121+
- `https://screeps.com/api/user/find?username=danny`
122+
- `{ ok, user: { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } }`
123+
124+
- `https://screeps.com/api/user/find?id=55c91dc66e36d62a6167e1b5`
125+
- `{ ok, user: { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } }`
126+
127+
- `https://screeps.com/api/user/overview?interval=1440&statName=energyControl`
128+
- `{ ok, rooms: [ <room name> ], stats: { <room name>: [ { value, endTime } ] }, statsMax }`
129+
130+
- `https://screeps.com/api/user/respawn-prohibited-rooms`
131+
- `{ ok, rooms: [ <room name> ] }`
132+
133+
- `https://screeps.com/api/user/world-status`
134+
- `{ ok, status }`
135+
- `status` is usually "normal"; it's unknown what else it can be
136+
137+
- `https://screeps.com/api/user/world-start-room`
138+
- `{ ok, room: [ <room name> ] }`
139+
- `room` is an array, but seems to always contain only one element
140+
141+
- `https://screeps.com/api/xsolla/user`
142+
- `{ ok, active }`
143+
- `active` is the Unix timestamp of the end of your current subscribed time
144+
145+
- [POST] `https://screeps.com/api/user/console`
146+
- post data: `{ expression }`
147+
- response: `{ ok, result: { ok, n }, ops: [ { user, expression, _id } ], insertedCount, insertedIds: [ <mongodb id> ] }`
148+
149+
- `https://screeps.com/api/user/memory?path=flags.Flag1`
150+
- `{ ok, data }`
151+
- `data` is the string `gz:` followed by base64-encoded gzipped JSON encoding of the requested memory path
152+
- the path may be empty or absent to retrieve all of Memory
153+
154+
- [POST] `https://screeps.com/api/user/memory`
155+
- post data: `{ path, value }`
156+
- response: `{ ok, result: { ok, n }, ops: [ { user, expression, hidden } ], data, insertedCount, insertedIds }`
157+
158+
- `https://screeps.com/api/user/memory-segment?segment=[0-99]`
159+
- `{ okay, data }`
160+
- response: `{ ok, data: '' }`
161+
162+
- [POST ]`https://screeps.com/api/user/memory-segment`
163+
- post data: `{ segment, data }`
164+
165+
166+
# Manipulating the game world
167+
- [POST] `https://screeps.com/api/game/gen-unique-object-name`
168+
- post data: `{ type }`
169+
- response: `{ ok, name }`
170+
- `type` can be at least "flag" or "spawn"
171+
172+
- [POST] `https://screeps.com/api/game/create-flag`
173+
- post data: `{ room, x, y, name, color, secondaryColor }`
174+
- response: `{ ok, result: { nModified, ok, upserted: [ { index, _id } ], n }, connection: { host, id, port } }`
175+
- if the name is new, `result.upserted[0]._id` is the game id of the created flag
176+
- if not, this moves the flag and the response does not contain the id (but the id doesn't change)
177+
- `connection` looks like some internal MongoDB thing that is irrelevant to us
178+
179+
- [POST] `https://screeps.com/api/game/change-flag`
180+
- post data: `{ _id, room, x, y }`
181+
- response: `{ ok, result: { nModified, ok, n }, connection: { host, id, port } }`
182+
183+
- [POST] `https://screeps.com/api/game/change-flag-color`
184+
- post data: `{ _id, color, secondaryColor }`
185+
- response: `{ ok, result: { nModified, ok, n }, connection: { host, id, port } }`
186+
187+
- [POST] `https://screeps.com/api/game/add-object-intent`
188+
- post data: `{ _id, room, name, intent }`
189+
- response: `{ ok, result: { nModified, ok, upserted: [ { index, _id } ], n }, connection: { host, id, port } }`
190+
- `_id` is the game id of the object to affect (except for destroying structures), `room` is the name of the room it's in
191+
- this method is used for a variety of actions, depending on the `name` and `intent` parameters
192+
- remove flag: `name = "remove"`, `intent = {}`
193+
- destroy structure: `_id = "room"`, `name = "destroyStructure"`, `intent = [ {id: <structure id>, roomName, <room name>, user: <user id>} ]`
194+
- can destroy multiple structures at once
195+
- suicide creep: `name = "suicide"`, `intent = {id: <creep id>}`
196+
- unclaim controller: `name = "unclaim"`, `intent = {id: <controller id>}`
197+
- intent can be an empty object for suicide and unclaim, but the web interface sends the id in it, as described
198+
- remove construction site: `name = "remove"`, `intent = {}`
199+
200+
- [POST] `https://screeps.com/api/game/set-notify-when-attacked`
201+
- post data: `{ _id, enabled }`
202+
- `enabled` is either `true` or `false` (literal values, not strings)
203+
- response: `{ ok, result: { ok, nModified, n }, connection: { id, host, port } }`
204+
205+
- [POST] `https://screeps.com/api/game/create-construction`
206+
- post data: `{ room, structureType, x, y}`
207+
- `structureType` is the same value as one of the in-game STRUCTURE_* constants ('road', 'spawn', etc.)
208+
- `{ ok, result: { ok, n }, ops: [ { type, room, x, y, structureType, user, progress, progressTotal, _id } ], insertedCount, insertedIds }`
209+
210+
# Other
211+
- `https://screeps.com/api/game/time`
212+
- `{ ok, time }`
213+
214+
- [GET/POST] `https://screeps.com/api/user/code`
215+
- for pushing or pulling code, as documented at http://support.screeps.com/hc/en-us/articles/203022612
216+
217+
- [POST] `https://screeps.com/api/game/map-stats`
218+
- post data: `{ rooms: [ <room name> ], statName: "..."}`
219+
- statName can be "owner0", "claim0", or a stat (see the enumeration above) followed by an interval
220+
- if it is owner0 or claim0, there's no separate stat block in the response
221+
- response: `{ ok, stats: { <room name>: { status, novice, own: { user, level }, <stat>: [ { user, value } ] } }, users: { <user's _id>: { _id, username, badge: { type, color1, color2, color3, param, flip } } } }`
222+
- `status` and `novice` are as per the `room-status` call
223+
- `level` can be 0 to indicate a reserved room
224+
225+
- `https://screeps.com/room-history/E1N8/12340.json`
226+
- `{ timestamp, room, base, ticks: { <time>: <tick update object> } }`
227+
- the number in the URL is the tick to retrieve information for (from 5e5 ticks ago to about 50 ticks ago)
228+
- note that the web interface only shows up to 3e5 ticks ago
229+
- the tick must be a multiple of 20; the response includes information for the 20 ticks starting then
230+
- for the first tick in the result, the tick update object maps from object ids to full object descriptions
231+
- for later ticks, the update includes only changed attributes
232+
- timestamp is milliseconds since the Unix epoch
233+
234+
- [POST] `https://screeps.com/user/activate-ptr`
235+
- `{ok, result: { ok, nModified, n }, connection: { id, host, port } }`
236+
- only works on the PTR

examples/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
Warning: These examples may be outdated and plain broken.

examples/console.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const ScreepsAPI = require('../')
2+
const { ScreepsAPI } = require('../')
33
// const auth = require('../auth')
44
const readline = require('readline')
55
const util = require('util')

0 commit comments

Comments
 (0)