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.

399 lines
14 KiB

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