You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

use std::net::Ipv4Addr;
use async_std::channel;
use serde::{Serialize, Deserialize};
use serde::de::DeserializeOwned;
use crate::common::serverstate::{ClientId, SendServerPacket};
use crate::entity::account::UserAccountId;
use crate::entity::team::TeamEntityId;
use crate::entity::character::CharacterEntityId;
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ServerId(pub usize);
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AuthToken(pub String);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ship {
pub name: String,
pub ip: Ipv4Addr,
pub port: u16,
pub block_count: u32,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum LoginMessage {
SendMail {
character_id: CharacterEntityId,
title: String,
message: String,
},
ShipList {
ships: Vec<Ship>,
},
RequestUsers,
CreatedTeam(UserAccountId, TeamEntityId),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum ShipMessage {
Authenticate(AuthToken),
NewShip(Ship),
SendMail {
character_id: CharacterEntityId,
title: String,
message: String,
},
RequestShipList,
AddUser(UserAccountId),
RemoveUser(UserAccountId),
CreateTeam(UserAccountId, String),
}
pub enum InterserverMessage<S, C> {
Server(ServerId, S),
Client(ClientId, C),
}
#[async_trait::async_trait]
pub trait InterserverActor: Clone {
type SendClientMessage: SendServerPacket;
type SendServerMessage: Serialize;
type RecvServerMessage: DeserializeOwned;
type Error;
async fn on_connect(&mut self, id: ServerId) -> Vec<(ServerId, Self::SendServerMessage)>;
//async fn on_action(&mut self, id: ServerId, msg: Self::RecvMessage) -> Result<Vec<(ServerId, Self::SendMessage)>, Self::Error>;
async fn on_action(&mut self, id: ServerId, msg: Self::RecvServerMessage) -> Result<Vec<InterserverMessage<Self::SendServerMessage, Self::SendClientMessage>>, Self::Error>;
async fn on_disconnect(&mut self, id: ServerId) -> Vec<(ServerId, Self::SendServerMessage)>;
async fn set_sender(&mut self, server_id: ServerId, tx: channel::Sender<Self::SendServerMessage>);
}