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.

414 lines
14 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 elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::entity::item;
  4. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  5. use elseware::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. /*
  265. let mut p1_inv = Vec::new();
  266. for tool in vec![item::tool::ToolType::PowerMaterial, item::tool::ToolType::].into_iter() {
  267. let mut item = Vec::new();
  268. for _ in 0..5usize {
  269. item.push(entity_gateway.create_item(
  270. item::NewItemEntity {
  271. item: item::ItemDetail::Tool(
  272. item::tool::Tool {
  273. tool: tool
  274. }
  275. ),
  276. }).await.unwrap());
  277. }
  278. p1_inv.push(item::InventoryItemEntity::Stacked(item));
  279. }*/
  280. let inv = vec![
  281. entity_gateway.create_item(
  282. item::NewItemEntity {
  283. item: item::ItemDetail::TechniqueDisk(
  284. item::tech::TechniqueDisk {
  285. tech: item::tech::Technique::Foie,
  286. level: 3,
  287. }
  288. )
  289. }
  290. ).await.unwrap(),
  291. entity_gateway.create_item(
  292. item::NewItemEntity {
  293. item: item::ItemDetail::TechniqueDisk(
  294. item::tech::TechniqueDisk {
  295. tech: item::tech::Technique::Barta,
  296. level: 4,
  297. }
  298. )
  299. }
  300. ).await.unwrap(),
  301. entity_gateway.create_item(
  302. item::NewItemEntity {
  303. item: item::ItemDetail::TechniqueDisk(
  304. item::tech::TechniqueDisk {
  305. tech: item::tech::Technique::Zonde,
  306. level: 5,
  307. }
  308. )
  309. }
  310. ).await.unwrap()
  311. ];
  312. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inv)).await.unwrap();
  313. let mut ship = Box::new(ShipServerState::builder()
  314. .gateway(entity_gateway.clone())
  315. .build());
  316. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  317. join_lobby(&mut ship, ClientId(1)).await;
  318. create_room(&mut ship, ClientId(1), "room", "").await;
  319. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  320. client: 0,
  321. target: 0,
  322. item_id: 0x10000,
  323. })))).await.unwrap();
  324. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  325. client: 0,
  326. target: 0,
  327. item_id: 0x10002,
  328. })))).await.unwrap();
  329. let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  330. let char = characters[0].as_ref().unwrap();
  331. assert_eq!(char.techs.techs.len(), 2);
  332. assert_eq!(char.techs.techs.get(&item::tech::Technique::Foie).unwrap(), &TechLevel(3));
  333. assert_eq!(char.techs.techs.get(&item::tech::Technique::Zonde).unwrap(), &TechLevel(5));
  334. assert!(char.techs.techs.get(&item::tech::Technique::Barta).is_none());
  335. }
  336. // TODO: tests for ALL ITEMS WOW
  337. /*
  338. #[async_std::test]
  339. pub async fn test_learn_new_tech() {}
  340. #[async_std::test]
  341. pub async fn test_new_fo_has_foie_1() {}
  342. #[async_std::test]
  343. pub async fn test_char_cannot_use_lower_level_tech() {}
  344. #[async_std::test]
  345. pub async fn test_char_cannot_learn_wrong_tech() {}
  346. #[async_std::test]
  347. pub async fn test_char_cannot_learn_high_level_tech() {}
  348. #[async_std::test]
  349. pub async fn test_android_cannot_learn_tech() {}
  350. */