From cbaac247a36329c3914d23d476d8db9fa6f62ac6 Mon Sep 17 00:00:00 2001 From: William Kennedy Date: Sun, 17 Mar 2024 16:03:19 -0400 Subject: [PATCH] more js cleanup --- app/services/ui/website/scripts/app.js | 7 +++-- app/services/ui/website/scripts/engine.js | 31 +++++++++++------------ app/services/ui/website/scripts/init.js | 15 +++++++++++ app/services/ui/website/scripts/wallet.js | 14 +++++----- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/app/services/ui/website/scripts/app.js b/app/services/ui/website/scripts/app.js index 679d034a..bd1af5a5 100644 --- a/app/services/ui/website/scripts/app.js +++ b/app/services/ui/website/scripts/app.js @@ -45,8 +45,7 @@ class App { // ------------------------------------------------------------------------- // gameConnect does everything to connect the browser to the wallet and - // to the game engine. If successful, a JWT is returned that is needed - // for other game engine API calls. + // to the game engine. async gameConnect() { // Get configuration information from the game engine. @@ -55,14 +54,14 @@ class App { return err; } - // Ask the user's wallet is talking to the same blockchain as + // Ask the user's wallet if it's talking to the same blockchain as // the game engine. var [_, err] = await Wallet.switchChain(cfg.chainId); if (err != null) { // The blockchain does not exist in the user's wallet so // let's try to help them. - var [_, err] = await Wallet.addEthereumChain(cfg); + var [_, err] = await Wallet.addEthereumChain(cfg.chainId, cfg.network); if (err != null) { return err; } diff --git a/app/services/ui/website/scripts/engine.js b/app/services/ui/website/scripts/engine.js index 466921ba..718136cf 100644 --- a/app/services/ui/website/scripts/engine.js +++ b/app/services/ui/website/scripts/engine.js @@ -10,6 +10,10 @@ class Engine { // ------------------------------------------------------------------------- + async isConnected() { + return (token != null) ? true : false; + } + async config() { try { const result = await $.ajax({ @@ -21,21 +25,18 @@ class Engine { } catch (e) { - if ('responseJSON' in e) { - return [null, e.responseJSON]; - } - return [null, e.responseText]; + return [null, parseError(e)]; } } async connect(address, chainId, dateTime, sigature) { - const data = `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}`; - try { + this.token = null; + const result = await $.ajax({ type: "post", url: `${this.url}/v1/game/connect`, - data: data + data: `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}` }); this.token = result.token; @@ -44,15 +45,16 @@ class Engine { } catch (e) { - if ('responseJSON' in e) { - return [null, e.responseJSON]; - } - return e.responseText; + return [null, parseError(e)]; } } async tables() { try { + if (!this.isConnected) { + return [null, "not connected to game engine"]; + } + const tables = await $.ajax({ type: "get", url: `${this.url}/v1/game/tables`, @@ -63,10 +65,7 @@ class Engine { } catch (e) { - if ('responseJSON' in e) { - return [null, e.responseJSON]; - } - return [null, e.responseText]; + return [null, parseError(e)]; } } -} \ No newline at end of file +} diff --git a/app/services/ui/website/scripts/init.js b/app/services/ui/website/scripts/init.js index 33a4b5d7..fe9a2be9 100644 --- a/app/services/ui/website/scripts/init.js +++ b/app/services/ui/website/scripts/init.js @@ -16,4 +16,19 @@ $.ajaxSetup({ window.onload = function () { app.init(); +} + +// ============================================================================= + +function parseError(e) { + switch (true) { + case ('responseJSON' in e): + return e.responseJSON.error; + case ('message' in e): + return e.message; + case ('responseText' in e): + return e.responseText; + } + + return "no error field identified"; } \ No newline at end of file diff --git a/app/services/ui/website/scripts/wallet.js b/app/services/ui/website/scripts/wallet.js index a819b6f3..47015c95 100644 --- a/app/services/ui/website/scripts/wallet.js +++ b/app/services/ui/website/scripts/wallet.js @@ -14,17 +14,17 @@ class Wallet { } catch (e) { - return [null, e.message]; + return [null, parseError(e)]; } } - static async addEthereumChain(cfg) { + static async addEthereumChain(chainId, network) { try { const result = await ethereum.request({ method: 'wallet_addEthereumChain', params: [ { - chainId: '0x' + cfg.chainId.toString(16), + chainId: '0x' + chainId.toString(16), chainName: "Liars Dice Local", rpcUrls: [ cfg.network, @@ -36,7 +36,7 @@ class Wallet { "decimals": 18 }, blockExplorerUrls: [ - cfg.network + network ] } ], @@ -46,7 +46,7 @@ class Wallet { } catch (e) { - return [null, e.message]; + return [null, parseError(e)]; } } @@ -65,7 +65,7 @@ class Wallet { } catch (e) { - return [null, e.message]; + return [null, parseError(e)]; } } @@ -85,7 +85,7 @@ class Wallet { } catch (e) { - return [null, e.message]; + return [null, parseError(e)]; } } }