diff --git a/planetwars-server/src/db/users.rs b/planetwars-server/src/db/users.rs index 1098204..ebb2268 100644 --- a/planetwars-server/src/db/users.rs +++ b/planetwars-server/src/db/users.rs @@ -57,6 +57,12 @@ pub fn create_user(credentials: &Credentials, conn: &PgConnection) -> QueryResul .get_result::(conn) } +pub fn find_user(user_id: i32, db_conn: &PgConnection) -> QueryResult { + users::table + .filter(users::id.eq(user_id)) + .first::(db_conn) +} + pub fn find_user_by_name(username: &str, db_conn: &PgConnection) -> QueryResult { users::table .filter(users::username.eq(username)) diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index ccf7cfc..3ad0c88 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -124,9 +124,9 @@ pub fn api() -> Router { "/bots", get(routes::bots::list_bots).post(routes::bots::create_bot), ) - .route("/bots/:bot_id", get(routes::bots::get_bot)) + .route("/bots/:bot_name", get(routes::bots::get_bot)) .route( - "/bots/:bot_id/upload", + "/bots/:bot_name/upload", post(routes::bots::upload_code_multipart), ) .route("/matches", get(routes::matches::list_matches)) diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 896359c..8de479f 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -20,6 +20,8 @@ use crate::modules::bots::save_code_string; use crate::{DatabaseConnection, GlobalConfig}; use bots::Bot; +use super::users::UserData; + #[derive(Serialize, Deserialize, Debug)] pub struct SaveBotParams { pub bot_name: String, @@ -148,14 +150,23 @@ pub async fn create_bot( // TODO: handle errors pub async fn get_bot( conn: DatabaseConnection, - Path(bot_id): Path, + Path(bot_name): Path, ) -> Result, StatusCode> { - let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; - let bundles = + let bot = db::bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?; + let owner: Option = match bot.owner_id { + Some(user_id) => { + let user = db::users::find_user(user_id, &conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + Some(user.into()) + } + None => None, + }; + let versions = bots::find_bot_versions(bot.id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(Json(json!({ "bot": bot, - "bundles": bundles, + "owner": owner, + "versions": versions, }))) } @@ -187,13 +198,13 @@ pub async fn get_ranking(conn: DatabaseConnection) -> Result pub async fn upload_code_multipart( conn: DatabaseConnection, user: User, - Path(bot_id): Path, + Path(bot_name): Path, mut multipart: Multipart, Extension(config): Extension>, ) -> Result, StatusCode> { let bots_dir = PathBuf::from(&config.bots_directory); - let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; + let bot = bots::find_bot_by_name(&bot_name, &conn).map_err(|_| StatusCode::NOT_FOUND)?; if Some(user.id) != bot.owner_id { return Err(StatusCode::FORBIDDEN); diff --git a/web/pw-server/src/lib/components/Leaderboard.svelte b/web/pw-server/src/lib/components/Leaderboard.svelte index 8582198..d29d5d6 100644 --- a/web/pw-server/src/lib/components/Leaderboard.svelte +++ b/web/pw-server/src/lib/components/Leaderboard.svelte @@ -41,11 +41,17 @@ {formatRating(entry)} - {entry["bot"]["name"]} + + {entry["bot"]["name"]} + {#if entry["author"]} - - {entry["author"]["username"]} + + {entry["author"]["username"]} {/if} @@ -71,7 +77,7 @@ color: #333; } - .leaderboard-author a{ + .leaderboard-href { text-decoration: none; color: black; } diff --git a/web/pw-server/src/lib/components/navbar/UserControls.svelte b/web/pw-server/src/lib/components/navbar/UserControls.svelte index a9bd87b..5646982 100644 --- a/web/pw-server/src/lib/components/navbar/UserControls.svelte +++ b/web/pw-server/src/lib/components/navbar/UserControls.svelte @@ -36,8 +36,8 @@
{#if $currentUser} - - {$currentUser["username"]} + + {$currentUser["username"]}
Sign out
{:else} diff --git a/web/pw-server/src/routes/bots/[bot_id].svelte b/web/pw-server/src/routes/bots/[bot_id].svelte deleted file mode 100644 index 3eece10..0000000 --- a/web/pw-server/src/routes/bots/[bot_id].svelte +++ /dev/null @@ -1,74 +0,0 @@ - - - - -
- {bot["name"]} -
- -
Upload code
-
- - -
- -
    - {#each bundles as bundle} -
  • - bundle created at {dayjs(bundle["created_at"]).format("YYYY-MM-DD HH:mm")} -
  • - {/each} -
diff --git a/web/pw-server/src/routes/bots/[bot_name].svelte b/web/pw-server/src/routes/bots/[bot_name].svelte new file mode 100644 index 0000000..9e9f016 --- /dev/null +++ b/web/pw-server/src/routes/bots/[bot_name].svelte @@ -0,0 +1,139 @@ + + + + + + +
+
+

{bot["name"]}

+ {#if owner} + + {owner["username"]} + + {/if} +
+ + {#if $currentUser && $currentUser["user_id"] === bot["owner_id"]} +
+ + Publish a new version by pushing a docker container to + registry.planetwars.dev/{bot["name"]}:latest, or using the web editor. +
+ {/if} + +
+

Versions

+
    + {#each versions as version} +
  • + {dayjs(version["created_at"]).format("YYYY-MM-DD HH:mm")} +
  • + {/each} +
+
+
+ + diff --git a/web/pw-server/src/routes/users/[user_name].svelte b/web/pw-server/src/routes/users/[user_name].svelte index fab3a96..a1801f4 100644 --- a/web/pw-server/src/routes/users/[user_name].svelte +++ b/web/pw-server/src/routes/users/[user_name].svelte @@ -1,12 +1,4 @@