Compare commits
4 commits
7a28a7e314
...
fd172a48ed
Author | SHA1 | Date | |
---|---|---|---|
fd172a48ed | |||
63a56e5b11 | |||
bc16d9607f | |||
01fea03112 |
5 changed files with 185 additions and 4 deletions
|
@ -9,8 +9,16 @@
|
|||
<body>
|
||||
<div id="game">
|
||||
</div>
|
||||
<button class="button new-game">
|
||||
New Game
|
||||
</button>
|
||||
<form class="menu-bar">
|
||||
<button class="button new-game">
|
||||
New Game
|
||||
</button>
|
||||
<button class="button save">
|
||||
Save Game
|
||||
</button>
|
||||
<button class="button load">
|
||||
Load Game
|
||||
</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -73,6 +73,37 @@ export class Game
|
|||
return this.state.board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps the state of the game.
|
||||
*
|
||||
* @returns {IState}
|
||||
* The JSON string representing the state.
|
||||
*/
|
||||
dump()
|
||||
{
|
||||
return {
|
||||
turnCount: this.state.turnCount,
|
||||
board: {
|
||||
...this.state.board
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the game from the specified {@link data `data`}.
|
||||
*
|
||||
* @param {IState} data
|
||||
* The data to load.
|
||||
*/
|
||||
load(data)
|
||||
{
|
||||
this.state.turnCount = data.turnCount;
|
||||
this.state.board.splice(0);
|
||||
this.#state = new State(Game.#width, Game.#height);
|
||||
Object.assign(this.state.board, data.board);
|
||||
this.draw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the game.
|
||||
*/
|
||||
|
|
|
@ -7,6 +7,56 @@ import { Game } from "./Game.js";
|
|||
*/
|
||||
let game;
|
||||
|
||||
/**
|
||||
* A value indicating whether a transfer is pending.
|
||||
*/
|
||||
let transferPending = false;
|
||||
|
||||
/**
|
||||
* The id of the save game.
|
||||
*/
|
||||
let id = "";
|
||||
|
||||
/**
|
||||
* Gets the save button.
|
||||
*
|
||||
* @returns {HTMLButtonElement}
|
||||
* The save button.
|
||||
*/
|
||||
function getSaveButton()
|
||||
{
|
||||
return document.querySelector(".save");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the load button.
|
||||
*
|
||||
* @returns {HTMLButtonElement}
|
||||
* The load button.
|
||||
*/
|
||||
function getLoadButton()
|
||||
{
|
||||
return document.querySelector(".load");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an url for storing and loading the save game.
|
||||
*
|
||||
* @returns {URL}
|
||||
* The url for storing and loading the save game.
|
||||
*/
|
||||
function getUrl()
|
||||
{
|
||||
let result = new URL(
|
||||
id,
|
||||
new URL(
|
||||
"/api/data/",
|
||||
window.location.origin));
|
||||
|
||||
result.searchParams.append("token", "c4game");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the board.
|
||||
*/
|
||||
|
@ -19,6 +69,81 @@ function initialize()
|
|||
{
|
||||
game.reset();
|
||||
};
|
||||
|
||||
getSaveButton().onclick = async () =>
|
||||
{
|
||||
if (!transferPending)
|
||||
{
|
||||
transferPending = true;
|
||||
getSaveButton().disabled = true;
|
||||
getLoadButton().disabled = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (id === "")
|
||||
{
|
||||
let result = await (await fetch(
|
||||
getUrl(),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(game.dump())
|
||||
})).json();
|
||||
|
||||
({ id } = result);
|
||||
}
|
||||
else
|
||||
{
|
||||
await fetch(
|
||||
getUrl(),
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(game.dump())
|
||||
});
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
getSaveButton().disabled = false;
|
||||
getLoadButton().disabled = false;
|
||||
transferPending = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log("Already busy");
|
||||
}
|
||||
};
|
||||
|
||||
getLoadButton().onclick = async () =>
|
||||
{
|
||||
if (!transferPending)
|
||||
{
|
||||
transferPending = true;
|
||||
getSaveButton().disabled = true;
|
||||
getLoadButton().disabled = true;
|
||||
|
||||
try
|
||||
{
|
||||
game.load(
|
||||
await (
|
||||
await fetch(getUrl())).json());
|
||||
}
|
||||
catch { }
|
||||
|
||||
getSaveButton().disabled = false;
|
||||
getLoadButton().disabled = false;
|
||||
transferPending = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log("Already busy");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
initialize();
|
||||
|
|
16
packages/game/src/js/types.d.ts
vendored
16
packages/game/src/js/types.d.ts
vendored
|
@ -12,3 +12,19 @@ type Row = CellOwner[];
|
|||
* Represents a game board.
|
||||
*/
|
||||
type Board = Row[];
|
||||
|
||||
/**
|
||||
* Represents the state of a game.
|
||||
*/
|
||||
interface IState
|
||||
{
|
||||
/**
|
||||
* The number of rounds that have been played.
|
||||
*/
|
||||
turnCount: number;
|
||||
|
||||
/**
|
||||
* The board of the game.
|
||||
*/
|
||||
board: Board;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ let data = {};
|
|||
*/
|
||||
function createGuid()
|
||||
{
|
||||
return randexp(/[0-9a-f]{8}(-[0-9a-f]){4}[0-9a-f]{8}/);
|
||||
return randexp(/[0-9a-f]{8}(-[0-9a-f]{4}){4}[0-9a-f]{8}/);
|
||||
}
|
||||
|
||||
app.use(express.static(join(dirname, "..", "..", "game", "lib", "static")));
|
||||
|
@ -97,6 +97,7 @@ app.put(
|
|||
if (id in data)
|
||||
{
|
||||
data[id] = request.body;
|
||||
response.send(data[id]);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue