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.

468 lines
16 KiB

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