use std::sync::Arc;
use super::result::{EventResult, ResultedEvent};
use crate::event::PlayerInfo;
use crate::server::ServerId;
pub struct LoginEvent {
pub player: Arc<PlayerInfo>,
result: LoginResult,
}
impl LoginEvent {
#[must_use]
pub const fn new(player: Arc<PlayerInfo>) -> Self {
Self {
player,
result: LoginResult::Allow,
}
}
pub fn deny(&mut self, reason: impl Into<String>) {
self.result = LoginResult::Deny(reason.into());
}
pub fn allow(&mut self) {
self.result = LoginResult::Allow;
}
}
#[derive(Debug, Clone)]
pub enum LoginResult {
Allow,
Deny(String),
}
impl EventResult for LoginResult {
fn is_allowed(&self) -> bool {
matches!(self, Self::Allow)
}
}
impl ResultedEvent for LoginEvent {
type Result = LoginResult;
fn result(&self) -> &Self::Result {
&self.result
}
fn set_result(&mut self, result: Self::Result) {
self.result = result;
}
}
pub struct ChooseServerEvent {
pub player: Arc<PlayerInfo>,
result: ChooseServerResult,
}
impl ChooseServerEvent {
#[must_use]
pub const fn new(player: Arc<PlayerInfo>) -> Self {
Self {
player,
result: ChooseServerResult::Default,
}
}
pub fn set_server(&mut self, server: &ServerId) {
self.result = ChooseServerResult::Override(server.id.to_owned());
}
}
#[derive(Debug, Clone)]
pub enum ChooseServerResult {
Default,
Override(String),
}
impl EventResult for ChooseServerResult {
fn is_allowed(&self) -> bool {
true }
}
impl From<ServerId> for ChooseServerResult {
fn from(server: ServerId) -> Self {
Self::Override(server.id.to_owned())
}
}
impl ResultedEvent for ChooseServerEvent {
type Result = ChooseServerResult;
fn result(&self) -> &Self::Result {
&self.result
}
fn set_result(&mut self, result: Self::Result) {
self.result = result;
}
}
pub struct ServerSwitchEvent {
pub player: Arc<PlayerInfo>,
pub from: String,
pub to: String,
result: ServerSwitchResult,
}
impl ServerSwitchEvent {
#[must_use]
pub const fn new(player: Arc<PlayerInfo>, from: String, to: String) -> Self {
Self {
player,
from,
to,
result: ServerSwitchResult::Allow,
}
}
pub fn deny(&mut self, reason: impl Into<String>) {
self.result = ServerSwitchResult::Deny(reason.into());
}
pub fn allow(&mut self) {
self.result = ServerSwitchResult::Allow;
}
}
#[derive(Debug, Clone)]
pub enum ServerSwitchResult {
Allow,
Deny(String),
}
impl EventResult for ServerSwitchResult {
fn is_allowed(&self) -> bool {
matches!(self, Self::Allow)
}
}
impl ResultedEvent for ServerSwitchEvent {
type Result = ServerSwitchResult;
fn result(&self) -> &Self::Result {
&self.result
}
fn set_result(&mut self, result: Self::Result) {
self.result = result;
}
}
pub struct ServerConnectedEvent {
pub player: Arc<PlayerInfo>,
pub server_id: String,
}
impl ServerConnectedEvent {
#[must_use]
pub const fn new(player: Arc<PlayerInfo>, server_id: String) -> Self {
Self { player, server_id }
}
}
pub struct DisconnectEvent {
pub player: Arc<PlayerInfo>,
}
impl DisconnectEvent {
#[must_use]
pub const fn new(player: Arc<PlayerInfo>) -> Self {
Self { player }
}
}
pub struct PingEvent {
pub default_response: serde_json::Value,
pub client_protocol: i32,
pub virtual_host: String,
result: PingResult,
}
impl PingEvent {
#[must_use]
pub const fn new(
default_response: serde_json::Value,
client_protocol: i32,
virtual_host: String,
) -> Self {
Self {
default_response,
client_protocol,
virtual_host,
result: PingResult::Default,
}
}
fn effective_response(&self) -> serde_json::Value {
match &self.result {
PingResult::Override(json) => json.clone(),
PingResult::Default => self.default_response.clone(),
}
}
pub fn set_motd(&mut self, motd: impl Into<String>) {
let mut response = self.effective_response();
response["description"]["text"] = serde_json::Value::String(motd.into());
self.result = PingResult::Override(response);
}
pub fn set_players(&mut self, online: i32) {
let mut response = self.effective_response();
response["players"]["online"] = serde_json::Value::from(online);
self.result = PingResult::Override(response);
}
pub fn set_max_players(&mut self, max: i32) {
let mut response = self.effective_response();
response["players"]["max"] = serde_json::Value::from(max);
self.result = PingResult::Override(response);
}
pub fn set_response(&mut self, response: serde_json::Value) {
self.result = PingResult::Override(response);
}
}
#[derive(Debug, Clone)]
pub enum PingResult {
Default,
Override(serde_json::Value),
}
impl EventResult for PingResult {
fn is_allowed(&self) -> bool {
true }
}
impl ResultedEvent for PingEvent {
type Result = PingResult;
fn result(&self) -> &Self::Result {
&self.result
}
fn set_result(&mut self, result: Self::Result) {
self.result = result;
}
}