Skip to content

Commit

Permalink
playing with classes in js
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 17, 2024
1 parent 56417da commit 898e267
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 213 deletions.
2 changes: 1 addition & 1 deletion app/services/ui/website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>Testing Metamask Wallet</title>
<script src="https://c0f4f41c-2f55-4863-921b-sdk-docs.github.io/cdn/metamask-sdk.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="scripts/system.js"></script>
<script src="scripts/engine.js"></script>
<script src="scripts/wallet.js"></script>
<script src="scripts/app.js"></script>
<script src="scripts/events.js"></script>
<script src="scripts/system.js"></script>
</head>
<body>
<h1>Testing Metamask Wallet</h1>
Expand Down
108 changes: 55 additions & 53 deletions app/services/ui/website/scripts/app.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
// 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.
async function gameConnect(url) {
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.
static async gameConnect(url) {

// Get configuration information from the game engine.
var [cfg, err] = await config(url);
if (err != null) {
return [null, err];
}

// Ask the user's wallet is talking to the same blockchain as
// the game engine.
var [_, err] = await 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 addEthereumChain(cfg);
// Get configuration information from the game engine.
var [cfg, err] = await Engine.config(url);
if (err != null) {
return [null, err];
}

// Try one more time to switch the wallet.
var [_, err] = await switchChain(cfg.chainId);
// Ask the user's wallet is 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);
if (err != null) {
return [null, err];
}

// Try one more time to switch the wallet.
var [_, err] = await Wallet.switchChain(cfg.chainId);
if (err != null) {
return [null, err];
}
}

// Request permission to use the wallet. The user will select an
// account to use.
var [rp, err] = await Wallet.requestPermissions();
if (err != null) {
return [null, err];
}
}

// Request permission to use the wallet. The user will select an
// account to use.
var [rp, err] = await requestPermissions();
if (err != null) {
return [null, err];
}
// Capture the account that the user selected.
if (rp.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats[0].value.length != 1) {
return [null, "user didn't select one account"];
}
const address = rp[0].caveats[0].value[0];

// Capture the account that the user selected.
if (rp.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats[0].value.length != 1) {
return [null, "user didn't select one account"];
}
const address = rp[0].caveats[0].value[0];
// Get the current time to sign data to send to the game engine.
const dateTime = currentDateTime();

// Get the current time to sign data to send to the game engine.
const dateTime = currentDateTime();
// Sign the arbitrary data.
var [sig, err] = await Wallet.personalSign(address, cfg.chainId, dateTime);
if (err != null) {
return [null, err];
}

// Connect to the game engine to get a token for game play.
var [cge, err] = await Engine.connectGameEngine(url, address, cfg.chainId, dateTime, sig);
if (err != null) {
return [null, err];
}

// Sign the arbitrary data.
var [sig, err] = await personalSign(address, cfg.chainId, dateTime);
if (err != null) {
return [null, err];
}

// Connect to the game engine to get a token for game play.
var [cge, err] = await connectGameEngine(url, address, cfg.chainId, dateTime, sig);
if (err != null) {
return [null, err];
return [cge.token, null];
}

return [cge.token, null];
}
}
101 changes: 56 additions & 45 deletions app/services/ui/website/scripts/engine.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
async function config(url) {
try {
const result = await $.ajax({
type: "get",
url: `${url}/v1/game/config`
});

return [result, null];
class Engine {
static async config(url) {
try {
const result = await $.ajax({
type: "get",
url: `${url}/v1/game/config`
});

return [result, null];
}

catch (e) {
if ('responseJSON' in e) {
return [null, e.responseJSON];
}
return [null, e.responseText];
}
}

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

async function connectGameEngine(url, address, chainId, dateTime, sigature) {
const data = `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}`;

try {
const token = await $.ajax({
type: "post",
url: `${url}/v1/game/connect`,
data: data
});

return [token, null];
}

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

async function getGameTables(url, token) {
try {
const tables = await $.ajax({
type: "get",
url: `${url}/v1/game/table`,
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Bearer " + token);
},
});

return [tables, null];
static async connectGameEngine(url, address, chainId, dateTime, sigature) {
const data = `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}`;

try {
const token = await $.ajax({
type: "post",
url: `${url}/v1/game/connect`,
data: data
});

return [token, null];
}

catch (e) {
if ('responseJSON' in e) {
return [null, e.responseJSON];
}
return [null, e.responseText];
}
}

catch (e) {
return [null, e.responseJSON];
static async queryGameTables(url, token) {
try {
const tables = await $.ajax({
type: "get",
url: `${url}/v1/game/table`,
beforeSend: function(xhr) {
xhr.setRequestHeader ("Authorization", "Bearer " + token);
},
});

return [tables, null];
}

catch (e) {
if ('responseJSON' in e) {
return [null, e.responseJSON];
}
return [null, e.responseText];
}
}
}
}
85 changes: 45 additions & 40 deletions app/services/ui/website/scripts/events.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
var connect = {
url: "http://0.0.0.0:3000",
token: ""
};

window.onload = function () {
wireEvents();
}

function wireEvents() {
const gameConnect = document.getElementById("gameConnect");
gameConnect.addEventListener(
'click',
function () { eventGameConnect(connect.url); },
false
);

const gameTables = document.getElementById("gameTables");
gameTables.addEventListener(
'click',
function () { eventGameTables(connect.url, connect.token); },
false
);
}

async function eventGameConnect(url) {
const [token, err] = await gameConnect(url);
if (err != null) {
$("#error").text(err);
return;
class Events {
url;
token;

constructor(url) {
this.url = url;
}

init() {
// Make sure 'this' is the object and not the html element
// when these methods are executed by the event listener.
this.eventGameConnect = this.eventGameConnect.bind(this);
this.eventGameTables = this.eventGameTables.bind(this);

const gameConnect = document.getElementById("gameConnect");
gameConnect.addEventListener(
'click',
this.eventGameConnect,
false
);

const gameTables = document.getElementById("gameTables");
gameTables.addEventListener(
'click',
this.eventGameTables,
false
);
}

connect.token = token;
async eventGameConnect() {
const [token, err] = await App.gameConnect(this.url);
if (err != null) {
$("#error").text(err);
return;
}

// For now display the token.
$("#error").text(token);
}
this.token = token;

async function eventGameTables(url, token) {
const [tables, err] = await getGameTables(url, token);
if (err != null) {
$("#error").text(err);
return;
// For now display the token.
$("#error").text(token);
}

$("#error").text(JSON.stringify(tables));
async eventGameTables() {
const [tables, err] = await Engine.queryGameTables(this.url, this.token);
if (err != null) {
$("#error").text(err);
return;
}

$("#error").text(JSON.stringify(tables));
}
}
6 changes: 6 additions & 0 deletions app/services/ui/website/scripts/system.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const events = new Events("http://0.0.0.0:3000");

window.onload = function () {
events.init();
}

const sdk = new MetaMaskSDK.MetaMaskSDK({
dappMetadata: {
name: "Pure JS example",
Expand Down
Loading

0 comments on commit 898e267

Please sign in to comment.