summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 1b2c8c7b3e3a37d49fba3872339cf9878917b732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
    }
}