mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Refactor rc_microtransactions to use actix-web, remove rocket dependency
This commit is contained in:
@@ -8,8 +8,11 @@ repository.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
rocket.workspace = true
|
||||
actix-web.workspace = true
|
||||
libfj.workspace = true
|
||||
log.workspace = true
|
||||
env_logger.workspace = true
|
||||
git-version.workspace = true
|
||||
clap.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
## defaults for _all_ profiles
|
||||
[default]
|
||||
address = "127.0.0.1"
|
||||
port = 8011
|
||||
|
||||
## set only when compiled in debug mode, i.e, `cargo build`
|
||||
[debug]
|
||||
port = 8011
|
||||
|
||||
## set only when compiled in release mode, i.e, `cargo build --release`
|
||||
[release]
|
||||
address = "0.0.0.0"
|
||||
27
rc_microtransactions/src/cli.rs
Normal file
27
rc_microtransactions/src/cli.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct CliArgs {
|
||||
/// TCP port on which to accept connections
|
||||
#[arg(short, long, default_value_t = 8011)]
|
||||
pub port: u16,
|
||||
|
||||
/// IP Address on which to accept connections
|
||||
#[arg(long, default_value_t = {"127.0.0.1".to_string()})]
|
||||
pub ip: String,
|
||||
|
||||
/// Assets root
|
||||
#[arg(long, default_value_t = {"../assets/robocraft".to_string()})]
|
||||
pub assets_robocraft: String,
|
||||
|
||||
/// User data root
|
||||
#[arg(long, default_value_t = {"../data/robocraft".to_string()})]
|
||||
pub data_robocraft: String,
|
||||
}
|
||||
|
||||
impl CliArgs {
|
||||
pub fn get() -> Self {
|
||||
Self::parse()
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
#![forbid(unsafe_code)]
|
||||
mod cli;
|
||||
mod robocraft;
|
||||
|
||||
#[rocket::get("/")]
|
||||
fn index() -> String {
|
||||
use actix_web::{App, HttpServer, Responder};
|
||||
|
||||
#[actix_web::get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
let name = env!("CARGO_PKG_NAME");
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
let git_version = git_version::git_version!(args = ["--always", "--dirty=+"]);
|
||||
@@ -12,10 +15,21 @@ fn index() -> String {
|
||||
format!("{} {}:{} by [{}]\n{}\n{}", name, version, git_version, authors, license, repo)
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
fn rocket() -> _ {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init();
|
||||
rocket::build().mount("/", rocket::routes![index])
|
||||
.attach(robocraft::stage())
|
||||
let cli_args = cli::CliArgs::get();
|
||||
|
||||
let cli_args2 = actix_web::web::Data::new(cli_args.clone());
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(cli_args2.clone())
|
||||
.service(index)
|
||||
.service(robocraft::robopay_store)
|
||||
})
|
||||
.bind((cli_args.ip, cli_args.port))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,2 @@
|
||||
mod store;
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("JSON", |rocket| async {
|
||||
rocket.attach(store::stage())
|
||||
})
|
||||
}
|
||||
pub use store::robopay_store;
|
||||
|
||||
@@ -1,17 +1,98 @@
|
||||
use rocket::{post, routes, serde::json::{json, Value}};
|
||||
use actix_web::{web::{Data, Json}, Error, post};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Response<T> {
|
||||
response: T
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ResponseData<T> {
|
||||
data: Vec<T>
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct StoreBundle {
|
||||
#[serde(rename = "items")]
|
||||
items: Vec<StoreItem>,
|
||||
#[serde(rename = "itemSku")]
|
||||
item_sku: String,
|
||||
#[serde(rename = "currencyCode")]
|
||||
currency_code: String,
|
||||
#[serde(rename = "currencyString")]
|
||||
currency_string: String,
|
||||
#[serde(rename = "oldCurrencyString")]
|
||||
old_currency_string: String,
|
||||
#[serde(rename = "priceForCheck")]
|
||||
price_for_check: f32,
|
||||
#[serde(rename = "oldPriceForCheck")]
|
||||
old_price_for_check: f32,
|
||||
#[serde(rename = "additionalValue")]
|
||||
additional_value: i32,
|
||||
#[serde(rename = "mostPopular")]
|
||||
most_popular: bool,
|
||||
#[serde(rename = "bestValue")]
|
||||
best_value: bool,
|
||||
#[serde(rename = "currencyType")]
|
||||
currency_type: String,
|
||||
#[serde(rename = "available")]
|
||||
is_available: bool,
|
||||
#[serde(rename = "owned")]
|
||||
is_owned: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct StoreItem {
|
||||
#[serde(rename = "itemType")]
|
||||
item_type: StoreItemType,
|
||||
#[serde(rename = "amount")]
|
||||
amount: i32,
|
||||
#[serde(rename = "data")]
|
||||
data: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Serialize)]
|
||||
enum StoreItemType {
|
||||
RoboPass = 0,
|
||||
CosmeticCredits = 1,
|
||||
Premium = 2,
|
||||
PremiumForLife = 3,
|
||||
Cube = 4,
|
||||
Robits = 5,
|
||||
Other = 6
|
||||
}
|
||||
|
||||
type ItemResponse = Response<ResponseData<StoreBundle>>;
|
||||
|
||||
#[post("/robopay/store")]
|
||||
pub fn robopay_store() -> Value {
|
||||
pub async fn robopay_store(_cli: Data<crate::cli::CliArgs>) -> Result<Json<ItemResponse>, Error> {
|
||||
// TODO authentication (Authorization header is "Robocraft {JWT token from auth}")
|
||||
json!({
|
||||
"response": {
|
||||
"data": []
|
||||
Ok(Json(Response {
|
||||
response: ResponseData {
|
||||
data: vec![
|
||||
StoreBundle {
|
||||
items: vec![
|
||||
StoreItem {
|
||||
item_type: StoreItemType::CosmeticCredits,
|
||||
amount: 1,
|
||||
data: "RE_store_item_data_01".to_owned(),
|
||||
},
|
||||
],
|
||||
item_sku: "CosmeticCredits1".to_owned(), // SKUs can be found in RealMoneyStoreExtraData of sharedassets2.assets
|
||||
currency_code: "RE_no_currency".to_owned(),
|
||||
currency_string: "RE_DO_NOT_BUY_01".to_owned(),
|
||||
old_currency_string: "RE_DO_NOT_BUY_OLD_01".to_owned(),
|
||||
price_for_check: 42.0,
|
||||
old_price_for_check: 41.0,
|
||||
additional_value: 999888777,
|
||||
most_popular: true,
|
||||
best_value: true,
|
||||
currency_type: "RE_price_type_01".to_owned(),
|
||||
is_available: true,
|
||||
is_owned: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stage() -> rocket::fairing::AdHoc {
|
||||
rocket::fairing::AdHoc::on_ignite("Robocraft store info", |rocket| async {
|
||||
rocket.mount("/", routes![robopay_store])
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user