some cleanups

This commit is contained in:
Rusty Striker 2025-07-13 20:20:41 +03:00
parent 838c89ac73
commit 2a36485b1b
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
4 changed files with 85 additions and 81 deletions

View file

@ -110,12 +110,6 @@ tavern.onlogin = (s) => {
let game = document.getElementById('game');
login.style.display = 'none';
game.style.display = 'flex';
// get last 50 msgs (i think that is enough for now) when we get in
// TODO: Maybe move this into the server itself? that is a lot of stuff that we know are gonna happen...
// For now i'll keep it like that tho
tavern.get_last_msgs(50);
// TODO: Perhaps figure out a way to show a certain scene? maybe on the server it would make more sense
tavern.get_scene_list();
}
else {
alert("Invalid username or password!");

View file

@ -10,7 +10,8 @@ Honestly, i dont think i need a todo list currently as there is enough for me to
[x] move tokens
[ ] change map
[x] show grid
[ ] multiple maps
[x] multiple maps
[ ] Edit map grid (and save it)
[ ] Chat actions
[x] Define chat action
[x] show roll dialog on chat aciton

View file

@ -11,4 +11,4 @@ pub struct LoginResult {
pub success: bool,
pub username: String,
// TODO: Figure out what the user needs on successful login to reduce traffic
}
}

View file

@ -62,7 +62,6 @@ impl GameServer {
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);
@ -78,13 +77,17 @@ impl GameServer {
.unwrap_or(false)
{
self.users.insert(login.username.clone(), login.username == "rusty"); // rusty will be admin for now :)
// Send login confirmation
_ = broadcast.send((
Some(id),
Some(id.clone()),
api::Response::Login(api::login::LoginResult {
success: true,
username: login.username,
username: login.username.clone(),
}),
));
// Send the list of scenes and chat history and such
_ = broadcast.send((Some(login.username.clone()), self.get_scene_list(&id)));
_ = broadcast.send((Some(login.username.clone()), self.get_last_messages(50)));
} else {
_ = broadcast.send((
Some(id.clone()),
@ -113,33 +116,11 @@ impl GameServer {
}
_ = 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::GetChatHistory { amount, from } => {
_ = broadcast.send((Some(id), self.get_chat_history(amount, from)));
}
api::Request::GetLastMessages { mut amount } => {
if amount == 0 {
amount = self.chat.len();
}
let start = if amount >= self.chat.len() {
0
} 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::GetLastMessages { amount } => {
_ = broadcast.send((Some(id), self.get_last_messages(amount)));
}
api::Request::GetTokens { scene } => {
for token_id in self.game.available_tokens(scene) {
@ -157,52 +138,11 @@ impl GameServer {
}
}
api::Request::GetScene { id: scene_id } => {
if self
.game
.get_scene(scene_id)
.map(|s| s.visible_to_users)
.unwrap_or(false) ||
*self.users.get(&id).unwrap_or(&false)
{
let scene_tokens = self
.game
.available_tokens(scene_id)
.iter()
.map(|&id| self.game.token_info(scene_id, id).map(|info| (id, info)))
.flatten()
.map(|(id, info)| SpawnToken {
token_id: id,
x: info.x,
y: info.y,
img: info.img_source.clone(),
})
.collect::<Vec<_>>();
let map = self.game.get_scene(scene_id).map(|s| s.map.as_ref()).flatten();
_ = broadcast.send((
Some(id.clone()),
api::Response::ShowScene {
scene: scene_id,
tokens: scene_tokens,
background: map.map(|m| m.background.clone()),
grid_cell_size: map.map(|m| m.grid_cell_size),
grid_offset: map.map(|m| m.grid_offset.clone()),
},
));
if let Some(response) = self.get_scene(&id, scene_id) {
_ = broadcast.send((Some(id), response));
}
}
api::Request::GetSceneList => {
let admin = *self.users.get(&id).unwrap_or(&false);
let scenes = self
.game
.scenes()
.iter()
.map(|id| self.game.get_scene(*id).map(|s| (id, s)))
.flatten()
.filter(|(_, scene)| admin || scene.visible_to_users)
.map(|(id, s)| (*id, s.title.to_string()))
.collect::<Vec<_>>();
_ = broadcast.send((Some(id.clone()), api::Response::SceneList { scenes: scenes }))
}
api::Request::GetSceneList => _ = broadcast.send((Some(id.clone()), self.get_scene_list(&id))),
api::Request::SpawnToken {
map_id,
character,
@ -280,4 +220,73 @@ impl GameServer {
}
_ = broadcast.send((None, api::Response::Shutdown));
}
fn get_scene_list(&self, id: &str) -> api::Response {
let admin = *self.users.get(id).unwrap_or(&false);
let scenes = self
.game
.scenes()
.iter()
.map(|id| self.game.get_scene(*id).map(|s| (id, s)))
.flatten()
.filter(|(_, scene)| admin || scene.visible_to_users)
.map(|(id, s)| (*id, s.title.to_string()))
.collect::<Vec<_>>();
api::Response::SceneList { scenes }
}
fn get_chat_history(&self, mut amount: usize, from: usize) -> api::Response {
if amount == 0 {
amount = self.chat.len();
}
let history: Vec<ChatMessage> = self.chat.iter().skip(from).take(amount).map(|m| m.1.clone()).collect();
api::Response::GetChatHistory(history)
}
fn get_last_messages(&self, mut amount: usize) -> api::Response {
if amount == 0 {
amount = self.chat.len();
}
let start = if amount >= self.chat.len() {
0
} else {
self.chat.len() - amount
};
let history: Vec<ChatMessage> = self.chat.iter().skip(start).map(|m| m.1.clone()).collect();
api::Response::GetChatHistory(history)
}
fn get_scene(&self, user_id: &str, scene_id: usize) -> Option<api::Response> {
if self
.game
.get_scene(scene_id)
.map(|s| s.visible_to_users)
.unwrap_or(false) ||
*self.users.get(user_id).unwrap_or(&false)
{
let scene_tokens = self
.game
.available_tokens(scene_id)
.iter()
.map(|&id| self.game.token_info(scene_id, id).map(|info| (id, info)))
.flatten()
.map(|(id, info)| SpawnToken {
token_id: id,
x: info.x,
y: info.y,
img: info.img_source.clone(),
})
.collect::<Vec<_>>();
let map = self.game.get_scene(scene_id).map(|s| s.map.as_ref()).flatten();
Some(api::Response::ShowScene {
scene: scene_id,
tokens: scene_tokens,
background: map.map(|m| m.background.clone()),
grid_cell_size: map.map(|m| m.grid_cell_size),
grid_offset: map.map(|m| m.grid_offset.clone()),
})
} else {
None
}
}
}