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.

452 lines
16 KiB

4 years ago
  1. use networking::serverstate::{ClientId, ServerState};
  2. use entity::gateway::{EntityGateway, InMemoryGateway};
  3. use entity::item;
  4. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  5. use entity::character::TechLevel;
  6. use libpso::packet::ship::*;
  7. use libpso::packet::messages::*;
  8. #[path = "common.rs"]
  9. mod common;
  10. use common::*;
  11. #[async_std::test]
  12. async fn test_use_monomate() {
  13. let mut entity_gateway = InMemoryGateway::default();
  14. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  15. let mut p1_items = Vec::new();
  16. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  17. let mut item = Vec::new();
  18. for _ in 0..2usize {
  19. item.push(entity_gateway.create_item(
  20. item::NewItemEntity {
  21. item: item::ItemDetail::Tool(
  22. item::tool::Tool {
  23. tool: tool
  24. }
  25. ),
  26. }).await.unwrap());
  27. }
  28. p1_items.push(item::InventoryItemEntity::Stacked(item));
  29. }
  30. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  31. let mut ship = standard_ship(entity_gateway.clone());
  32. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  33. join_lobby(&mut ship, ClientId(1)).await;
  34. create_room(&mut ship, ClientId(1), "room", "").await;
  35. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  36. client: 0,
  37. target: 0,
  38. item_id: 0x10000,
  39. })))).await.unwrap();
  40. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  41. assert_eq!(inventory_items.items.len(), 2);
  42. inventory_items.items[0].with_stacked(|items| {
  43. assert_eq!(items.len(), 1)
  44. }).unwrap();
  45. inventory_items.items[1].with_stacked(|items| {
  46. assert_eq!(items.len(), 2)
  47. }).unwrap();
  48. }
  49. #[async_std::test]
  50. async fn test_use_monomate_twice() {
  51. let mut entity_gateway = InMemoryGateway::default();
  52. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  53. let mut p1_items = Vec::new();
  54. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  55. let mut item = Vec::new();
  56. for _ in 0..3usize {
  57. item.push(entity_gateway.create_item(
  58. item::NewItemEntity {
  59. item: item::ItemDetail::Tool(
  60. item::tool::Tool {
  61. tool: tool
  62. }
  63. ),
  64. }).await.unwrap());
  65. }
  66. p1_items.push(item::InventoryItemEntity::Stacked(item));
  67. }
  68. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  69. let mut ship = standard_ship(entity_gateway.clone());
  70. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  71. join_lobby(&mut ship, ClientId(1)).await;
  72. create_room(&mut ship, ClientId(1), "room", "").await;
  73. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  74. client: 0,
  75. target: 0,
  76. item_id: 0x10000,
  77. })))).await.unwrap();
  78. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  79. client: 0,
  80. target: 0,
  81. item_id: 0x10000,
  82. })))).await.unwrap();
  83. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  84. assert_eq!(inventory_items.items.len(), 2);
  85. inventory_items.items[0].with_stacked(|items| {
  86. assert_eq!(items.len(), 1)
  87. }).unwrap();
  88. inventory_items.items[1].with_stacked(|items| {
  89. assert_eq!(items.len(), 3)
  90. }).unwrap();
  91. }
  92. #[async_std::test]
  93. async fn test_use_last_monomate() {
  94. let mut entity_gateway = InMemoryGateway::default();
  95. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  96. let mut p1_inv = Vec::new();
  97. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  98. p1_inv.push(item::InventoryItemEntity::Stacked(vec![entity_gateway.create_item(
  99. item::NewItemEntity {
  100. item: item::ItemDetail::Tool(
  101. item::tool::Tool {
  102. tool: tool
  103. }
  104. ),
  105. }).await.unwrap()]));
  106. }
  107. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  108. let mut ship = standard_ship(entity_gateway.clone());
  109. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  110. join_lobby(&mut ship, ClientId(1)).await;
  111. create_room(&mut ship, ClientId(1), "room", "").await;
  112. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  113. client: 0,
  114. target: 0,
  115. item_id: 0x10000,
  116. })))).await.unwrap();
  117. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  118. assert_eq!(inventory_items.items.len(), 1);
  119. inventory_items.items[0].with_stacked(|items| {
  120. assert_eq!(items.len(), 1);
  121. assert_eq!(items[0].item.item_type(), item::ItemType::Tool(item::tool::ToolType::Monofluid));
  122. }).unwrap();
  123. }
  124. #[async_std::test]
  125. async fn test_use_nonstackable_tool() {
  126. let mut entity_gateway = InMemoryGateway::default();
  127. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  128. let mut p1_items = Vec::new();
  129. p1_items.push(entity_gateway.create_item(
  130. item::NewItemEntity {
  131. item: item::ItemDetail::Tool(
  132. item::tool::Tool {
  133. tool: item::tool::ToolType::HuntersReport,
  134. }
  135. ),
  136. }).await.unwrap());
  137. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  138. let mut ship = standard_ship(entity_gateway.clone());
  139. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  140. join_lobby(&mut ship, ClientId(1)).await;
  141. create_room(&mut ship, ClientId(1), "room", "").await;
  142. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  143. client: 0,
  144. target: 0,
  145. item_id: 0x10000,
  146. })))).await.unwrap();
  147. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  148. assert_eq!(inventory_items.items.len(), 0);
  149. }
  150. #[async_std::test]
  151. async fn test_use_materials() {
  152. let mut entity_gateway = InMemoryGateway::default();
  153. let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  154. let mut p1_inv = Vec::new();
  155. for tool in vec![item::tool::ToolType::PowerMaterial, item::tool::ToolType::LuckMaterial].into_iter() {
  156. let mut item = Vec::new();
  157. for _ in 0..5usize {
  158. item.push(entity_gateway.create_item(
  159. item::NewItemEntity {
  160. item: item::ItemDetail::Tool(
  161. item::tool::Tool {
  162. tool: tool
  163. }
  164. ),
  165. }).await.unwrap());
  166. }
  167. p1_inv.push(item::InventoryItemEntity::Stacked(item));
  168. }
  169. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  170. let mut ship = standard_ship(entity_gateway.clone());
  171. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  172. join_lobby(&mut ship, ClientId(1)).await;
  173. create_room(&mut ship, ClientId(1), "room", "").await;
  174. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  175. client: 0,
  176. target: 0,
  177. item_id: 0x10000,
  178. })))).await.unwrap();
  179. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  180. client: 0,
  181. target: 0,
  182. item_id: 0x10001,
  183. })))).await.unwrap();
  184. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  185. client: 0,
  186. target: 0,
  187. item_id: 0x10001,
  188. })))).await.unwrap();
  189. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  190. assert_eq!(inventory_items.items.len(), 2);
  191. inventory_items.items[0].with_stacked(|items| {
  192. assert_eq!(items.len(), 4)
  193. }).unwrap();
  194. inventory_items.items[1].with_stacked(|items| {
  195. assert_eq!(items.len(), 3)
  196. }).unwrap();
  197. let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  198. let char = characters[0].as_ref().unwrap();
  199. assert!(char.materials.power == 1);
  200. assert!(char.materials.luck == 2);
  201. }
  202. #[async_std::test]
  203. async fn test_jackolantern() {
  204. let mut entity_gateway = InMemoryGateway::default();
  205. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  206. let p1_inv = vec![
  207. item::InventoryItemEntity::Stacked(
  208. vec![
  209. entity_gateway.create_item(
  210. item::NewItemEntity {
  211. item: item::ItemDetail::Tool(
  212. item::tool::Tool {
  213. tool: item::tool::ToolType::JackOLantern,
  214. }
  215. ),
  216. }).await.unwrap(),
  217. entity_gateway.create_item(item::NewItemEntity {
  218. item: item::ItemDetail::Tool(
  219. item::tool::Tool {
  220. tool: item::tool::ToolType::JackOLantern,
  221. }
  222. ),
  223. }).await.unwrap(),
  224. ])];
  225. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  226. let mut ship = standard_ship(entity_gateway.clone());
  227. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  228. join_lobby(&mut ship, ClientId(1)).await;
  229. create_room(&mut ship, ClientId(1), "room", "").await;
  230. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  231. client: 0,
  232. target: 0,
  233. item_id: 0x10000,
  234. })))).await.unwrap();
  235. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  236. client: 0,
  237. target: 0,
  238. item_id: 0x10000,
  239. })))).await.unwrap();
  240. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  241. for item in inventory_items.items {
  242. for sitem in item.stacked().unwrap() {
  243. assert!(sitem.item.clone().as_tool().unwrap().tool.is_mag_cell());
  244. }
  245. }
  246. }
  247. #[async_std::test]
  248. async fn test_use_barta_1() {
  249. let mut entity_gateway = InMemoryGateway::default();
  250. let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  251. let inv = vec![
  252. entity_gateway.create_item(
  253. item::NewItemEntity {
  254. item: item::ItemDetail::TechniqueDisk(
  255. item::tech::TechniqueDisk {
  256. tech: item::tech::Technique::Foie,
  257. level: 3,
  258. }
  259. )
  260. }
  261. ).await.unwrap(),
  262. entity_gateway.create_item(
  263. item::NewItemEntity {
  264. item: item::ItemDetail::TechniqueDisk(
  265. item::tech::TechniqueDisk {
  266. tech: item::tech::Technique::Barta,
  267. level: 4,
  268. }
  269. )
  270. }
  271. ).await.unwrap(),
  272. entity_gateway.create_item(
  273. item::NewItemEntity {
  274. item: item::ItemDetail::TechniqueDisk(
  275. item::tech::TechniqueDisk {
  276. tech: item::tech::Technique::Zonde,
  277. level: 5,
  278. }
  279. )
  280. }
  281. ).await.unwrap()
  282. ];
  283. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inv)).await.unwrap();
  284. let mut ship = standard_ship(entity_gateway.clone());
  285. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  286. join_lobby(&mut ship, ClientId(1)).await;
  287. create_room(&mut ship, ClientId(1), "room", "").await;
  288. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  289. client: 0,
  290. target: 0,
  291. item_id: 0x10000,
  292. })))).await.unwrap();
  293. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  294. client: 0,
  295. target: 0,
  296. item_id: 0x10002,
  297. })))).await.unwrap();
  298. let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  299. let char = characters[0].as_ref().unwrap();
  300. assert_eq!(char.techs.techs.len(), 2);
  301. assert_eq!(char.techs.techs.get(&item::tech::Technique::Foie).unwrap(), &TechLevel(3));
  302. assert_eq!(char.techs.techs.get(&item::tech::Technique::Zonde).unwrap(), &TechLevel(5));
  303. assert!(char.techs.techs.get(&item::tech::Technique::Barta).is_none());
  304. }
  305. #[async_std::test]
  306. async fn test_use_monogrinder() {
  307. let mut entity_gateway = InMemoryGateway::default();
  308. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
  309. let saber = entity_gateway.create_item(
  310. item::NewItemEntity {
  311. item: item::ItemDetail::Weapon(
  312. item::weapon::Weapon {
  313. weapon: item::weapon::WeaponType::Saber,
  314. grind: 0,
  315. special: None,
  316. attrs: [None, None, None],
  317. tekked: true,
  318. }
  319. ),
  320. }).await.unwrap();
  321. let mut grinders = Vec::new();
  322. for _ in 0..3usize {
  323. grinders.push(entity_gateway.create_item(
  324. item::NewItemEntity {
  325. item: item::ItemDetail::Tool(
  326. item::tool::Tool {
  327. tool: item::tool::ToolType::Monogrinder,
  328. }
  329. ),
  330. }).await.unwrap());
  331. }
  332. let equipped = item::EquippedEntity {
  333. weapon: Some(saber.id),
  334. armor: None,
  335. shield: None,
  336. unit: [None; 4],
  337. mag: None,
  338. };
  339. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  340. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![item::InventoryItemEntity::Individual(saber),
  341. item::InventoryItemEntity::Stacked(grinders)])).await.unwrap();
  342. let mut ship = standard_ship(entity_gateway.clone());
  343. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  344. join_lobby(&mut ship, ClientId(1)).await;
  345. create_room(&mut ship, ClientId(1), "room", "").await;
  346. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  347. client: 0,
  348. target: 0,
  349. item_id: 0x10001,
  350. })))).await.unwrap();
  351. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  352. client: 0,
  353. target: 0,
  354. item_id: 0x10001,
  355. })))).await.unwrap();
  356. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  357. assert_eq!(inventory_items.items.len(), 2);
  358. assert!(matches!(inventory_items.items[0], item::InventoryItemEntity::Individual(item::ItemEntity{ item: item::ItemDetail::Weapon(item::weapon::Weapon {grind: 2, ..}), ..})));
  359. }
  360. // TODO: tests for ALL ITEMS WOW
  361. /*
  362. #[async_std::test]
  363. pub async fn test_learn_new_tech() {}
  364. #[async_std::test]
  365. pub async fn test_new_fo_has_foie_1() {}
  366. #[async_std::test]
  367. pub async fn test_char_cannot_use_lower_level_tech() {}
  368. #[async_std::test]
  369. pub async fn test_char_cannot_learn_wrong_tech() {}
  370. #[async_std::test]
  371. pub async fn test_char_cannot_learn_high_level_tech() {}
  372. #[async_std::test]
  373. pub async fn test_android_cannot_learn_tech() {}
  374. */