Skip to content

Commit

Permalink
Add collectables simulation for web
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Dec 17, 2024
1 parent 8a96ea9 commit 158fd54
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
21 changes: 17 additions & 4 deletions src-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ fn err_to_string<T: ToString>(v: T) -> String {
}

#[wasm_bindgen]
pub fn new_status(
attrs: JsValue,
recipe: JsValue,
) -> Result<JsValue, JsValue> {
pub fn new_status(attrs: JsValue, recipe: JsValue) -> Result<JsValue, JsValue> {
let attrs: Attributes = from_value(attrs)?;
let recipe: Recipe = from_value(recipe)?;
let result = app_libs::new_status(attrs, recipe).map_err(err_to_string)?;
Expand Down Expand Up @@ -151,6 +148,22 @@ pub fn rand_simulation(
Ok(to_value(&result)?)
}

#[wasm_bindgen]
pub fn rand_collectables_simulation(
status: JsValue,
actions: JsValue,
n: usize,
ignore_errors: bool,
collectables_shop_refine: JsValue,
) -> Result<JsValue, JsValue> {
use app_libs::analyzer::rand_simulations::{stat_collectables, CollectablesShopRefine};
let status: Status = from_value(status)?;
let actions: Vec<Actions> = from_value(actions)?;
let collectables_shop_refine: CollectablesShopRefine = from_value(collectables_shop_refine)?;
let result = stat_collectables(status, &actions, n, ignore_errors, collectables_shop_refine);
Ok(to_value(&result)?)
}

#[wasm_bindgen]
pub fn calc_attributes_scope(status: JsValue, actions: JsValue) -> Result<JsValue, JsValue> {
let status: Status = from_value(status)?;
Expand Down
31 changes: 15 additions & 16 deletions src/libs/Analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,21 @@ export async function rand_collectables_simulation(
let { invoke } = await pkgTauri;
return invoke('rand_collectables_simulation', args);
} else {
throw 'unsupported';
// return new Promise((resolve, reject) => {
// const worker = new Worker(
// new URL('./AnalyzerWorker.ts', import.meta.url),
// { type: 'module' },
// );
// worker.onmessage = ev => {
// if (ev.data.error == undefined) resolve(ev.data);
// else reject(ev.data.error);
// };
// worker.onerror = ev => reject(ev);
// worker.postMessage({
// name: 'rand_simulation',
// args: JSON.stringify(args),
// });
// });
return new Promise((resolve, reject) => {
const worker = new Worker(
new URL('./AnalyzerWorker.ts', import.meta.url),
{ type: 'module' },
);
worker.onmessage = ev => {
if (ev.data.error == undefined) resolve(ev.data);
else reject(ev.data.error);
};
worker.onerror = ev => reject(ev);
worker.postMessage({
name: 'rand_collectables_simulation',
args: JSON.stringify(args),
});
});
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/libs/AnalyzerWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

onmessage = async e => {
if (import.meta.env.VITE_BESTCRAFT_TARGET == 'web') {
var { rand_simulation, calc_attributes_scope } = await import(
'../../pkg-wasm/app_wasm'
);
var {
rand_simulation,
rand_collectables_simulation,
calc_attributes_scope,
} = await import('../../pkg-wasm/app_wasm');
} else return;
const { name, args: argsJson } = e.data;
const args = JSON.parse(argsJson);
Expand All @@ -34,6 +36,16 @@ onmessage = async e => {
),
);
break;
case 'rand_collectables_simulation':
postMessage(
rand_collectables_simulation(
args.status,
args.actions,
args.n,
args.ignoreErrors,
args.collectablesShopRefine,
),
);
case 'calc_attributes_scope':
postMessage(calc_attributes_scope(args.status, args.actions));
}
Expand Down

0 comments on commit 158fd54

Please sign in to comment.