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.

160 lines
6.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. use elseware::common::serverstate::{ClientId, ServerState};
  2. use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::entity::item;
  4. use elseware::ship::ship::{ShipServerState, RecvShipPacket, SendShipPacket};
  5. use libpso::packet::ship::*;
  6. //use libpso::packet::messages::*;
  7. #[path = "common.rs"]
  8. mod common;
  9. use common::*;
  10. #[async_std::test]
  11. async fn test_item_ids_reset_when_rejoining_rooms() {
  12. let mut entity_gateway = InMemoryGateway::default();
  13. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  14. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  15. let mut p1_inv = Vec::new();
  16. for _ in 0..3usize {
  17. p1_inv.push(entity_gateway.create_item(
  18. item::NewItemEntity {
  19. item: item::ItemDetail::Weapon(
  20. item::weapon::Weapon {
  21. weapon: item::weapon::WeaponType::Saber,
  22. grind: 0,
  23. special: None,
  24. attrs: [None, None, None],
  25. tekked: true,
  26. kills: None,
  27. }
  28. ),
  29. }).await.unwrap());
  30. }
  31. let mut p2_inv = Vec::new();
  32. for _ in 0..10usize {
  33. p2_inv.push(entity_gateway.create_item(
  34. item::NewItemEntity {
  35. item: item::ItemDetail::Weapon(
  36. item::weapon::Weapon {
  37. weapon: item::weapon::WeaponType::Saber,
  38. grind: 0,
  39. special: None,
  40. attrs: [None, None, None],
  41. tekked: true,
  42. kills: None,
  43. }
  44. ),
  45. }).await.unwrap());
  46. }
  47. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  48. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();
  49. let mut ship = Box::new(ShipServerState::builder()
  50. .gateway(entity_gateway.clone())
  51. .build());
  52. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  53. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  54. join_lobby(&mut ship, ClientId(1)).await;
  55. join_lobby(&mut ship, ClientId(2)).await;
  56. create_room(&mut ship, ClientId(1), "room", "").await;
  57. let p = ship.handle(ClientId(2), &RecvShipPacket::MenuSelect(MenuSelect {
  58. menu: ROOM_MENU_ID,
  59. item: 0,
  60. })).await.unwrap().collect::<Vec<_>>();
  61. ship.handle(ClientId(2), &RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap().for_each(drop);
  62. match &p[1].1 {
  63. SendShipPacket::AddToRoom(add_to) => {
  64. println!("addto {:?}", add_to);
  65. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  66. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  67. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  68. },
  69. _ => panic!(),
  70. }
  71. leave_room(&mut ship, ClientId(2)).await;
  72. let p = ship.handle(ClientId(2), &RecvShipPacket::MenuSelect(MenuSelect {
  73. menu: ROOM_MENU_ID,
  74. item: 0,
  75. })).await.unwrap().collect::<Vec<_>>();
  76. match &p[1].1 {
  77. SendShipPacket::AddToRoom(add_to) => {
  78. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  79. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  80. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  81. },
  82. _ => panic!(),
  83. }
  84. }
  85. #[async_std::test]
  86. async fn test_load_rare_monster_default_appear_rates() {
  87. let mut entity_gateway = InMemoryGateway::default();
  88. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  89. let mut ship = Box::new(ShipServerState::builder()
  90. .gateway(entity_gateway.clone())
  91. .build());
  92. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  93. join_lobby(&mut ship, ClientId(1)).await;
  94. create_room(&mut ship, ClientId(1), "room", "").await;
  95. // assume episode 1
  96. let room = ship.blocks.0[0].rooms[0].as_ref().unwrap();
  97. println!("rare monster table: {:?}", room.rare_monster_table);
  98. let rates = &*room.rare_monster_table;
  99. for (_monster, rate) in rates.clone().appear_rate {
  100. assert_eq!(rate, 0.001953125f32); // 1/512 = 0.001953125
  101. }
  102. }
  103. #[async_std::test]
  104. async fn test_set_valid_quest_group() {
  105. let mut entity_gateway = InMemoryGateway::default();
  106. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  107. let mut ship = Box::new(ShipServerState::builder()
  108. .gateway(entity_gateway.clone())
  109. .build());
  110. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  111. join_lobby(&mut ship, ClientId(1)).await;
  112. create_room(&mut ship, ClientId(1), "room", "").await;
  113. let packets = ship.handle(ClientId(1), &RecvShipPacket::RequestQuestList(RequestQuestList{flag: 0})).await.unwrap().collect::<Vec<_>>();
  114. match &packets[0].1 {
  115. SendShipPacket::QuestCategoryList(quest_cat) => {
  116. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Retrieval"));
  117. },
  118. _ => panic!("Wrong quest category"),
  119. }
  120. }
  121. #[async_std::test]
  122. async fn test_set_invalid_quest_group() {
  123. let mut entity_gateway = InMemoryGateway::default();
  124. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  125. let mut ship = Box::new(ShipServerState::builder()
  126. .gateway(entity_gateway.clone())
  127. .build());
  128. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  129. join_lobby(&mut ship, ClientId(1)).await;
  130. create_room(&mut ship, ClientId(1), "room", "").await;
  131. let packets = ship.handle(ClientId(1), &RecvShipPacket::RequestQuestList(RequestQuestList{flag: 100})).await.unwrap().collect::<Vec<_>>();
  132. match &packets[0].1 {
  133. SendShipPacket::QuestCategoryList(quest_cat) => {
  134. // flag > quest category length should take the highest value allowed for quest category which is 1 in multimode (for govt quests) and 0 in other modes.
  135. // assuming we create an ep1 room in multimode, we should load the government quests in this test case
  136. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Government"));
  137. },
  138. _ => panic!("Wrong quest category"),
  139. }
  140. }