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.

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