diff options
author | Daniel Jones <daniel@danieljon.es> | 2024-08-29 17:34:49 +1000 |
---|---|---|
committer | Daniel Jones <daniel@danieljon.es> | 2024-08-29 17:34:49 +1000 |
commit | 3aee60f52b146bcfc010afab78331872180ecf64 (patch) | |
tree | a2c04125204bc3744ac20908a4b765ea6a0a3af1 /src | |
download | cstracker-3aee60f52b146bcfc010afab78331872180ecf64.tar.gz cstracker-3aee60f52b146bcfc010afab78331872180ecf64.zip |
initial GSI implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1b2c8c7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,99 @@ +use hyper::{Body, Request, Response, Server, service::{make_service_fn, service_fn}}; +use serde_json::Value; +use std::convert::Infallible; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct MatchData { + map: Option<MapData>, + round: Option<RoundData>, + player_id: Option<PlayerIdData>, + player_state: Option<PlayerStateData>, + player_weapons: Option<PlayerWeaponsData>, + player_match_stats: Option<PlayerMatchStatsData>, + allplayers_id: Option<AllPlayersIdData>, +} + +#[derive(Debug, Deserialize)] +struct MapData { + name: Option<String>, + // Add other map-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct RoundData { + phase: Option<String>, + // Add other round-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerIdData { + id: Option<u32>, + // Add other player_id-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerStateData { + health: Option<u32>, + armor: Option<u32>, + flashed: Option<u32>, + // Add other player_state-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerWeaponsData { + primary: Option<String>, + secondary: Option<String>, + // Add other player_weapons-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct PlayerMatchStatsData { + kills: Option<u32>, + deaths: Option<u32>, + // Add other player_match_stats-specific fields as necessary +} + +#[derive(Debug, Deserialize)] +struct AllPlayersIdData { + players: Vec<PlayerIdData>, + // Add other allplayers_id-specific fields as necessary +} + +async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> { + if req.method() == hyper::Method::POST { + let whole_body = hyper::body::to_bytes(req.into_body()).await.unwrap(); + let json: Value = serde_json::from_slice(&whole_body).unwrap(); + + // Print the raw JSON for debugging + println!("Received JSON: {}", json); + + // Print out the parsed data for debugging + if let Some(map) = json.as_object() { + for (key, value) in map { + println!("{}: {}", key, value); + } + } else { + println!("Received data is not a JSON object"); + } + + // Respond to the GSI service + Ok(Response::new(Body::from("Received"))) + } else { + Ok(Response::new(Body::from("Unsupported HTTP method"))) + } +} + +#[tokio::main] +async fn main() { + let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); + + let addr = ([0, 0, 0, 0], 5000).into(); // Listen on all interfaces on port 3000 + let server = Server::bind(&addr).serve(make_svc); + + println!("Listening on http://{}", addr); + + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } +}
\ No newline at end of file |