Compare commits

..

No commits in common. "3a483049a46dfec47f448785689671325d9d3e5f" and "ee9a6222fac7cd6ac3259f32dd3253beceac49a9" have entirely different histories.

2 changed files with 33 additions and 118 deletions

View file

@ -10,14 +10,9 @@ assuming nix flake
nix develop nix develop
# or if using direnv # or if using direnv
echo "use flake ." > .envrc echo "use flake ." > .envrc
direnv allow
# endif
npm run dev npm run dev
# runs dev server on localhost:4000 # runs dev server on localhost:4000
# instances run on incremntal ports # instances run on incremntal ports
# refresh page to reflect changes # refresh page to reflect changes
npm run build
# builds new xdc as dist/webxdc-go.xdc
``` ```

View file

@ -69,7 +69,6 @@ const gameState = { // pre-init values (render prior to any player input)
komi: null, // komi depends on handicap stones + player rank komi: null, // komi depends on handicap stones + player rank
handicap: null, handicap: null,
boardSize: null, boardSize: null,
players: new Set (),
playerState: { playerState: {
bCaptures: null, bCaptures: null,
wCaptures: null, wCaptures: null,
@ -272,31 +271,6 @@ boardSizeEl.addEventListener('click', clickBoardSize);
gameStartEl.addEventListener('click', clickSubmitStart); gameStartEl.addEventListener('click', clickSubmitStart);
/*----- FUNCTIONS ----------------------------------*/ /*----- FUNCTIONS ----------------------------------*/
/*----- webxdc functions -----*/
function sendUpdate(update, descr) {
window.webxdc.sendUpdate(update, descr);
}
const userName = window.webxdc.selfName;
window.webxdc.setUpdateListener((update) => {
let { payload, summary } = update;
switch (payload.event) {
case 'make_move':
makeMove (payload);
break;
case 'new_game':
initGame (payload);
break;
case 'join_room':
updateRoom (payload);
break;
default:
console.log ("couldn't recognize update event");
}
});
/*----- init functions -----*/ /*----- init functions -----*/
init(); init();
@ -314,27 +288,11 @@ function init() {
gameState.gameRecord = []; gameState.gameRecord = [];
boardState = []; boardState = [];
gameState.gameMeta.start = false; gameState.gameMeta.start = false;
joinRoom ();
startMenu(); startMenu();
}; };
function joinRoom () {
const update = {
payload: {
event: "join_room",
player: userName
}
};
const descr = `${userName} joined room`;
sendUpdate (update, descr);
}
function updateRoom (payload) {
gameState.players.add (payload .player);
}
function getDate() { function getDate() {
let d = new Date; let d = new Date;
return `${d.getFullYear()}-${String(d.getMonth()+1).charAt(-1)||0}${String(d.getMonth()+1).charAt(-0)}-${String(d.getDate()).charAt(-1)||0}${String(d.getDate()+1).charAt(-0)}`; return `${d.getFullYear()}-${String(d.getMonth()+1).charAt(-1)||0}${String(d.getMonth()+1).charAt(-0)}-${String(d.getDate()).charAt(-1)||0}${String(d.getDate()+1).charAt(-0)}`;
} }
@ -342,36 +300,24 @@ function startMenu() {
modalEl.style.visibility = 'visible'; modalEl.style.visibility = 'visible';
renderMenu(); renderMenu();
} }
function clickSubmitStart(evt) { function clickSubmitStart(evt) {
let players = gameState.players.values (); if (gameState.gameMeta.start) return init();
const update = { evt.preventDefault();
payload: { evt.stopPropagation();
event: "new_game", gameState.playerMeta.b.name = blackNameInputEl.value || 'black';
players: [players.next ().value, players.next ().value], gameState.playerMeta.w.name = whiteNameInputEl.value || 'white';
}, modalEl.style.visibility = 'hidden';
// document: "", return initGame();
summary: "start game",
};
const descr = `${userName} started game`;
sendUpdate(update, descr);
if (gameState.gameMeta.start) return init();
evt.preventDefault();
evt.stopPropagation();
return null;
} }
function initGame(payload) { function initGame() {
gameState.playerMeta.b.name = payload.players [0] || 'black'; gameState.winner = null;
gameState.playerMeta.w.name = payload.players [1] || 'white'; gameState.pass = null;
modalEl.style.visibility = 'hidden'; gameState.turn = gameState.handicap ? -1 : 1;
gameState.winner = null; gameState.gameMeta.start = true;
gameState.pass = null; initBoard();
gameState.turn = gameState.handicap ? -1 : 1; renderBoardInit();
gameState.gameMeta.start = true; renderGame();
initBoard();
renderBoardInit();
renderGame();
} }
function initBoard() { function initBoard() {
@ -458,6 +404,7 @@ function clickUpdatePlayerMeta(evt) {
if (evt.target.name == 'black-rank-certain') gameState.playerMeta.b.rankCertain = !gameState.playerMeta.b.rankCertain; if (evt.target.name == 'black-rank-certain') gameState.playerMeta.b.rankCertain = !gameState.playerMeta.b.rankCertain;
if (evt.target.name == 'white-rank-certain') gameState.playerMeta.w.rankCertain = !gameState.playerMeta.w.rankCertain; if (evt.target.name == 'white-rank-certain') gameState.playerMeta.w.rankCertain = !gameState.playerMeta.w.rankCertain;
renderMenu(); renderMenu();
} }
function clickBoardSize(evt) { function clickBoardSize(evt) {
@ -490,40 +437,23 @@ function clickCloseMenu(evt) {
/*----- gameplay functions -----*/ /*----- gameplay functions -----*/
function clickBoard(evt) { function clickBoard(evt) {
evt.stopPropagation(); evt.stopPropagation();
if (gameState.pass > 1 || gameState.winner) return editTerritory(evt); if (gameState.pass > 1 || gameState.winner) return editTerritory(evt);
// checks for placement and pushes to cell // checks for placement and pushes to cell
let placement = [ let placement = [ parseInt(evt.target.closest('td').id.split('-')[0]), parseInt(evt.target.closest('td').id.split('-')[1]) ];
parseInt(evt.target.closest('td').id.split('-')[0]), let point = findPointFromIdx(placement);
parseInt(evt.target.closest('td').id.split('-')[1]) //checks that this placement was marked as legal
]; if ( !checkLegal(point) ) return null;
let update = { clearKo();
payload: { clearPass();
event: 'make_move', resolveCaptures(point);
placement, point.stone = gameState.turn;
player: userName point.joinGroup();
} playSound(point);
}; clearCaptures();
let descr = `${userName} move at ${placement}`; gameState.gameRecord.push(`${STONES_DATA[gameState.turn]}: ${point.pos}`);
return sendUpdate (update, descr); gameState.turn*= -1;
} renderGame();
function makeMove (payload) {
let { placement, player } = payload;
let point = findPointFromIdx(placement);
if (!checkLegal (point)) return;
if (!checkTurn (player)) return;
clearKo();
clearPass();
resolveCaptures(point);
point.stone = gameState.turn;
point.joinGroup();
playSound(point);
clearCaptures();
gameState.gameRecord.push(`${STONES_DATA[gameState.turn]}: ${point.pos}`);
gameState.turn*= -1;
renderGame();
return;
} }
function clearKo() { function clearKo() {
@ -565,16 +495,6 @@ function checkLegal(point) {
return true; return true;
} }
function checkTurn (player) {
if (gameState.turn == 1) {
return gameState.playerMeta.b.name == player;
}
if (gameState.turn == -1) {
return gameState.playerMeta.w.name == player;
}
return false;
}
function clearOverlay() { function clearOverlay() {
for (let point in boardState) { for (let point in boardState) {
point = boardState[point]; point = boardState[point];