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.

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