1
Fork 0
mirror of https://github.com/Steffo99/distributed-arcade.git synced 2024-10-16 06:27:30 +00:00

Use ZADD CH to return different status codes based on the score change

This commit is contained in:
Steffo 2022-11-12 16:47:00 +01:00
parent ea90ad7b51
commit b88f66e1e1
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 22 additions and 2 deletions

View file

@ -138,6 +138,22 @@ paths:
security:
- XBoardToken: []
responses:
200:
description: "Score discarded as it was worse than the previous one"
content:
application/json:
schema:
type: number
description: "The previous score."
example: 2468.12
201:
description: "Score submitted and updated"
content:
application/json:
schema:
type: number
description: "The new score."
example: 1234.56
401:
description: "Missing, invalid or malformed Authorization header"
content:

View file

@ -93,7 +93,8 @@ pub(crate) async fn route_score_put(
log::trace!("Sorting order is: {order:?}");
log::trace!("Inserting score: {score:?}");
redis::cmd("ZADD").arg(&scores_key).arg(order.zadd_mode()).arg(&score).arg(&player).query_async(&mut rconn).await
let changed = redis::cmd("ZADD").arg(&scores_key).arg(order.zadd_mode()).arg("CH").arg(&score).arg(&player)
.query_async::<redis::aio::Connection, i32>(&mut rconn).await
.map_err(|_| outcome::redis_cmd_failed())?;
log::trace!("Getting the new score...");
@ -102,7 +103,10 @@ pub(crate) async fn route_score_put(
log::trace!("Received score: {nscore:?}");
Ok((
StatusCode::OK,
match changed.gt(&0) {
true => StatusCode::CREATED,
false => StatusCode::OK,
},
outcome::req_success!(nscore)
))
}