Skip to content

Commit

Permalink
Staring phaser work (#14)
Browse files Browse the repository at this point in the history
* some working code

* reworked backend js for modules

* add backend files back

* fixing menu.js

* minor fix from service

* fixing test file

---------

Co-authored-by: Ale Kennedy <[email protected]>
  • Loading branch information
ardan-bkennedy and alehacker authored Mar 22, 2024
1 parent f9da55c commit 7d7fcf2
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 190 deletions.
Binary file added app/services/ui/website/.DS_Store
Binary file not shown.
Binary file added app/services/ui/website/assets/.DS_Store
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
9 changes: 3 additions & 6 deletions app/services/ui/website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
<html>
<head>
<title>Testing Metamask Wallet</title>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.60.0/dist/phaser-arcade-physics.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
<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/backend/engine.js"></script>
<script src="scripts/backend/wallet.js"></script>
<script src="scripts/backend/app.js"></script>
<script src="scripts/backend/init.js"></script>
<script type="module" src="scripts/backend/backend.js"></script>
<script type="module" src="scripts/frontend/index.js"></script>
</head>
<body>
<h1>Testing Metamask Wallet</h1>
<button id="gameConnect">Game Connect</button>
<button id="gameTables">Game Tables</button>
<!-- <script src="scripts/frontend/index.js"></script> -->
<h2></h2>
<div>
<div id="error"></div>
Expand Down
65 changes: 34 additions & 31 deletions app/services/ui/website/scripts/backend/app.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,82 @@
import Engine from './engine.js';
import Wallet from './wallet.js';

class App {
engine;
#engine;

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

constructor(url) {
this.engine = new Engine(url);
this.#engine = new Engine(url);
}

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

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

$("#gameConnect").click(this.handlerGameConnect);
$("#gameTables").click(this.handlerGameTables);
$('#gameConnect').click(this.handlerGameConnect);
$('#gameTables').click(this.handlerGameTables);
}

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

async handlerGameConnect() {
const err = await this.gameConnect();
async #handlerGameConnect() {
const err = await this.#gameConnect();
if (err != null) {
$("#error").text(err);
$('#error').text(err);
return;
}

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

async handlerGameTables() {
const [tables, err] = await this.engine.tables();
async #handlerGameTables() {
const [tables, err] = await this.#engine.Tables();
if (err != null) {
$("#error").text(err);
$('#error').text(err);
return;
}

$("#error").text(JSON.stringify(tables));
$('#error').text(JSON.stringify(tables));
}

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

// gameConnect does everything to connect the browser to the wallet and
// to the game engine.
async gameConnect() {

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

// Ask the user's wallet if it's talking to the same blockchain as
// the game engine.
var [_, err] = await Wallet.switchChain(cfg.chainId);
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.chainId, cfg.network);
var [_, err] = await Wallet.AddEthereumChain(cfg.chainId, cfg.network);
if (err != null) {
return err;
}

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

// Request permission to use the wallet. The user will select an
// account to use.
var [rp, err] = await Wallet.requestPermissions();
var [rp, err] = await Wallet.RequestPermissions();
if (err != null) {
return err;
}
Expand All @@ -96,13 +97,13 @@ class App {
const dateTime = currentDateTime();

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

// Connect to the game engine to get a token for game play.
var err = await this.engine.connect(address, cfg.chainId, dateTime, sig);
var err = await this.#engine.Connect(address, cfg.chainId, dateTime, sig);
if (err != null) {
return err;
}
Expand All @@ -111,17 +112,19 @@ class App {
}
}

export default App;

// =============================================================================

function currentDateTime() {
const dt = new Date();
const year = dt.getUTCFullYear();
const month = String(dt.getUTCMonth() + 1).padStart(2, '0'); // Month (0-indexed)
const day = String(dt.getUTCDate()).padStart(2, '0');
const hours = String(dt.getUTCHours()).padStart(2, '0');

const year = dt.getUTCFullYear();
const month = String(dt.getUTCMonth() + 1).padStart(2, '0'); // Month (0-indexed)
const day = String(dt.getUTCDate()).padStart(2, '0');
const hours = String(dt.getUTCHours()).padStart(2, '0');
const minutes = String(dt.getUTCMinutes()).padStart(2, '0');
const seconds = String(dt.getUTCSeconds()).padStart(2, '0');

return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
}
21 changes: 21 additions & 0 deletions app/services/ui/website/scripts/backend/backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import App from './app.js';

const app = new App('http://0.0.0.0:3000');

const sdk = new MetaMaskSDK.MetaMaskSDK({
dappMetadata: {
name: 'Pure JS example',
url: window.location.host,
},
logging: {
sdk: false,
},
});

$.ajaxSetup({
contentType: 'application/json; charset=utf-8',
});

window.onload = function () {
app.Init();
};
65 changes: 37 additions & 28 deletions app/services/ui/website/scripts/backend/engine.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,80 @@
class Engine {
url;
token;
#url;
#token;

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

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

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

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

async config() {
async Config() {
try {
const result = await $.ajax({
type: "get",
url: `${this.url}/v1/game/config`
type: 'get',
url: `${this.#url}/v1/game/config`,
});

return [result, null];
}

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

async connect(address, chainId, dateTime, sigature) {
async Connect(address, chainId, dateTime, sigature) {
try {
this.token = null;
this.#token = null;

const result = await $.ajax({
type: "post",
url: `${this.url}/v1/game/connect`,
data: `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}`
type: 'post',
url: `${this.#url}/v1/game/connect`,
data: `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}","sig":"${sigature}"}`,
});

this.token = result.token;
this.#token = result.token;

return null;
}

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

async tables() {
async Tables() {
try {
if (!this.isConnected) {
return [null, "not connected to game engine"];
return [null, 'not connected to game engine'];
}

const tables = await $.ajax({
type: "get",
url: `${this.url}/v1/game/tables`,
headers: { "Authorization": "Bearer " + this.token }
type: 'get',
url: `${this.#url}/v1/game/tables`,
headers: {Authorization: 'Bearer ' + this.#token},
});

return [tables, null];
}

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

export default Engine;

// =============================================================================

function parseError(e) {
switch (true) {
case 'responseJSON' in e:
return e.responseJSON.error;
case 'responseText' in e:
return e.responseText;
}

return 'no error field identified';
}
34 changes: 0 additions & 34 deletions app/services/ui/website/scripts/backend/init.js

This file was deleted.

Loading

0 comments on commit 7d7fcf2

Please sign in to comment.