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.

180 lines
6.1 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 elseware::ship::ship::{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 entity::item;
  16. use libpso::packet::ship::*;
  17. use libpso::packet::login::{Login, Session};
  18. use libpso::{utf8_to_array, utf8_to_utf16_array};
  19. fn null_quest_builder(_mode: RoomMode) -> Result<QuestList, QuestLoadError> {
  20. Ok(Default::default())
  21. }
  22. struct NullDropTable;
  23. impl DropTable for NullDropTable {
  24. fn get_drop(&mut self, _map_area: &MapArea, _monster: &MonsterType) -> Option<ItemDropType> {
  25. None
  26. }
  27. fn get_box_drop(&mut self, _map_area: &MapArea, _object: &MapObject) -> Option<ItemDropType> {
  28. None
  29. }
  30. }
  31. pub fn null_drop_table_builder(_episode: Episode, _difficult: Difficulty, _section_id: SectionID) -> Box<dyn DropTable + Send + Sync> {
  32. Box::new(NullDropTable)
  33. }
  34. pub fn standard_ship_buildable<EG: EntityGateway + Clone>(gateway: EG) -> ShipServerStateBuilder<EG> {
  35. ShipServerState::builder()
  36. .gateway(gateway)
  37. .standard_quest_builder(Box::new(null_quest_builder))
  38. .government_quest_builder(Box::new(null_quest_builder))
  39. .drop_table_builder(Box::new(null_drop_table_builder))
  40. .map_builder(Box::new(null_free_roam_maps))
  41. }
  42. pub fn standard_ship<EG: EntityGateway + Clone>(gateway: EG) -> ShipServerState<EG> {
  43. ShipServerState::builder()
  44. .gateway(gateway)
  45. .standard_quest_builder(Box::new(null_quest_builder))
  46. .government_quest_builder(Box::new(null_quest_builder))
  47. .drop_table_builder(Box::new(null_drop_table_builder))
  48. .map_builder(Box::new(null_free_roam_maps))
  49. .build()
  50. }
  51. //TODO: remove kb_conf_preset
  52. pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, username: &str, password: &str, kb_conf_preset: usize) -> (UserAccountEntity, CharacterEntity) {
  53. let new_user = NewUserAccountEntity {
  54. email: format!("{}@pso.com", username),
  55. username: username.into(),
  56. password: bcrypt::hash(password, 5).unwrap(),
  57. guildcard: 1,
  58. activated: true,
  59. ..NewUserAccountEntity::default()
  60. };
  61. let user = entity_gateway.create_user(new_user).await.unwrap();
  62. let new_settings = NewUserSettingsEntity::new(user.id);
  63. let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap();
  64. let new_character = NewCharacterEntity::new(user.id);
  65. let character = entity_gateway.create_character(new_character).await.unwrap();
  66. entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap();
  67. entity_gateway.set_bank_meseta(&character.id, &BankIdentifier::Character, Meseta(0)).await.unwrap();
  68. (user, character)
  69. }
  70. pub async fn log_in_char<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, username: &str, password: &str) {
  71. let username = username.to_string();
  72. let password = password.to_string();
  73. ship.handle(id, RecvShipPacket::Login(Login {
  74. tag: 0,
  75. guildcard: 0,
  76. version: 0,
  77. unknown1: [0; 6],
  78. team: 0,
  79. username: utf8_to_array!(username, 16),
  80. unknown2: [0; 32],
  81. password: utf8_to_array!(password, 16),
  82. unknown3: [0; 40],
  83. hwinfo: [0; 8],
  84. session: Session::new(),
  85. })).await.unwrap();
  86. }
  87. pub async fn join_lobby<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId) {
  88. ship.handle(id, RecvShipPacket::CharData(CharData {
  89. _unknown: [0; 0x828]
  90. })).await.unwrap();
  91. }
  92. pub async fn create_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str) {
  93. create_room_with_difficulty(ship, id, name, password, Difficulty::Normal).await;
  94. }
  95. pub async fn leave_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId) {
  96. ship.handle(id, RecvShipPacket::LobbySelect(LobbySelect {
  97. menu: 3,
  98. lobby: 0,
  99. })).await.unwrap();
  100. }
  101. pub async fn create_room_with_difficulty<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str, difficulty: Difficulty) {
  102. ship.handle(id, RecvShipPacket::CreateRoom(CreateRoom {
  103. unknown: [0; 2],
  104. name: utf8_to_utf16_array!(name, 16),
  105. password: utf8_to_utf16_array!(password, 16),
  106. difficulty: difficulty.into(),
  107. battle: 0,
  108. challenge: 0,
  109. episode: 1,
  110. single_player: 0,
  111. padding: [0; 3],
  112. })).await.unwrap();
  113. ship.handle(id, RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap();
  114. }
  115. pub async fn join_room<EG: EntityGateway + Clone>(ship: &mut ShipServerState<EG>, id: ClientId, room_id: u32) {
  116. ship.handle(id, RecvShipPacket::MenuSelect(MenuSelect {
  117. menu: ROOM_MENU_ID,
  118. item: room_id,
  119. })).await.unwrap();
  120. ship.handle(id, RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap();
  121. }
  122. pub struct WeaponBuilder {
  123. weapon: item::weapon::WeaponType,
  124. grind: u8,
  125. }
  126. impl WeaponBuilder {
  127. pub fn grind(self, grind: u8) -> WeaponBuilder {
  128. WeaponBuilder {
  129. grind,
  130. ..self
  131. }
  132. }
  133. pub fn as_new(self) -> item::NewItemEntity {
  134. item::NewItemEntity {
  135. item: item::ItemDetail::Weapon(
  136. item::weapon::Weapon {
  137. weapon: self.weapon,
  138. grind: self.grind,
  139. special: None,
  140. attrs: [None, None, None],
  141. tekked: true,
  142. }
  143. )
  144. }
  145. }
  146. }
  147. pub struct ItemBuilder;
  148. impl ItemBuilder {
  149. pub fn weapon(weapon: item::weapon::WeaponType) -> WeaponBuilder {
  150. WeaponBuilder {
  151. weapon,
  152. grind: 0,
  153. }
  154. }
  155. }