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.

83 lines
3.2 KiB

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