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.

128 lines
5.3 KiB

4 years ago
5 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
  1. #![allow(dead_code)]
  2. use elseware::common::serverstate::{ClientId, ServerState};
  3. use elseware::entity::gateway::EntityGateway;
  4. use elseware::entity::account::{UserAccountEntity, NewUserAccountEntity, NewUserSettingsEntity};
  5. use elseware::entity::item::{Meseta, BankName};
  6. use elseware::entity::character::{CharacterEntity, NewCharacterEntity, SectionID};
  7. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  8. use elseware::ship::room::Difficulty;
  9. use libpso::packet::ship::*;
  10. use libpso::packet::login::{Login, Session};
  11. use libpso::{utf8_to_array, utf8_to_utf16_array};
  12. pub async fn new_user_character<EG: EntityGateway>(entity_gateway: &mut EG, username: &str, password: &str, kb_conf_preset: usize) -> (UserAccountEntity, CharacterEntity) {
  13. let new_user = NewUserAccountEntity {
  14. email: format!("{}@pso.com", username),
  15. username: username.into(),
  16. password: bcrypt::hash(password, 5).unwrap(),
  17. guildcard: 1,
  18. activated: true,
  19. ..NewUserAccountEntity::default()
  20. };
  21. let user = entity_gateway.create_user(new_user).await.unwrap();
  22. let new_settings = NewUserSettingsEntity::new(user.id);
  23. let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap();
  24. let new_character = NewCharacterEntity::new(user.id, kb_conf_preset);
  25. let character = entity_gateway.create_character(new_character).await.unwrap();
  26. entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap();
  27. entity_gateway.set_bank_meseta(&character.id, BankName("".into()), Meseta(0)).await.unwrap();
  28. (user, character)
  29. }
  30. pub async fn new_user_character_with_sid<EG: EntityGateway>(entity_gateway: &mut EG, username: &str, password: &str, sid: SectionID) -> (UserAccountEntity, CharacterEntity) {
  31. let new_user = NewUserAccountEntity {
  32. email: format!("{}@pso.com", username),
  33. username: username.into(),
  34. password: bcrypt::hash(password, 5).unwrap(),
  35. guildcard: 1,
  36. activated: true,
  37. ..NewUserAccountEntity::default()
  38. };
  39. let user = entity_gateway.create_user(new_user).await.unwrap();
  40. let new_settings = NewUserSettingsEntity::new(user.id);
  41. let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap();
  42. let mut new_character = NewCharacterEntity::new(user.id);
  43. new_character.section_id = sid;
  44. let character = entity_gateway.create_character(new_character).await.unwrap();
  45. (user, character)
  46. }
  47. pub async fn log_in_char<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, username: &str, password: &str) {
  48. let username = username.to_string();
  49. let password = password.to_string();
  50. ship.handle(id, &RecvShipPacket::Login(Login {
  51. tag: 0,
  52. guildcard: 0,
  53. version: 0,
  54. unknown1: [0; 6],
  55. team: 0,
  56. username: utf8_to_array!(username, 16),
  57. unknown2: [0; 32],
  58. password: utf8_to_array!(password, 16),
  59. unknown3: [0; 40],
  60. hwinfo: [0; 8],
  61. session: Session::new(),
  62. })).await.unwrap().for_each(drop);
  63. }
  64. pub async fn join_lobby<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId) {
  65. ship.handle(id, &RecvShipPacket::CharData(CharData {
  66. _unknown: [0; 0x828]
  67. })).await.unwrap().for_each(drop);
  68. }
  69. pub async fn create_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str) {
  70. create_room_with_difficulty(ship, id, name, password, Difficulty::Normal).await;
  71. }
  72. pub async fn leave_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId) {
  73. ship.handle(id, &RecvShipPacket::LobbySelect(LobbySelect {
  74. menu: 3,
  75. lobby: 0,
  76. })).await.unwrap().for_each(drop);
  77. }
  78. pub async fn create_room_with_difficulty<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str, difficulty: Difficulty) {
  79. ship.handle(id, &RecvShipPacket::CreateRoom(CreateRoom {
  80. unknown: [0; 2],
  81. name: utf8_to_utf16_array!(name, 16),
  82. password: utf8_to_utf16_array!(password, 16),
  83. difficulty: difficulty.into(),
  84. battle: 0,
  85. challenge: 0,
  86. episode: 1,
  87. single_player: 0,
  88. padding: [0; 3],
  89. })).await.unwrap().for_each(drop);
  90. ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop);
  91. }
  92. pub async fn create_ep2_room_with_difficulty<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str, difficulty: Difficulty) {
  93. ship.handle(id, &RecvShipPacket::CreateRoom(CreateRoom {
  94. unknown: [0; 2],
  95. name: utf8_to_utf16_array!(name, 16),
  96. password: utf8_to_utf16_array!(password, 16),
  97. difficulty: difficulty.into(),
  98. battle: 0,
  99. challenge: 0,
  100. episode: 2,
  101. single_player: 0,
  102. padding: [0; 3],
  103. })).await.unwrap().for_each(drop);
  104. ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop);
  105. }
  106. pub async fn join_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, room_id: u32) {
  107. ship.handle(id, &RecvShipPacket::MenuSelect(MenuSelect {
  108. menu: ROOM_MENU_ID,
  109. item: room_id,
  110. })).await.unwrap().for_each(drop);
  111. ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop);
  112. }