Browse Source

Fix CRF2 sort, order, and add mod filter

master
NGnius (Graham) 1 year ago
parent
commit
f024193b43
5 changed files with 80 additions and 16 deletions
  1. +6
    -0
      src/robocraft2/factory.rs
  2. +45
    -7
      src/robocraft2/factory_json.rs
  3. +1
    -1
      src/robocraft2/mod.rs
  4. +26
    -5
      tests/robocraft2_factory.rs
  5. +2
    -3
      tests/robocraft_factory.rs

+ 6
- 0
src/robocraft2/factory.rs View File

@@ -111,6 +111,9 @@ impl FactoryAPI {
if let Some(x) = &params.date_maximum {
url.query_pairs_mut().append_pair("dateMaximum", x);
}
if let Some(x) = &params.purchased_only {
url.query_pairs_mut().append_pair("purchasedOnly", if *x { "true" } else { "false" });
}
if let Some(x) = &params.creator_id {
url.query_pairs_mut().append_pair("creatorId", x);
}
@@ -122,11 +125,14 @@ impl FactoryAPI {
}
url.query_pairs_mut().append_pair("sortBy", &params.sort_by);
url.query_pairs_mut().append_pair("orderBy", &params.order_by);
url.query_pairs_mut().append_pair("modFilter", &params.moderation_filter);
let token = self.token.lock().unwrap().token().await.map_err(FactoryError::Protocol)?;
let result = self.client.get(url)
.header("Authorization", "Bearer ".to_owned() + &token)
.send().await
.map_err(FactoryError::Protocol)?;
//println!("result: {}", result.text().await.map_err(FactoryError::Protocol)?);
//todo!()
handle_json_response::<SearchResponse>(result).await
}



+ 45
- 7
src/robocraft2/factory_json.rs View File

@@ -1,5 +1,37 @@
use serde::{Deserialize, Serialize};

pub mod order {
pub const ASCENDING: &str = "ascending";
pub const DESCENDING: &str = "descending";
pub(super) fn default() -> &'static str {
ASCENDING
}
}

pub mod sort {
pub const CPU_POWER: &str = "cpuPower";
pub const FIRE_POWER: &str = "firePower";
pub const ENGINE_POWER: &str = "enginePower";
pub const PRICE: &str = "price";
pub const DATE: &str = "date";
pub const CLUSTER_COUNT: &str = "clusterCount";
pub const VIEWS: &str = "views";
pub const MOST_PURCHASED: &str = "mostPurchased";
pub const DEFAULT: &str = "default";
pub(super) fn default() -> &'static str {
DEFAULT
}
}

pub mod moderation {
pub const NO_FILTER: &str = "none";
pub const NEEDS_MODERATION: &str = "needsModeration";
pub const MODERATED: &str = "moderatedOnly";
pub(super) fn default() -> &'static str {
NO_FILTER
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ErrorPayload {
#[serde(rename = "error")]
@@ -34,16 +66,20 @@ pub struct SearchPayload {
pub date_minimum: Option<String>,
#[serde(rename = "dateMaximum")]
pub date_maximum: Option<String>,
#[serde(rename = "purchasedOnly")]
pub purchased_only: Option<bool>,
#[serde(rename = "creatorId")]
pub creator_id: Option<String>, // GUID
#[serde(rename = "page")]
pub page: Option<isize>,
#[serde(rename = "count")]
pub count: Option<isize>,
#[serde(rename = "sortBy")]
pub sort_by: String,
#[serde(rename = "orderBy")]
pub order_by: String,
#[serde(rename = "sortBy", default="sort::default")]
pub sort_by: &'static str,
#[serde(rename = "orderBy", default="order::default")]
pub order_by: &'static str,
#[serde(rename = "modFilter", default="moderation::default")]
pub moderation_filter: &'static str,
}

impl Default for SearchPayload {
@@ -60,11 +96,13 @@ impl Default for SearchPayload {
cluster_maximum: None,
date_minimum: None,
date_maximum: None,
purchased_only: None,
creator_id: None,
page: None,
count: None,
sort_by: "default".to_owned(),
order_by: "ascending".to_owned(),
sort_by: sort::DEFAULT,
order_by: order::ASCENDING,
moderation_filter: moderation::NO_FILTER,
}
}
}
@@ -98,7 +136,7 @@ pub struct RobotInfo {
#[serde(rename = "created")]
pub created: String, // date
#[serde(rename = "image")]
pub image: String, // base64??
pub image: Option<String>, // url
#[serde(rename = "baseCpu")]
pub base_cpu: isize,
#[serde(rename = "weaponCpu")]


+ 1
- 1
src/robocraft2/mod.rs View File

@@ -5,7 +5,7 @@ mod factory;
pub use factory::{FactoryAPI, FactoryError};

mod factory_json;
pub use factory_json::{ErrorPayload, SearchPayload, SearchResponse, SearchResponseItem, RobotInfo, RobotPrice, CreateRobotPayload, CreateRobotResponse, FactoryInfoResponse, PublishRobotPayload, PublishRobotResponse, MyRobotsResponse, GetRobotResponse, ModerateRobotPayload, ReportRobotPayload};
pub use factory_json::{ErrorPayload, SearchPayload, SearchResponse, SearchResponseItem, RobotInfo, RobotPrice, CreateRobotPayload, CreateRobotResponse, FactoryInfoResponse, PublishRobotPayload, PublishRobotResponse, MyRobotsResponse, GetRobotResponse, ModerateRobotPayload, ReportRobotPayload, order, sort, moderation};

mod portal;
pub use self::portal::{PortalTokenProvider, AccountInfo, PortalCheckResponse, ITokenProvider};

+ 26
- 5
tests/robocraft2_factory.rs View File

@@ -35,8 +35,7 @@ async fn robocraft2_factory_report() -> Result<(), ()> {
}

#[cfg(feature = "robocraft2")]
//#[tokio::test]
#[allow(dead_code)]
#[tokio::test]
async fn robocraft2_factory_default_query() -> Result<(), ()> {
let api = builder().await;
let result = api.list().await;
@@ -47,13 +46,33 @@ async fn robocraft2_factory_default_query() -> Result<(), ()> {
assert_ne!(robot.robot.name, "");
assert_ne!(robot.robot.creator_id, "");
assert_ne!(robot.robot.creator_id, "");
assert_ne!(robot.robot.image, "");
//println!("RobotInfo.to_string() -> `{}`", robot.robot.to_string());
println!("SearchResponseItem {}", serde_json::to_string_pretty(&robot).unwrap());
}
Ok(())
}

#[cfg(feature = "robocraft2")]
#[tokio::test]
async fn robocraft2_factory_sort() -> Result<(), ()> {
let api = builder().await;
let mut query = robocraft2::SearchPayload::default();
query.sort_by = robocraft2::sort::DEFAULT;
query.order_by = robocraft2::order::ASCENDING;
let result = api.search(query).await;
let robo_info = unwrap_factory2(result);
assert_ne!(robo_info.results.len(), 0);
for robot in &robo_info.results {
assert_ne!(robot.robot.name, "");
assert_ne!(robot.robot.creator_id, "");
assert_ne!(robot.robot.creator_id, "");
//println!("RobotInfo.to_string() -> `{}`", robot.robot.to_string());
//println!("SearchResponseItem {}", serde_json::to_string_pretty(&robot).unwrap());
println!("date: {}", robot.robot.created);
}
Ok(())
}


#[cfg(feature = "robocraft2")]
#[tokio::test]
@@ -138,7 +157,8 @@ async fn robocraft2_factory_my_bots() -> Result<(), ()> {
assert_ne!(robot.name, "");
assert_ne!(robot.creator_id, "");
assert_ne!(robot.creator_id, "");
assert_ne!(robot.image, "");
assert_ne!(robot.image, None);
assert_ne!(robot.image.as_ref().unwrap(), "");
println!("My bot `{}`", robot.to_string());
//println!("my vehicle {}", serde_json::to_string_pretty(&robot).unwrap());
}
@@ -157,7 +177,8 @@ async fn robocraft2_factory_my_published_bots() -> Result<(), ()> {
assert_ne!(robot.name, "");
assert_ne!(robot.creator_id, "");
assert_ne!(robot.creator_id, "");
assert_ne!(robot.image, "");
assert_ne!(robot.image, None);
assert_ne!(robot.image.as_ref().unwrap(), "");
println!("My pub bot `{}`", robot.to_string());
//println!("pub vehicle {}", serde_json::to_string_pretty(&robot).unwrap());
}


+ 2
- 3
tests/robocraft_factory.rs View File

@@ -1,8 +1,6 @@
#[cfg(feature = "robocraft")]
use libfj::robocraft;
#[cfg(feature = "robocraft")]
use libfj::robocraft2;
#[cfg(feature = "robocraft")]
use std::convert::From;

#[cfg(feature = "robocraft")]
@@ -77,7 +75,8 @@ async fn robocraft_factory_custom_query() -> Result<(), ()> {
}

#[cfg(feature = "robocraft")]
#[tokio::test]
//#[tokio::test]
#[allow(dead_code)]
async fn robocraft_factory_player_query() -> Result<(), ()> {
let result = builder()
.text("Zalera57".to_string()) // there is a featured robot by this user, so this should never fail


Loading…
Cancel
Save