move structs
This commit is contained in:
parent
33d81fd316
commit
840a839541
4 changed files with 42 additions and 42 deletions
41
src/api.rs
41
src/api.rs
|
@ -1,49 +1,10 @@
|
|||
use crate::gamemaster::*;
|
||||
use crate::AppState;
|
||||
use axum::{
|
||||
extract::{
|
||||
ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
ConnectInfo, State,
|
||||
},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use axum::extract::ws::{Message, WebSocket};
|
||||
use futures::{sink::SinkExt, stream::StreamExt};
|
||||
use serde::Deserialize;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
pub mod message_handler;
|
||||
use crate::message_handler::*;
|
||||
|
||||
/// New game request structure
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct NewGameRequest {
|
||||
/// Game name
|
||||
pub name: String,
|
||||
/// Game host
|
||||
pub host: CAHPlayer,
|
||||
/// Chosen packs
|
||||
pub packs: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Game join request structure
|
||||
pub struct GameJoinRequest {
|
||||
/// Game id
|
||||
pub id: u8, // increase later
|
||||
/// Game password
|
||||
pub password: String,
|
||||
/// Player info
|
||||
pub player: CAHPlayer,
|
||||
}
|
||||
|
||||
pub async fn websocket_handler(
|
||||
ws: WebSocketUpgrade,
|
||||
// user_agent: Option<TypedHeader<headers::UserAgent>>,
|
||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> impl IntoResponse {
|
||||
tracing::debug!("New connection from {addr}");
|
||||
ws.on_upgrade(move |socket| websocket(socket, state, addr))
|
||||
}
|
||||
|
||||
fn greeting(state: &Arc<AppState>) -> String {
|
||||
format!(
|
||||
"{:#?} Card packs loaded\n\
|
||||
|
|
|
@ -1,8 +1,45 @@
|
|||
use crate::api::{greeting, Message, SocketAddr};
|
||||
use crate::websocket;
|
||||
use crate::AppState;
|
||||
use crate::Arc;
|
||||
use crate::CAHGame;
|
||||
use crate::NewGameRequest;
|
||||
use crate::CAHPlayer;
|
||||
use axum::{
|
||||
extract::{ConnectInfo, State, WebSocketUpgrade},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
/// New game request structure
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct NewGameRequest {
|
||||
/// Game name
|
||||
pub name: String,
|
||||
/// Game host
|
||||
pub host: CAHPlayer,
|
||||
/// Chosen packs
|
||||
pub packs: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Game join request structure
|
||||
pub struct GameJoinRequest {
|
||||
/// Game id
|
||||
pub id: u8, // increase later
|
||||
/// Game password
|
||||
pub password: String,
|
||||
/// Player info
|
||||
pub player: CAHPlayer,
|
||||
}
|
||||
|
||||
pub async fn websocket_handler(
|
||||
ws: WebSocketUpgrade,
|
||||
// user_agent: Option<TypedHeader<headers::UserAgent>>,
|
||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> impl IntoResponse {
|
||||
tracing::debug!("New connection from {addr}");
|
||||
ws.on_upgrade(move |socket| websocket(socket, state, addr))
|
||||
}
|
||||
|
||||
pub async fn message_handler(message: Message, state: &Arc<AppState>, who: SocketAddr) {
|
||||
let tx = &state.tx;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api::NewGameRequest; // change this
|
||||
use crate::message_handler::*; //change this
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -101,6 +101,7 @@ impl CAHGame {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn new(request: NewGameRequest) -> Result<Self> {
|
||||
let mut game = CAHGame {
|
||||
..Default::default()
|
||||
|
|
|
@ -13,6 +13,7 @@ pub mod gamemaster;
|
|||
use crate::gamemaster::*;
|
||||
pub mod api;
|
||||
use crate::api::*;
|
||||
use crate::message_handler::*;
|
||||
|
||||
/// Parse json for card data
|
||||
fn load_json(path: &str) -> Result<Vec<CAHCardSet>, Box<dyn Error>> {
|
||||
|
|
Loading…
Add table
Reference in a new issue