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.

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