2022-03-29 05:45:49 +00:00
|
|
|
mod ai;
|
|
|
|
mod constants;
|
2022-03-18 10:29:38 +00:00
|
|
|
mod db_handler;
|
2022-03-29 05:45:49 +00:00
|
|
|
mod game_logic;
|
2022-03-16 03:26:18 +00:00
|
|
|
mod json_handlers;
|
2022-03-29 05:45:49 +00:00
|
|
|
mod random_helper;
|
|
|
|
mod state;
|
2022-03-16 03:26:18 +00:00
|
|
|
|
2022-03-18 10:29:38 +00:00
|
|
|
const SQLITE_DB_PATH: &str = "./fourLineDropper.db";
|
|
|
|
|
2022-03-28 07:31:53 +00:00
|
|
|
use db_handler::DBHandlerRequest;
|
|
|
|
|
|
|
|
use std::sync::mpsc::sync_channel;
|
2022-03-18 10:29:38 +00:00
|
|
|
|
|
|
|
use db_handler::start_db_handler_thread;
|
2022-03-18 14:43:15 +00:00
|
|
|
use tokio::sync::oneshot;
|
2022-03-16 04:10:21 +00:00
|
|
|
use warp::{Filter, Rejection};
|
2022-03-16 03:26:18 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-03-28 07:31:53 +00:00
|
|
|
let (db_tx, db_rx) = sync_channel::<DBHandlerRequest>(32);
|
2022-03-18 10:29:38 +00:00
|
|
|
let db_tx_clone = db_tx.clone();
|
|
|
|
|
2022-03-18 14:43:15 +00:00
|
|
|
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
|
|
|
|
|
|
|
|
// Required because shutdown_tx is not cloneable, and its "send" consumes
|
|
|
|
// itself.
|
|
|
|
let (s_helper_tx, s_helper_rx) = sync_channel::<()>(1);
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
if let Ok(_unused_value) = s_helper_rx.recv() {
|
|
|
|
shutdown_tx
|
|
|
|
.send(())
|
|
|
|
.expect("Should be able to send shutdown signal");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
start_db_handler_thread(db_rx, SQLITE_DB_PATH.into(), s_helper_tx.clone());
|
2022-03-18 10:29:38 +00:00
|
|
|
|
2022-03-16 03:26:18 +00:00
|
|
|
let route = warp::body::content_length_limit(1024 * 32)
|
2022-03-16 04:09:07 +00:00
|
|
|
.and(warp::body::bytes())
|
2022-03-18 10:29:38 +00:00
|
|
|
.and_then(move |bytes: bytes::Bytes| {
|
|
|
|
let db_tx_clone = db_tx_clone.clone();
|
2022-03-18 14:43:15 +00:00
|
|
|
let s_helper_tx_clone = s_helper_tx.clone();
|
2022-03-18 10:29:38 +00:00
|
|
|
async move {
|
|
|
|
let body_str_result = std::str::from_utf8(bytes.as_ref());
|
|
|
|
if let Ok(body_str) = body_str_result {
|
|
|
|
let json_result = serde_json::from_str(body_str);
|
|
|
|
if let Ok(json_value) = json_result {
|
2022-03-18 14:43:15 +00:00
|
|
|
Ok(
|
|
|
|
json_handlers::handle_json(json_value, db_tx_clone, s_helper_tx_clone)
|
|
|
|
.unwrap_or_else(|e| e),
|
|
|
|
)
|
2022-03-18 10:29:38 +00:00
|
|
|
} else {
|
|
|
|
Ok(String::from("{\"type\": \"invalid_syntax\"}"))
|
|
|
|
}
|
2022-03-16 04:09:07 +00:00
|
|
|
} else {
|
2022-03-18 10:29:38 +00:00
|
|
|
Ok::<String, Rejection>(String::from("{\"type\": \"invalid_syntax\"}"))
|
2022-03-16 04:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 03:26:18 +00:00
|
|
|
});
|
|
|
|
|
2022-03-18 14:43:15 +00:00
|
|
|
let (_addr, server) =
|
|
|
|
warp::serve(route).bind_with_graceful_shutdown(([0, 0, 0, 0], 1237), async move {
|
|
|
|
shutdown_rx.await.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
tokio::task::spawn(server).await.unwrap();
|
2022-03-14 05:33:51 +00:00
|
|
|
}
|