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.

241 lines
8.7 KiB

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