Skip to content

Commit

Permalink
fix: handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Jan 30, 2024
1 parent 8383d42 commit 183f504
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/scp-foundation-site-director/game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use web_sys::console;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = twGamificationSaveGameData)]
fn save_game_data(data: &str);
#[wasm_bindgen(js_name = twGamificationSaveGameData, catch)]
fn save_game_data(data: &str) -> Result<(), JsValue>;
}

#[wasm_bindgen]
Expand Down Expand Up @@ -71,8 +71,11 @@ pub fn set_gamification_events(events_json_string: &str) {
console::log_1(&format!("Event: {:?}", event).into());
}

// Send the serialized data to JavaScript
save_game_data(&get_example_gamification_events());
// Attempt to send the serialized data to JavaScript
match save_game_data(&get_example_gamification_events()) {
Ok(_) => console::log_1(&"Data successfully saved".into()),
Err(e) => console::log_1(&format!("Failed to save data: {:?}", e).into()),
}
}
Err(e) => {
console::log_1(&format!("Error parsing JSON: {:?}", e).into());
Expand Down
16 changes: 8 additions & 8 deletions src/scp-foundation-site-director/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IChangedTiddlers } from 'tiddlywiki';

import wbgInit, { InitOutput, set_gamification_events, start_game } from './game/wasm/game';
import { InitOutput, initSync, set_gamification_events, start_game } from './game/wasm/game';
import './index.css';
import { BasicGamificationEventTypes, IGamificationEvent } from 'src/tw-gamification/event-generator/GamificationEventTypes';
import { GameWidget } from 'src/tw-gamification/game-wiki-adaptor/GameWidgetType';
Expand All @@ -27,15 +27,14 @@ class ScpFoundationSiteDirectorGameWidget extends GameWidget {
nextSibling === null ? parent.append(containerElement) : nextSibling.before(containerElement);
this.domNodes.push(containerElement);
// TODO: load assets from asset sub-plugin, and push list and item to game by call rust function
void this.initializeGameCanvas().then(() => {
if (this.gameInitialized) {
this.popGamificationEvents();
}
});
this.initializeGameCanvas();
if (this.gameInitialized) {
this.popGamificationEvents();
}
// TODO: handle destroy using https://github.com/Jermolene/TiddlyWiki5/discussions/5945#discussioncomment-8173023
}

private async initializeGameCanvas() {
private initializeGameCanvas() {
const gameWasm = $tw.wiki.getTiddlerText('$:/plugins/linonetwo/scp-foundation-site-director/game_bg.wasm');
// wasm is bundled into tw using `game/tiddlywiki.files` as base64

Expand All @@ -44,7 +43,8 @@ class ScpFoundationSiteDirectorGameWidget extends GameWidget {
console.time('gameLoad'); // 384 ~ 1551 ms
try {
this.setLoading(true);
this.gameInstance = await wbgInit(wasmBuffer);
const wasmModule = new WebAssembly.Module(wasmBuffer);
this.gameInstance = initSync(wasmModule);
start_game();
} catch (error) {
// https://users.rust-lang.org/t/getting-rid-of-this-error-error-using-exceptions-for-control-flow-dont-mind-me-this-isnt-actually-an-error/92209
Expand Down

0 comments on commit 183f504

Please sign in to comment.