add onshowscene and get_current_scene functions

This commit is contained in:
Rusty Striker 2025-06-16 20:57:51 +03:00
parent f59c06c348
commit 731f22a2c2
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
7 changed files with 188 additions and 103 deletions

View file

@ -1,7 +1,5 @@
//! General game actions
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Ping {
@ -14,23 +12,23 @@ pub struct Ping {
#[derive(Serialize, Deserialize)]
pub struct ShowImage {
/// Which texture to show
pub texture: String
pub texture: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct SpawnToken {
pub token_id: usize,
pub x: f32,
pub y: f32,
pub img: String
pub token_id: usize,
pub x: f32,
pub y: f32,
pub img: String,
}
impl std::fmt::Debug for SpawnToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// I dont want it to print the `img` field because it VERY long :)
// I dont want it to print the `img` field because it is VERY long :)
f.debug_struct("SpawnToken")
.field("token_id", &self.token_id)
.field("x", &self.x)
.field("y", &self.y)
.finish()
}
}
}

View file

@ -1,5 +1,5 @@
pub mod login;
pub mod game_actions;
pub mod login;
pub mod map_actions;
use game_actions::SpawnToken;
@ -12,28 +12,60 @@ use crate::game::{character_sheet::EntryType, chat_message::ChatMessage, entry::
pub enum Request {
#[default]
Error,
// Connection requests
// Connection requests
Login(login::LoginRequest),
Quit,
Kick(String),
Shutdown,
// Character stuff
CreateCharacter,
CharacterDisplay { id: usize },
CharacterInputs { id: usize },
CharacterGetField { id: usize, field: String },
CharacterSetField { id: usize, field: String, val: EntryType },
CharacterAssign { id: usize, user: String },
CharacterDisplay {
id: usize,
},
CharacterInputs {
id: usize,
},
CharacterGetField {
id: usize,
field: String,
},
CharacterSetField {
id: usize,
field: String,
val: EntryType,
},
CharacterAssign {
id: usize,
user: String,
},
// Chat requests
Message(ChatMessage),
GetChatHistory { amount: usize, from: usize },
GetLastMessages { amount: usize, },
GetChatHistory {
amount: usize,
from: usize,
},
GetLastMessages {
amount: usize,
},
// Map requests
GetTokens { scene: usize },
SpawnToken { map_id: usize, character: String, x: f32, y: f32, img_path: String },
MoveToken { token_id: usize, x: f32, y: f32 },
GetCurrentScene,
GetTokens {
scene: usize,
},
SpawnToken {
map_id: usize,
character: String,
x: f32,
y: f32,
img_path: String,
},
MoveToken {
token_id: usize,
x: f32,
y: f32,
},
// Actions requests
ActionResult(ActionResult)
ActionResult(ActionResult),
}
#[derive(Serialize, Clone, Debug)]
@ -43,12 +75,12 @@ pub enum Response {
Login(login::LoginResult),
Message(ChatMessage),
GetChatHistory(Vec<ChatMessage>),
ShowScene { scene: usize, tokens: Vec<SpawnToken> },
MoveToken { token_id: usize, x: f32, y: f32 },
SpawnToken(SpawnToken),
CharacterCreated(usize),
Quit { id: String },
Shutdown,
}
#[derive(Serialize, Debug, Clone)]
@ -56,4 +88,4 @@ pub enum Response {
pub enum RequestError {
InvalidRequest,
AlreadyLoggedIn,
}
}

View file

@ -42,7 +42,7 @@ impl GameServer {
ActionDefinition::new("weapon/attack".to_string())
.display_name(Some("Attack +7".to_string()))
.with_roll(DiceRoll::new("Pierce".to_string(), 12, 1).constant(1))
.with_roll(DiceRoll::new("Fire".to_string(), 4, 2)),
.with_roll(DiceRoll::new("Fire".to_string(), 4, 2).enabled(false)),
)
.with_action(ActionDefinition::new("Attack +3".to_string()).with_roll(DiceRoll::new(
"Base".to_string(),
@ -158,6 +158,33 @@ impl GameServer {
}
}
}
api::Request::GetCurrentScene => {
let scene = self.game.current_scene();
let mut scene_tokens = self
.game
.available_tokens(scene)
.iter()
.map(|id| self.game.token_info(0, *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<_>>();
for spawn in scene_tokens.iter_mut() {
let bits = std::fs::read(&spawn.img).expect("FAILED READING TOKEN IMAGE");
spawn.img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
}
_ = broadcast.send((
Some(id.clone()),
api::Response::ShowScene {
scene: scene,
tokens: scene_tokens,
},
));
}
api::Request::SpawnToken {
map_id,
character,

View file

@ -111,9 +111,9 @@ async fn handle_socket(
b = brecv.recv() => {
println!("{} trying to log in: {:?}", &temp_id, &b);
if let Ok((to_id, msg)) = b {
if let Response::Login(open_tavern::api::login::LoginResult { success, username }) = msg.clone() {
if let Response::Login(open_tavern::api::login::LoginResult { success, username }) = &msg {
let to_id = to_id.map(|ti| ti == temp_id).unwrap_or(false);
if to_id && success && id.as_ref().map(|id| id == &username).unwrap_or(false) {
if to_id && *success && id.as_ref().map(|id| id == username).unwrap_or(false) {
_ = socket.send(Message::Text(serde_json::to_string(&msg).unwrap_or_default().into())).await;
break;
}