got some webserver

This commit is contained in:
2026-01-22 22:33:46 -07:00
parent 28696b2358
commit 6e082c24df
5 changed files with 91 additions and 200 deletions

View File

@@ -38,10 +38,11 @@ impl AiModule for ProtosBot {
return;
};
// Apply desired game speed
// Apply desired game speed from shared state
let desired_speed = self.shared_speed.get();
unsafe {
let game_ptr = game as *const Game as *mut Game;
(*game_ptr).set_local_speed(locked_state.desired_game_speed);
(*game_ptr).set_local_speed(desired_speed);
}
build_manager::on_frame(game, &player, &mut locked_state);
@@ -90,10 +91,11 @@ impl AiModule for ProtosBot {
pub struct ProtosBot {
game_state: Arc<Mutex<GameState>>,
shared_speed: crate::web_server::SharedGameSpeed,
}
impl ProtosBot {
pub fn new(game_state: Arc<Mutex<GameState>>) -> Self {
Self { game_state }
pub fn new(game_state: Arc<Mutex<GameState>>, shared_speed: crate::web_server::SharedGameSpeed) -> Self {
Self { game_state, shared_speed }
}
}

View File

@@ -6,18 +6,20 @@ mod web_server;
use bot::ProtosBot;
use state::game_state::GameState;
use std::sync::{Arc, Mutex};
use web_server::SharedGameSpeed;
fn main() {
println!("Starting RustBot...");
let game_state = Arc::new(Mutex::new(GameState::default()));
let shared_speed = SharedGameSpeed::new(42); // Default speed (slowest)
// Start web server in a separate thread
let game_state_clone = game_state.clone();
let shared_speed_clone = shared_speed.clone();
std::thread::spawn(move || {
let runtime = tokio::runtime::Runtime::new().unwrap();
runtime.block_on(web_server::start_web_server(game_state_clone));
runtime.block_on(web_server::start_web_server(shared_speed_clone));
});
rsbwapi::start(move |_game| ProtosBot::new(game_state.clone()));
rsbwapi::start(move |_game| ProtosBot::new(game_state.clone(), shared_speed.clone()));
}

View File

@@ -1,8 +1,7 @@
use crate::state::game_state::GameState;
use axum::{
extract::State,
http::StatusCode,
response::IntoResponse,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
@@ -11,8 +10,24 @@ use std::sync::{Arc, Mutex};
use tower_http::{cors::CorsLayer, services::ServeDir};
#[derive(Clone)]
pub struct AppState {
pub game_state: Arc<Mutex<GameState>>,
pub struct SharedGameSpeed {
speed: Arc<Mutex<i32>>,
}
impl SharedGameSpeed {
pub fn new(initial_speed: i32) -> Self {
Self {
speed: Arc::new(Mutex::new(initial_speed)),
}
}
pub fn get(&self) -> i32 {
*self.speed.lock().unwrap()
}
pub fn set(&self, speed: i32) {
*self.speed.lock().unwrap() = speed;
}
}
#[derive(Serialize, Deserialize)]
@@ -25,69 +40,51 @@ pub struct GameSpeedRequest {
pub speed: i32,
}
// GET /api/speed - Get current game speed
async fn get_game_speed(State(state): State<AppState>) -> impl IntoResponse {
let game_state = match state.game_state.lock() {
Ok(gs) => gs,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(GameSpeedResponse { speed: 0 })),
};
(
StatusCode::OK,
Json(GameSpeedResponse {
speed: game_state.desired_game_speed,
}),
)
async fn get_game_speed(State(state): State<SharedGameSpeed>) -> Response {
let speed = state.get();
(StatusCode::OK, Json(GameSpeedResponse { speed })).into_response()
}
// POST /api/speed - Set game speed
async fn set_game_speed(
State(state): State<AppState>,
State(state): State<SharedGameSpeed>,
Json(payload): Json<GameSpeedRequest>,
) -> impl IntoResponse {
// Validate speed is within reasonable range (0-1000)
if payload.speed < 0 || payload.speed > 1000 {
) -> Response {
if payload.speed < -1 || payload.speed > 1000 {
return (
StatusCode::BAD_REQUEST,
Json(GameSpeedResponse { speed: 0 }),
);
)
.into_response();
}
let mut game_state = match state.game_state.lock() {
Ok(gs) => gs,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(GameSpeedResponse { speed: 0 })),
};
game_state.desired_game_speed = payload.speed;
state.set(payload.speed);
(
StatusCode::OK,
Json(GameSpeedResponse {
speed: game_state.desired_game_speed,
speed: payload.speed,
}),
)
.into_response()
}
pub async fn start_web_server(game_state: Arc<Mutex<GameState>>) {
let app_state = AppState { game_state };
pub async fn start_web_server(shared_speed: SharedGameSpeed) {
let static_dir = std::env::current_dir().unwrap().join("static");
// Serve static files from the "static" directory
let static_dir = std::env::current_dir()
.unwrap()
.join("static");
let cors = CorsLayer::very_permissive();
let app = Router::new()
.route("/api/speed", get(get_game_speed))
.route("/api/speed", post(set_game_speed))
.layer(cors)
.fallback_service(ServeDir::new(static_dir))
.layer(CorsLayer::permissive())
.with_state(app_state);
.with_state(shared_speed);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
let addr = "127.0.0.1:3333";
println!("Web server running at http://127.0.0.1:3000");
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
println!("Web server running at http://{}", addr);
axum::serve(listener, app).await.unwrap();
}