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.

228 lines
9.6 KiB

3 years ago
2 years ago
3 years ago
3 years ago
2 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 elseware::ship::location::RoomId;
  6. use libpso::packet::ship::*;
  7. //use libpso::packet::messages::*;
  8. #[path = "common.rs"]
  9. mod common;
  10. use common::*;
  11. #[async_std::test]
  12. async fn test_item_ids_reset_when_rejoining_rooms() {
  13. let mut entity_gateway = InMemoryGateway::default();
  14. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  15. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  16. let mut p1_inv = Vec::new();
  17. for _ in 0..3usize {
  18. p1_inv.push(entity_gateway.create_item(
  19. item::NewItemEntity {
  20. item: item::ItemDetail::Weapon(
  21. item::weapon::Weapon {
  22. weapon: item::weapon::WeaponType::Saber,
  23. grind: 0,
  24. special: None,
  25. attrs: [None, None, None],
  26. tekked: true,
  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. }
  43. ),
  44. }).await.unwrap());
  45. }
  46. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  47. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();
  48. let mut ship = Box::new(ShipServerState::builder()
  49. .gateway(entity_gateway.clone())
  50. .build());
  51. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  52. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  53. join_lobby(&mut ship, ClientId(1)).await;
  54. join_lobby(&mut ship, ClientId(2)).await;
  55. create_room(&mut ship, ClientId(1), "room", "").await;
  56. let p = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect {
  57. menu: ROOM_MENU_ID,
  58. item: 0,
  59. })).await.unwrap();
  60. ship.handle(ClientId(2), RecvShipPacket::DoneBursting(DoneBursting {})).await.unwrap();
  61. match &p[1].1 {
  62. SendShipPacket::AddToRoom(add_to) => {
  63. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  64. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  65. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  66. },
  67. _ => panic!(),
  68. }
  69. leave_room(&mut ship, ClientId(2)).await;
  70. let p = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect {
  71. menu: ROOM_MENU_ID,
  72. item: 0,
  73. })).await.unwrap();
  74. match &p[1].1 {
  75. SendShipPacket::AddToRoom(add_to) => {
  76. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  77. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  78. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  79. },
  80. _ => panic!(),
  81. }
  82. }
  83. /*
  84. #[async_std::test]
  85. async fn test_load_rare_monster_default_appear_rates() {
  86. let mut entity_gateway = InMemoryGateway::default();
  87. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  88. let mut ship = Box::new(ShipServerState::builder()
  89. .gateway(entity_gateway.clone())
  90. .build());
  91. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  92. join_lobby(&mut ship, ClientId(1)).await;
  93. create_room(&mut ship, ClientId(1), "room", "").await;
  94. // assume episode 1
  95. ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  96. let rates = &*room.rare_monster_table;
  97. for (_monster, rate) in rates.clone().appear_rate {
  98. assert_eq!(rate, 0.001953125f32); // 1/512 = 0.001953125
  99. }
  100. })).await.unwrap();
  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();
  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();
  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. }
  141. #[async_std::test]
  142. async fn test_get_room_info() {
  143. let mut entity_gateway = InMemoryGateway::default();
  144. let (_user1, mut _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  145. _char1.name = String::from("GODmar");
  146. entity_gateway.save_character(&_char1).await.unwrap();
  147. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  148. let mut ship = Box::new(ShipServerState::builder()
  149. .gateway(entity_gateway.clone())
  150. .build());
  151. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  152. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  153. join_lobby(&mut ship, ClientId(1)).await;
  154. join_lobby(&mut ship, ClientId(2)).await;
  155. create_room(&mut ship, ClientId(1), "room", "").await;
  156. let _expectedmsg = String::from("1 Lv1 GODmar\nHUmar Pioneer 2\n");
  157. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  158. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  159. padding: [17664, 1157645568],
  160. msg: _expectedmsg,
  161. }))));
  162. }
  163. #[async_std::test]
  164. async fn test_cannot_get_room_info_after_room_is_closed() {
  165. let mut entity_gateway = InMemoryGateway::default();
  166. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  167. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  168. let mut ship = Box::new(ShipServerState::builder()
  169. .gateway(entity_gateway.clone())
  170. .build());
  171. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  172. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  173. join_lobby(&mut ship, ClientId(1)).await;
  174. join_lobby(&mut ship, ClientId(2)).await;
  175. create_room(&mut ship, ClientId(1), "room", "").await;
  176. leave_room(&mut ship, ClientId(1)).await;
  177. let _expectedmsg = String::from("Game is no longer active!\0");
  178. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  179. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  180. padding: [17664, 1157645568],
  181. msg: _expectedmsg,
  182. }))));
  183. }
  184. #[async_std::test]
  185. async fn test_cannot_join_room_after_its_closed() {
  186. let mut entity_gateway = InMemoryGateway::default();
  187. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  188. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  189. let mut ship = Box::new(ShipServerState::builder()
  190. .gateway(entity_gateway.clone())
  191. .build());
  192. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  193. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  194. join_lobby(&mut ship, ClientId(1)).await;
  195. join_lobby(&mut ship, ClientId(2)).await;
  196. create_room(&mut ship, ClientId(1), "room", "").await;
  197. leave_room(&mut ship, ClientId(1)).await;
  198. let _expectedmsg = String::from("This room no longer exists!\0");
  199. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect{menu: 3, item: 0})).await.unwrap();
  200. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallDialog(SmallDialog{
  201. padding: [0,0],
  202. msg: _expectedmsg, // wow yes cool rust is so great literally the best i can't put a String::from() directly in here.
  203. }))));
  204. }