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.

193 lines
6.9 KiB

3 years ago
  1. use networking::serverstate::{ClientId, ServerState};
  2. use entity::gateway::{EntityGateway, InMemoryGateway};
  3. use ship_server::RecvShipPacket;
  4. use entity::item;
  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_equip_unit_from_equip_menu() {
  12. let mut entity_gateway = InMemoryGateway::default();
  13. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  14. let mut p1_inv = Vec::new();
  15. p1_inv.push(entity_gateway.create_item(
  16. ItemBuilder::armor(item::armor::ArmorType::Frame)
  17. .slots(4)
  18. .as_new()
  19. ).await.unwrap());
  20. p1_inv.push(entity_gateway.create_item(
  21. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  22. .as_new()
  23. ).await.unwrap());
  24. p1_inv.push(entity_gateway.create_item(
  25. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  26. .modifier(item::unit::UnitModifier::Plus)
  27. .as_new()
  28. ).await.unwrap());
  29. let equipped = item::EquippedEntity {
  30. weapon: None,
  31. armor: Some(p1_inv[0].id),
  32. shield: None,
  33. unit: [None; 4],
  34. mag: None,
  35. };
  36. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  37. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  38. let mut ship = standard_ship(entity_gateway.clone());
  39. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  40. join_lobby(&mut ship, ClientId(1)).await;
  41. create_room(&mut ship, ClientId(1), "room", "").await;
  42. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerEquipItem(PlayerEquipItem {
  43. client: 0,
  44. target: 0,
  45. item_id: 0x10001,
  46. sub_menu: 9,
  47. unknown1: 0,
  48. })))).await.unwrap();
  49. // case when someone tries to send invalid submenu? submenu is 9-12 in normal gameplay
  50. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerEquipItem(PlayerEquipItem {
  51. client: 0,
  52. target: 0,
  53. item_id: 0x10002,
  54. sub_menu: 14,
  55. unknown1: 0,
  56. })))).await.unwrap();
  57. let equips = entity_gateway.get_character_equips(&char1.id).await.unwrap();
  58. assert_eq!(equips.unit[0].unwrap(), item::ItemEntityId(2));
  59. assert_eq!(equips.unit[1].unwrap(), item::ItemEntityId(3));
  60. assert!(equips.unit[2].is_none());
  61. assert!(equips.unit[3].is_none());
  62. }
  63. #[async_std::test]
  64. async fn test_unequip_armor_with_units() {
  65. let mut entity_gateway = InMemoryGateway::default();
  66. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  67. let mut p1_inv = Vec::new();
  68. p1_inv.push(entity_gateway.create_item(
  69. ItemBuilder::armor(item::armor::ArmorType::Frame)
  70. .slots(4)
  71. .as_new()
  72. ).await.unwrap());
  73. p1_inv.push(entity_gateway.create_item(
  74. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  75. .as_new()
  76. ).await.unwrap());
  77. p1_inv.push(entity_gateway.create_item(
  78. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  79. .modifier(item::unit::UnitModifier::Plus)
  80. .as_new()
  81. ).await.unwrap());
  82. let equipped = item::EquippedEntity {
  83. weapon: None,
  84. armor: Some(p1_inv[0].id),
  85. shield: None,
  86. unit: [Some(p1_inv[1].id), Some(p1_inv[2].id), None, None],
  87. mag: None,
  88. };
  89. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  90. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  91. let mut ship = standard_ship(entity_gateway.clone());
  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. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUnequipItem(PlayerUnequipItem {
  96. client: 0,
  97. target: 0,
  98. item_id: 0x10000,
  99. unknown1: 0,
  100. })))).await.unwrap();
  101. let equips = entity_gateway.get_character_equips(&char1.id).await.unwrap();
  102. assert!(equips.armor.is_none());
  103. assert!(equips.unit[0].is_none());
  104. assert!(equips.unit[1].is_none());
  105. assert!(equips.unit[2].is_none());
  106. assert!(equips.unit[3].is_none());
  107. }
  108. #[async_std::test]
  109. async fn test_sort_items() {
  110. let mut entity_gateway = InMemoryGateway::default();
  111. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  112. let mut p1_inv = Vec::new();
  113. p1_inv.push(entity_gateway.create_item(
  114. ItemBuilder::armor(item::armor::ArmorType::Frame)
  115. .slots(4)
  116. .as_new()
  117. ).await.unwrap());
  118. p1_inv.push(entity_gateway.create_item(
  119. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  120. .as_new()
  121. ).await.unwrap());
  122. p1_inv.push(entity_gateway.create_item(
  123. ItemBuilder::unit(item::unit::UnitType::KnightPower)
  124. .modifier(item::unit::UnitModifier::Plus)
  125. .as_new()
  126. ).await.unwrap());
  127. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  128. let mut ship = standard_ship(entity_gateway.clone());
  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 inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  133. assert_eq!(inventory_items.items.len(), 3);
  134. inventory_items.items[0].with_individual(|item| {
  135. assert_eq!(item.id, item::ItemEntityId(1));
  136. }).unwrap();
  137. inventory_items.items[1].with_individual(|item| {
  138. assert_eq!(item.id, item::ItemEntityId(2));
  139. }).unwrap();
  140. inventory_items.items[2].with_individual(|item| {
  141. assert_eq!(item.id, item::ItemEntityId(3));
  142. }).unwrap();
  143. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::SortItems(SortItems {
  144. client: 255,
  145. target: 255,
  146. item_ids: [0x10001u32, 0x10002, 0x10000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  147. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  148. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF],
  149. })))).await.unwrap();
  150. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  151. assert_eq!(inventory_items.items.len(), 3);
  152. inventory_items.items[0].with_individual(|item| {
  153. assert_eq!(item.id, item::ItemEntityId(2));
  154. }).unwrap();
  155. inventory_items.items[1].with_individual(|item| {
  156. assert_eq!(item.id, item::ItemEntityId(3));
  157. }).unwrap();
  158. inventory_items.items[2].with_individual(|item| {
  159. assert_eq!(item.id, item::ItemEntityId(1));
  160. }).unwrap();
  161. }