some tokens stuff (ye i started to work on the map, also i 'fixed' the context menu for msgs but still needs to be impl-ed)

This commit is contained in:
Rusty Striker 2024-10-08 19:55:48 +03:00
parent 97475599a7
commit be6dd7c0e4
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
13 changed files with 296 additions and 76 deletions

View file

@ -1,75 +1,109 @@
use game::{chat_message::ChatMessage, Game, GameImpl};
use tokio::sync::{broadcast, mpsc};
pub mod user;
pub mod table;
pub mod api;
pub mod game;
pub mod pathfinder2r_impl;
pub mod table;
pub mod user;
pub struct GameServer {
_game: Game<pathfinder2r_impl::Pathfinder2rCharacterSheet, pathfinder2r_impl::entry::Entry>,
tokens: Vec<(String, i32, i32)>,
chat: Vec<(String, ChatMessage)>,
}
impl GameServer {
pub fn new() -> Self {
Self {
_game: Game::new(),
tokens: vec![("assets/pf2r/tokens/louise.jpg".to_string(), 2, 2)],
chat: Vec::new(),
}
}
pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender<(Option<String>, api::Response)>) {
pub async fn server_loop(
mut self,
mut msgs: mpsc::Receiver<(String, api::Request)>,
broadcast: broadcast::Sender<(Option<String>, 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(mut msg) => {
if msg.id == 0 || msg.id >= self.chat.len() {
api::Request::Error => {}
api::Request::Login(_) => {}
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 {
else if id == self.chat[msg.id - 1].0 {
self.chat[msg.id - 1] = (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 = format!("({})", id.clone());
}
else {
msg.source = format!("{} ({})", msg.source, id.clone());
}
// Force the sender id to be the new id of the message, even if an id was provided
msg.source = id.clone();
self.chat.push((id.clone(), msg.clone()));
_ = broadcast.send((None, api::Response::Message(msg)));
},
api::Request::Quit => { _ = broadcast.send((None, 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()
if msg.whisper.is_some() {
_ = broadcast.send((Some(id.clone()), api::Response::Message(msg.clone())));
}
_ = broadcast.send((msg.whisper.clone(), api::Response::Message(msg)));
}
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((Some(id), api::Response::GetChatHistory(history)));
},
}
api::Request::GetLastMessages { mut amount } => {
if amount == 0 { amount = self.chat.len(); }
let start = if amount >= self.chat.len() { self.chat.len() } else { self.chat.len() - amount };
let history: Vec<ChatMessage> = self.chat.iter()
.skip(start)
.map(|m| m.1.clone())
.collect();
if amount == 0 {
amount = self.chat.len();
}
let start = if amount >= self.chat.len() {
self.chat.len()
} else {
self.chat.len() - amount
};
let history: Vec<ChatMessage> =
self.chat.iter().skip(start).map(|m| m.1.clone()).collect();
_ = broadcast.send((Some(id), api::Response::GetChatHistory(history)));
},
api::Request::GetTokens => {
for (i, (path, x, y)) in self.tokens.iter().enumerate() {
let bits = std::fs::read(path).expect("FAILED READING TOKEN IMAGE");
let img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken { token_id: i, x: *x, y: *y, img }));
}
},
api::Request::SpawnToken { x, y, img_path } => {
let token_id = self.tokens.len();
self.tokens.push((img_path.clone(), x, y));
let bits = std::fs::read(img_path).expect("FAILED READING TOKEN IMAGE");
let img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken { token_id, x, y, img }));
},
api::Request::MoveToken { token_id, x, y } => {
// TODO: add check to make sure the actor is authorized to move the token
_ = broadcast.send((None, api::Response::MoveToken { token_id, x, y }));
},
api::Request::Quit => _ = broadcast.send((None, api::Response::Quit { id })),
api::Request::Kick(id) => _ = broadcast.send((Some(id), api::Response::Shutdown)),
api::Request::Shutdown => break,
}
}
_ = broadcast.send((None, api::Response::Shutdown));