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.

227 lines
9.6 KiB

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 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. println!("addto {:?}", add_to);
  64. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  65. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  66. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  67. },
  68. _ => panic!(),
  69. }
  70. leave_room(&mut ship, ClientId(2)).await;
  71. let p = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect {
  72. menu: ROOM_MENU_ID,
  73. item: 0,
  74. })).await.unwrap();
  75. match &p[1].1 {
  76. SendShipPacket::AddToRoom(add_to) => {
  77. assert_eq!(add_to.playerinfo.inventory.items.iter().map(|k| k.item_id).collect::<Vec<_>>(),
  78. vec![0x210000,0x210001,0x210002,0x210003,0x210004,0x210005,0x210006,0x210007,0x210008,0x210009,
  79. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  80. },
  81. _ => panic!(),
  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. #[async_std::test]
  103. async fn test_set_valid_quest_group() {
  104. let mut entity_gateway = InMemoryGateway::default();
  105. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  106. let mut ship = Box::new(ShipServerState::builder()
  107. .gateway(entity_gateway.clone())
  108. .build());
  109. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  110. join_lobby(&mut ship, ClientId(1)).await;
  111. create_room(&mut ship, ClientId(1), "room", "").await;
  112. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 0})).await.unwrap();
  113. match &packets[0].1 {
  114. SendShipPacket::QuestCategoryList(quest_cat) => {
  115. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Retrieval"));
  116. },
  117. _ => panic!("Wrong quest category"),
  118. }
  119. }
  120. #[async_std::test]
  121. async fn test_set_invalid_quest_group() {
  122. let mut entity_gateway = InMemoryGateway::default();
  123. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  124. let mut ship = Box::new(ShipServerState::builder()
  125. .gateway(entity_gateway.clone())
  126. .build());
  127. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  128. join_lobby(&mut ship, ClientId(1)).await;
  129. create_room(&mut ship, ClientId(1), "room", "").await;
  130. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 100})).await.unwrap();
  131. match &packets[0].1 {
  132. SendShipPacket::QuestCategoryList(quest_cat) => {
  133. // 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.
  134. // assuming we create an ep1 room in multimode, we should load the government quests in this test case
  135. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Government"));
  136. },
  137. _ => panic!("Wrong quest category"),
  138. }
  139. }
  140. #[async_std::test]
  141. async fn test_get_room_info() {
  142. let mut entity_gateway = InMemoryGateway::default();
  143. let (_user1, mut _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  144. _char1.name = String::from("GODmar");
  145. entity_gateway.save_character(&_char1).await.unwrap();
  146. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  147. let mut ship = Box::new(ShipServerState::builder()
  148. .gateway(entity_gateway.clone())
  149. .build());
  150. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  151. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  152. join_lobby(&mut ship, ClientId(1)).await;
  153. join_lobby(&mut ship, ClientId(2)).await;
  154. create_room(&mut ship, ClientId(1), "room", "").await;
  155. let _expectedmsg = String::from("1 Lv1 GODmar\nHUmar Pioneer 2\n");
  156. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  157. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  158. padding: [17664, 1157645568],
  159. msg: _expectedmsg,
  160. }))));
  161. }
  162. #[async_std::test]
  163. async fn test_cannot_get_room_info_after_room_is_closed() {
  164. let mut entity_gateway = InMemoryGateway::default();
  165. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  166. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  167. let mut ship = Box::new(ShipServerState::builder()
  168. .gateway(entity_gateway.clone())
  169. .build());
  170. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  171. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  172. join_lobby(&mut ship, ClientId(1)).await;
  173. join_lobby(&mut ship, ClientId(2)).await;
  174. create_room(&mut ship, ClientId(1), "room", "").await;
  175. leave_room(&mut ship, ClientId(1)).await;
  176. let _expectedmsg = String::from("Game is no longer active!\0");
  177. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  178. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  179. padding: [17664, 1157645568],
  180. msg: _expectedmsg,
  181. }))));
  182. }
  183. #[async_std::test]
  184. async fn test_cannot_join_room_after_its_closed() {
  185. let mut entity_gateway = InMemoryGateway::default();
  186. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  187. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  188. let mut ship = Box::new(ShipServerState::builder()
  189. .gateway(entity_gateway.clone())
  190. .build());
  191. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  192. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  193. join_lobby(&mut ship, ClientId(1)).await;
  194. join_lobby(&mut ship, ClientId(2)).await;
  195. create_room(&mut ship, ClientId(1), "room", "").await;
  196. leave_room(&mut ship, ClientId(1)).await;
  197. let _expectedmsg = String::from("This room no longer exists!\0");
  198. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect{menu: 3, item: 0})).await.unwrap();
  199. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallDialog(SmallDialog{
  200. padding: [0,0],
  201. msg: _expectedmsg, // wow yes cool rust is so great literally the best i can't put a String::from() directly in here.
  202. }))));
  203. }