@screepts/screeps-api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2017,
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # Screeps API
2
+
3
+ ## JavaScript API client for the Screeps World server
4
+
5
+ [![npm](https://nodei.co/npm/@screepts/screeps-api.png "NPM")](https://npmjs.com/package/@screepts/screeps-api)
6
+
7
+ ## CLI Usage
8
+
9
+ As of 1.7.0, a small CLI program (`screeps-api`) is included.
10
+
11
+ Server config is specified via a `.screeps.yml` file conforming to the [Unified Credentials File format](https://github.com/screepers/screepers-standards/blob/master/SS3-Unified_Credentials_File.md)
12
+
13
+ ```
14
+ screeps-api
15
+
16
+ Usage: [options] [command]
17
+
18
+ Options:
19
+
20
+ -V, --version output the version number
21
+ --server <server> Server config to use (default: main)
22
+ -h, --help output usage information
23
+
24
+ Commands:
25
+
26
+ raw <cmd> [args...] Execute raw API call
27
+ memory [options] [path] Get Memory contents
28
+ segment [options] <segment> Get segment contents. Use 'all' to get all)
29
+ download [options] Download code
30
+ upload [options] <files...> Upload code
31
+
32
+ ```
33
+
34
+ ## API Usage
35
+
36
+ As of 1.0, all functions return Promises
37
+
38
+ ```javascript
39
+ const { ScreepsAPI } = require('screeps-api');
40
+ const fs = require('fs');
41
+
42
+ // Supports @tedivm's [Unified Credentials File format](https://github.com/screepers/screepers-standards/blob/34bd4e6e5c8250fa0794d915d9f78d3c45326076/SS3-Unified_Credentials_File.md) (Pending [screepers-standard PR #8](https://github.com/screepers/screepers-standards/pull/8))
43
+ const api = await ScreepsAPI.fromConfig('main', 'appName')
44
+ // This loads the server config 'main' and the configs section 'appName' if it exists
45
+ // config section can be accessed like this:
46
+ // If making a CLI app, its suggested to have a `--server` argument for selection
47
+ console.log(api.appConfig.myConfigVar)
48
+
49
+ // All options are optional
50
+ const api = new ScreepsAPI({
51
+ token: 'Your Token from Account/Auth Tokens'
52
+ protocol: 'https',
53
+ hostname: 'screeps.com',
54
+ port: 443,
55
+ path: '/' // Do no include '/api', it will be added automatically
56
+ });
57
+
58
+ // You can overwrite parameters if needed
59
+ api.auth('screeps@email.com','notMyPass',{
60
+ protocol: 'https',
61
+ hostname: 'screeps.com',
62
+ port: 443,
63
+ path: '/' // Do no include '/api', it will be added automatically
64
+ })
65
+
66
+ // If you want to point to the screeps PTR (Public Test Realm),
67
+ // you can set the 'path' option to '/ptr' and it will work fine.
68
+
69
+ // Dump Memory
70
+ api.memory.get()
71
+ .then(memory => {
72
+ fs.writeFileSync('memory.json', JSON.stringify(memory))
73
+ })
74
+ .catch(err => console.error(err));
75
+
76
+
77
+ // Dump Memory Path
78
+ api.memory.get('rooms.W0N0')
79
+ .then(memory => {
80
+ fs.writeFileSync('memory.rooms.W0N0.json', JSON.stringify(memory))
81
+ })
82
+ .catch(err => console.error(err));
83
+
84
+ // Get user info
85
+ api.me().then((user)=>console.log(user))
86
+
87
+ // Socket API
88
+
89
+ api.socket.connect()
90
+ // Events have the structure of:
91
+ // {
92
+ // channel: 'room',
93
+ // id: 'E3N3', // Only on certain events
94
+ // data: { ... }
95
+ // }
96
+ api.socket.on('connected',()=>{
97
+ // Do stuff after connected
98
+ })
99
+ api.socket.on('auth',(event)=>{
100
+ event.data.status contains either 'ok' or 'failed'
101
+ // Do stuff after auth
102
+ })
103
+
104
+ // Events: (Not a complete list)
105
+ // connected disconnected message auth time protocol package subscribe unsubscribe console
106
+
107
+ // Subscribtions can be queued even before the socket connects or auths,
108
+ // although you may want to subscribe from the connected or auth callback to better handle reconnects
109
+
110
+ api.socket.subscribe('console')
111
+ api.socket.on('console',(event)=>{
112
+ event.data.messages.log // List of console.log output for tick
113
+ })
114
+
115
+
116
+ // Starting in 1.0, you can also pass a handler straight to subscribe!
117
+ api.socket.subscribe('console', (event)=>{
118
+ event.data.messages.log // List of console.log output for tick
119
+ })
120
+
121
+ // More common examples
122
+ api.socket.subscribe('cpu',(event)=>console.log('cpu',event.data))
123
+ api.code.get('default').then(data=>console.log('code',data))
124
+ api.code.set('default',{
125
+ main: 'module.exports.loop = function(){ ... }'
126
+ })
127
+ api.socket.subscribe('memory/stats',(event)=>{
128
+ console.log('stats',event.data)
129
+ })
130
+ api.socket.subscribe('memory/rooms.E0N0',(event)=>{
131
+ console.log('E0N0 Memory',event.data)
132
+ })
133
+ ```
134
+
135
+ ## Endpoint documentation
136
+
137
+ Server endpoints are listed in the `docs` folder:
138
+
139
+ - [Endpoints.md](/docs/Endpoints.md) for direct access
140
+ - [Websocket_endpoints.md](/docs/Websocket_endpoints.md) for web socket endpoints
141
+ Those lists are currently not exhaustive.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import "../dist/cli.mjs"
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };