logs and cleaner

master
Bel LaPointe 2021-03-12 11:20:48 -06:00
parent 83531757e9
commit 6ec195d2ac
3 changed files with 35 additions and 20 deletions

View File

@ -27,7 +27,8 @@ player > * {
flex-grow: 1; flex-grow: 1;
} }
player[participating] { player:not([participating]) {
display: none;
} }
player:not([active]) { player:not([active]) {

View File

@ -66,14 +66,14 @@ class Games {
}); });
} }
forGame(id, cb) { forGame(id, cb, log) {
this.get(id, (game) => { this.get(id, (game) => {
cb(game); cb(game);
games.update(id, game, (game) => {ui.drawGame(game)}); games.update(id, game, (game) => {ui.drawGame(game)}, console.log, log);
}); });
} }
forUser(id, uid, cb) { forUser(id, uid, cb, log) {
this.get(id, (game) => { this.get(id, (game) => {
var found = false; var found = false;
game.Players.forEach((player, idx) => { game.Players.forEach((player, idx) => {
@ -83,7 +83,7 @@ class Games {
} }
}); });
if (found) if (found)
games.update(id, game, (game) => {ui.drawGame(game)}); games.update(id, game, (game) => {ui.drawGame(game)}, console.log, log);
}); });
} }
@ -113,12 +113,12 @@ class Games {
}); });
} }
update(id, game, cb, catcher) { update(id, game, cb, catcher, log) {
// TODO compute turn
var n = 0; var n = 0;
var m = game.Players.length; var m = game.Players.length;
game.Players.forEach((player) => n += player.Participating ? 1 : 0); game.Players.forEach((player) => n += player.Participating ? 1 : 0);
game.Turn = game.Turn % (m || 1); game.Turn = game.Turn % (m || 1);
game.Log += "<br>" + log;
if (n) if (n)
while (m > 0 && (!game.Players[game.Turn].Active || !game.Players[game.Turn].Participating)) { while (m > 0 && (!game.Players[game.Turn].Active || !game.Players[game.Turn].Participating)) {
game.Turn = (game.Turn + 1) % game.Players.length; game.Turn = (game.Turn + 1) % game.Players.length;
@ -175,10 +175,10 @@ class UI {
case 3 : suit = "clubs"; break; case 3 : suit = "clubs"; break;
} }
switch (value) { switch (value) {
case 10 : value = "jack"; break; case 11 : value = "jack"; break;
case 11 : value = "queen"; break; case 12 : value = "queen"; break;
case 12 : value = "king"; break; case 13 : value = "king"; break;
case 13 : value = "ace"; break; case 14 : value = "ace"; break;
} }
return `<br>${value} of ${suit}`; return `<br>${value} of ${suit}`;
} }
@ -202,6 +202,7 @@ class UI {
this.ts = this.now(); this.ts = this.now();
var state = ` var state = `
<pot>${this.formatCurrency(game.Pot)}</pot> <pot>${this.formatCurrency(game.Pot)}</pot>
<log>${game.Log.split("<br>").slice(-15).join("<br>")}</log>
<players> <players>
`; `;
var myseat = -1; var myseat = -1;
@ -312,6 +313,7 @@ function init() {
{ID: id, Name: name}, {ID: id, Name: name},
], ],
}, (game) => ui.drawGame(game)); }, (game) => ui.drawGame(game));
resetGame();
}); });
} }
@ -319,6 +321,7 @@ function start() {
var db = new DB(); var db = new DB();
var deck = new Deck() var deck = new Deck()
var id = db.get("id"); var id = db.get("id");
var name = db.get("name");
games.get("id", (game) => { games.get("id", (game) => {
var myturn = 0; var myturn = 0;
var n = 0; var n = 0;
@ -334,11 +337,13 @@ function start() {
if (n == 0) { if (n == 0) {
throw new Error("will not start game with no participants"); throw new Error("will not start game with no participants");
} }
game.Pot = 0;
game.Log = "start!";
game.Turn = myturn; game.Turn = myturn;
while (!game.Players[game.Turn].Participating) { while (!game.Players[game.Turn].Participating) {
game.Turn = (game.Turn+1) % game.Players.length; game.Turn = (game.Turn+1) % game.Players.length;
} }
games.update("id", game, (game) => {ui.drawGame(game)}); games.update("id", game, (game) => {ui.drawGame(game)}, console.log, `${name} started the game`);
}); });
} }
@ -367,24 +372,26 @@ function join() {
if (redundant) { if (redundant) {
throw new Error("cannot take 2 seats"); throw new Error("cannot take 2 seats");
} }
}); }, `${name} joined the game`);
} }
function drop() { function drop() {
var db = new DB(); var db = new DB();
var id = db.get("id"); var id = db.get("id");
var name = db.get("name");
games.forUser("id", id, (player) => { games.forUser("id", id, (player) => {
player.Participating = false; player.Participating = false;
player.Active = false; player.Active = false;
}); }, `${name} dropped`);
} }
function fold(ele) { function fold(ele) {
var db = new DB(); var db = new DB();
var id = db.get("id"); var id = db.get("id");
var name = db.get("name");
games.forUser("id", id, (player) => { games.forUser("id", id, (player) => {
player.Active = false; player.Active = false;
}); }, `${name} folded`);
} }
function check(ele) { function check(ele) {
@ -396,12 +403,13 @@ function check(ele) {
if (player.ID == id) if (player.ID == id)
player.Checked = true; player.Checked = true;
}); });
}); }, `${name} checked`);
} }
function collect(ele) { function collect(ele) {
var db = new DB(); var db = new DB();
var id = db.get("id"); var id = db.get("id");
var name = db.get("name");
games.forGame("id", (game) => { games.forGame("id", (game) => {
var found = false; var found = false;
game.Players.forEach((player, idx) => { game.Players.forEach((player, idx) => {
@ -414,31 +422,36 @@ function collect(ele) {
throw new Error("nil player cannot collect pot"); throw new Error("nil player cannot collect pot");
} }
game.Pot = 0; game.Pot = 0;
}); }, `${name} collected the pot`);
} }
function resetGame() { function resetGame() {
console.log("resetting game"); console.log("resetting game");
var db = new DB();
var name = db.get("name");
games.forGame("id", (game) => { games.forGame("id", (game) => {
game.Players = []; game.Players = [];
game.Turn = 0; game.Turn = 0;
game.Pot = 0; game.Pot = 0;
}); game.Log = "reset";
}, `${name} reset the game`);
} }
function setWallet(ele) { function setWallet(ele) {
var db = new DB(); var db = new DB();
var id = db.get("id"); var id = db.get("id");
var balance = parseInt(ele.parentNode.getElementsByTagName("input")[0].value); var balance = parseInt(ele.parentNode.getElementsByTagName("input")[0].value);
var name = db.get("name");
games.forUser("id", id, (player) => { games.forUser("id", id, (player) => {
player.Balance = balance; player.Balance = balance;
}); }, `${name} set their wallet to ${balance}`);
} }
function raise(ele) { function raise(ele) {
var db = new DB(); var db = new DB();
var id = db.get("id"); var id = db.get("id");
var bump = parseInt(ele.parentNode.getElementsByTagName("input")[0].value); var bump = parseInt(ele.parentNode.getElementsByTagName("input")[0].value);
var name = db.get("name");
games.forGame("id", (game) => { games.forGame("id", (game) => {
game.Turn += 1; game.Turn += 1;
game.Players.forEach((player, idx) => { game.Players.forEach((player, idx) => {
@ -451,7 +464,7 @@ function raise(ele) {
player.Checked = false; player.Checked = false;
} }
}); });
}); }, `${name} raises by ${bump}`);
} }
var games = new Games(); var games = new Games();

View File

@ -6,6 +6,7 @@ type Game struct {
Pot Currency Pot Currency
Turn int Turn int
Players Players Players Players
Log string
} }
type Player struct { type Player struct {