Spaces:
Paused
Paused
Download static/js/gameplay.js from igardner/StockAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/static/js/gameplay.js
- Command line
-
hf download hf://spaces/igardner/StockAnalysis/static/js/gameplay.js
-
curl -L -o gameplay.js https://huggingface.co/spaces/igardner/StockAnalysis/resolve/main/static/js/gameplay.js
10.1 kB
| /* Real room play: private role, investigator clue, accusation, reveal, repeat. */ | |
| (function () { | |
| const gameId = window.ROOM_GAME_ID; | |
| const panel = document.getElementById('gameplay-panel'); | |
| const phase = document.getElementById('game-phase'); | |
| const round = document.getElementById('game-round'); | |
| const prompt = document.getElementById('game-prompt'); | |
| const roleCard = document.getElementById('game-role-card'); | |
| const roleName = document.getElementById('game-role-name'); | |
| const roleDescription = document.getElementById('game-role-description'); | |
| const roleInstructions = document.getElementById('game-role-instructions'); | |
| const privateNotes = document.getElementById('game-private-notes'); | |
| const playerList = document.getElementById('game-player-list'); | |
| const resultBox = document.getElementById('game-result'); | |
| const actions = document.getElementById('game-actions'); | |
| const feedback = document.getElementById('game-feedback'); | |
| if (!gameId || !panel || !playerList || !actions) return; | |
| let currentState = null; | |
| let actionPending = false; | |
| let refreshPending = false; | |
| async function api(url, options) { | |
| const response = await fetch(url, Object.assign({ credentials: 'same-origin' }, options || {})); | |
| const body = await response.json().catch(function () { return {}; }); | |
| if (!response.ok || body.ok === false) { | |
| throw new Error(body.error || ('Request failed (' + response.status + ')')); | |
| } | |
| return body.data; | |
| } | |
| function make(tag, className, text) { | |
| const element = document.createElement(tag); | |
| if (className) element.className = className; | |
| if (text != null) element.textContent = text; | |
| return element; | |
| } | |
| function phaseCopy(state) { | |
| if (state.phase === 'discussion') return 'Compare what you noticed. The Investigator can privately check one guest each round.'; | |
| if (state.phase === 'vote') return 'The table is voting. Choose one living guest you suspect.'; | |
| if (state.phase === 'reveal') return 'The table has made its accusation.'; | |
| if (state.phase === 'finished') { | |
| return state.winner === 'parlor' ? 'The Parlor found the Mastermind.' : 'The Mastermind outlasted the table.'; | |
| } | |
| return 'The table is being prepared.'; | |
| } | |
| function renderPlayers(state) { | |
| playerList.replaceChildren(); | |
| (state.players || []).forEach(function (player) { | |
| const row = make('div', 'game-player' + (player.alive ? '' : ' eliminated')); | |
| if (player.avatar) { | |
| const image = make('img', 'avatar'); | |
| image.src = player.avatar; | |
| image.alt = ''; | |
| row.appendChild(image); | |
| } | |
| const name = make('span', 'game-player-name', player.nickname || 'Guest'); | |
| row.appendChild(name); | |
| if (player.is_you) row.appendChild(make('span', 'badge badge-teal', 'you')); | |
| if (player.is_simulated) row.appendChild(make('span', 'badge badge-sim', 'SIM')); | |
| if (player.role_revealed && player.revealed_role) { | |
| row.appendChild(make('span', 'badge badge-pink', player.revealed_role)); | |
| } else { | |
| row.appendChild(make('span', 'alive-indicator', player.alive ? 'IN' : 'OUT')); | |
| } | |
| playerList.appendChild(row); | |
| }); | |
| } | |
| function renderRole(state) { | |
| const role = state.my_role; | |
| roleCard.hidden = !role; | |
| if (!role) return; | |
| roleName.textContent = role.name; | |
| roleDescription.textContent = role.description; | |
| roleInstructions.textContent = role.instructions; | |
| roleCard.dataset.team = role.team; | |
| } | |
| function targetSelect(state, label) { | |
| const group = make('div', 'game-action-group'); | |
| const fieldLabel = make('label', 'label', label); | |
| const select = make('select', 'field'); | |
| select.setAttribute('aria-label', label); | |
| (state.players || []).forEach(function (player) { | |
| if (!player.alive || player.is_you) return; | |
| const option = make('option', '', player.nickname || 'Guest'); | |
| option.value = String(player.user_id); | |
| select.appendChild(option); | |
| }); | |
| group.appendChild(fieldLabel); | |
| group.appendChild(select); | |
| return { group: group, select: select }; | |
| } | |
| function actionButton(text, action, payload) { | |
| const button = make('button', 'btn btn-primary game-action-button', text); | |
| button.type = 'button'; | |
| button.disabled = actionPending; | |
| button.addEventListener('click', function () { submitAction(action, payload ? payload() : {}); }); | |
| return button; | |
| } | |
| function renderActions(state) { | |
| actions.replaceChildren(); | |
| const available = new Set(state.available_actions || []); | |
| if (available.has('investigate')) { | |
| const target = targetSelect(state, 'Choose a guest to investigate'); | |
| actions.appendChild(target.group); | |
| actions.appendChild(actionButton('Read one guest', 'investigate', function () { | |
| return { target_user_id: target.select.value }; | |
| })); | |
| } | |
| if (available.has('begin_vote')) { | |
| actions.appendChild(actionButton('Call the vote', 'begin_vote')); | |
| } | |
| if (available.has('vote')) { | |
| const target = targetSelect(state, 'Who do you accuse?'); | |
| actions.appendChild(target.group); | |
| actions.appendChild(actionButton('Lock my accusation', 'vote', function () { | |
| return { target_user_id: target.select.value }; | |
| })); | |
| } | |
| if (available.has('continue')) { | |
| actions.appendChild(actionButton('Return to the discussion', 'continue')); | |
| } | |
| if (!actions.children.length) { | |
| let message = 'Waiting for the table to move.'; | |
| if (state.phase === 'vote' && state.my_vote) message = 'Your vote is locked. Waiting for the other guests.'; | |
| else if (state.phase === 'finished') message = 'This game has ended. The verdict is above.'; | |
| else if (state.you_alive === false) message = 'You are out of this round. Watch the living guests decide what happens next.'; | |
| actions.appendChild(make('p', 'faint fine-print', message)); | |
| } | |
| } | |
| function renderResult(state) { | |
| const result = (state.phase === 'reveal' || state.phase === 'finished') ? state.last_result : null; | |
| resultBox.hidden = !result; | |
| if (!result) return; | |
| resultBox.replaceChildren(); | |
| resultBox.appendChild(make('h3', '', state.phase === 'finished' ? 'The verdict' : 'The reveal')); | |
| resultBox.appendChild(make('p', '', result.outcome)); | |
| if (result.revealed_user_id && result.revealed_role) { | |
| const target = (state.players || []).find(function (player) { | |
| return Number(player.user_id) === Number(result.revealed_user_id); | |
| }); | |
| if (target) resultBox.appendChild(make('p', 'faint fine-print', target.nickname + ' was ' + result.revealed_role + '.')); | |
| } | |
| const totals = Object.entries(result.vote_totals || {}); | |
| if (totals.length) { | |
| const list = make('ul', 'game-vote-totals'); | |
| totals.forEach(function (entry) { | |
| const player = (state.players || []).find(function (candidate) { | |
| return Number(candidate.user_id) === Number(entry[0]); | |
| }); | |
| list.appendChild(make('li', '', (player ? player.nickname : 'Guest') + ' · ' + entry[1] + ' vote' + (entry[1] === 1 ? '' : 's'))); | |
| }); | |
| resultBox.appendChild(list); | |
| } | |
| } | |
| function render(state) { | |
| currentState = state; | |
| phase.textContent = String(state.phase || 'setup').replaceAll('_', ' '); | |
| round.textContent = state.phase === 'finished' | |
| ? 'THE TABLE HAS SPOKEN' | |
| : 'ROUND ' + (state.round || 1) + (state.phase === 'vote' ? ' · ' + state.votes_submitted + '/' + state.votes_required + ' VOTES' : ''); | |
| prompt.textContent = phaseCopy(state); | |
| renderRole(state); | |
| renderPlayers(state); | |
| privateNotes.replaceChildren(); | |
| (state.investigation_notes || []).forEach(function (note) { | |
| const target = (state.players || []).find(function (player) { | |
| return Number(player.user_id) === Number(note.target_user_id); | |
| }); | |
| privateNotes.appendChild(make( | |
| 'p', | |
| 'private-clue', | |
| 'Private clue · ' + (target ? target.nickname : 'A guest') + ' belongs to the ' + note.team + ' team.' | |
| )); | |
| }); | |
| renderResult(state); | |
| renderActions(state); | |
| if (state.phase === 'finished') panel.dataset.finished = 'true'; | |
| else delete panel.dataset.finished; | |
| } | |
| async function refresh() { | |
| if (refreshPending || document.hidden) return; | |
| refreshPending = true; | |
| try { | |
| const state = await api('/api/room/' + window.ROOM_ID + '/game'); | |
| render(state); | |
| } catch (error) { | |
| feedback.textContent = 'Could not load the game: ' + error.message; | |
| } finally { | |
| refreshPending = false; | |
| } | |
| } | |
| async function submitAction(action, payload) { | |
| if (actionPending) return; | |
| actionPending = true; | |
| feedback.textContent = 'The table is considering your move…'; | |
| renderActions(currentState || { available_actions: [] }); | |
| try { | |
| const result = await api('/api/room/' + window.ROOM_ID + '/game/action', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ action: action, payload: payload }), | |
| }); | |
| feedback.textContent = result.result && result.result.message ? result.result.message : 'Your move was recorded.'; | |
| if (result.result && result.result.private_result) { | |
| const note = result.result.private_result; | |
| const target = (result.state.players || []).find(function (player) { | |
| return Number(player.user_id) === Number(note.target_user_id); | |
| }); | |
| feedback.textContent += ' ' + (target ? target.nickname : 'That guest') + ' is on the ' + note.team + ' team.'; | |
| } | |
| render(result.state); | |
| } catch (error) { | |
| feedback.textContent = error.message; | |
| await refresh(); | |
| } finally { | |
| actionPending = false; | |
| if (currentState) renderActions(currentState); | |
| } | |
| } | |
| document.addEventListener('room:game-updated', refresh); | |
| document.addEventListener('visibilitychange', function () { | |
| if (!document.hidden) refresh(); | |
| }); | |
| window.setInterval(function () { | |
| if (!window.ROOM_WS_CONNECTED && !document.hidden) refresh(); | |
| }, 20000); | |
| refresh(); | |
| })(); | |