Skip to content

Commit

Permalink
more js cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 17, 2024
1 parent 401c0e4 commit cbaac24
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
7 changes: 3 additions & 4 deletions app/services/ui/website/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down
31 changes: 15 additions & 16 deletions app/services/ui/website/scripts/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Engine {

// -------------------------------------------------------------------------

async isConnected() {
return (token != null) ? true : false;
}

async config() {
try {
const result = await $.ajax({
Expand All @@ -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;
Expand All @@ -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`,
Expand All @@ -63,10 +65,7 @@ class Engine {
}

catch (e) {
if ('responseJSON' in e) {
return [null, e.responseJSON];
}
return [null, e.responseText];
return [null, parseError(e)];
}
}
}
}
15 changes: 15 additions & 0 deletions app/services/ui/website/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
14 changes: 7 additions & 7 deletions app/services/ui/website/scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,7 +36,7 @@ class Wallet {
"decimals": 18
},
blockExplorerUrls: [
cfg.network
network
]
}
],
Expand All @@ -46,7 +46,7 @@ class Wallet {
}

catch (e) {
return [null, e.message];
return [null, parseError(e)];
}
}

Expand All @@ -65,7 +65,7 @@ class Wallet {
}

catch (e) {
return [null, e.message];
return [null, parseError(e)];
}
}

Expand All @@ -85,7 +85,7 @@ class Wallet {
}

catch (e) {
return [null, e.message];
return [null, parseError(e)];
}
}
}
Expand Down

0 comments on commit cbaac24

Please sign in to comment.