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.

398 lines
14 KiB

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