|
| 1 | +Contributing to screeps-api |
| 2 | +=========================== |
| 3 | + |
| 4 | +This is a fairly barebones document. If you have any questions, please file an |
| 5 | +issue in the repository to ask them! |
| 6 | + |
| 7 | +# Some protocol update procedures |
| 8 | + |
| 9 | +## Fixing an error you've found |
| 10 | + |
| 11 | +If you are using this library, and you get either a logged warning or error |
| 12 | +about missing fields, you've come to the right place! The libary is designed to |
| 13 | +be fairly easy to update. |
| 14 | + |
| 15 | +First, you should get your error message - it'll contain JSON of what was |
| 16 | +received, and some note for what struct is wrong. |
| 17 | + |
| 18 | +Second, find the struct in this repository, and look at its structure. Then, |
| 19 | +depending on your error, either make some of the fields optional by wrapping the |
| 20 | +type in an `Option<>`, or add new fields, or if you believe an update removed a |
| 21 | +field, remove it. |
| 22 | + |
| 23 | +Third, compile your changes - run `cargo check --tests`, and see what breaks. |
| 24 | +Keep going around fixing compilation errors until it works. |
| 25 | + |
| 26 | +Fourth, run the tests - `cargo test`. If any fail, see if its the fault of your |
| 27 | +changes. IF the tests are now wrong because they contain old protocol data, and |
| 28 | +the server no longer responds with that data, follow the "Updating an old test" |
| 29 | +procedure listed below. |
| 30 | + |
| 31 | +Last, make a new test using the JSON in the warning message. The crate should |
| 32 | +now support it, so you should make a new unit test under whatever structure you |
| 33 | +edited which validates that it does. Everything should have existing unit tests, |
| 34 | +so there should be one you can copy/paste and then stick your data into. The |
| 35 | +only thing to know when doing this is that the `json!()` macro sometimes |
| 36 | +requires you to suffix your numbers with rust number suffixes - so you'll need |
| 37 | +to modify the json `"key": 314` into `"key": 314u64` when copying it in (or |
| 38 | +`f64` if a float). |
| 39 | + |
| 40 | +## Updating an old test |
| 41 | + |
| 42 | +When adapting to protocol changes, it's likely that old tests will completely |
| 43 | +break. When this happens, I prefer to _completely redo the test_ rather than try |
| 44 | +to fix it piecemail. Ideally, every test contains some data the current server |
| 45 | +sent you - and if you simply modify bits of the test, then this guarantee will |
| 46 | +be broken. It's easy to write tests which work for your code (but don't |
| 47 | +actually represent the server), so instead, we should grab data from the server. |
| 48 | + |
| 49 | +Luckily, we have some tools to do just that! This crate comes with a few |
| 50 | +examples, and the most important one is `ws-debug`. Once you get the crate |
| 51 | +compiling, even if tests fail, you can run `ws-debug` to observe and grab raw |
| 52 | +JSON data from any room on the screeps server. |
| 53 | + |
| 54 | +So - say you've just updated `StructureStorage`, and its old test fails. First, |
| 55 | +go onto the screeps web interface, and find a room with a storage in it which |
| 56 | +exhibits the properties you're looking to test (if it's just the |
| 57 | +`parse_storage` test, this is literally any storage). |
| 58 | + |
| 59 | +Note the room number and shard, and then run `ws-debug`: |
| 60 | + |
| 61 | +``` |
| 62 | +cargo run --example ws-debug -- --room W39N49 --shard shard0 -v |
| 63 | +``` |
| 64 | + |
| 65 | +This requires authentication. If you haven't already, go to |
| 66 | +https://screeps.com/a/#!/account/auth-tokens to create a new authentication |
| 67 | +token, and then stick it into a new `.env` file in this repository like so: |
| 68 | + |
| 69 | +``` |
| 70 | +# this is .env |
| 71 | +SCREEPS_API_TOKEN='your-token-here' |
| 72 | +``` |
| 73 | + |
| 74 | +The `-v` flag here is required to actually print out the raw JSOn. If ommitted, |
| 75 | +`ws-debug` will only output warnings/errors, but not successful messages. That |
| 76 | +can be useful, for example, if you're updating to include a new feature and want |
| 77 | +to see what fails when reading a room with it in it. |
| 78 | + |
| 79 | + |
| 80 | +Anyways, if that works, you'll connect to the server, and `ws-debug` will start |
| 81 | +spitting out room updates for your room. |
| 82 | + |
| 83 | +Find the part of the update representing your structure. For example, for |
| 84 | +`StructureStorage`, you'd look for something like this: |
| 85 | + |
| 86 | +```json |
| 87 | + "599ca7f2e48c09254b443791": { |
| 88 | + "_id": "599ca7f2e48c09254b443791", |
| 89 | + "hits": 10000, |
| 90 | + "hitsMax": 10000, |
| 91 | + "notifyWhenAttacked": true, |
| 92 | + "room": "W39N49", |
| 93 | + "store": { |
| 94 | + "energy": 913026 |
| 95 | + }, |
| 96 | + "storeCapacity": 1000000, |
| 97 | + "type": "storage", |
| 98 | + "user": "5788389e3fd9069e6b546e2d", |
| 99 | + "x": 6, |
| 100 | + "y": 13 |
| 101 | + }, |
| 102 | +``` |
| 103 | + |
| 104 | +Finally, copy this data into the failing test, and update the test's assertions |
| 105 | +to match the new coordinates/inventory/etc. You'll probably want to reformat the |
| 106 | +json data as well, so it looks like what the test did before. |
0 commit comments