Browse Source

initial guildcard send/recv

pbs
Andy Newjack 5 years ago
parent
commit
177b694870
  1. 2
      Cargo.toml
  2. 9
      src/entity/account.rs
  3. 8
      src/entity/gateway/entitygateway.rs
  4. 39
      src/entity/gateway/inmemory.rs
  5. 10
      src/login/character.rs
  6. 6
      src/login/login.rs
  7. 16
      src/main.rs
  8. 39
      src/ship/ship.rs

2
Cargo.toml

@ -39,3 +39,5 @@ byteorder = "1"
enum-utils = "0.1.2" enum-utils = "0.1.2"
derive_more = { version = "0.99.3", features = ["display"]} derive_more = { version = "0.99.3", features = ["display"]}
[patch."http://git.sharnoth.com/jake/libpso"]
libpso = { path = "../libpso" }

9
src/entity/account.rs

@ -11,7 +11,7 @@ pub struct UserAccount {
pub id: u32, pub id: u32,
pub username: String, pub username: String,
pub password: String, pub password: String,
pub guildcard: Option<u32>,
pub guildcard: u32,
pub team_id: Option<u32>, pub team_id: Option<u32>,
pub banned: bool, pub banned: bool,
pub muted_until: SystemTime, pub muted_until: SystemTime,
@ -32,3 +32,10 @@ pub struct GuildCardData {
pub user_id: u32, pub user_id: u32,
pub guildcard: guildcard::GuildCardData, pub guildcard: guildcard::GuildCardData,
} }
#[derive(Clone, Default)]
pub struct GuildCard {
pub id: u32,
pub character_id: u32,
pub guildcard: guildcard::GuildCard,
}

8
src/entity/gateway/entitygateway.rs

@ -52,4 +52,12 @@ pub trait EntityGateway {
fn get_items_by_character(&self, _char: &Character) -> Vec<Item> { fn get_items_by_character(&self, _char: &Character) -> Vec<Item> {
unimplemented!(); unimplemented!();
} }
fn create_guild_card_by_character(&self, _character: &Character) -> GuildCard {
unimplemented!();
}
fn get_guild_card_by_character(&self, _character: &Character) -> Option<GuildCard> {
unimplemented!();
}
} }

39
src/entity/gateway/inmemory.rs

@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex};
pub struct InMemoryGateway { pub struct InMemoryGateway {
users: Arc<Mutex<HashMap<u32, UserAccount>>>, users: Arc<Mutex<HashMap<u32, UserAccount>>>,
user_settings: Arc<Mutex<HashMap<u32, UserSettings>>>, user_settings: Arc<Mutex<HashMap<u32, UserSettings>>>,
//guildcard: Arc<Mutex<HashMap<u32, GuildCardData>>>,
guildcards: Arc<Mutex<HashMap<u32, GuildCard>>>,
characters: Arc<Mutex<HashMap<u32, Character>>>, characters: Arc<Mutex<HashMap<u32, Character>>>,
items: Arc<Mutex<HashMap<ItemEntityId, Item>>>, items: Arc<Mutex<HashMap<ItemEntityId, Item>>>,
} }
@ -25,6 +25,7 @@ impl InMemoryGateway {
InMemoryGateway { InMemoryGateway {
users: Arc::new(Mutex::new(HashMap::new())), users: Arc::new(Mutex::new(HashMap::new())),
user_settings: Arc::new(Mutex::new(HashMap::new())), user_settings: Arc::new(Mutex::new(HashMap::new())),
guildcards: Arc::new(Mutex::new(HashMap::new())),
characters: Arc::new(Mutex::new(HashMap::new())), characters: Arc::new(Mutex::new(HashMap::new())),
items: Arc::new(Mutex::new(HashMap::new())), items: Arc::new(Mutex::new(HashMap::new())),
} }
@ -142,4 +143,40 @@ impl EntityGateway for InMemoryGateway {
}) })
.collect() .collect()
} }
fn create_guild_card_by_character(&self, character: &Character) -> GuildCard {
let mut guildcards = self.guildcards.lock().unwrap();
let user = self.get_user_by_id(character.user_id).unwrap();
let settings = self.get_user_settings_by_user(&user).unwrap();
let id = guildcards
.iter()
.fold(0, |sum, (i, _)| std::cmp::max(sum, *i))
+ 1;
let new_guildcard = GuildCard {
id: id,
character_id: character.id,
guildcard: libpso::character::guildcard::GuildCard {
guildcard: user.guildcard,
name: libpso::utf8_to_utf16_array!(character.name, 24),
team: settings.settings.team_name,
desc: [0; 88], // TODO?
reserved1: 1,
language: 0,
section_id: character.section_id.into(),
class: character.char_class.into(),
padding: 0,
comment: [0; 88], // TODO?
},
};
guildcards.insert(id, new_guildcard.clone());
new_guildcard
}
fn get_guild_card_by_character(&self, character: &Character) -> Option<GuildCard> {
let guildcards = self.guildcards.lock().unwrap();
guildcards
.iter()
.find(|(_, k)| k.character_id == character.id)
.map(|(_, k)| k.clone())
}
} }

10
src/login/character.rs

@ -281,7 +281,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
Ok(match get_login_status(&self.entity_gateway, pkt) { Ok(match get_login_status(&self.entity_gateway, pkt) {
Ok(user) => { Ok(user) => {
let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new());
response.guildcard = user.guildcard.map_or(0, |gc| gc) as u32;
response.guildcard = user.guildcard;
response.team_id = user.team_id.map_or(0, |ti| ti) as u32; response.team_id = user.team_id.map_or(0, |ti| ti) as u32;
client.user = Some(user); client.user = Some(user);
client.session = pkt.session; client.session = pkt.session;
@ -352,7 +352,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
let user = client.user.as_ref().unwrap(); let user = client.user.as_ref().unwrap();
client.session.action = SessionAction::SelectCharacter; client.session.action = SessionAction::SelectCharacter;
client.session.character_slot = select.slot as u8; client.session.character_slot = select.slot as u8;
Ok(vec![SendCharacterPacket::LoginResponse(LoginResponse::by_char_select(user.guildcard.unwrap_or(0),
Ok(vec![SendCharacterPacket::LoginResponse(LoginResponse::by_char_select(user.guildcard,
user.team_id.unwrap_or(1), user.team_id.unwrap_or(1),
client.session)), client.session)),
SendCharacterPacket::CharAck(CharAck { SendCharacterPacket::CharAck(CharAck {
@ -440,7 +440,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
client.session.character_slot = preview.slot as u8; client.session.character_slot = preview.slot as u8;
user.flags = 0; user.flags = 0;
self.entity_gateway.set_user(&user); self.entity_gateway.set_user(&user);
Ok(vec![SendCharacterPacket::LoginResponse(LoginResponse::by_char_select(user.guildcard.unwrap_or(0),
Ok(vec![SendCharacterPacket::LoginResponse(LoginResponse::by_char_select(user.guildcard,
user.team_id.unwrap_or(1), user.team_id.unwrap_or(1),
client.session)), client.session)),
SendCharacterPacket::CharAck(CharAck { SendCharacterPacket::CharAck(CharAck {
@ -642,7 +642,7 @@ mod test {
id: 1, id: 1,
username: "testuser".to_owned(), username: "testuser".to_owned(),
password: bcrypt::hash("mypassword", 5).unwrap(), password: bcrypt::hash("mypassword", 5).unwrap(),
guildcard: None,
guildcard: 0,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -684,7 +684,7 @@ mod test {
id: 3, id: 3,
username: "hi3".to_string(), username: "hi3".to_string(),
password: bcrypt::hash("qwer", 5).unwrap(), password: bcrypt::hash("qwer", 5).unwrap(),
guildcard: Some(3),
guildcard: 3,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),

6
src/login/login.rs

@ -178,7 +178,7 @@ mod test {
id: 1, id: 1,
username: "testuser".to_owned(), username: "testuser".to_owned(),
password: bcrypt::hash("mypassword", 5).unwrap(), password: bcrypt::hash("mypassword", 5).unwrap(),
guildcard: None,
guildcard: 0,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -258,7 +258,7 @@ mod test {
id: 1, id: 1,
username: "testuser".to_owned(), username: "testuser".to_owned(),
password: bcrypt::hash("notpassword", 5).unwrap(), password: bcrypt::hash("notpassword", 5).unwrap(),
guildcard: None,
guildcard: 0,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -301,7 +301,7 @@ mod test {
id: 1, id: 1,
username: "testuser".to_owned(), username: "testuser".to_owned(),
password: bcrypt::hash("mypassword", 5).unwrap(), password: bcrypt::hash("mypassword", 5).unwrap(),
guildcard: None,
guildcard: 0,
team_id: None, team_id: None,
banned: true, banned: true,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),

16
src/main.rs

@ -62,7 +62,7 @@ fn main() {
id: 1, id: 1,
username: "hi".to_string(), username: "hi".to_string(),
password: bcrypt::hash("qwer", 5).unwrap(), password: bcrypt::hash("qwer", 5).unwrap(),
guildcard: Some(1),
guildcard: 1u32,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -73,11 +73,13 @@ fn main() {
entity_gateway.create_user_settings_by_user(&fake_user); entity_gateway.create_user_settings_by_user(&fake_user);
let mut character = entity_gateway.new_character_by_user(&fake_user); let mut character = entity_gateway.new_character_by_user(&fake_user);
character.name = "Test Char 1".into(); character.name = "Test Char 1".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let mut character = entity_gateway.new_character_by_user(&fake_user); let mut character = entity_gateway.new_character_by_user(&fake_user);
character.slot = 2; character.slot = 2;
character.name = "\tE12345678".into(); character.name = "\tE12345678".into();
character.exp = 80000000; character.exp = 80000000;
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
entity_gateway.new_item( entity_gateway.new_item(
@ -102,7 +104,7 @@ fn main() {
id: 2, id: 2,
username: "hi2".to_string(), username: "hi2".to_string(),
password: bcrypt::hash("qwer", 5).unwrap(), password: bcrypt::hash("qwer", 5).unwrap(),
guildcard: Some(2),
guildcard: 2u32,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -113,17 +115,19 @@ fn main() {
entity_gateway.create_user_settings_by_user(&fake_user2); entity_gateway.create_user_settings_by_user(&fake_user2);
let mut character = entity_gateway.new_character_by_user(&fake_user2); let mut character = entity_gateway.new_character_by_user(&fake_user2);
character.name = "Test Char 3".into(); character.name = "Test Char 3".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let mut character = entity_gateway.new_character_by_user(&fake_user2); let mut character = entity_gateway.new_character_by_user(&fake_user2);
character.slot = 2; character.slot = 2;
character.name = "Test Char 4".into(); character.name = "Test Char 4".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let fake_user3 = UserAccount { let fake_user3 = UserAccount {
id: 3, id: 3,
username: "hi3".to_string(), username: "hi3".to_string(),
password: bcrypt::hash("qwer", 5).unwrap(), password: bcrypt::hash("qwer", 5).unwrap(),
guildcard: Some(3),
guildcard: 3u32,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -134,17 +138,19 @@ fn main() {
entity_gateway.create_user_settings_by_user(&fake_user3); entity_gateway.create_user_settings_by_user(&fake_user3);
let mut character = entity_gateway.new_character_by_user(&fake_user3); let mut character = entity_gateway.new_character_by_user(&fake_user3);
character.name = "Test Char 5".into(); character.name = "Test Char 5".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let mut character = entity_gateway.new_character_by_user(&fake_user3); let mut character = entity_gateway.new_character_by_user(&fake_user3);
character.slot = 2; character.slot = 2;
character.name = "Test Char 6".into(); character.name = "Test Char 6".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let fake_user4 = UserAccount { let fake_user4 = UserAccount {
id: 4, id: 4,
username: "hi4".to_string(), username: "hi4".to_string(),
password: bcrypt::hash("qwer", 5).unwrap(), password: bcrypt::hash("qwer", 5).unwrap(),
guildcard: Some(4),
guildcard: 4u32,
team_id: None, team_id: None,
banned: false, banned: false,
muted_until: SystemTime::now(), muted_until: SystemTime::now(),
@ -155,10 +161,12 @@ fn main() {
entity_gateway.create_user_settings_by_user(&fake_user4); entity_gateway.create_user_settings_by_user(&fake_user4);
let mut character = entity_gateway.new_character_by_user(&fake_user4); let mut character = entity_gateway.new_character_by_user(&fake_user4);
character.name = "Test Char 7".into(); character.name = "Test Char 7".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
let mut character = entity_gateway.new_character_by_user(&fake_user4); let mut character = entity_gateway.new_character_by_user(&fake_user4);
character.slot = 2; character.slot = 2;
character.name = "Test Char 8".into(); character.name = "Test Char 8".into();
entity_gateway.create_guild_card_by_character(&character);
entity_gateway.set_character(&character); entity_gateway.set_character(&character);
async_std::task::block_on(async move { async_std::task::block_on(async move {

39
src/ship/ship.rs

@ -15,7 +15,7 @@ use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState
use crate::common::leveltable::CharacterLevelTable; use crate::common::leveltable::CharacterLevelTable;
use crate::entity::gateway::EntityGateway; use crate::entity::gateway::EntityGateway;
use crate::entity::account::{UserAccount, UserSettings, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM};
use crate::entity::account::{UserAccount, UserSettings, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM, GuildCard};
use crate::entity::character::Character; use crate::entity::character::Character;
use crate::entity::item::{ItemLocation, Item}; use crate::entity::item::{ItemLocation, Item};
use crate::login::login::get_login_status; use crate::login::login::get_login_status;
@ -119,18 +119,20 @@ struct ClientState {
character: Character, character: Character,
session: Session, session: Session,
inventory: items::Inventory, inventory: items::Inventory,
guildcard: GuildCard,
//bank: Bank, //bank: Bank,
block: u32, block: u32,
} }
impl ClientState { impl ClientState {
fn new(user: UserAccount, settings: UserSettings, character: Character, inventory: items::Inventory, /*bank: Bank,*/ session: Session) -> ClientState {
fn new(user: UserAccount, settings: UserSettings, character: Character, inventory: items::Inventory, /*bank: Bank,*/ session: Session, guildcard: GuildCard) -> ClientState {
ClientState { ClientState {
user: user, user: user,
settings: settings, settings: settings,
character: character, character: character,
session: session, session: session,
inventory: inventory, inventory: inventory,
guildcard: guildcard,
//bank: bank, //bank: bank,
block: 1, block: 1,
} }
@ -186,8 +188,10 @@ impl<EG: EntityGateway> ShipServerState<EG> {
let stacked_items = items::stack_items(inventory); let stacked_items = items::stack_items(inventory);
let activated_items = stacked_items.into_iter().map(|item| self.item_activator.activate_item(item)).collect(); let activated_items = stacked_items.into_iter().map(|item| self.item_activator.activate_item(item)).collect();
let inventory = items::Inventory::new(activated_items); let inventory = items::Inventory::new(activated_items);
let guildcard = self.entity_gateway.get_guild_card_by_character(&character)
.ok_or(ShipError::ClientNotFound(id))?;
self.clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session));
self.clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session, guildcard));
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&self.name, 3))] vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&self.name, 3))]
}, },
Err(err) => { Err(err) => {
@ -332,11 +336,40 @@ impl<EG: EntityGateway> ShipServerState<EG> {
fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> { fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
let cmsg = msg.clone(); let cmsg = msg.clone();
let client = self.clients.get_mut(&id).unwrap();
let gc = self.entity_gateway.get_guild_card_by_character(&client.character).unwrap();
match &cmsg.msg {
GameMessage::GuildcardSend(GuildcardSend) => {
let out_msg = DirectMessage{
flag: cmsg.flag,
msg: GameMessage::GuildcardRecv(GuildcardRecv {
client: 141,
target: 8,
guildcard: gc.guildcard.guildcard,
name: gc.guildcard.name,
team: gc.guildcard.team,
desc: gc.guildcard.desc,
one: 1,
language: gc.guildcard.language,
section_id: gc.guildcard.section_id,
class: gc.guildcard.class,
}),
};
Box::new(self.client_location.get_area_by_user(id).clients().iter()
.filter(|client| client.index == cmsg.flag as usize)
.map(|client| {
(client.client_id, SendShipPacket::DirectMessage(out_msg.clone()))
}).collect::<Vec<_>>().into_iter())
},
_ => {
Box::new(self.client_location.get_area_by_user(id).clients().iter() Box::new(self.client_location.get_area_by_user(id).clients().iter()
.filter(|client| client.index == cmsg.flag as usize) .filter(|client| client.index == cmsg.flag as usize)
.map(|client| { .map(|client| {
(client.client_id, SendShipPacket::DirectMessage(cmsg.clone())) (client.client_id, SendShipPacket::DirectMessage(cmsg.clone()))
}).collect::<Vec<_>>().into_iter()) }).collect::<Vec<_>>().into_iter())
},
}
} }
fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> { fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {

Loading…
Cancel
Save