Skip to content

Commit

Permalink
Merge pull request #12 from amiller68/board-lifecycle
Browse files Browse the repository at this point in the history
Board lifecycle
  • Loading branch information
amiller68 authored Jan 25, 2024
2 parents 5bdeeae + a9e7b13 commit 78e8b94
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 136 deletions.

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

0 comments on commit 78e8b94

Please sign in to comment.