diff --git a/src/lib.rs b/src/lib.rs index d0d2d5b..e49447c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,6 +59,7 @@ async fn main(req: Request, env: Env, _: Context) -> Result { .get_async("/admin/:teamkey/:teamsecret", admin) .post_async("/admin/:teamkey/:teamsecret/player", add_player) .post_async("/admin/:teamkey/:teamsecret/player/:playerid/delete", delete_player) + .post_async("/admin/:teamkey/:teamsecret/reset_game", reset_game) .get_async("/team/:teamkey", team) .post_async("/team/:teamkey/new_game", new_game) .post_async("/team/:teamkey/player/:playerid/play", play) @@ -152,10 +153,8 @@ async fn admin(_: Request, ctx: RouteContext) -> Result { template .render(mjctx! { - team_name => team.name, key, - secret, - players => team.players, + team, }) .map_or( Response::error("failed to render team_admin page", 500), @@ -248,6 +247,44 @@ async fn delete_player(req: Request, ctx: RouteContext) -> Result) -> Result { + let auth_err = Response::error("team not found", 404); + + let key = ctx.param("teamkey").unwrap(); + let secret = ctx.param("teamsecret").unwrap(); + + let teams_kv = ctx.kv("teams")?; + + let mut team: Team = { + let t = teams_kv.get(key).text().await?; + if t.is_none() { + return auth_err; + } + let t = t.unwrap(); + serde_json::from_str(&t).unwrap() + }; + + if &team.secret != secret { + return auth_err; + } + + team.next_game = None; + + return match teams_kv + .put(key, serde_json::to_string(&team).unwrap())? + .execute() + .await + { + Ok(_) => { + let mut admin_link = req.url()?.clone(); + admin_link.set_path(&format!("/admin/{}/{}", key, secret)); + + Response::redirect(admin_link) + } + Err(_) => Response::error("failed to reset game", 500), + }; +} + async fn team(_: Request, ctx: RouteContext) -> Result { let not_found = Response::error("team not found", 404); diff --git a/templates/head.html b/templates/head.html index d185603..b4b8753 100644 --- a/templates/head.html +++ b/templates/head.html @@ -2,7 +2,7 @@ - nextgame{% if team_name %} :: {{ team_name }}{% endif %} + nextgame diff --git a/templates/team_admin.html b/templates/team_admin.html index fa693a0..e092bff 100644 --- a/templates/team_admin.html +++ b/templates/team_admin.html @@ -7,7 +7,7 @@

- nextgame :: {{ team_name }} + nextgame :: {{ team.name }}

Manage players, and more (to come).. @@ -16,8 +16,22 @@

+ {% if team.next_game %}
-
+ +
+
+ +
+
+
+
+ {% endif %} + +
+
@@ -36,13 +50,13 @@

- {% for id, pn in players|items %} + {% for id, pn in team.players|items %}
{{ pn }} - +