diff --git a/README.md b/README.md index 5901d71..88e72cc 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,23 @@ This is an unofficial client for the [Screeps](https://screeps.com/) Unoffical A Since the API is unofficial it could in theory change at any time. In practice though breaking changes are rare. +## Setup -## Setup: - -Simply install the library using `pip`. - +Simply install the Python `screepsapi` library using `pip`. ## Usage ### Authentication -To authenticate to the primary servers just supply a username and password. +The recommended way to authenticate to the official servers is providing an [Authorization Token](https://docs.screeps.com/auth-tokens.html) in the `token` parameter. ```python import screepsapi -USER = "MyUsername" -PASSWORD = "TimeTravelingSecretAgentForHire" -api = screepsapi.API(USER, PASSWORD) +TOKEN = "3bdd1da7-3002-4aaa-be91-330562f54093" +api = screepsapi.API(token=TOKEN) ``` +An optional `prefix` parameter can be included with values such as `"/ptr"` for the public test realm or `"/season"` for seasonal server. It is also possible to access private servers with the `host` and `secure` parameters. @@ -30,38 +28,41 @@ It is also possible to access private servers with the `host` and `secure` param import screepsapi USER = "MyUsername" PASSWORD = "TimeTravelingSecretAgentForHire" -api = screepsapi.API(USER, PASSWORD, host="server1.screepspl.us:443", secure=True) +HOST = "server1.screepspl.us:443" +api = screepsapi.API(USER, PASSWORD, host=HOST, secure=True) ``` Note that by default private servers do not use SSL and all traffic is unencrypted. +### Credentials + +Developers are encouraged to align with [SS3: Unified Credentials File v1.0](https://github.com/screepers/screepers-standards/blob/master/SS3-Unified_Credentials_File.md) to standardize Screeps credentials storage with other third party tools. + ### API -The API itself is a simple REST-based API. Each method in the api library corresponds to a different endpoint for the API. +The API class is a simple REST-based API. Each method corresponds to a different Screeps API endpoint. -The best way to discover functionality is to read through the library itself. +The best way to discover functionality is to read through the [screepsapi library](screepsapi/screepsapi.py) or [Endpoints.md](docs/Endpoints.md) #### Console Example ```python import screepsapi -USER = "MyUsername" -PASSWORD = "TimeTravelingSecretAgentForHire" -api = screepsapi.API(USER, PASSWORD, host="server1.screepspl.us:443", secure=True) +TOKEN = "3bdd1da7-3002-4aaa-be91-330562f54093" +api = screepsapi.API(token=TOKEN) # Run "Game.time" on shard1 via the console -api.console('Game.time', shard='shard1') +api.console("Game.time", shard="shard1") ``` #### User Information Example ```python import screepsapi -USER = "MyUsername" -PASSWORD = "TimeTravelingSecretAgentForHire" -api = screepsapi.API(USER, PASSWORD, host="server1.screepspl.us:443", secure=True) +TOKEN = "3bdd1da7-3002-4aaa-be91-330562f54093" +api = screepsapi.API(token=TOKEN) -# Find the GCL for `tedivm` +# Find the GCL for "tedivm" user = api.user_find("tedivm") print user["user"]["gcl"] ``` diff --git a/docs/Endpoints.md b/docs/Endpoints.md index 03f0eef..05d5ebd 100644 --- a/docs/Endpoints.md +++ b/docs/Endpoints.md @@ -17,60 +17,103 @@ Memory and console endpoints are from You can access the PTR by changing `screeps.com` to `screeps.com/ptr` in all URLs. # Enumeration values + When an endpoint takes `interval` or `statName` as an argument, the valid values are the ones listed below. -- interval: 8, 180, 1440 +- interval: 8, 180, 1440 (8 for 1h, 180 for 24h and 1440 for 7 days) - statName: creepsLost, creepsProduced, energyConstruction, energyControl, energyCreeps, energyHarvested # Authentication + - [POST] `https://screeps.com/api/auth/signin` - post data: `{ email, password }` - response: `{ ok, token }` +- [POST] `https://screeps.com/api/auth/steam-ticket` + - post data: `{ ticket, useNativeAuth }` + +- [POST] `https://screeps.com/api/auth/query-token` + - post data: `{ token }` + +# Registration + +- [GET] `https://screeps.com/api/register/check-email?email=user@example.com` + +- [GET] `https://screeps.com/api/register/check-username?username=danny` + +- [POST] `https://screeps.com/api/register/set-username` + - post data: `{ username }` + +- [POST] `https://screeps.com/api/register/submit` + - post data: `{ username, email, password, modules }` + +# Messaging + +- [GET] `https://screeps.com/api/user/messages/index` + - `{ ok, messages: [ { _id, message: { _id, user, respondent, date, type, text, unread } } ], users: { : { _id, username, badge: { type, color1, color2, color3, param, flip } } } }` + +- [GET] `https://screeps.com/api/user/messages/list?respondent={userId}` + - `{ ok, messages: [ { _id, date, type, text, unread } ] }` + +- [GET] `https://screeps.com/api/user/messages/unread-count` + - `{ ok, count }` + +- [POST] `https://screeps.com/api/user/messages/send` + - post data: `{ respondent, text }` + - response: `{ ok }` + +- [POST] `https://screeps.com/api/user/messages/mark-read` + - post data: `{ id }` + # Room information -- `https://screeps.com/api/game/room-overview?interval=8&room=E1N8` + +- [GET] `https://screeps.com/api/game/room-overview?interval=8&room=E1N8` - `{ 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 } }` -- `https://screeps.com/api/game/room-terrain?room=E1N8` +- [GET] `https://screeps.com/api/game/room-terrain?room=E1N8` - `{ ok, terrain: [ { room, x, y, type } ] }` - `type` in each element of `terrain` can be "wall" or "swamp" -- `https://screeps.com/api/game/room-terrain?room=E1N8&encoded=1` +- [GET] `https://screeps.com/api/game/room-terrain?room=E1N8&encoded=1` - `{ ok, terrain: [ { _id, room, terrain, type } ] }` - `terrain` is a string of digits, giving the terrain left-to-right and top-to-bottom - 0: plain, 1: wall, 2: swamp, 3: also wall - encoded argument can be anything non-empty -- `https://screeps.com/api/game/room-status?room=E1N8` +- [GET] `https://screeps.com/api/game/room-status?room=E1N8` - `{ _id, status, novice }` - `status` can at least be "normal" or "out of borders" - if the room is in a novice area, `novice` will contain the Unix timestamp of the end of the protection (otherwise it is absent) -- `https://screeps.com/api/experimental/pvp?interval=50` +- [GET] `https://screeps.com/api/game/room-objects?room=E1N8` + - `{ ok, objects: [ { _id, room, type, x, y } ], users: { : { _id, username, badge: { type, color1, color2, color3, param, flip } } } }` + - `objects` contains a list of all game objects in the room (creeps, structures, sources, mineral, etc) + +- [GET] `https://screeps.com/api/experimental/pvp?interval=100` - `{ ok, time, rooms: [ { _id, lastPvpTime } ] }` - `time` is the current server tick - `_id` contains the room name for each room, and `lastPvpTime` contains the last tick pvp occurred - 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. -- `https://screeps.com/api/experimental/pvp?start=14787157` - - `{ ok, time, rooms: [ { _id, lastPvpTime } ] }` +- [GET] `https://screeps.com/api/experimental/nukes` + - `{ ok, nukes, : [ { _id, type, room, x, y, landTime, launchRoomName } ] }` # Market information -- `https://screeps.com/api/game/market/orders-index` - - `{"ok":1,"list":[{"_id":"XUHO2","count":2}]}` - - `_id` is the resource type, and there will only be one of each type. - - `count` is the number of orders. +- [GET] `https://screeps.com/api/game/market/orders-index` + - `{"ok":1,"list":[{"_id":"XUHO2","count":2}]}` + - `_id` is the resource type, and there will only be one of each type. + - `count` is the number of orders. - - `https://screeps.com/api/game/market/my-orders` +- [GET] `https://screeps.com/api/game/market/my-orders` - `{ ok, list: [ { _id, created, user, active, type, amount, remainingAmount, resourceType, price, totalAmount, roomName } ] }` - - `https://screeps.com/api/game/market/orders?resourceType=Z` +- [GET] `https://screeps.com/api/game/market/orders?resourceType=Z` - `{ ok, list: [ { _id, created, user, active, type, amount, remainingAmount, resourceType, price, totalAmount, roomName } ] }` - `resourceType` is one of the RESOURCE_* constants. - - `https://screeps.com/api/user/money-history` +- [GET] `https://screeps.com/api/user/money-history` - `{"ok":1,"page":0,"list":[ { _id, date, tick, user, type, balance, change, market: {} } ] }` - `page` used for pagination. - `hasMore` is true if there are more pages to view. @@ -81,29 +124,29 @@ are the ones listed below. - Price Change - `{ changeOrderPrice: { orderId, oldPrice, newPrice } }` # Leaderboard -- `https://screeps.com/api/leaderboard/seasons` + +- [GET] `https://screeps.com/api/leaderboard/seasons` - `{ ok, seasons: [ { _id, name, date } ] }` - the `_id` returned here is used for the season name in the other leaderboard calls -- `https://screeps.com/api/leaderboard/find?mode=world&season=2015-09&username=danny` +- [GET] `https://screeps.com/api/leaderboard/find?mode=world&season=2015-09&username=danny` - `{ ok, _id, season, user, score, rank }` - `user` (not `_id`) is the user's _id, as returned by `me` and `user/find` - `rank` is 0-based -- `https://screeps.com/api/leaderboard/find?mode=world&username=danny` +- [GET] `https://screeps.com/api/leaderboard/find?mode=world&username=danny` - `{ ok, list: [ _id, season, user, score, rank ] }` - lists ranks in all seasons -- `https://screeps.com/api/leaderboard/list?limit=10&mode=world&offset=10&season=2015-09` +- [GET] `https://screeps.com/api/leaderboard/list?limit=10&mode=world&offset=10&season=2015-09` - `{ ok, list: [ { _id, season, user, score, rank } ], count, users: { : { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } } }` - - mode can be "world" or "power" - - setting limit to too high a value will give an "invalid parameters" response # Messages -- `https://screeps.com/api/user/messages/index` + +- [GET] `https://screeps.com/api/user/messages/index` - `{ ok, messages: [ { _id, message: { _id, user, respondent, date, type, text, unread } } ], users: { : { _id, username, badge: { type, color1, color2, color3, param, flip } } } }` -- `https://screeps.com/api/user/messages/list?respondent=55605a6654db1fa952de8d90` +- [GET] `https://screeps.com/api/user/messages/list?respondent=55605a6654db1fa952de8d90` - `{ ok, messages: [ { _id, date, type, text, unread } ] }` - [POST] `https://screeps.com/api/user/messages/send` @@ -111,42 +154,48 @@ are the ones listed below. - `respondent` is the long _id of the user, not the username - response: `{ ok }` -- `https://screeps.com/api/user/messages/unread-count` +- [GET] `https://screeps.com/api/user/messages/unread-count` - `{ ok, count }` -# User information -- `https://screeps.com/api/auth/me` +# User information and settings + +- [GET] `https://screeps.com/api/auth/me` - `{ 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 } }` -- `https://screeps.com/api/user/find?username=danny` +- [GET] `https://screeps.com/api/user/name` + +- [GET] `https://screeps.com/api/user/stats?interval=8` + +- [GET] `https://screeps.com/api/user/rooms?interval=8` + +- [GET] `https://screeps.com/api/user/find?id={userId}` - `{ ok, user: { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } }` -- `https://screeps.com/api/user/find?id=55c91dc66e36d62a6167e1b5` +- [GET] `https://screeps.com/api/user/find?username=danny` - `{ ok, user: { _id, username, badge: { type, color1, color2, color3, param, flip }, gcl } }` -- `https://screeps.com/api/user/overview?interval=1440&statName=energyControl` +- [GET] `https://screeps.com/api/user/overview?interval=1440&statName=energyControl` - `{ ok, rooms: [ ], stats: { : [ { value, endTime } ] }, statsMax }` -- `https://screeps.com/api/user/respawn-prohibited-rooms` +- [GET] `https://screeps.com/api/user/respawn-prohibited-rooms` - `{ ok, rooms: [ ] }` -- `https://screeps.com/api/user/world-status` +- [GET] `https://screeps.com/api/user/world-size` + - `{ ok, width, height }` + +- [GET] `https://screeps.com/api/user/world-status` - `{ ok, status }` - - `status` is "normal" is the player is spawned, "empty" otherwise + - `status` can be `"lost"`, `"empty"` or `"normal"`, lost is when you loose all your spawns, empty is when you have respawned and not placed your spawn yet. -- `https://screeps.com/api/user/world-start-room` +- [GET] `https://screeps.com/api/user/world-start-room` - `{ ok, room: [ ] }` - `room` is an array, but seems to always contain only one element -- `https://screeps.com/api/xsolla/user` - - `{ ok, active }` - - `active` is the Unix timestamp of the end of your current subscribed time - - [POST] `https://screeps.com/api/user/console` - post data: `{ expression }` - response: `{ ok, result: { ok, n }, ops: [ { user, expression, _id } ], insertedCount, insertedIds: [ ] }` -- `https://screeps.com/api/user/memory?path=flags.Flag1` +- [GET] `https://screeps.com/api/user/memory?path=flags.Flag1` - `{ ok, data }` - `data` is the string `gz:` followed by base64-encoded gzipped JSON encoding of the requested memory path - the path may be empty or absent to retrieve all of Memory @@ -155,20 +204,50 @@ are the ones listed below. - post data: `{ path, value }` - response: `{ ok, result: { ok, n }, ops: [ { user, expression, hidden } ], data, insertedCount, insertedIds }` -- `https://screeps.com/api/user/memory-segment?segment=[0-99]` - - `{ okay, data }` - - response: `{ ok, data: '' }` +- [GET] `https://screeps.com/api/user/memory-segment?segment=[0-99]` + - `{ ok, data }` -- [POST ]`https://screeps.com/api/user/memory-segment` +- [POST] `https://screeps.com/api/user/memory-segment` - post data: `{ segment, data }` +- [GET/POST] `https://screeps.com/api/user/code` + - for pushing or pulling code, as documented at http://support.screeps.com/hc/en-us/articles/203022612 + +- [GET] `https://screeps.com/api/user/branches` + - `{ ok, list: [ { _id, branch, activeWorld, activeSim } ] }` + +- [POST] `https://screeps.com/api/user/set-active-branch` + - post data: `{ branch, activeName }` + +- [POST] `https://screeps.com/api/user/clone-branch` + - post data: `{ branch, newName, defaultModules }` + +- [POST] `https://screeps.com/api/user/delete-branch` + - post data: `{ branch }` + +- [POST] `https://screeps.com/api/user/notify-prefs` + - post data: `{ prefs }` + - `prefs` can be any of: `disabled`, `disabledOnMessages`, `sendOnline`, `interval`, `errorsInterval` + +- [POST] `https://screeps.com/api/user/tutorial-done` + +- [POST] `https://screeps.com/api/user/badge?badge={Badge}` + - `badge` object format: `{ type, color1, color2, color3, param, flip }` + # Manipulating the game world + - [POST] `https://screeps.com/api/game/gen-unique-object-name` - post data: `{ type }` - response: `{ ok, name }` - `type` can be at least "flag" or "spawn" +- [POST] `https://screeps.com/api/game/check-unique-object-name` + - post data: `{ type, name, shard }` + +- [POST] `https://screeps.com/api/game/gen-unique-flag-name` + - post data: `{ shard }` + - [POST] `https://screeps.com/api/game/create-flag` - post data: `{ room, x, y, name, color, secondaryColor }` - response: `{ ok, result: { nModified, ok, upserted: [ { index, _id } ], n }, connection: { host, id, port } }` @@ -184,6 +263,9 @@ are the ones listed below. - post data: `{ _id, color, secondaryColor }` - response: `{ ok, result: { nModified, ok, n }, connection: { host, id, port } }` +- [POST] `https://screeps.com/api/game/remove-flag` + - post data: `{ room, name, shard }` + - [POST] `https://screeps.com/api/game/add-object-intent` - post data: `{ _id, room, name, intent }` - response: `{ ok, result: { nModified, ok, upserted: [ { index, _id } ], n }, connection: { host, id, port } }` @@ -207,12 +289,48 @@ are the ones listed below. - `structureType` is the same value as one of the in-game STRUCTURE_* constants ('road', 'spawn', etc.) - `{ ok, result: { ok, n }, ops: [ { type, room, x, y, structureType, user, progress, progressTotal, _id } ], insertedCount, insertedIds }` +- [POST] `https://screeps.com/api/game/place-spawn` + - post data: `{ room, name, x, y, shard }` + +- [POST] `https://screeps.com/api/game/create-invader` + - post data: `{ x, y, size, type, boosted, shard }` + +- [POST] `https://screeps.com/api/game/remove-invader` + - post data: `{ _id, shard }` + +# Decorations + +- [GET] `https://screeps.com/api/decorations/inventory` + +- [GET] `https://screeps.com/api/decorations/themes` + +- [POST] `https://screeps.com/api/decorations/convert` + - post data: `{ decorations }` + - `decorations` is a string array of ids + +- [POST] `https://screeps.com/api/decorations/pixelize` + - post data: `{ count, theme }` + +- [POST] `https://screeps.com/api/decorations/activate` + - post data: `{ _id, active}` + +- [POST] `https://screeps.com/api/decorations/deactivate` + - post data: `{ decorations }` + - `decorations` is a string array of ids + # Other -- `https://screeps.com/api/game/time` - - `{ ok, time }` -- [GET/POST] `https://screeps.com/api/user/code` - - for pushing or pulling code, as documented at http://support.screeps.com/hc/en-us/articles/203022612 +- [POST] `https://screeps.com/api/servers/list` + - `{ ok, servers }` + +- [GET] `https://screeps.com/api/game/shards/info` + - `{ ok, shards: [ { name, lastTicks, cpuLimit, rooms, users, tick } ] }` + +- [GET] `https://screeps.com/api/version` + - `{ ok, package, protocol, serverData, users }` + +- [GET] `https://screeps.com/api/game/time` + - `{ ok, time }` - [POST] `https://screeps.com/api/game/map-stats` - post data: `{ rooms: [ ], statName: "..."}` @@ -222,7 +340,7 @@ are the ones listed below. - `status` and `novice` are as per the `room-status` call - `level` can be 0 to indicate a reserved room -- `https://screeps.com/room-history/E1N8/12340.json` +- [GET] `https://screeps.com/room-history/shard0/E1N8/12340.json` - `{ timestamp, room, base, ticks: {