provide a way to send messages

This commit is contained in:
Marcel Märtens 2024-03-13 10:37:29 +01:00
parent 794fd41df0
commit 09f44646c5
4 changed files with 54 additions and 2 deletions

View File

@ -74,6 +74,9 @@ pub enum Message {
/// returns active player names /// returns active player names
ListPlayers, ListPlayers,
ListLogs, ListLogs,
SendWorldMsg {
msg: String,
},
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -26,7 +26,11 @@ use crate::{
tui_runner::Tui, tui_runner::Tui,
tuilog::TuiLog, tuilog::TuiLog,
}; };
use common::{clock::Clock, comp::Player, consts::MIN_RECOMMENDED_TOKIO_THREADS}; use common::{
clock::Clock,
comp::{ChatType, Player},
consts::MIN_RECOMMENDED_TOKIO_THREADS,
};
use common_base::span; use common_base::span;
use core::sync::atomic::{AtomicUsize, Ordering}; use core::sync::atomic::{AtomicUsize, Ordering};
use server::{persistence::DatabaseSettings, settings::Protocol, Event, Input, Server}; use server::{persistence::DatabaseSettings, settings::Protocol, Event, Input, Server};
@ -394,6 +398,11 @@ fn server_loop(
.collect(); .collect();
let _ = response.send(MessageReturn::Logs(lines)); let _ = response.send(MessageReturn::Logs(lines));
}, },
Message::SendWorldMsg { msg } => {
use server::state_ext::StateExt;
let msg = ChatType::Meta.into_plain_msg(msg);
server.state().send_chat(msg);
},
} }
false false
}; };

View File

@ -150,6 +150,9 @@ fn inner() -> &'static str {
</div> </div>
<div id="players" class="tabcontent"> <div id="players" class="tabcontent">
<label for="world_msg">Message:</label>
<input type="text" id="world_msg" name="world_msg" onchange="sendGlobalMsg();"></input>
<input type="button" value="Send" onclick="sendGlobalMsg();"></input>
<h3>Players</h3> <h3>Players</h3>
<ul id="players_list"> <ul id="players_list">
</ul> </ul>
@ -192,6 +195,25 @@ function changeSlider(evt, sliderId, showId) {
sliderNo.innerHTML = slider.value; sliderNo.innerHTML = slider.value;
} }
async function sendGlobalMsg() {
var world_msg = document.getElementById("world_msg");
const msg_text = world_msg.value;
const msg_response = await fetch("/ui_api/v1/send_world_msg", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
msg: msg_text
})
});
if (msg_response.status == 200) {
world_msg.value = '';
}
}
async function update_players() { async function update_players() {
const players_response = await fetch("/ui_api/v1/players"); const players_response = await fetch("/ui_api/v1/players");
const players = await players_response.json(); const players = await players_response.json();

View File

@ -4,10 +4,11 @@ use axum::{
http::header::COOKIE, http::header::COOKIE,
middleware::Next, middleware::Next,
response::{IntoResponse, Response}, response::{IntoResponse, Response},
routing::get, routing::{get, post},
Json, Router, Json, Router,
}; };
use hyper::{Request, StatusCode}; use hyper::{Request, StatusCode};
use serde::Deserialize;
use std::{ use std::{
collections::HashSet, collections::HashSet,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
@ -69,6 +70,7 @@ pub fn router(web_ui_request_s: UiRequestSender, secret_token: String) -> Router
Router::new() Router::new()
.route("/players", get(players)) .route("/players", get(players))
.route("/logs", get(logs)) .route("/logs", get(logs))
.route("/send_world_msg", post(send_world_msg))
.layer(axum::middleware::from_fn_with_state(ip_addrs, log_users)) .layer(axum::middleware::from_fn_with_state(ip_addrs, log_users))
.layer(axum::middleware::from_fn_with_state(token, validate_secret)) .layer(axum::middleware::from_fn_with_state(token, validate_secret))
.with_state(web_ui_request_s) .with_state(web_ui_request_s)
@ -101,3 +103,19 @@ async fn logs(
_ => Err(StatusCode::INTERNAL_SERVER_ERROR), _ => Err(StatusCode::INTERNAL_SERVER_ERROR),
} }
} }
#[derive(Deserialize)]
struct SendWorldMsgBody {
msg: String,
}
async fn send_world_msg(
State(web_ui_request_s): State<UiRequestSender>,
Json(payload): Json<SendWorldMsgBody>,
) -> Result<impl IntoResponse, StatusCode> {
let (dummy_s, _) = tokio::sync::oneshot::channel();
let _ = web_ui_request_s
.send((Message::SendWorldMsg { msg: payload.msg }, dummy_s))
.await;
Ok(())
}