Skip to content

Commit

Permalink
Add manual reset game button
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarhead committed Sep 27, 2024
1 parent 6ec855b commit 5ec8be7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
43 changes: 40 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async fn main(req: Request, env: Env, _: Context) -> Result<Response> {
.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)
Expand Down Expand Up @@ -152,10 +153,8 @@ async fn admin(_: Request, ctx: RouteContext<AppCtx>) -> Result<Response> {

template
.render(mjctx! {
team_name => team.name,
key,
secret,
players => team.players,
team,
})
.map_or(
Response::error("failed to render team_admin page", 500),
Expand Down Expand Up @@ -248,6 +247,44 @@ async fn delete_player(req: Request, ctx: RouteContext<AppCtx>) -> Result<Respon
};
}

async fn reset_game(req: Request, ctx: RouteContext<AppCtx>) -> Result<Response> {
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<AppCtx>) -> Result<Response> {
let not_found = Response::error("team not found", 404);

Expand Down
2 changes: 1 addition & 1 deletion templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>nextgame{% if team_name %} :: {{ team_name }}{% endif %}</title>
<title>nextgame</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css">
</head>
22 changes: 18 additions & 4 deletions templates/team_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<section class="hero is-info">
<div class="hero-body">
<h1 class="title">
nextgame :: {{ team_name }}
nextgame :: {{ team.name }}
</h1>
<p class="subtitle">
Manage players, and more (to come)..
Expand All @@ -16,8 +16,22 @@ <h1 class="title">
</section>

<section class="section">
{% if team.next_game %}
<div class="container is-max-desktop">
<form action="/admin/{{ key }}/{{ secret }}/player" method="post">
<form action="/admin/{{ key }}/{{ team.secret }}/reset_game" method="post">
<div class="field">
<div class="control">
<button type="submit" class="button is-medium is-warning">
Force reset game
</button>
</div>
</div>
</form>
</div>
{% endif %}

<div class="container is-max-desktop">
<form action="/admin/{{ key }}/{{ team.secret }}/player" method="post">
<div class="field has-addons">
<div class="control is-expanded">
<input type="text" name="player_name" class="input is-medium" placeholder="Player name">
Expand All @@ -36,13 +50,13 @@ <h1 class="title">
<div class="container is-max-desktop">
<table class="table is-striped is-hoverable is-fullwidth">
<tbody>
{% for id, pn in players|items %}
{% for id, pn in team.players|items %}
<tr>
<th class="is-two-thirds">
{{ pn }}
</th>
<td>
<form action="/admin/{{ key }}/{{ secret }}/player/{{ id }}/delete" method="post">
<form action="/admin/{{ key }}/{{ team.secret }}/player/{{ id }}/delete" method="post">
<button type="submit" class="button is-danger">
Delete
</button>
Expand Down

0 comments on commit 5ec8be7

Please sign in to comment.