|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use axum::{extract::State, http::StatusCode, routing::post, Json, Router}; |
| 4 | +use log::info; |
| 5 | +use rand::{rngs::StdRng, Rng, SeedableRng}; |
| 6 | +use tokio::sync::{Mutex, RwLock}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + config::{self, Config}, |
| 10 | + gamestateintegration::{MapPhase, Payload, RoundPhase}, |
| 11 | + pishock, AppState, GameState, PlayerState, |
| 12 | +}; |
| 13 | + |
| 14 | +pub async fn run(config: Arc<RwLock<Config>>) { |
| 15 | + info!("Sending test beep"); |
| 16 | + pishock::beep(config.clone(), 1).await; |
| 17 | + |
| 18 | + let state = AppState { |
| 19 | + game_state: Arc::from(Mutex::from(GameState::default())), |
| 20 | + config: config.clone(), |
| 21 | + }; |
| 22 | + |
| 23 | + let app = Router::new() |
| 24 | + .route("/data", post(read_data)) |
| 25 | + .with_state(state); |
| 26 | + |
| 27 | + info!("Starting server on {}", "127.0.0.1:3000"); |
| 28 | + |
| 29 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") |
| 30 | + .await |
| 31 | + .unwrap(); |
| 32 | + axum::serve(listener, app).await.unwrap(); |
| 33 | +} |
| 34 | + |
| 35 | +async fn read_data(State(state): State<AppState>, Json(payload): Json<Payload>) -> StatusCode { |
| 36 | + let mut game_state = state.game_state.lock().await; |
| 37 | + let config = state.config.read().await; |
| 38 | + |
| 39 | + if let Some(provider) = payload.provider { |
| 40 | + game_state.steam_id = provider.steamid; |
| 41 | + } |
| 42 | + |
| 43 | + if let Some(map) = payload.map { |
| 44 | + if game_state.map_phase == MapPhase::Warmup && map.phase == MapPhase::Live { |
| 45 | + info!("Match started"); |
| 46 | + |
| 47 | + if config.beep_on_match_start { |
| 48 | + pishock::beep(state.config.clone(), 2).await; |
| 49 | + } |
| 50 | + |
| 51 | + // Reset game state to default |
| 52 | + game_state.reset(); |
| 53 | + } |
| 54 | + |
| 55 | + game_state.map_phase = map.phase; |
| 56 | + } |
| 57 | + |
| 58 | + if let Some(round) = payload.round { |
| 59 | + if game_state.round_phase == RoundPhase::Freezetime && round.phase == RoundPhase::Live { |
| 60 | + if config.beep_on_round_start { |
| 61 | + info!("Round started"); |
| 62 | + pishock::beep(state.config.clone(), 1).await; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + game_state.round_phase = round.phase; |
| 67 | + } |
| 68 | + |
| 69 | + if game_state.map_phase != MapPhase::Live { |
| 70 | + return StatusCode::OK; |
| 71 | + } |
| 72 | + |
| 73 | + if let Some(player) = payload.player { |
| 74 | + if player.steamid != game_state.steam_id { |
| 75 | + return StatusCode::OK; |
| 76 | + } |
| 77 | + |
| 78 | + if let Some(player_state) = &mut game_state.player_state { |
| 79 | + if player_state.health > player.state.health && player.state.health > 0 { |
| 80 | + // Took damage and survived |
| 81 | + |
| 82 | + /* |
| 83 | + println!("Player took damage, vibrating"); |
| 84 | +
|
| 85 | + let diff = player_state.health - player.state.health; |
| 86 | +
|
| 87 | + let res = pishock::post( |
| 88 | + &config, |
| 89 | + pishock::PiShockOp::Vibrate { |
| 90 | + intensity: diff, |
| 91 | + duration: 1, |
| 92 | + }, |
| 93 | + ) |
| 94 | + .await; |
| 95 | +
|
| 96 | + match res { |
| 97 | + Ok(code) => println!("Vibrated with code {}", code), |
| 98 | + Err(e) => println!("Error while vibrating: {}", e), |
| 99 | + }; |
| 100 | + */ |
| 101 | + } |
| 102 | + |
| 103 | + if player.match_stats.deaths > player_state.deaths { |
| 104 | + // Died |
| 105 | + |
| 106 | + // Github Copilot is based |
| 107 | + // let res = pishock::post(&api_state, pishock::PiShockOp::Shock { intensity: 100, duration: 1 }).await; |
| 108 | + |
| 109 | + info!("Player died, shocking"); |
| 110 | + |
| 111 | + match config.shock_mode { |
| 112 | + config::ShockMode::Random => { |
| 113 | + let mut rng = StdRng::from_entropy(); |
| 114 | + let intensity = rng.gen_range(config.min_intensity..=config.max_intensity); |
| 115 | + let duration = rng.gen_range(config.min_duration..=config.max_duration); |
| 116 | + |
| 117 | + pishock::shock(state.config.clone(), intensity, duration).await; |
| 118 | + } |
| 119 | + config::ShockMode::LastHitPercentage => { |
| 120 | + let intensity = (player_state.health as f32 / 100.0 |
| 121 | + * config.max_intensity as f32) |
| 122 | + as i32; |
| 123 | + let duration = (player_state.health as f32 / 100.0 |
| 124 | + * config.max_duration as f32) |
| 125 | + as i32; |
| 126 | + |
| 127 | + pishock::shock(state.config.clone(), intensity, duration).await; |
| 128 | + } |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + player_state.health = player.state.health; |
| 133 | + player_state.armor = player.state.armor; |
| 134 | + player_state.kills = player.match_stats.kills; |
| 135 | + player_state.deaths = player.match_stats.deaths; |
| 136 | + } else { |
| 137 | + println!("Player state initialized"); |
| 138 | + |
| 139 | + game_state.player_state = Some(PlayerState { |
| 140 | + health: player.state.health, |
| 141 | + armor: player.state.armor, |
| 142 | + kills: player.match_stats.kills, |
| 143 | + deaths: player.match_stats.deaths, |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + StatusCode::OK |
| 149 | +} |
0 commit comments