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.

469 lines
16 KiB

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