not sure what is going on anymore, a lot of changes as im working on the chat thing. currently simple chat seems to work
This commit is contained in:
parent
dd34317d14
commit
acf96db186
11 changed files with 170 additions and 52 deletions
40
src/lib.rs
40
src/lib.rs
|
@ -1,4 +1,4 @@
|
|||
use game::{Game, GameImpl};
|
||||
use game::{chat_message::ChatMessage, Game, GameImpl};
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
|
||||
pub mod user;
|
||||
|
@ -8,25 +8,55 @@ pub mod game;
|
|||
pub mod pathfinder2r_impl;
|
||||
|
||||
pub struct GameServer {
|
||||
game: Game<pathfinder2r_impl::Pathfinder2rCharacterSheet, pathfinder2r_impl::entry::PEntry>
|
||||
_game: Game<pathfinder2r_impl::Pathfinder2rCharacterSheet, pathfinder2r_impl::entry::Entry>,
|
||||
chat: Vec<(String, ChatMessage)>,
|
||||
}
|
||||
impl GameServer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
game: Game::new(),
|
||||
_game: Game::new(),
|
||||
chat: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, mut broadcast: broadcast::Sender<api::Response>) {
|
||||
pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender<api::Response>) {
|
||||
while let Some(req) = msgs.recv().await {
|
||||
// TODO: do stuff yo!
|
||||
let (id, req) = req;
|
||||
println!("Got message from {}: {:?}", &id, &req);
|
||||
|
||||
match req {
|
||||
api::Request::Error => {},
|
||||
api::Request::Login(_) => {},
|
||||
api::Request::Message(msg) => { _ = broadcast.send(api::Response::Message(msg)); },
|
||||
api::Request::Message(mut msg) => {
|
||||
if msg.id == 0 || msg.id >= self.chat.len() {
|
||||
msg.id = self.chat.len() + 1; // set the message id, 0 is invalid
|
||||
}
|
||||
// TODO: check if the editor is an admin as well
|
||||
else if id == self.chat[msg.id].0 {
|
||||
self.chat[msg.id] = (id.clone(), msg.clone());
|
||||
}
|
||||
else {
|
||||
// if its an edit message and editor is not the owner, skip
|
||||
continue;
|
||||
}
|
||||
if msg.source.is_empty() {
|
||||
msg.source = id.clone();
|
||||
}
|
||||
self.chat.push((id.clone(), msg.clone()));
|
||||
_ = broadcast.send(api::Response::Message(msg));
|
||||
},
|
||||
api::Request::Quit => { _ = broadcast.send(api::Response::Quit { id })},
|
||||
api::Request::Shutdown => todo!(),
|
||||
api::Request::GetChatHistory { mut amount, from: last_msg } => {
|
||||
if amount == 0 { amount = self.chat.len(); }
|
||||
let history: Vec<ChatMessage> = self.chat.iter()
|
||||
.skip(last_msg)
|
||||
.take(amount)
|
||||
.map(|m| m.1.clone())
|
||||
.collect();
|
||||
_ = broadcast.send(api::Response::GetChatHistory(history));
|
||||
},
|
||||
}
|
||||
}
|
||||
_ = broadcast.send(api::Response::Shutdown);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue