An unofficial collection of APIs used in FreeJam games and mods
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.

299 lines
11KB

  1. #[cfg(feature = "robocraft")]
  2. use libfj::robocraft;
  3. #[cfg(feature = "robocraft")]
  4. use libfj::robocraft2;
  5. #[cfg(feature = "robocraft")]
  6. use std::convert::From;
  7. #[cfg(feature = "robocraft")]
  8. #[test]
  9. fn robocraft_factory_api_init() -> Result<(), ()> {
  10. robocraft::FactoryAPI::new();
  11. Ok(())
  12. }
  13. #[cfg(feature = "robocraft")]
  14. #[tokio::test]
  15. async fn robocraft_factory_default_query() -> Result<(), ()> {
  16. let api = robocraft::FactoryAPI::new();
  17. let result = api.list().await;
  18. assert!(result.is_ok());
  19. let robo_info = result.unwrap();
  20. assert_ne!(robo_info.response.roboshop_items.len(), 0);
  21. assert_eq!(robo_info.status_code, 200);
  22. for robot in &robo_info.response.roboshop_items {
  23. assert_ne!(robot.item_name, "");
  24. assert_ne!(robot.added_by, "");
  25. assert_ne!(robot.added_by_display_name, "");
  26. assert_ne!(robot.thumbnail, "");
  27. println!("FactoryRobotListInfo.to_string() -> `{}`", robot.to_string());
  28. }
  29. Ok(())
  30. }
  31. #[cfg(feature = "robocraft")]
  32. fn builder() -> robocraft::FactorySearchBuilder {
  33. robocraft::FactoryAPI::new().list_builder()
  34. }
  35. #[cfg(feature = "robocraft")]
  36. fn assert_factory_list(robo_info: robocraft::FactoryInfo<robocraft::RoboShopItemsInfo>) -> Result<(), ()> {
  37. assert_ne!(robo_info.response.roboshop_items.len(), 0);
  38. assert_eq!(robo_info.status_code, 200);
  39. for robot in &robo_info.response.roboshop_items {
  40. assert_ne!(robot.item_name, "");
  41. assert_ne!(robot.added_by, "");
  42. assert_ne!(robot.added_by_display_name, "");
  43. assert_ne!(robot.thumbnail, "");
  44. println!("FactoryRobotListInfo.to_string() -> `{}`", robot.to_string());
  45. }
  46. Ok(())
  47. }
  48. #[cfg(feature = "robocraft")]
  49. #[tokio::test]
  50. async fn robocraft_factory_custom_query() -> Result<(), ()> {
  51. let api = robocraft::FactoryAPI::new();
  52. let result = api.list_builder()
  53. .movement_or(robocraft::FactoryMovementType::Wheels)
  54. .weapon_or(robocraft::FactoryWeaponType::Laser)
  55. .page(2)
  56. .items_per_page(10)
  57. .send().await;
  58. assert!(result.is_ok());
  59. let robo_info = result.unwrap();
  60. assert_ne!(robo_info.response.roboshop_items.len(), 0);
  61. //assert_eq!(robo_info.response.roboshop_items.len(), 16); the API behaviour is weird, I swear it's not me!
  62. assert!(robo_info.response.roboshop_items.len() >= 10);
  63. assert_eq!(robo_info.status_code, 200);
  64. for robot in &robo_info.response.roboshop_items {
  65. assert_ne!(robot.item_name, "");
  66. assert_ne!(robot.added_by, "");
  67. assert_ne!(robot.added_by_display_name, "");
  68. assert_ne!(robot.thumbnail, "");
  69. println!("FactoryRobotListInfo.to_string() -> `{}`", robot.to_string());
  70. }
  71. Ok(())
  72. }
  73. #[cfg(feature = "robocraft")]
  74. #[tokio::test]
  75. async fn robocraft_factory_player_query() -> Result<(), ()> {
  76. let result = builder()
  77. .text("Spacecam".to_string()) // there is a featured robot by this user, so this should never fail
  78. .text_search_type(robocraft::FactoryTextSearchType::Player)
  79. .items_per_page(10)
  80. .send().await;
  81. assert!(result.is_ok());
  82. assert_factory_list(result.unwrap())
  83. }
  84. #[cfg(feature = "robocraft")]
  85. #[tokio::test]
  86. async fn robocraft_factory_robot_query() -> Result<(), ()> {
  87. let api = robocraft::FactoryAPI::new();
  88. let result = api.get(6478345 /* featured robot id */).await;
  89. assert!(result.is_ok());
  90. let bot_info = result.unwrap();
  91. assert_ne!(bot_info.response.item_name, "");
  92. assert_eq!(bot_info.response.item_id, 6478345);
  93. assert_ne!(bot_info.response.cube_data, "");
  94. assert_ne!(bot_info.response.colour_data, "");
  95. Ok(())
  96. }
  97. #[cfg(feature = "robocraft")]
  98. #[tokio::test]
  99. async fn robocraft_factory_robot_cubes() -> Result<(), ()> {
  100. let api = robocraft::FactoryAPI::new();
  101. let result = api.get(6478345 /* featured robot id */).await;
  102. assert!(result.is_ok());
  103. let bot_info = result.unwrap();
  104. let cubes = robocraft::Cubes::from(bot_info.clone());
  105. println!("cube count: {} or {}", cubes.provided_len, cubes.len());
  106. /*for c in cubes.into_iter() {
  107. println!("Cube.to_string() -> `{}`", c.to_string());
  108. }*/
  109. let (cube_d, colour_d) = cubes.dump();
  110. let cube_str = base64::encode(&cube_d);
  111. let colour_str = base64::encode(&colour_d);
  112. assert_eq!(cube_str, bot_info.response.cube_data);
  113. assert_eq!(colour_str, bot_info.response.colour_data);
  114. Ok(())
  115. }
  116. #[cfg(feature = "robocraft2")]
  117. //#[tokio::test]
  118. async fn robocraft2_factory_default_query() -> Result<(), ()> {
  119. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  120. let result = api.list().await;
  121. assert!(result.is_ok());
  122. let robo_info = unwrap_factory2(result);
  123. assert_ne!(robo_info.results.len(), 0);
  124. for robot in &robo_info.results {
  125. assert_ne!(robot.robot.name, "");
  126. assert_ne!(robot.robot.creator_id, "");
  127. assert_ne!(robot.robot.creator_id, "");
  128. assert_ne!(robot.robot.image, "");
  129. //println!("RobotInfo.to_string() -> `{}`", robot.robot.to_string());
  130. println!("SearchResponseItem {}", serde_json::to_string_pretty(&robot).unwrap());
  131. }
  132. Ok(())
  133. }
  134. #[cfg(feature = "robocraft2")]
  135. #[tokio::test]
  136. async fn robocraft2_factory_info() -> Result<(), ()> {
  137. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  138. let result = api.factory_info().await;
  139. assert!(result.is_ok());
  140. let crf_info = unwrap_factory2(result);
  141. println!("FactoryInfo {:?}", crf_info);
  142. Ok(())
  143. }
  144. #[cfg(feature = "robocraft2")]
  145. //#[tokio::test]
  146. async fn robocraft2_factory_upload() -> Result<(), ()> {
  147. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  148. // copy default bot
  149. let result = api.get("08dab2c9-7a72-4ec4-843c-154fe8768e91".to_owned()).await;
  150. assert!(result.is_ok());
  151. let robot = unwrap_factory2(result);
  152. let result = api.create_robot(
  153. robocraft2::CreateRobotPayload {
  154. name: "API is easy".to_owned(),
  155. data: robot.data, // base64
  156. image: "".to_owned(), // base64
  157. base_cpu: 42,
  158. weapon_cpu: 1,
  159. cosmetic_cpu: 6,
  160. cluster_count: 1,
  161. block_counts: vec![
  162. (42, 3),
  163. (3, 6)
  164. ].drain(..).collect(),
  165. materials_used: vec![
  166. 8,
  167. 4
  168. ].drain(..).collect(),
  169. minimum_offset_x: 0.0,
  170. minimum_offset_y: 0.0,
  171. minimum_offset_z: 0.0,
  172. maximum_offset_x: 0.0,
  173. maximum_offset_y: 0.0,
  174. maximum_offset_z: 0.0,
  175. }
  176. ).await;
  177. //assert!(result.is_ok());
  178. let robot_info = unwrap_factory2(result);
  179. println!("CreateRobotInfo {:?}", robot_info);
  180. let result = api.publish_robot(
  181. robocraft2::PublishRobotPayload {
  182. name: "CRF API oh my".to_owned(),
  183. description: "There once was a person named NGnius, who simply wasn't that bright.\nBut he had a misleading name, and wasn't not quite unhated\nSo he thought it alright to put up a fight\nand get banned for reverse engineering".to_owned(),
  184. techpoints: -42,
  185. bloxcoin: 123
  186. }, robot_info.header.id.clone()).await;
  187. //assert!(result.is_ok());
  188. let _publish_info = unwrap_factory2(result);
  189. // clean up
  190. let result = api.unpublish_bot(robot_info.header.id.clone()).await;
  191. //assert!(result.is_ok());
  192. let _robot = unwrap_factory2(result);
  193. let result = api.delete_robot(robot_info.header.id).await;
  194. //assert!(result.is_ok());
  195. let _robot = unwrap_factory2(result);
  196. Ok(())
  197. }
  198. #[cfg(feature = "robocraft2")]
  199. #[tokio::test]
  200. async fn robocraft2_factory_my_bots() -> Result<(), ()> {
  201. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  202. let result = api.my_robots().await;
  203. assert!(result.is_ok());
  204. let robo_info = unwrap_factory2(result);
  205. assert_ne!(robo_info.vehicles.len(), 0);
  206. for robot in &robo_info.vehicles {
  207. assert_ne!(robot.name, "");
  208. assert_ne!(robot.creator_id, "");
  209. assert_ne!(robot.creator_id, "");
  210. assert_ne!(robot.image, "");
  211. println!("My bot `{}`", robot.to_string());
  212. //println!("my vehicle {}", serde_json::to_string_pretty(&robot).unwrap());
  213. }
  214. Ok(())
  215. }
  216. #[cfg(feature = "robocraft2")]
  217. #[tokio::test]
  218. async fn robocraft2_factory_my_published_bots() -> Result<(), ()> {
  219. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  220. let result = api.my_published_robots().await;
  221. assert!(result.is_ok());
  222. let robo_info = unwrap_factory2(result);
  223. //assert_ne!(robo_info.vehicles.len(), 0);
  224. for robot in &robo_info.vehicles {
  225. assert_ne!(robot.name, "");
  226. assert_ne!(robot.creator_id, "");
  227. assert_ne!(robot.creator_id, "");
  228. assert_ne!(robot.image, "");
  229. println!("My pub bot `{}`", robot.to_string());
  230. //println!("pub vehicle {}", serde_json::to_string_pretty(&robot).unwrap());
  231. }
  232. Ok(())
  233. }
  234. #[cfg(feature = "robocraft2")]
  235. //#[tokio::test]
  236. async fn robocraft2_factory_bot() -> Result<(), ()> {
  237. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  238. let result = api.get("08dab2c9-7a72-4ec4-843c-154fe8768e91".to_owned()).await;
  239. //assert!(result.is_ok());
  240. let robot = unwrap_factory2(result);
  241. assert_ne!(robot.header.name, "");
  242. assert_ne!(robot.header.creator_id, "");
  243. assert_ne!(robot.header.creator_id, "");
  244. //assert_ne!(robot.header.image, "");
  245. //assert_ne!(robot.description, "");
  246. assert_ne!(robot.data, "");
  247. println!("robot {}", serde_json::to_string_pretty(&robot).unwrap());
  248. Ok(())
  249. }
  250. #[cfg(feature = "robocraft2")]
  251. //#[tokio::test]
  252. async fn robocraft2_factory_delete_bot() -> Result<(), ()> {
  253. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  254. let result = api.delete_robot("08dab2d2-dcae-4f52-8a77-bbce7cf10124".to_owned()).await;
  255. //assert!(result.is_ok());
  256. let _robot = unwrap_factory2(result);
  257. Ok(())
  258. }
  259. #[cfg(feature = "robocraft2")]
  260. //#[tokio::test]
  261. async fn robocraft2_factory_unpublish_bot() -> Result<(), ()> {
  262. let api = robocraft2::FactoryAPI::with_auth(Box::new(robocraft2::PortalTokenProvider::with_username("FJAPIC00L", "P4$$w0rd").await.unwrap()));
  263. let result = api.unpublish_bot("08dab2d3-2a68-48c6-8fd6-a59a663336ca".to_owned()).await;
  264. //assert!(result.is_ok());
  265. let _robot = unwrap_factory2(result);
  266. Ok(())
  267. }
  268. fn unwrap_factory2<T>(result: Result<T, robocraft2::FactoryError>) -> T {
  269. match result {
  270. Ok(t) => t,
  271. Err(e) => {
  272. //println!("FactoryError: {}", e);
  273. panic!("CRF2 Error: {}", e);
  274. }
  275. }
  276. }