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.

302 lines
9.9 KiB

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