Skip to content

Commit

Permalink
Working game stream!
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo committed Jan 7, 2024
1 parent 9799df2 commit e0693db
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
8 changes: 1 addition & 7 deletions web/src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ export const OnlineActions = (state, actions) => ({
state.ground.set({ fen: state.chess.fen() })

actions.lightBoard(true)
playOtherSide(state.chess, state.ground)(orig, dest)
m.redraw()
// send to lichess api
let move_uci = uci(move)
let body = state.auth.fetchBody('https://lichess.org/api/board/game/' +
let body = state.auth.fetchBody('/api/board/game/' +
m.route.param('id') +
'/move/' +
move_uci,
Expand All @@ -111,9 +110,4 @@ export const OnlineActions = (state, actions) => ({
})
console.log('played move', move_uci, body)
},
afterInit: () => {
console.log('initing online actions')
actions.afterInit()
actions.streamGame()
},
})
48 changes: 19 additions & 29 deletions web/src/Games.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Chessground } from 'chessground'
import { Board } from './Board'
import { Toolbar, OnlineToolbar } from './Toolbar'
import '../node_modules/material-design-icons-iconfont/dist/material-design-icons.css'
import { toDests, toColor, playOtherSide } from './utils'
import { toDests } from './utils'
import { NOTE_ON, CONTROL_CHANGE, COLORS } from './Launchpad'

export const GamesActions = (state, actions) => ({
Expand Down Expand Up @@ -37,16 +37,7 @@ export const GamesActions = (state, actions) => ({
break
}
})
},
streamGames: () => {
streamJson(LICHESS_API_URL + 'stream/event', state.user.token, res => {
console.log('new lichess event', res)
if (res.type == 'gameStart' && state.games().length == 0) {
console.log('auto starting game')
actions.getGames()
}
})
},
}
})

export const GameThumb = (state, game) =>
Expand Down Expand Up @@ -85,21 +76,20 @@ export const Games = (state, actions) => {
console.log('one active game. loading now!')
state.game = games[0]
m.route.set('/online', { id: state.game.gameId })
} else if (games.length == 0) {
actions.streamGames()
}

listener = state.input.addListener('noteon', 'all', message => {
console.log('connector got message', message)
let n = actions.launchToN(message.data[1])
/* n is 0-63, but represented bottom to top.
We need to invert the row to make it read top to bottom, left to right. */
let g = (n % 8) + (7 - Math.floor(n / 8)) * 8
console.log('selected game', g)
if (g < games.length) {
m.route.set('/online', { id: games[g].gameId })
}
})
if (state.input) {
listener = state.input.addListener('noteon', 'all', message => {
console.log('connector got message', message)
let n = actions.launchToN(message.data[1])
/* n is 0-63, but represented bottom to top.
We need to invert the row to make it read top to bottom, left to right. */
let g = (n % 8) + (7 - Math.floor(n / 8)) * 8
console.log('selected game', g)
if (g < games.length) {
m.route.set('/online', { id: games[g].gameId })
}
})
}
games.map((g, i) => {
let note = g.isMyTurn ? NOTE_ON | 2 : NOTE_ON
/* Invert the row to get buttons to go top to bottom. */
Expand Down Expand Up @@ -155,7 +145,7 @@ export const Game = (state, actions) =>
m('.board', {
oninit: vnode => {
console.log('game loading', vnode.attrs, state.chess.ascii())
actions.afterInit()
actions.streamGame()
},
oncreate: vnode => {
state.ground = Chessground(vnode.dom, {
Expand Down Expand Up @@ -195,7 +185,7 @@ export const GamePage = (state, actions) => ({
m('.gamePage', {}, [Toolbar(state, actions), Game(state, actions)]),
})

export const Player = () => m('.me', {}, state.user.username)
export const Player = (state) => m('.me', {}, state.user.username)
export const Opponent = state =>
state.opponent ? m('.opponent', {}, JSON.stringify(state.opponent)) : null

Expand All @@ -212,13 +202,13 @@ export const GamePageOnline = (state, actions) => ({
m(
'.top_user',
{},
state.invert() != (state.color == 'b') ? Player() : Opponent(state)
state.invert() != (state.color == 'b') ? Player(state) : Opponent(state)
),
Game(state, actions),
m(
'.bottom_user',
{},
state.invert() != (state.color == 'b') ? Opponent(state) : Player()
state.invert() != (state.color == 'b') ? Opponent(state) : Player(state)
),
]),
})
11 changes: 8 additions & 3 deletions web/src/LaunchGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export const LaunchGame = (state, actions) => ({
actions.lightBoard()
},
streamGame: () => {
if (!state.loggedIn) {
m.route.set('/login')
}
console.log('streaming game', m.route.param('id'))

state.auth.openStream('/api/board/game/stream/' + m.route.param('id'), {}, v => {
console.log('calling back', v)
if (v.type == 'gameFull') {
Expand All @@ -216,9 +221,9 @@ export const LaunchGame = (state, actions) => ({
console.log('loading game', v.state.moves)
console.log(
'loaded?',
state.chess.load_pgn(v.state.moves, { sloppy: true })
state.chess.loadPgn(v.state.moves, { sloppy: true })
)
if (v.black.id == User.profile.id && !state.invert()) {
if (v.black.id == state.user.profile.id && !state.invert()) {
// if playing black, and not already inverted, flip board
actions.flipBoard()
state.color = 'b'
Expand All @@ -229,7 +234,7 @@ export const LaunchGame = (state, actions) => ({
console.log('move played', v.moves)
console.log(
'loaded?',
state.chess.load_pgn(v.moves, { sloppy: true })
state.chess.loadPgn(v.moves, { sloppy: true })
)
}
let turn = state.chess.turn() == 'w' ? 'white' : 'black'
Expand Down
3 changes: 1 addition & 2 deletions web/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Api, Color, Key } from 'chessground'
import { getPieceLocations, SQUARES } from './ChessMaths'
import { SQUARES } from './ChessMaths'
import { Graph, astar } from 'javascript-astar'

export function toDests(chess) {
Expand Down

0 comments on commit e0693db

Please sign in to comment.