1use js_sys::{Array, JsString, Object};
5use wasm_bindgen::prelude::*;
6
7use crate::{
8 constants::{MarketResourceType, OrderType, ResourceType},
9 enums::action_error_codes::game::market::*,
10 local::{LodashFilter, RoomName},
11 prelude::*,
12};
13
14#[wasm_bindgen]
15extern "C" {
16 #[wasm_bindgen(js_name = "market")]
17 type Market;
18
19 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = credits)]
20 fn credits() -> f64;
21
22 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = incomingTransactions)]
23 fn incoming_transactions() -> Array;
24
25 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = outgoingTransactions)]
26 fn outgoing_transactions() -> Array;
27
28 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, getter, js_name = orders)]
29 fn orders() -> Object;
30
31 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = calcTransactionCost)]
32 fn calc_transaction_cost(amount: u32, room_1: &JsString, room_2: &JsString) -> u32;
33
34 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = cancelOrder)]
35 fn cancel_order(order_id: &JsString) -> i8;
36
37 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = changeOrderPrice)]
38 fn change_order_price(order_id: &JsString, new_price: f64) -> i8;
39
40 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = createOrder)]
41 fn create_order(order_parameters: &Object) -> i8;
42
43 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = deal)]
44 fn deal(order_id: &JsString, amount: u32, room_name: Option<&JsString>) -> i8;
45
46 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = extendOrder)]
47 fn extend_order(order_id: &JsString, add_amount: u32) -> i8;
48
49 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getAllOrders)]
50 fn get_all_orders(filter: Option<&LodashFilter>) -> Array;
51
52 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getHistory)]
53 fn get_history(resource: Option<ResourceType>) -> Option<Array>;
54
55 #[wasm_bindgen(js_namespace = ["Game"], js_class = "market", static_method_of = Market, js_name = getOrderById)]
56 fn get_order_by_id(order_id: &JsString) -> Option<Order>;
57}
58
59pub fn credits() -> f64 {
63 Market::credits()
64}
65
66pub fn incoming_transactions() -> Vec<Transaction> {
70 Market::incoming_transactions()
71 .iter()
72 .map(Into::into)
73 .collect()
74}
75
76pub fn outgoing_transactions() -> Vec<Transaction> {
80 Market::outgoing_transactions()
81 .iter()
82 .map(Into::into)
83 .collect()
84}
85
86pub fn orders() -> JsHashMap<String, MyOrder> {
91 Market::orders().into()
92}
93
94pub fn orders_jsstring() -> JsHashMap<JsString, MyOrder> {
99 Market::orders().into()
100}
101
102pub fn calc_transaction_cost(amount: u32, room_1: &JsString, room_2: &JsString) -> u32 {
111 Market::calc_transaction_cost(amount, room_1, room_2)
112}
113
114pub fn cancel_order(order_id: &JsString) -> Result<(), MarketCancelOrderErrorCode> {
119 MarketCancelOrderErrorCode::result_from_i8(Market::cancel_order(order_id))
120}
121
122pub fn change_order_price(
130 order_id: &JsString,
131 new_price: f64,
132) -> Result<(), ChangeOrderPriceErrorCode> {
133 ChangeOrderPriceErrorCode::result_from_i8(Market::change_order_price(order_id, new_price))
134}
135
136pub fn create_order(order_parameters: &Object) -> Result<(), CreateOrderErrorCode> {
141 CreateOrderErrorCode::result_from_i8(Market::create_order(order_parameters))
142}
143
144pub fn deal(
150 order_id: &JsString,
151 amount: u32,
152 room_name: Option<RoomName>,
153) -> Result<(), DealErrorCode> {
154 DealErrorCode::result_from_i8(match room_name {
155 Some(r) => Market::deal(order_id, amount, Some(&r.into())),
156 None => Market::deal(order_id, amount, None),
157 })
158}
159
160pub fn extend_order(order_id: &JsString, add_amount: u32) -> Result<(), ExtendOrderErrorCode> {
165 ExtendOrderErrorCode::result_from_i8(Market::extend_order(order_id, add_amount))
166}
167
168pub fn get_all_orders(filter: Option<&LodashFilter>) -> Vec<Order> {
176 Market::get_all_orders(filter)
177 .iter()
178 .map(Into::into)
179 .collect()
180}
181
182pub fn get_history(resource: Option<ResourceType>) -> Vec<OrderHistoryRecord> {
189 Market::get_history(resource)
190 .map(|arr| arr.iter().map(Into::into).collect())
191 .unwrap_or_default()
192}
193
194pub fn get_order_by_id(order_id: &str) -> Option<Order> {
199 let order_id: JsString = order_id.into();
200
201 Market::get_order_by_id(&order_id)
202}
203
204#[wasm_bindgen]
205extern "C" {
206 #[wasm_bindgen]
208 #[derive(Debug)]
209 pub type Order;
210 #[wasm_bindgen(method, getter)]
213 pub fn id(this: &Order) -> JsString;
214 #[wasm_bindgen(method, getter)]
216 pub fn created(this: &Order) -> Option<u32>;
217 #[wasm_bindgen(method, getter = createdTimestamp)]
220 pub fn created_timestamp(this: &Order) -> f64;
221 #[wasm_bindgen(method, getter = type)]
224 pub fn order_type(this: &Order) -> OrderType;
225 #[wasm_bindgen(method, getter = resourceType)]
227 pub fn resource_type(this: &Order) -> MarketResourceType;
228 #[wasm_bindgen(method, getter = roomName)]
230 pub fn room_name(this: &Order) -> Option<JsString>;
231 #[wasm_bindgen(method, getter)]
234 pub fn amount(this: &Order) -> u32;
235 #[wasm_bindgen(method, getter = remainingAmount)]
238 pub fn remaining_amount(this: &Order) -> u32;
239 #[wasm_bindgen(method, getter)]
241 pub fn price(this: &Order) -> f64;
242}
243
244#[wasm_bindgen]
246extern "C" {
247 #[wasm_bindgen]
248 #[derive(Debug)]
249 pub type Transaction;
250 #[wasm_bindgen(method, getter = transactionId)]
251 pub fn transaction_id(this: &Transaction) -> JsString;
252 #[wasm_bindgen(method, getter)]
253 pub fn time(this: &Transaction) -> u32;
254 #[wasm_bindgen(method, getter)]
257 pub fn sender(this: &Transaction) -> Option<Player>;
258 #[wasm_bindgen(method, getter)]
261 pub fn recipient(this: &Transaction) -> Option<Player>;
262 #[wasm_bindgen(method, getter = resourceType)]
263 pub fn resource_type(this: &Transaction) -> ResourceType;
264 #[wasm_bindgen(method, getter)]
265 pub fn amount(this: &Transaction) -> u32;
266 #[wasm_bindgen(method, getter)]
268 pub fn from(this: &Transaction) -> JsString;
269 #[wasm_bindgen(method, getter)]
271 pub fn to(this: &Transaction) -> JsString;
272 #[wasm_bindgen(method, getter)]
275 pub fn description(this: &Transaction) -> Option<JsString>;
276 #[wasm_bindgen(method, getter = order)]
279 pub fn order(this: &Transaction) -> Option<TransactionOrder>;
280}
281
282#[wasm_bindgen]
283extern "C" {
284 #[wasm_bindgen]
285 #[derive(Debug)]
286 pub type Player;
287 #[wasm_bindgen(method, getter)]
288 pub fn username(this: &Player) -> JsString;
289}
290
291#[wasm_bindgen]
292extern "C" {
293 #[wasm_bindgen]
294 #[derive(Debug)]
295 pub type TransactionOrder;
296 #[wasm_bindgen(method, getter)]
297 pub fn id(this: &TransactionOrder) -> JsString;
298 #[wasm_bindgen(method, getter = type)]
299 pub fn order_type(this: &TransactionOrder) -> OrderType;
300 #[wasm_bindgen(method, getter)]
301 pub fn price(this: &TransactionOrder) -> f64;
302}
303
304#[wasm_bindgen]
305extern "C" {
306 #[wasm_bindgen]
307 #[derive(Debug)]
308 pub type MyOrder;
309 #[wasm_bindgen(method, getter)]
310 pub fn id(this: &MyOrder) -> JsString;
311 #[wasm_bindgen(method, getter)]
313 pub fn created(this: &MyOrder) -> Option<u32>;
314 #[wasm_bindgen(method, getter = createdTimestamp)]
317 pub fn created_timestamp(this: &MyOrder) -> f64;
318 #[wasm_bindgen(method, getter)]
319 pub fn active(this: &MyOrder) -> bool;
320 #[wasm_bindgen(method, getter = type)]
321 pub fn order_type(this: &MyOrder) -> OrderType;
322 #[wasm_bindgen(method, getter = resourceType)]
323 pub fn resource_type(this: &MyOrder) -> MarketResourceType;
324 #[wasm_bindgen(method, getter = roomName)]
326 pub fn room_name(this: &MyOrder) -> Option<JsString>;
327 #[wasm_bindgen(method, getter)]
328 pub fn amount(this: &MyOrder) -> u32;
329 #[wasm_bindgen(method, getter = remainingAmount)]
330 pub fn remaining_amount(this: &MyOrder) -> u32;
331 #[wasm_bindgen(method, getter = totalAmount)]
332 pub fn total_amount(this: &MyOrder) -> u32;
333 #[wasm_bindgen(method, getter)]
334 pub fn price(this: &MyOrder) -> f64;
335}
336
337impl JsCollectionFromValue for MyOrder {
338 fn from_value(val: JsValue) -> Self {
339 val.unchecked_into()
340 }
341}
342
343#[wasm_bindgen]
344extern "C" {
345 #[wasm_bindgen]
346 #[derive(Debug)]
347 pub type OrderHistoryRecord;
348 #[wasm_bindgen(method, getter = resourceType)]
349 pub fn resource_type(this: &OrderHistoryRecord) -> MarketResourceType;
350 #[wasm_bindgen(method, getter)]
352 pub fn date(this: &OrderHistoryRecord) -> JsString;
353 #[wasm_bindgen(method, getter)]
355 pub fn transactions(this: &OrderHistoryRecord) -> u32;
356 #[wasm_bindgen(method, getter)]
358 pub fn volume(this: &OrderHistoryRecord) -> u32;
359 #[wasm_bindgen(method, getter = avgPrice)]
360 pub fn avg_price(this: &OrderHistoryRecord) -> f64;
361 #[wasm_bindgen(method, getter = stddevPrice)]
362 pub fn stddev_price(this: &OrderHistoryRecord) -> f64;
363}