diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-01 11:26:49 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-01-01 11:26:49 +0100 |
commit | e145947d052450618af3ba094e66a27c3c7f86e4 (patch) | |
tree | 5aff15f6ffb98710323b0ebadb58f733e75cbead | |
parent | 8d3b8fd8aa9603f5377f1b37963190ac2c01e9dd (diff) | |
download | planetwars.dev-e145947d052450618af3ba094e66a27c3c7f86e4.tar.xz planetwars.dev-e145947d052450618af3ba094e66a27c3c7f86e4.zip |
match form stubs
-rw-r--r-- | planetwars-server/src/db/bots.rs | 5 | ||||
-rw-r--r-- | planetwars-server/src/lib.rs | 6 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs | 6 | ||||
-rw-r--r-- | planetwars-server/src/routes/matches.rs | 12 | ||||
-rw-r--r-- | planetwars-server/src/routes/mod.rs | 1 | ||||
-rw-r--r-- | web/pw-server/src/routes/matches/index.svelte | 1 | ||||
-rw-r--r-- | web/pw-server/src/routes/matches/new.svelte | 82 |
7 files changed, 112 insertions, 1 deletions
diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index eb66c05..bef69c0 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -34,6 +34,11 @@ pub fn find_bots_by_owner(owner_id: i32, conn: &PgConnection) -> QueryResult<Vec .get_results(conn) } +pub fn find_all_bots(conn: &PgConnection) -> QueryResult<Vec<Bot>> { + // TODO: filter out bots that cannot be run (have no valid code bundle associated with them) + bots::table.get_results(conn) +} + #[derive(Insertable)] #[table_name = "code_bundles"] pub struct NewCodeBundle<'a> { diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 46c1100..01e9041 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -33,13 +33,17 @@ pub async fn api() -> Router { .route("/register", post(routes::users::register)) .route("/login", post(routes::users::login)) .route("/users/me", get(routes::users::current_user)) - .route("/bots", post(routes::bots::create_bot)) + .route( + "/bots", + get(routes::bots::list_bots).post(routes::bots::create_bot), + ) .route("/bots/my_bots", get(routes::bots::get_my_bots)) .route("/bots/:bot_id", get(routes::bots::get_bot)) .route( "/bots/:bot_id/upload", post(routes::bots::upload_code_multipart), ) + .route("/matches", post(routes::matches::play_match)) .layer(AddExtensionLayer::new(pool)); api } diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index f722e52..8327443 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -58,6 +58,12 @@ pub async fn get_my_bots( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) } +pub async fn list_bots(conn: DatabaseConnection) -> Result<Json<Vec<Bot>>, StatusCode> { + bots::find_all_bots(&conn) + .map(Json) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) +} + // TODO: currently this only implements the happy flow pub async fn upload_code_multipart( conn: DatabaseConnection, diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs new file mode 100644 index 0000000..7eca6ab --- /dev/null +++ b/planetwars-server/src/routes/matches.rs @@ -0,0 +1,12 @@ +use axum::Json; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct MatchParams { + // Just bot ids for now + players: Vec<i32>, +} + +pub async fn play_match(params: Json<MatchParams>) { + println!("start match: {:#?}", params); +} diff --git a/planetwars-server/src/routes/mod.rs b/planetwars-server/src/routes/mod.rs index 718d7ef..c2d3c44 100644 --- a/planetwars-server/src/routes/mod.rs +++ b/planetwars-server/src/routes/mod.rs @@ -1,2 +1,3 @@ pub mod bots; +pub mod matches; pub mod users; diff --git a/web/pw-server/src/routes/matches/index.svelte b/web/pw-server/src/routes/matches/index.svelte new file mode 100644 index 0000000..dcfb43b --- /dev/null +++ b/web/pw-server/src/routes/matches/index.svelte @@ -0,0 +1 @@ +<a href="/matches/new">new match</a> diff --git a/web/pw-server/src/routes/matches/new.svelte b/web/pw-server/src/routes/matches/new.svelte new file mode 100644 index 0000000..10ce093 --- /dev/null +++ b/web/pw-server/src/routes/matches/new.svelte @@ -0,0 +1,82 @@ +<script lang="ts" context="module"> + import { get_session_token } from "$lib/auth"; + import { mount_component } from "svelte/internal"; + + export async function load({ page }) { + const token = get_session_token(); + const res = await fetch("/api/bots", { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); + + if (res.ok) { + return { + props: { + bots: await res.json(), + }, + }; + } + + return { + status: res.status, + error: new Error("Could not load bot"), + }; + } +</script> + +<script lang="ts"> + import Select from "svelte-select"; + export let bots: object[]; + let items: object[]; + let players: object[] = []; + let selected: object | null = null; + + $: items = bots.map((bot) => { + return { + value: bot["id"], + label: bot["name"], + }; + }); + + function addPlayer() { + if (selected === null) { + return; + } + + players.push(selected); + players = players; + } + + async function submitMatch() { + const token = get_session_token(); + const res = await fetch("/api/matches", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + players: players.map((player) => player["value"]), + }), + }); + + if (res.ok) { + // TODO + } else { + alert(res.statusText); + } + } +</script> + +Select players: +<Select {items} bind:value={selected} /> +<button on:click={addPlayer}>add</button> +<h3>Selected Players</h3> +<ol> + {#each players as player} + <li>{player["label"]}</li> + {/each} +</ol> +<button on:click={submitMatch}>Play</button> |