mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
provide a way to send messages
This commit is contained in:
parent
794fd41df0
commit
09f44646c5
@ -74,6 +74,9 @@ pub enum Message {
|
||||
/// returns active player names
|
||||
ListPlayers,
|
||||
ListLogs,
|
||||
SendWorldMsg {
|
||||
msg: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -26,7 +26,11 @@ use crate::{
|
||||
tui_runner::Tui,
|
||||
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 core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use server::{persistence::DatabaseSettings, settings::Protocol, Event, Input, Server};
|
||||
@ -394,6 +398,11 @@ fn server_loop(
|
||||
.collect();
|
||||
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
|
||||
};
|
||||
|
@ -150,6 +150,9 @@ fn inner() -> &'static str {
|
||||
</div>
|
||||
|
||||
<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>
|
||||
<ul id="players_list">
|
||||
</ul>
|
||||
@ -192,6 +195,25 @@ function changeSlider(evt, sliderId, showId) {
|
||||
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() {
|
||||
const players_response = await fetch("/ui_api/v1/players");
|
||||
const players = await players_response.json();
|
||||
|
@ -4,10 +4,11 @@ use axum::{
|
||||
http::header::COOKIE,
|
||||
middleware::Next,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use hyper::{Request, StatusCode};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
net::{IpAddr, SocketAddr},
|
||||
@ -69,6 +70,7 @@ pub fn router(web_ui_request_s: UiRequestSender, secret_token: String) -> Router
|
||||
Router::new()
|
||||
.route("/players", get(players))
|
||||
.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(token, validate_secret))
|
||||
.with_state(web_ui_request_s)
|
||||
@ -101,3 +103,19 @@ async fn logs(
|
||||
_ => 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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user