use js_sys::{Array, JsString, Object};
use wasm_bindgen::{prelude::*, JsCast};
use crate::{
constants::{ErrorCode, MarketResourceType, OrderType, ResourceType},
local::{LodashFilter, RoomName},
prelude::*,
};
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = "market")]
type Market;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = credits)]
fn credits() -> f64;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = incomingTransactions)]
fn incoming_transactions() -> Array;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = outgoingTransactions)]
fn outgoing_transactions() -> Array;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = orders)]
fn orders() -> Object;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = calcTransactionCost)]
fn calc_transaction_cost(amount: u32, room_1: &JsString, room_2: &JsString) -> u32;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = cancelOrder)]
fn cancel_order(order_id: &JsString) -> i8;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = changeOrderPrice)]
fn change_order_price(order_id: &JsString, new_price: f64) -> i8;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = createOrder)]
fn create_order(order_parameters: &Object) -> i8;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = deal)]
fn deal(order_id: &JsString, amount: u32, room_name: Option<&JsString>) -> i8;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = extendOrder)]
fn extend_order(order_id: &JsString, add_amount: u32) -> i8;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getAllOrders)]
fn get_all_orders(filter: Option<&LodashFilter>) -> Array;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getHistory)]
fn get_history(resource: Option<ResourceType>) -> Option<Array>;
#[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getOrderById)]
fn get_order_by_id(order_id: &JsString) -> Option<Order>;
}
pub fn credits() -> f64 {
Market::credits()
}
pub fn incoming_transactions() -> Vec<Transaction> {
Market::incoming_transactions()
.iter()
.map(Into::into)
.collect()
}
pub fn outgoing_transactions() -> Vec<Transaction> {
Market::outgoing_transactions()
.iter()
.map(Into::into)
.collect()
}
pub fn orders() -> JsHashMap<String, MyOrder> {
Market::orders().into()
}
pub fn calc_transaction_cost(amount: u32, room_1: &JsString, room_2: &JsString) -> u32 {
Market::calc_transaction_cost(amount, room_1, room_2)
}
pub fn cancel_order(order_id: &JsString) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(Market::cancel_order(order_id))
}
pub fn change_order_price(order_id: &JsString, new_price: f64) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(Market::change_order_price(order_id, new_price))
}
pub fn create_order(order_parameters: &Object) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(Market::create_order(order_parameters))
}
pub fn deal(
order_id: &JsString,
amount: u32,
room_name: Option<RoomName>,
) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(match room_name {
Some(r) => Market::deal(order_id, amount, Some(&r.into())),
None => Market::deal(order_id, amount, None),
})
}
pub fn extend_order(order_id: &JsString, add_amount: u32) -> Result<(), ErrorCode> {
ErrorCode::result_from_i8(Market::extend_order(order_id, add_amount))
}
pub fn get_all_orders(filter: Option<&LodashFilter>) -> Vec<Order> {
Market::get_all_orders(filter)
.iter()
.map(Into::into)
.collect()
}
pub fn get_history(resource: Option<ResourceType>) -> Vec<OrderHistoryRecord> {
Market::get_history(resource)
.map(|arr| arr.iter().map(Into::into).collect())
.unwrap_or_else(Vec::new)
}
pub fn get_order_by_id(order_id: &str) -> Option<Order> {
let order_id: JsString = order_id.into();
Market::get_order_by_id(&order_id)
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type Order;
#[wasm_bindgen(method, getter)]
pub fn id(this: &Order) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn created(this: &Order) -> Option<u32>;
#[wasm_bindgen(method, getter = createdTimestamp)]
pub fn created_timestamp(this: &Order) -> f64;
#[wasm_bindgen(method, getter = type)]
pub fn order_type(this: &Order) -> OrderType;
#[wasm_bindgen(method, getter = resourceType)]
pub fn resource_type(this: &Order) -> MarketResourceType;
#[wasm_bindgen(method, getter = roomName)]
pub fn room_name(this: &Order) -> Option<JsString>;
#[wasm_bindgen(method, getter)]
pub fn amount(this: &Order) -> u32;
#[wasm_bindgen(method, getter = remainingAmount)]
pub fn remaining_amount(this: &Order) -> u32;
#[wasm_bindgen(method, getter)]
pub fn price(this: &Order) -> f64;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type Transaction;
#[wasm_bindgen(method, getter = transactionId)]
pub fn transaction_id(this: &Transaction) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn time(this: &Transaction) -> u32;
#[wasm_bindgen(method, getter)]
pub fn sender(this: &Transaction) -> Option<Player>;
#[wasm_bindgen(method, getter)]
pub fn recipient(this: &Transaction) -> Option<Player>;
#[wasm_bindgen(method, getter = resourceType)]
pub fn resource_type(this: &Transaction) -> ResourceType;
#[wasm_bindgen(method, getter)]
pub fn amount(this: &Transaction) -> u32;
#[wasm_bindgen(method, getter)]
pub fn from(this: &Transaction) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn to(this: &Transaction) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn description(this: &Transaction) -> Option<JsString>;
#[wasm_bindgen(method, getter = order)]
pub fn order(this: &Transaction) -> Option<TransactionOrder>;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type Player;
#[wasm_bindgen(method, getter)]
pub fn username(this: &Player) -> JsString;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type TransactionOrder;
#[wasm_bindgen(method, getter)]
pub fn id(this: &TransactionOrder) -> JsString;
#[wasm_bindgen(method, getter = type)]
pub fn order_type(this: &TransactionOrder) -> OrderType;
#[wasm_bindgen(method, getter)]
pub fn price(this: &TransactionOrder) -> f64;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type MyOrder;
#[wasm_bindgen(method, getter)]
pub fn id(this: &MyOrder) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn created(this: &MyOrder) -> Option<u32>;
#[wasm_bindgen(method, getter = createdTimestamp)]
pub fn created_timestamp(this: &MyOrder) -> u64;
#[wasm_bindgen(method, getter)]
pub fn active(this: &MyOrder) -> bool;
#[wasm_bindgen(method, getter = type)]
pub fn order_type(this: &MyOrder) -> OrderType;
#[wasm_bindgen(method, getter = resourceType)]
pub fn resource_type(this: &MyOrder) -> MarketResourceType;
#[wasm_bindgen(method, getter = roomName)]
pub fn room_name(this: &MyOrder) -> Option<JsString>;
#[wasm_bindgen(method, getter)]
pub fn amount(this: &MyOrder) -> u32;
#[wasm_bindgen(method, getter = remainingAmount)]
pub fn remaining_amount(this: &MyOrder) -> u32;
#[wasm_bindgen(method, getter = totalAmount)]
pub fn total_amount(this: &MyOrder) -> u32;
#[wasm_bindgen(method, getter)]
pub fn price(this: &MyOrder) -> f64;
}
impl JsCollectionFromValue for MyOrder {
fn from_value(val: JsValue) -> Self {
val.unchecked_into()
}
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
#[derive(Debug)]
pub type OrderHistoryRecord;
#[wasm_bindgen(method, getter = resourceType)]
pub fn resource_type(this: &OrderHistoryRecord) -> MarketResourceType;
#[wasm_bindgen(method, getter)]
pub fn date(this: &OrderHistoryRecord) -> JsString;
#[wasm_bindgen(method, getter)]
pub fn transactions(this: &OrderHistoryRecord) -> u32;
#[wasm_bindgen(method, getter)]
pub fn volume(this: &OrderHistoryRecord) -> u32;
#[wasm_bindgen(method, getter = avgPrice)]
pub fn avg_price(this: &OrderHistoryRecord) -> f64;
#[wasm_bindgen(method, getter = stddevPrice)]
pub fn stddev_price(this: &OrderHistoryRecord) -> f64;
}