2022-04-06 11:49:54 +00:00
//Four Line Dropper Backend - A server enabling networked multiplayer for Four Line Dropper
//Copyright (C) 2022 Stephen Seo
//
//This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
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-04-01 11:04:40 +00:00
let ( db_tx , db_rx ) = sync_channel ::< DBHandlerRequest > ( 128 ) ;
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-09-20 10:26:50 +00:00
let route = //warp::body::content_length_limit(1024 * 32)
//.and(warp::body::bytes())
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 {
2022-04-06 04:39:12 +00:00
let body_str_result = std ::str ::from_utf8 ( bytes . as_ref ( ) ) ;
2022-03-18 10:29:38 +00:00
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-09-20 10:26:50 +00:00
let reply = warp ::reply ::with_header (
2022-04-06 04:39:12 +00:00
json_handlers ::handle_json ( json_value , db_tx_clone , s_helper_tx_clone )
2022-03-18 14:43:15 +00:00
. unwrap_or_else ( | e | e ) ,
2022-04-06 04:39:12 +00:00
" Content-Type " ,
" application/json " ,
2022-09-20 10:26:50 +00:00
) ;
let reply = warp ::reply ::with_header (
reply ,
" Access-Control-Allow-Headers " ,
" * " ,
) ;
Ok ::< Box < dyn warp ::reply ::Reply > , Rejection > ( Box ::new (
warp ::reply ::with_header ( reply , " Access-Control-Allow-Origin " , " * " ) ,
2022-04-05 09:16:04 +00:00
) )
2022-03-18 10:29:38 +00:00
} else {
2022-09-20 10:26:50 +00:00
let reply = warp ::reply ::with_header (
2022-04-06 04:39:12 +00:00
String ::from ( " { \" type \" : \" invalid_syntax \" } " ) ,
" Content-Type " ,
" application/json " ,
2022-09-20 10:26:50 +00:00
) ;
let reply = warp ::reply ::with_header (
reply ,
" Access-Control-Allow-Headers " ,
" * " ,
) ;
Ok ::< Box < dyn warp ::reply ::Reply > , Rejection > ( Box ::new (
warp ::reply ::with_header ( reply , " Access-Control-Allow-Origin " , " * " ) ,
2022-04-06 04:39:12 +00:00
) )
2022-03-18 10:29:38 +00:00
}
2022-03-16 04:09:07 +00:00
} else {
2022-09-20 10:26:50 +00:00
let reply = warp ::reply ::with_header (
2022-04-06 04:39:12 +00:00
String ::from ( " { \" type \" : \" invalid_syntax \" } " ) ,
" Content-Type " ,
" application/json " ,
2022-09-20 10:26:50 +00:00
) ;
let reply = warp ::reply ::with_header (
reply ,
" Access-Control-Allow-Headers " ,
" * " ,
) ;
Ok ::< Box < dyn warp ::reply ::Reply > , Rejection > ( Box ::new (
warp ::reply ::with_header ( reply , " Access-Control-Allow-Origin " , " * " ) ,
2022-04-06 04:39:12 +00:00
) )
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
}