-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56417da
commit 898e267
Showing
6 changed files
with
239 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.