Compare commits
No commits in common. "3a483049a46dfec47f448785689671325d9d3e5f" and "ee9a6222fac7cd6ac3259f32dd3253beceac49a9" have entirely different histories.
3a483049a4
...
ee9a6222fa
2 changed files with 33 additions and 118 deletions
|
@ -10,14 +10,9 @@ assuming nix flake
|
|||
nix develop
|
||||
# or if using direnv
|
||||
echo "use flake ." > .envrc
|
||||
direnv allow
|
||||
# endif
|
||||
|
||||
npm run dev
|
||||
# runs dev server on localhost:4000
|
||||
# instances run on incremntal ports
|
||||
# refresh page to reflect changes
|
||||
|
||||
npm run build
|
||||
# builds new xdc as dist/webxdc-go.xdc
|
||||
```
|
||||
|
|
98
src/main.js
98
src/main.js
|
@ -69,7 +69,6 @@ const gameState = { // pre-init values (render prior to any player input)
|
|||
komi: null, // komi depends on handicap stones + player rank
|
||||
handicap: null,
|
||||
boardSize: null,
|
||||
players: new Set (),
|
||||
playerState: {
|
||||
bCaptures: null,
|
||||
wCaptures: null,
|
||||
|
@ -272,31 +271,6 @@ boardSizeEl.addEventListener('click', clickBoardSize);
|
|||
gameStartEl.addEventListener('click', clickSubmitStart);
|
||||
|
||||
/*----- 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();
|
||||
|
||||
|
@ -314,25 +288,9 @@ function init() {
|
|||
gameState.gameRecord = [];
|
||||
boardState = [];
|
||||
gameState.gameMeta.start = false;
|
||||
joinRoom ();
|
||||
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() {
|
||||
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)}`;
|
||||
|
@ -342,29 +300,17 @@ function startMenu() {
|
|||
modalEl.style.visibility = 'visible';
|
||||
renderMenu();
|
||||
}
|
||||
|
||||
function clickSubmitStart(evt) {
|
||||
let players = gameState.players.values ();
|
||||
const update = {
|
||||
payload: {
|
||||
event: "new_game",
|
||||
players: [players.next ().value, players.next ().value],
|
||||
},
|
||||
// document: "",
|
||||
summary: "start game",
|
||||
};
|
||||
const descr = `${userName} started game`;
|
||||
sendUpdate(update, descr);
|
||||
if (gameState.gameMeta.start) return init();
|
||||
evt.preventDefault();
|
||||
evt.stopPropagation();
|
||||
return null;
|
||||
gameState.playerMeta.b.name = blackNameInputEl.value || 'black';
|
||||
gameState.playerMeta.w.name = whiteNameInputEl.value || 'white';
|
||||
modalEl.style.visibility = 'hidden';
|
||||
return initGame();
|
||||
}
|
||||
|
||||
function initGame(payload) {
|
||||
gameState.playerMeta.b.name = payload.players [0] || 'black';
|
||||
gameState.playerMeta.w.name = payload.players [1] || 'white';
|
||||
modalEl.style.visibility = 'hidden';
|
||||
function initGame() {
|
||||
gameState.winner = null;
|
||||
gameState.pass = null;
|
||||
gameState.turn = gameState.handicap ? -1 : 1;
|
||||
|
@ -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 == 'white-rank-certain') gameState.playerMeta.w.rankCertain = !gameState.playerMeta.w.rankCertain;
|
||||
renderMenu();
|
||||
|
||||
}
|
||||
|
||||
function clickBoardSize(evt) {
|
||||
|
@ -493,26 +440,10 @@ function clickBoard(evt) {
|
|||
evt.stopPropagation();
|
||||
if (gameState.pass > 1 || gameState.winner) return editTerritory(evt);
|
||||
// checks for placement and pushes to cell
|
||||
let placement = [
|
||||
parseInt(evt.target.closest('td').id.split('-')[0]),
|
||||
parseInt(evt.target.closest('td').id.split('-')[1])
|
||||
];
|
||||
let update = {
|
||||
payload: {
|
||||
event: 'make_move',
|
||||
placement,
|
||||
player: userName
|
||||
}
|
||||
};
|
||||
let descr = `${userName} move at ${placement}`;
|
||||
return sendUpdate (update, descr);
|
||||
}
|
||||
|
||||
function makeMove (payload) {
|
||||
let { placement, player } = payload;
|
||||
let placement = [ parseInt(evt.target.closest('td').id.split('-')[0]), parseInt(evt.target.closest('td').id.split('-')[1]) ];
|
||||
let point = findPointFromIdx(placement);
|
||||
if (!checkLegal (point)) return;
|
||||
if (!checkTurn (player)) return;
|
||||
//checks that this placement was marked as legal
|
||||
if ( !checkLegal(point) ) return null;
|
||||
clearKo();
|
||||
clearPass();
|
||||
resolveCaptures(point);
|
||||
|
@ -523,7 +454,6 @@ function makeMove (payload) {
|
|||
gameState.gameRecord.push(`${STONES_DATA[gameState.turn]}: ${point.pos}`);
|
||||
gameState.turn*= -1;
|
||||
renderGame();
|
||||
return;
|
||||
}
|
||||
|
||||
function clearKo() {
|
||||
|
@ -565,16 +495,6 @@ function checkLegal(point) {
|
|||
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() {
|
||||
for (let point in boardState) {
|
||||
point = boardState[point];
|
||||
|
|
Loading…
Reference in a new issue