|
@@ -1,17 +1,13 @@
|
|
|
-use actix_web::{HttpRequest, Responder, web, post};
|
|
|
|
|
|
|
+use actix_web::{HttpResponse, HttpRequest, web, post};
|
|
|
use sqlx::PgPool;
|
|
use sqlx::PgPool;
|
|
|
use uuid::Uuid;
|
|
use uuid::Uuid;
|
|
|
-use tokio::sync::oneshot::Receiver;
|
|
|
|
|
use crate::{
|
|
use crate::{
|
|
|
http_error::HttpError,
|
|
http_error::HttpError,
|
|
|
auth::user_auth,
|
|
auth::user_auth,
|
|
|
models::{
|
|
models::{
|
|
|
- user::User,
|
|
|
|
|
- game::Game,
|
|
|
|
|
session::Session,
|
|
session::Session,
|
|
|
- turn::Turn
|
|
|
|
|
- },
|
|
|
|
|
- logic::prompt
|
|
|
|
|
|
|
+ game::Game
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
#[post("/game/{game_id}/session")]
|
|
#[post("/game/{game_id}/session")]
|
|
@@ -19,39 +15,11 @@ pub async fn route(
|
|
|
db: web::Data<PgPool>,
|
|
db: web::Data<PgPool>,
|
|
|
path: web::Path<String>,
|
|
path: web::Path<String>,
|
|
|
req: HttpRequest
|
|
req: HttpRequest
|
|
|
-) -> Result<impl Responder, HttpError> {
|
|
|
|
|
|
|
+) -> Result<HttpResponse, HttpError> {
|
|
|
let user = user_auth(&db, &req, true).await?;
|
|
let user = user_auth(&db, &req, true).await?;
|
|
|
let game_id = Uuid::parse_str(&path.into_inner())?;
|
|
let game_id = Uuid::parse_str(&path.into_inner())?;
|
|
|
- let (game, mut characters, sessions) = Game::get_full_game(&db, game_id.clone(), Some(user.id)).await?;
|
|
|
|
|
- let session = Session::new(game_id);
|
|
|
|
|
- let response = prompt(
|
|
|
|
|
- game,
|
|
|
|
|
- characters.remove(0),
|
|
|
|
|
- sessions,
|
|
|
|
|
- vec![],
|
|
|
|
|
- None,
|
|
|
|
|
- true
|
|
|
|
|
- ).await?;
|
|
|
|
|
- update_db(response.message, session, "".to_string(), user.id, db);
|
|
|
|
|
- Ok(response.sse)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-fn update_db(
|
|
|
|
|
- message: Receiver<(String, i32, i32)>,
|
|
|
|
|
- session: Session,
|
|
|
|
|
- user_text: String,
|
|
|
|
|
- user_id: Uuid,
|
|
|
|
|
- db: web::Data<PgPool>
|
|
|
|
|
-) {
|
|
|
|
|
- tokio::spawn(async move {
|
|
|
|
|
- let _ = if let Ok((m, it, ot)) = message.await {
|
|
|
|
|
- let new_turn = Turn::new(session.id, it, ot, user_text, m);
|
|
|
|
|
- let _ = session.insert(&db).await;
|
|
|
|
|
- let _ = new_turn.insert(&db).await;
|
|
|
|
|
- let _ = User::add_tokens(&db, user_id, -new_turn.tokens_used).await;
|
|
|
|
|
- Ok::<(), HttpError>(())
|
|
|
|
|
- } else {
|
|
|
|
|
- Ok::<(), HttpError>(())
|
|
|
|
|
- };
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ let game = Game::get_one(&db, game_id, Some(user.id)).await?;
|
|
|
|
|
+ let session = Session::new(game.id);
|
|
|
|
|
+ session.insert(&db).await?;
|
|
|
|
|
+ Ok(HttpResponse::Ok().json(session))
|
|
|
}
|
|
}
|