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.

229 lines
9.7 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. 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. /*
  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. ship.blocks.0[0].rooms.with(RoomId(0), |room| Box::pin(async move {
  97. let rates = &*room.rare_monster_table;
  98. for (_monster, rate) in rates.clone().appear_rate {
  99. assert_eq!(rate, 0.001953125f32); // 1/512 = 0.001953125
  100. }
  101. })).await.unwrap();
  102. }
  103. */
  104. #[async_std::test]
  105. async fn test_set_valid_quest_group() {
  106. let mut entity_gateway = InMemoryGateway::default();
  107. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  108. let mut ship = Box::new(ShipServerState::builder()
  109. .gateway(entity_gateway.clone())
  110. .build());
  111. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  112. join_lobby(&mut ship, ClientId(1)).await;
  113. create_room(&mut ship, ClientId(1), "room", "").await;
  114. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 0})).await.unwrap();
  115. match &packets[0].1 {
  116. SendShipPacket::QuestCategoryList(quest_cat) => {
  117. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Retrieval"));
  118. },
  119. _ => panic!("Wrong quest category"),
  120. }
  121. }
  122. #[async_std::test]
  123. async fn test_set_invalid_quest_group() {
  124. let mut entity_gateway = InMemoryGateway::default();
  125. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  126. let mut ship = Box::new(ShipServerState::builder()
  127. .gateway(entity_gateway.clone())
  128. .build());
  129. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  130. join_lobby(&mut ship, ClientId(1)).await;
  131. create_room(&mut ship, ClientId(1), "room", "").await;
  132. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 100})).await.unwrap();
  133. match &packets[0].1 {
  134. SendShipPacket::QuestCategoryList(quest_cat) => {
  135. // 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.
  136. // assuming we create an ep1 room in multimode, we should load the government quests in this test case
  137. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Government"));
  138. },
  139. _ => panic!("Wrong quest category"),
  140. }
  141. }
  142. #[async_std::test]
  143. async fn test_get_room_info() {
  144. let mut entity_gateway = InMemoryGateway::default();
  145. let (_user1, mut _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  146. _char1.name = String::from("GODmar");
  147. entity_gateway.save_character(&_char1).await.unwrap();
  148. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  149. let mut ship = Box::new(ShipServerState::builder()
  150. .gateway(entity_gateway.clone())
  151. .build());
  152. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  153. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  154. join_lobby(&mut ship, ClientId(1)).await;
  155. join_lobby(&mut ship, ClientId(2)).await;
  156. create_room(&mut ship, ClientId(1), "room", "").await;
  157. let _expectedmsg = String::from("1 Lv1 GODmar\nHUmar Pioneer 2\n");
  158. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  159. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  160. padding: [17664, 1157645568],
  161. msg: _expectedmsg,
  162. }))));
  163. }
  164. #[async_std::test]
  165. async fn test_cannot_get_room_info_after_room_is_closed() {
  166. let mut entity_gateway = InMemoryGateway::default();
  167. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  168. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  169. let mut ship = Box::new(ShipServerState::builder()
  170. .gateway(entity_gateway.clone())
  171. .build());
  172. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  173. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  174. join_lobby(&mut ship, ClientId(1)).await;
  175. join_lobby(&mut ship, ClientId(2)).await;
  176. create_room(&mut ship, ClientId(1), "room", "").await;
  177. leave_room(&mut ship, ClientId(1)).await;
  178. let _expectedmsg = String::from("Game is no longer active!\0");
  179. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  180. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  181. padding: [17664, 1157645568],
  182. msg: _expectedmsg,
  183. }))));
  184. }
  185. #[async_std::test]
  186. async fn test_cannot_join_room_after_its_closed() {
  187. let mut entity_gateway = InMemoryGateway::default();
  188. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  189. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  190. let mut ship = Box::new(ShipServerState::builder()
  191. .gateway(entity_gateway.clone())
  192. .build());
  193. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  194. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  195. join_lobby(&mut ship, ClientId(1)).await;
  196. join_lobby(&mut ship, ClientId(2)).await;
  197. create_room(&mut ship, ClientId(1), "room", "").await;
  198. leave_room(&mut ship, ClientId(1)).await;
  199. let _expectedmsg = String::from("This room no longer exists!\0");
  200. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect{menu: 3, item: 0})).await.unwrap();
  201. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallDialog(SmallDialog{
  202. padding: [0,0],
  203. msg: _expectedmsg, // wow yes cool rust is so great literally the best i can't put a String::from() directly in here.
  204. }))));
  205. }