remove json decoration
This commit is contained in:
@@ -66,6 +66,13 @@ class Games {
|
||||
});
|
||||
}
|
||||
|
||||
forGame(id, cb) {
|
||||
this.get(id, (game) => {
|
||||
cb(game);
|
||||
games.update(id, game, (game) => {ui.drawGame(game)});
|
||||
});
|
||||
}
|
||||
|
||||
forUser(id, uid, cb) {
|
||||
this.get(id, (game) => {
|
||||
game.Players.forEach((player, idx) => {
|
||||
@@ -98,6 +105,7 @@ class Games {
|
||||
}
|
||||
|
||||
update(id, game, cb) {
|
||||
// TODO compute turn
|
||||
this.requests.put("/api/games/"+id, JSON.stringify(game), (text, status) => {
|
||||
if (status != 200) {
|
||||
throw new Error("bad status updating game: "+status+": "+text);
|
||||
@@ -145,15 +153,16 @@ class UI {
|
||||
`;
|
||||
if (this.me(player)) {
|
||||
var myturn = seatnum == game.Turn
|
||||
var disabled = (!player.Active || !myturn) ? "disabled" : "";
|
||||
state += `
|
||||
<form>
|
||||
<button ${disabled} onclick="fold(this); return false;">Fold</button>
|
||||
<button ${disabled} onclick="check(this); return false;">Check</button>
|
||||
<input ${disabled} type="text"/>
|
||||
<button ${disabled} onclick="raise(this); return false;">Raise</button>
|
||||
</form>
|
||||
`;
|
||||
var enabled = player.Active && myturn;
|
||||
var eleControlGame = this.eleControlGame();
|
||||
var enabledWas = eleControlGame.getAttribute("disabled") == null;
|
||||
if (enabled != enabledWas) {
|
||||
if (enabled) {
|
||||
eleControlGame.removeAttribute("disabled");
|
||||
} else {
|
||||
eleControlGame.setAttribute("disabled", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
state += `</player>`;
|
||||
}
|
||||
@@ -170,6 +179,14 @@ class UI {
|
||||
this.ele().innerHTML = "";
|
||||
}
|
||||
|
||||
eleControlGame() {
|
||||
return document.getElementById("control-game");
|
||||
}
|
||||
|
||||
eleControlGames() {
|
||||
return document.getElementById("control-games");
|
||||
}
|
||||
|
||||
ele() {
|
||||
return document.getElementById("game");
|
||||
}
|
||||
@@ -273,6 +290,8 @@ function join() {
|
||||
var db = new DB();
|
||||
var id = db.get("id");
|
||||
games.forUser("id", id, (player) => {
|
||||
if (player.Participating)
|
||||
throw new Error("redundant join");
|
||||
player.Participating = true;
|
||||
player.Active = false;
|
||||
});
|
||||
@@ -282,18 +301,47 @@ function drop() {
|
||||
var db = new DB();
|
||||
var id = db.get("id");
|
||||
games.forUser("id", id, (player) => {
|
||||
if (!player.Participating)
|
||||
throw new Error("redundant drop");
|
||||
player.Participating = false;
|
||||
player.Active = false;
|
||||
});
|
||||
}
|
||||
|
||||
function fold(ele) {
|
||||
var db = new DB();
|
||||
var id = db.get("id");
|
||||
games.forUser("id", id, (player) => {
|
||||
if (!player.Active)
|
||||
throw new Error("redundant fold");
|
||||
player.Active = false;
|
||||
});
|
||||
}
|
||||
|
||||
function check(ele) {
|
||||
var db = new DB();
|
||||
var id = db.get("id");
|
||||
games.forGame("id", (game) => {
|
||||
game.Turn += 1;
|
||||
});
|
||||
}
|
||||
|
||||
function raise(ele) {
|
||||
var db = new DB();
|
||||
var id = db.get("id");
|
||||
var bump = ele.parentNode.getElementsByTagName("input")[0].value;
|
||||
games.forGame("id", (game) => {
|
||||
game.Turn += 1;
|
||||
game.Players.forEach((player, idx) => {
|
||||
if (player.ID == id) {
|
||||
// todo it's supposed to be a string, should render client-side
|
||||
player.Balance -= bump;
|
||||
if (player.Balance < 0)
|
||||
throw new Error("cannot bet more than you have");
|
||||
game.Pot += bump;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var games = new Games();
|
||||
|
||||
Reference in New Issue
Block a user