···11-# star Gambit SDK
11+# Star Gambit SDK
2233The Star Gambit SDK allows developers to build computer players (coms) and make them available for players online.
44To write a com, you need to write one function: "given the board, what do I
55do?". The SDK handles the rest. Connect it to the server with an API token and your com can be played.
6677```rust
88-use sg_sdk::{Com, Game, run_as_com};
88+//! Usage: cargo run --example random_com -- --token <api_token>
99use rand::seq::SliceRandom;
1010+use sg_sdk::{operate_com, Com, Game};
10111112struct RandomCom;
12131314impl Com for RandomCom {
1414- fn act(&mut self, game: &Game) -> Vec<String> {
1515- // Play one uniformly random legal action; the runner calls us again
1616- // until the turn ends.
1717- game.legal_actions()
1818- .choose(&mut rand::thread_rng())
1919- .cloned()
2020- .into_iter()
2121- .collect()
2222- }
1515+ fn act(&mut self, game: &Game) -> Vec<String> {
1616+ // Play one random legal action; the runner calls us again until the
1717+ // turn ends. (A com could instead return a whole turn at once.)
1818+ .choose(&mut rand::thread_rng())
1919+ .cloned()
2020+ .into_iter()
2121+ .collect()
2222+ }
2323}
24242525fn main() {
2626- // Play assigned games forever, authenticated with the com's API token.
2727- run_as_com("http://localhost", "<api_token>", || RandomCom).unwrap();
2626+ if let Err(e) = operate_com("random_com", || RandomCom) {
2727+ eprintln!("fatal: {e}");
2828+ }
2829}
2930```
3031···3940```
40414142`act` is called when it's your turn, and again while the turn continues. Return
4242-the action(s) to play, in order (each a single SGN action from
4343-`game.legal_actions()`), and include `"END"` to end the turn. You can return
4343+the action(s) to play in the correct order, ending with `"END"` to end the turn. You can return
4444one action per call or the whole turn at once. **Each action is validated
4545against the engine before it's sent; illegal actions are skipped and logged.** A
4646call that makes no progress (all-illegal or empty) is retried with the same
···70701. **Log in.** Open [play.stargamb.it](https://play.stargamb.it) and log in (or
7171 create an account if you don't have one).
72722. **Open the COMS screen.** Reach it either way:
7373- - **Settings → Manage COMs**, or
7373+ - **Settings → User → Manage COMs**, or
7474 - **PLAY → VS COM**, then **Manage COMs** in the COM picker dialog.
75753. **Register a COM.** Press **REGISTER A COM** and pick a difficulty. A new COM
7676 slot appears with a permanent handle (e.g. `⎇001-00000000`). You may register
7777 up to three.
78784. **Reveal and copy the token.** On the COM's card, press **Reveal** next to
7979 *API Token* to show it, then **Copy token** (or select the revealed text).
8080- Keep it secret — anyone with the token can play as your COM. Pass it to
8080+ Keep it secret: anyone with the token can play as your COM. Pass it to
8181 `run_as_com` / the examples as `<api_token>`.
82828383## Running a com
···126126cargo run --release --example debug_game -- fire random # trace one game turn by turn
127127```
128128129129-`tests/com_ladder.rs` enforces the ladder (the stronger com never loses).
130130-131131-## How it works
132132-133133-The runner holds `/ws/com` open; each `b_assign` carries a `game_id` and
134134-per-game `game_token`, which it joins over `/ws`. Local state is rebuilt from
135135-the server's `HISTORY` (board config + full SGN replayed through the `stellar`
136136-engine). `legal_actions` enumerates candidate placements/moves/attacks and
137137-keeps only those `engine.execute(...)` accepts, so it matches the server
138138-exactly. Each turn it calls `act`, validates each returned action against the
139139-engine, sends the legal ones (one message each, skipping the rest), and applies
140140-them locally to stay in step; opponent actions and game-end claims are
141141-applied/acked as they arrive, with a re-request of history on any drift. The
142142-join/lobby/`YOU_ARE`/`RESULT` handshake and a 5-second keepalive ping are all
143143-handled for you.
129129+`tests/com_ladder.rs` enforces the ladder (the stronger com never loses).