Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board lifecycle #12

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 36 additions & 17 deletions src/api/games/make_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use axum::{
};
use sqlx::types::Uuid;

use crate::api::models::ApiBoard;
use crate::database::models::{Game, GameError};
use crate::api::models::ApiGameBoard;
use crate::database::models::{Game, GameBoard, GameError};
use crate::AppState;

#[derive(serde::Deserialize)]
pub struct MakeMoveRequest {
#[serde(rename = "uciMove")]
uci_move: String,
resign: Option<bool>,
}

pub async fn handler(
Expand All @@ -22,31 +23,39 @@ pub async fn handler(
Form(request): Form<MakeMoveRequest>,
) -> Result<impl IntoResponse, ReadBoardError> {
let uci_move = request.uci_move;
let resign = request.resign.unwrap_or(false);
let mut conn = state.database().begin().await?;
let maybe_board = Game::make_move(&mut conn, game_id, &uci_move).await;
let board = match maybe_board {
Ok(board) => board,
Err(e) => match e {
GameError::InvalidMove(_, board) => board,
_ => return Err(e.into()),
},
};
if !Game::exists(&mut conn, game_id).await? {
return Err(ReadBoardError::NotFound);
}

// Returns the updated board if the move was valid. Otherwise, returns the latest board.
let game_board = GameBoard::make_move(&mut conn, game_id, &uci_move, resign).await?;

// If we got here, then either we made a valid move
// or no changes were made to the database (invalid move)
conn.commit().await?;

let api_board = ApiBoard {
let board = game_board.board().clone();
let status = game_board.status().clone();
let winner = game_board.winner().clone();
let outcome = game_board.outcome().clone();
let game_id = game_id.to_string();
let api_board = ApiGameBoard {
game_id,
board,
game_id: game_id.to_string(),
status,
winner,
outcome,
};

Ok(TemplateApiBoard { api_board })
Ok(TemplateApiGameBoard { api_board })
}

#[derive(Template)]
#[template(path = "board.html")]
struct TemplateApiBoard {
api_board: ApiBoard,
struct TemplateApiGameBoard {
api_board: ApiGameBoard,
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -55,11 +64,21 @@ pub enum ReadBoardError {
Sqlx(#[from] sqlx::Error),
#[error("game error: {0}")]
Game(#[from] GameError),
#[error("game not found")]
NotFound,
}

impl IntoResponse for ReadBoardError {
fn into_response(self) -> Response {
let body = format!("{}", self);
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
match self {
ReadBoardError::NotFound => {
let body = format!("{}", self);
(axum::http::StatusCode::NOT_FOUND, body).into_response()
}
_ => {
let body = format!("{}", self);
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}
}
}
}
35 changes: 26 additions & 9 deletions src/api/games/read_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ use axum::{
};
use sqlx::types::Uuid;

use crate::api::models::ApiBoard;
use crate::database::models::{Game, GameError};
use crate::api::models::ApiGameBoard;
use crate::database::models::{Game, GameBoard, GameError};
use crate::AppState;

pub async fn handler(
State(state): State<AppState>,
Path(game_id): Path<Uuid>,
) -> Result<impl IntoResponse, ReadBoardError> {
let mut conn = state.database().acquire().await?;
let board = Game::latest_board(&mut conn, game_id).await?;
let api_board = ApiBoard {
if !Game::exists(&mut conn, game_id).await? {
return Err(ReadBoardError::NotFound);
}
let game_board = GameBoard::latest(&mut conn, game_id).await?;
let board = game_board.board().clone();
let api_board = ApiGameBoard {
board,
status: game_board.status().clone(),
winner: game_board.winner().clone(),
outcome: game_board.outcome().clone(),
game_id: game_id.to_string(),
};

Ok(TemplateApiBoard { api_board })
Ok(TemplateApiGameBoard { api_board })
}

#[derive(Template)]
#[template(path = "board.html")]
struct TemplateApiBoard {
api_board: ApiBoard,
struct TemplateApiGameBoard {
api_board: ApiGameBoard,
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -35,11 +42,21 @@ pub enum ReadBoardError {
Sqlx(#[from] sqlx::Error),
#[error("game error: {0}")]
Game(#[from] GameError),
#[error("game not found")]
NotFound,
}

impl IntoResponse for ReadBoardError {
fn into_response(self) -> Response {
let body = format!("{}", self);
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
match self {
ReadBoardError::NotFound => {
let body = format!("{}", self);
(axum::http::StatusCode::NOT_FOUND, body).into_response()
}
_ => {
let body = format!("{}", self);
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}
}
}
}
Loading
Loading