You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
12 KiB

4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
  1. #![allow(dead_code)]
  2. use networking::serverstate::{ClientId, ServerState};
  3. use entity::gateway::EntityGateway;
  4. use entity::account::{UserAccountEntity, NewUserAccountEntity, NewUserSettingsEntity};
  5. use entity::character::{CharacterEntity, NewCharacterEntity, SectionID};
  6. use entity::item::{Meseta, BankIdentifier};
  7. use ship_server::{ShipServerState, ShipServerStateBuilder, RecvShipPacket};
  8. use maps::room::{RoomMode, Difficulty, Episode};
  9. use maps::area::MapArea;
  10. use maps::maps::null_free_roam_maps;
  11. use maps::object::MapObject;
  12. use maps::monster::MonsterType;
  13. use quests::{QuestList, QuestLoadError};
  14. use drops::{DropTable, ItemDropType};
  15. use shops::{ItemShops, WeaponShopItem, ToolShopItem, ArmorShopItem};
  16. use entity::item;
  17. use libpso::packet::ship::*;
  18. use libpso::packet::login::{Login, Session};
  19. use libpso::{utf8_to_array, utf8_to_utf16_array};
  20. fn null_quest_builder(_mode: RoomMode) -> Result<QuestList, QuestLoadError> {
  21. Ok(Default::default())
  22. }
  23. struct NullDropTable;
  24. impl DropTable for NullDropTable {
  25. fn get_drop(&mut self, _map_area: &MapArea, _monster: &MonsterType) -> Option<ItemDropType> {
  26. None
  27. }
  28. fn get_box_drop(&mut self, _map_area: &MapArea, _object: &MapObject) -> Option<ItemDropType> {
  29. None
  30. }
  31. }
  32. pub fn null_drop_table_builder(_episode: Episode, _difficult: Difficulty, _section_id: SectionID) -> Box<dyn DropTable + Send + Sync> {
  33. Box::new(NullDropTable)
  34. }
  35. struct NullItemShops;
  36. #[async_trait::async_trait]
  37. impl ItemShops for NullItemShops {
  38. async fn generate_weapon_list(&self, _difficulty: Difficulty, _section_id: SectionID, _char_level: usize) -> Option<Vec<WeaponShopItem>> {
  39. Some(Vec::new())
  40. }
  41. async fn generate_tool_list(&self, _char_level: usize) -> Vec<ToolShopItem> {
  42. Vec::new()
  43. }
  44. async fn generate_armor_list(&self, _char_level: usize) -> Vec<ArmorShopItem> {
  45. Vec::new()
  46. }
  47. }
  48. pub fn standard_ship_buildable<EG: EntityGateway + Clone>(gateway: EG) -> ShipServerStateBuilder<EG> {
  49. ShipServerState::builder()
  50. .gateway(gateway)
  51. .standard_quest_builder(Box::new(null_quest_builder))
  52. .government_quest_builder(Box::new(null_quest_builder))
  53. .drop_table_builder(Box::new(null_drop_table_builder))
  54. .map_builder(Box::new(null_free_roam_maps))
  55. .item_shops(NullItemShops)
  56. }
  57. pub fn standard_ship<EG: EntityGateway + Clone>(gateway: EG) -> ShipServerState<EG> {
  58. ShipServerState::builder()
  59. .gateway(gateway)
  60. .standard_quest_builder(Box::new(null_quest_builder))
  61. .government_quest_builder(Box::new(null_quest_builder))
  62. .drop_table_builder(Box::new(null_drop_table_builder))
  63. .map_builder(Box::new(null_free_roam_maps))
  64. .item_shops(NullItemShops)
  65. .build()
  66. }
  67. //TODO: remove kb_conf_preset
  68. pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, username: &str, password: &str) -> (UserAccountEntity, CharacterEntity) {
  69. let new_user = NewUserAccountEntity {
  70. email: format!("{}@pso.com", username),
  71. username: username.into(),
  72. password: bcrypt::hash(password, 5).unwrap(),
  73. guildcard: 1,
  74. activated: true,
  75. ..NewUserAccountEntity::default()
  76. };
  77. let user = entity_gateway.create_user(new_user).await.unwrap();
  78. let new_settings = NewUserSettingsEntity::new(user.id);
  79. let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap();
  80. let new_character = NewCharacterEntity::new(user.id);
  81. let character = entity_gateway.create_character(new_character).await.unwrap();
  82. entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap();
  83. entity_gateway.set_bank_meseta(&character.id, &BankIdentifier::Character, Meseta(0)).await.unwrap();
  84. (user, character)
  85. }
  86. pub async fn log_in_char<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, username: &str, password: &str) {
  87. let username = username.to_string();
  88. let password = password.to_string();
  89. ship.handle(id, RecvShipPacket::Login(Login {
  90. tag: 0,
  91. guildcard: 0,
  92. version: 0,
  93. unknown1: [0; 6],
  94. team: 0,
  95. username: utf8_to_array!(username, 16),
  96. unknown2: [0; 32],
  97. password: utf8_to_array!(password, 16),
  98. unknown3: [0; 40],
  99. hwinfo: [0; 8],
  100. session: Session::new(),
  101. })).await.unwrap();
  102. }
  103. pub async fn join_lobby<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId) {
  104. ship.handle(id, RecvShipPacket::CharData(CharData {
  105. _unknown: [0; 0x828]
  106. })).await.unwrap();
  107. }
  108. pub async fn create_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str) {
  109. create_room_with_difficulty(ship, id, name, password, Difficulty::Normal).await;
  110. }
  111. pub async fn leave_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId) {
  112. ship.handle(id, RecvShipPacket::LobbySelect(LobbySelect {
  113. menu: 3,
  114. lobby: 0,
  115. })).await.unwrap();
  116. }
  117. pub async fn create_room_with_difficulty<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str, difficulty: Difficulty) {
  118. ship.handle(id, RecvShipPacket::CreateRoom(CreateRoom {
  119. unknown: [0; 2],
  120. name: utf8_to_utf16_array!(name, 16),
  121. password: utf8_to_utf16_array!(password, 16),
  122. difficulty: difficulty.into(),
  123. battle: 0,
  124. challenge: 0,
  125. episode: 1,
  126. single_player: 0,
  127. padding: [0; 3],
  128. })).await.unwrap();
  129. ship.handle(id, RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap();
  130. }
  131. pub async fn join_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, room_id: u32) {
  132. ship.handle(id, RecvShipPacket::MenuSelect(MenuSelect {
  133. menu: ROOM_MENU_ID,
  134. item: room_id,
  135. })).await.unwrap();
  136. ship.handle(id, RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap();
  137. }
  138. pub struct WeaponBuilder {
  139. weapon: item::weapon::WeaponType,
  140. grind: u8,
  141. special: Option<item::weapon::WeaponSpecial>,
  142. attributes: [Option<item::weapon::WeaponAttribute>; 3],
  143. tekked: bool,
  144. }
  145. impl WeaponBuilder {
  146. fn new(weapon: item::weapon::WeaponType) -> WeaponBuilder {
  147. WeaponBuilder {
  148. weapon,
  149. grind: 0,
  150. special: None,
  151. attributes: [None; 3],
  152. tekked: true,
  153. }
  154. }
  155. pub fn grind(self, grind: u8) -> WeaponBuilder {
  156. WeaponBuilder {
  157. grind,
  158. ..self
  159. }
  160. }
  161. pub fn special(self, special: item::weapon::WeaponSpecial) -> WeaponBuilder {
  162. WeaponBuilder {
  163. special: Some(special),
  164. ..self
  165. }
  166. }
  167. pub fn attr(mut self, attr: item::weapon::Attribute, value: i8) -> WeaponBuilder {
  168. self.attributes
  169. .iter_mut()
  170. .find(|k| k.is_none())
  171. .map(|empty_attr| {
  172. *empty_attr = Some(item::weapon::WeaponAttribute {
  173. attr,
  174. value,
  175. })
  176. });
  177. self
  178. }
  179. pub fn untekked(self) -> WeaponBuilder {
  180. WeaponBuilder {
  181. tekked: false,
  182. ..self
  183. }
  184. }
  185. pub fn as_new(self) -> item::NewItemEntity {
  186. item::NewItemEntity {
  187. item: item::ItemDetail::Weapon(
  188. item::weapon::Weapon {
  189. weapon: self.weapon,
  190. grind: self.grind,
  191. special: self.special,
  192. attrs: self.attributes,
  193. tekked: self.tekked,
  194. }
  195. )
  196. }
  197. }
  198. }
  199. pub struct ArmorBuilder {
  200. armor: item::armor::ArmorType,
  201. dfp: u8,
  202. evp: u8,
  203. slots: u8,
  204. }
  205. impl ArmorBuilder {
  206. pub fn new(armor: item::armor::ArmorType) -> ArmorBuilder {
  207. ArmorBuilder {
  208. armor: armor,
  209. dfp: 0,
  210. evp: 0,
  211. slots: 0,
  212. }
  213. }
  214. pub fn slots(self, slots: u8) -> ArmorBuilder {
  215. ArmorBuilder {
  216. slots,
  217. ..self
  218. }
  219. }
  220. pub fn dfp(self, dfp: u8) -> ArmorBuilder {
  221. ArmorBuilder {
  222. dfp,
  223. ..self
  224. }
  225. }
  226. pub fn evp(self, evp: u8) -> ArmorBuilder {
  227. ArmorBuilder {
  228. evp,
  229. ..self
  230. }
  231. }
  232. pub fn as_new(self) -> item::NewItemEntity {
  233. item::NewItemEntity {
  234. item: item::ItemDetail::Armor(
  235. item::armor::Armor {
  236. armor: self.armor,
  237. dfp: self.dfp,
  238. evp: self.evp,
  239. slots: self.slots,
  240. }
  241. )
  242. }
  243. }
  244. }
  245. pub struct ShieldBuilder {
  246. shield: item::shield::ShieldType,
  247. dfp: u8,
  248. evp: u8,
  249. }
  250. impl ShieldBuilder {
  251. pub fn new(shield: item::shield::ShieldType) -> ShieldBuilder {
  252. ShieldBuilder {
  253. shield: shield,
  254. dfp: 0,
  255. evp: 0,
  256. }
  257. }
  258. pub fn dfp(self, dfp: u8) -> ShieldBuilder {
  259. ShieldBuilder {
  260. dfp,
  261. ..self
  262. }
  263. }
  264. pub fn evp(self, evp: u8) -> ShieldBuilder {
  265. ShieldBuilder {
  266. evp,
  267. ..self
  268. }
  269. }
  270. pub fn as_new(self) -> item::NewItemEntity {
  271. item::NewItemEntity {
  272. item: item::ItemDetail::Shield(
  273. item::shield::Shield {
  274. shield: self.shield,
  275. dfp: self.dfp,
  276. evp: self.evp,
  277. }
  278. )
  279. }
  280. }
  281. }
  282. pub struct UnitBuilder {
  283. unit: item::unit::UnitType,
  284. modifier: Option<item::unit::UnitModifier>,
  285. }
  286. impl UnitBuilder {
  287. pub fn modifier(self, modifier: item::unit::UnitModifier) -> UnitBuilder {
  288. UnitBuilder {
  289. modifier: Some(modifier),
  290. ..self
  291. }
  292. }
  293. pub fn as_new(self) -> item::NewItemEntity {
  294. item::NewItemEntity {
  295. item: item::ItemDetail::Unit(
  296. item::unit::Unit {
  297. unit: self.unit,
  298. modifier: self.modifier,
  299. }
  300. )
  301. }
  302. }
  303. }
  304. pub struct MagBuilder {
  305. }
  306. impl MagBuilder {
  307. pub fn as_new(self) -> item::NewItemEntity {
  308. item::NewItemEntity {
  309. item: item::ItemDetail::Mag(
  310. item::mag::Mag::baby_mag(0)
  311. )
  312. }
  313. }
  314. }
  315. pub struct ToolBuilder {
  316. tool: item::tool::ToolType,
  317. }
  318. impl ToolBuilder {
  319. pub fn as_new(self) -> item::NewItemEntity {
  320. item::NewItemEntity {
  321. item: item::ItemDetail::Tool (
  322. item::tool::Tool {
  323. tool: self.tool,
  324. }
  325. ),
  326. }
  327. }
  328. }
  329. pub struct TechBuilder {
  330. tech: item::tech::Technique,
  331. level: u32,
  332. }
  333. impl TechBuilder {
  334. pub fn level(self, level: u32) -> TechBuilder {
  335. TechBuilder {
  336. level,
  337. ..self
  338. }
  339. }
  340. pub fn as_new(self) -> item::NewItemEntity {
  341. item::NewItemEntity {
  342. item: item::ItemDetail::TechniqueDisk (
  343. item::tech::TechniqueDisk {
  344. tech: self.tech,
  345. level: self.level,
  346. }
  347. )
  348. }
  349. }
  350. }
  351. pub struct ItemBuilder;
  352. impl ItemBuilder {
  353. pub fn weapon(weapon: item::weapon::WeaponType) -> WeaponBuilder {
  354. WeaponBuilder::new(weapon)
  355. }
  356. pub fn armor(armor: item::armor::ArmorType) -> ArmorBuilder {
  357. ArmorBuilder::new(armor)
  358. }
  359. pub fn shield(shield: item::shield::ShieldType) -> ShieldBuilder {
  360. ShieldBuilder::new(shield)
  361. }
  362. pub fn unit(unit: item::unit::UnitType) -> UnitBuilder {
  363. UnitBuilder {
  364. unit: unit,
  365. modifier: None,
  366. }
  367. }
  368. pub fn baby_mag() -> MagBuilder {
  369. MagBuilder {
  370. }
  371. }
  372. pub fn tool(tool: item::tool::ToolType) -> ToolBuilder {
  373. ToolBuilder {
  374. tool: tool,
  375. }
  376. }
  377. pub fn tech(tech: item::tech::Technique) -> TechBuilder {
  378. TechBuilder {
  379. tech: tech,
  380. level: 0,
  381. }
  382. }
  383. }