did some server work, its still shit tho
This commit is contained in:
parent
9895a4797f
commit
50976d0f3f
9 changed files with 212 additions and 68 deletions
81
src/lib.rs
81
src/lib.rs
|
@ -1,5 +1,8 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use api::game_actions::SpawnToken;
|
||||
use game::{chat_message::ChatMessage, entry::{ActionDefinition, DiceRoll}, Game, GameImpl};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
|
||||
pub mod api;
|
||||
|
@ -8,16 +11,23 @@ pub mod pathfinder2r_impl;
|
|||
pub mod table;
|
||||
pub mod user;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GameServer {
|
||||
_game: Game<pathfinder2r_impl::Pathfinder2rCharacterSheet, pathfinder2r_impl::entry::Entry>,
|
||||
tokens: Vec<(String, i32, i32)>,
|
||||
game: Game<pathfinder2r_impl::Pathfinder2rCharacterSheet, pathfinder2r_impl::entry::Entry>,
|
||||
chat: Vec<(String, ChatMessage)>,
|
||||
#[serde(skip)] // we dont want to save the logged users as it will always be empty when the server restarts
|
||||
users: HashMap<String, bool>,
|
||||
// TODO: JEESH REPLACE THIS WITH A DATABASE AND PROPER SECURITY ONCE ITS DONE PLEASE GOD
|
||||
creds: HashMap<String, String>,
|
||||
}
|
||||
impl GameServer {
|
||||
pub fn new() -> Self {
|
||||
let mut creds = HashMap::new();
|
||||
creds.insert("rusty".to_string(), String::new());
|
||||
creds.insert("artist".to_string(), "artist".to_string());
|
||||
creds.insert("dragonfly".to_string(), "cool".to_string());
|
||||
Self {
|
||||
_game: Game::new(),
|
||||
tokens: vec![("assets/pf2r/tokens/louise.jpg".to_string(), 2, 2)],
|
||||
game: Game::new(),
|
||||
chat: vec![
|
||||
(
|
||||
"Server".to_string(),
|
||||
|
@ -34,6 +44,8 @@ impl GameServer {
|
|||
.with_action(ActionDefinition::new("Attack -1".to_string()))
|
||||
)
|
||||
],
|
||||
users: HashMap::new(),
|
||||
creds,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,9 +60,18 @@ impl GameServer {
|
|||
println!("Got message from {}: {:?}", &id, &req);
|
||||
|
||||
match req {
|
||||
// ignore errors and re-login requests
|
||||
// ignore errors, should probably be blocked before they are sent here
|
||||
api::Request::Error => {}
|
||||
api::Request::Login(_) => {}
|
||||
api::Request::Login(login) => {
|
||||
println!("login req from {}: {:?}", &id, &login);
|
||||
if !self.users.contains_key(&login.username) && self.creds.get(&login.username).map(|p| p == &login.password).unwrap_or(false) {
|
||||
self.users.insert(login.username.clone(), login.username == "rusty"); // rusty will be admin for now :)
|
||||
_ = broadcast.send((Some(id), api::Response::Login(api::login::LoginResult { success: true, username: login.username })));
|
||||
}
|
||||
else {
|
||||
_ = broadcast.send((Some(id.clone()), api::Response::Login(api::login::LoginResult { success: false, username: login.username })));
|
||||
}
|
||||
}
|
||||
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
|
||||
|
@ -101,24 +122,24 @@ impl GameServer {
|
|||
_ = 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(SpawnToken { token_id: i, x: *x, y: *y, img })));
|
||||
for token_id in self.game.available_tokens() {
|
||||
if let Some(ti) = self.game.token_info(0, token_id) {
|
||||
let bits = std::fs::read(&ti.img_source).expect("FAILED READING TOKEN IMAGE");
|
||||
let img = base64::Engine::encode(&base64::prelude::BASE64_STANDARD, &bits);
|
||||
_ = broadcast.send((Some(id.clone()), api::Response::SpawnToken(SpawnToken { token_id: token_id, x: ti.x, y: ti.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");
|
||||
api::Request::SpawnToken { map_id, character, x, y, img_path } => {
|
||||
let token_id = self.game.create_token(map_id, character, 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(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
|
||||
if token_id < self.tokens.len() {
|
||||
self.tokens[token_id].1 = x;
|
||||
self.tokens[token_id].2 = y;
|
||||
if self.game.move_token(0, token_id, x, y) {
|
||||
// TODO: maybe chage move_token to return optional x,y values if succeeded to make sure the token is where it was going to be
|
||||
_ = broadcast.send((None, api::Response::MoveToken { token_id, x, y }));
|
||||
}
|
||||
},
|
||||
|
@ -135,10 +156,32 @@ impl GameServer {
|
|||
self.chat.push((id, msg.clone()));
|
||||
_ = broadcast.send((None, api::Response::Message(msg)));
|
||||
|
||||
},
|
||||
api::Request::CreateCharacter => {
|
||||
// check if user is admin
|
||||
if self.users.get(&id).map(|a| *a).unwrap_or(false) {
|
||||
let new_id = self.game.create_character();
|
||||
// return the new id with the character i think
|
||||
}
|
||||
},
|
||||
api::Request::Quit => {
|
||||
if self.users.contains_key(&id) {
|
||||
self.users.remove(&id);
|
||||
}
|
||||
_ = broadcast.send((None, api::Response::Quit { id }));
|
||||
},
|
||||
api::Request::Kick(id) => {
|
||||
if self.users.contains_key(&id) {
|
||||
self.users.remove(&id);
|
||||
}
|
||||
_ = broadcast.send((Some(id), api::Response::Shutdown));
|
||||
}
|
||||
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,
|
||||
api::Request::CharacterDisplay { id } => todo!(),
|
||||
api::Request::CharacterInputs { id } => todo!(),
|
||||
api::Request::CharacterGetField { id, field } => todo!(),
|
||||
api::Request::CharacterSetField { id, field, val } => todo!(),
|
||||
api::Request::CharacterAssign { id, user } => todo!(),
|
||||
}
|
||||
}
|
||||
_ = broadcast.send((None, api::Response::Shutdown));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue