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.

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