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.

79 lines
2.9 KiB

5 years ago
  1. use std::time::SystemTime;
  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::character::{CharacterEntity, NewCharacterEntity};
  6. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  7. use libpso::packet::ship::*;
  8. use libpso::packet::login::{Login, Session};
  9. use libpso::{utf8_to_array, utf8_to_utf16_array};
  10. pub async fn new_user_character<EG: EntityGateway>(entity_gateway: &mut EG, username: &str, password: &str) -> (UserAccountEntity, CharacterEntity) {
  11. let new_user = NewUserAccountEntity {
  12. username: username.into(),
  13. password: bcrypt::hash(password, 5).unwrap(),
  14. guildcard: 1,
  15. team_id: None,
  16. banned: false,
  17. muted_until: SystemTime::now(),
  18. created_at: SystemTime::now(),
  19. flags: 0,
  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);
  25. let character = entity_gateway.create_character(new_character).await.unwrap();
  26. (user, character)
  27. }
  28. pub async fn log_in_char<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, username: &str, password: &str) {
  29. let username = username.to_string();
  30. let password = password.to_string();
  31. ship.handle(id, &RecvShipPacket::Login(Login {
  32. tag: 0,
  33. guildcard: 0,
  34. version: 0,
  35. unknown1: [0; 6],
  36. team: 0,
  37. username: utf8_to_array!(username, 16),
  38. unknown2: [0; 32],
  39. password: utf8_to_array!(password, 16),
  40. unknown3: [0; 40],
  41. hwinfo: [0; 8],
  42. session: Session::new(),
  43. })).await.unwrap().for_each(drop);
  44. }
  45. pub async fn join_lobby<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId) {
  46. ship.handle(id, &RecvShipPacket::CharData(CharData {
  47. _unknown: [0; 0x828]
  48. })).await.unwrap().for_each(drop);
  49. }
  50. pub async fn create_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, name: &str, password: &str) {
  51. ship.handle(id, &RecvShipPacket::CreateRoom(CreateRoom {
  52. unknown: [0; 2],
  53. name: utf8_to_utf16_array!(name, 16),
  54. password: utf8_to_utf16_array!(password, 16),
  55. difficulty: 0,
  56. battle: 0,
  57. challenge: 0,
  58. episode: 1,
  59. single_player: 0,
  60. padding: [0; 3],
  61. })).await.unwrap().for_each(drop);
  62. ship.handle(id, &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop);
  63. }
  64. pub async fn join_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, room_id: u32) {
  65. ship.handle(id, &RecvShipPacket::MenuSelect(MenuSelect {
  66. menu: ROOM_MENU_ID,
  67. item: room_id,
  68. })).await.unwrap().for_each(drop);
  69. }