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.

235 lines
8.5 KiB

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