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
8.2 KiB

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