match form stubs

This commit is contained in:
Ilion Beyst 2022-01-01 11:26:49 +01:00
parent 8d3b8fd8aa
commit e145947d05
7 changed files with 112 additions and 1 deletions

View file

@ -34,6 +34,11 @@ pub fn find_bots_by_owner(owner_id: i32, conn: &PgConnection) -> QueryResult<Vec
.get_results(conn) .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)] #[derive(Insertable)]
#[table_name = "code_bundles"] #[table_name = "code_bundles"]
pub struct NewCodeBundle<'a> { pub struct NewCodeBundle<'a> {

View file

@ -33,13 +33,17 @@ pub async fn api() -> Router {
.route("/register", post(routes::users::register)) .route("/register", post(routes::users::register))
.route("/login", post(routes::users::login)) .route("/login", post(routes::users::login))
.route("/users/me", get(routes::users::current_user)) .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/my_bots", get(routes::bots::get_my_bots))
.route("/bots/:bot_id", get(routes::bots::get_bot)) .route("/bots/:bot_id", get(routes::bots::get_bot))
.route( .route(
"/bots/:bot_id/upload", "/bots/:bot_id/upload",
post(routes::bots::upload_code_multipart), post(routes::bots::upload_code_multipart),
) )
.route("/matches", post(routes::matches::play_match))
.layer(AddExtensionLayer::new(pool)); .layer(AddExtensionLayer::new(pool));
api api
} }

View file

@ -58,6 +58,12 @@ pub async fn get_my_bots(
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) .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 // TODO: currently this only implements the happy flow
pub async fn upload_code_multipart( pub async fn upload_code_multipart(
conn: DatabaseConnection, conn: DatabaseConnection,

View file

@ -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);
}

View file

@ -1,2 +1,3 @@
pub mod bots; pub mod bots;
pub mod matches;
pub mod users; pub mod users;

View file

@ -0,0 +1 @@
<a href="/matches/new">new match</a>

View file

@ -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>