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.

127 lines
5.9 KiB

3 years ago
3 years ago
  1. use networking::serverstate::{ClientId, ServerState};
  2. use entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::ship::ship::{ShipServerState, RecvShipPacket, SendShipPacket};
  4. use libpso::packet::ship::*;
  5. //use libpso::packet::messages::*;
  6. #[path = "common.rs"]
  7. mod common;
  8. use common::*;
  9. #[async_std::test]
  10. async fn test_set_valid_quest_group() {
  11. let mut entity_gateway = InMemoryGateway::default();
  12. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  13. let mut ship = Box::new(ShipServerState::builder()
  14. .gateway(entity_gateway.clone())
  15. .standard_quest_builder(Box::new(quests::load_standard_quests))
  16. .government_quest_builder(Box::new(quests::load_government_quests))
  17. .build());
  18. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  19. join_lobby(&mut ship, ClientId(1)).await;
  20. create_room(&mut ship, ClientId(1), "room", "").await;
  21. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 0})).await.unwrap();
  22. match &packets[0].1 {
  23. SendShipPacket::QuestCategoryList(quest_cat) => {
  24. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Retrieval"));
  25. },
  26. _ => panic!("Wrong quest category"),
  27. }
  28. }
  29. #[async_std::test]
  30. async fn test_set_invalid_quest_group() {
  31. let mut entity_gateway = InMemoryGateway::default();
  32. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  33. let mut ship = Box::new(ShipServerState::builder()
  34. .gateway(entity_gateway.clone())
  35. .standard_quest_builder(Box::new(quests::load_standard_quests))
  36. .government_quest_builder(Box::new(quests::load_government_quests))
  37. .build());
  38. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  39. join_lobby(&mut ship, ClientId(1)).await;
  40. create_room(&mut ship, ClientId(1), "room", "").await;
  41. let packets = ship.handle(ClientId(1), RecvShipPacket::RequestQuestList(RequestQuestList{flag: 100})).await.unwrap();
  42. match &packets[0].1 {
  43. SendShipPacket::QuestCategoryList(quest_cat) => {
  44. // 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.
  45. // assuming we create an ep1 room in multimode, we should load the government quests in this test case
  46. assert!(String::from_utf16_lossy(&quest_cat.quest_categories[0].name).starts_with("Government"));
  47. },
  48. _ => panic!("Wrong quest category"),
  49. }
  50. }
  51. #[async_std::test]
  52. async fn test_get_room_info() {
  53. let mut entity_gateway = InMemoryGateway::default();
  54. let (_user1, mut _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  55. _char1.name = String::from("GODmar");
  56. entity_gateway.save_character(&_char1).await.unwrap();
  57. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  58. let mut ship = Box::new(ShipServerState::builder()
  59. .gateway(entity_gateway.clone())
  60. .build());
  61. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  62. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  63. join_lobby(&mut ship, ClientId(1)).await;
  64. join_lobby(&mut ship, ClientId(2)).await;
  65. create_room(&mut ship, ClientId(1), "room", "").await;
  66. let _expectedmsg = String::from("1 Lv1 GODmar\nHUmar Pioneer 2\n");
  67. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  68. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  69. padding: [17664, 1157645568],
  70. msg: _expectedmsg,
  71. }))));
  72. }
  73. #[async_std::test]
  74. async fn test_cannot_get_room_info_after_room_is_closed() {
  75. let mut entity_gateway = InMemoryGateway::default();
  76. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  77. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  78. let mut ship = Box::new(ShipServerState::builder()
  79. .gateway(entity_gateway.clone())
  80. .build());
  81. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  82. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  83. join_lobby(&mut ship, ClientId(1)).await;
  84. join_lobby(&mut ship, ClientId(2)).await;
  85. create_room(&mut ship, ClientId(1), "room", "").await;
  86. leave_room(&mut ship, ClientId(1)).await;
  87. let _expectedmsg = String::from("Game is no longer active!\0");
  88. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuDetail(MenuDetail{menu: 3, item: 0})).await.unwrap();
  89. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallLeftDialog(SmallLeftDialog{
  90. padding: [17664, 1157645568],
  91. msg: _expectedmsg,
  92. }))));
  93. }
  94. #[async_std::test]
  95. async fn test_cannot_join_room_after_its_closed() {
  96. let mut entity_gateway = InMemoryGateway::default();
  97. let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  98. let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;
  99. let mut ship = Box::new(ShipServerState::builder()
  100. .gateway(entity_gateway.clone())
  101. .build());
  102. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  103. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  104. join_lobby(&mut ship, ClientId(1)).await;
  105. join_lobby(&mut ship, ClientId(2)).await;
  106. create_room(&mut ship, ClientId(1), "room", "").await;
  107. leave_room(&mut ship, ClientId(1)).await;
  108. let _expectedmsg = String::from("This room no longer exists!\0");
  109. let packets = ship.handle(ClientId(2), RecvShipPacket::MenuSelect(MenuSelect{menu: 3, item: 0})).await.unwrap();
  110. assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::SmallDialog(SmallDialog{
  111. padding: [0,0],
  112. msg: _expectedmsg, // wow yes cool rust is so great literally the best i can't put a String::from() directly in here.
  113. }))));
  114. }
  115. // TODO: test joining twice errors not hangs forever