Skip to content

Commit

Permalink
Moves HistoryService to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo committed Feb 4, 2024
1 parent aead051 commit 5ff9d28
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 81 deletions.
48 changes: 2 additions & 46 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { Chess } from 'chess.js'
import { State } from './State'
import { Game } from './Game.ts'
import { Toolbar } from './meiosis.ts'
import { toDests } from './utils'

export const DEFAULT_POSITION =
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
import { HistoryService } from './Services'

export const state: State = JSON.parse(
localStorage.getItem('launchchess') || '{}'
Expand Down Expand Up @@ -39,48 +36,7 @@ export const App: MeiosisViewComponent<State> = {
},

services: [
{
onchange: (state: State) => state.historyIndex,
run: cell => {
console.log('historyIndex', cell.state.historyIndex)
if (cell.state.historyIndex < 0) {
cell.update({ historyIndex: 0 })
return
}
if (cell.state.historyIndex > cell.state.history.length) {
cell.update({ historyIndex: cell.state.history.length })
return
}
// if (cell.state.historyIndex != cell.state.history.length) {
let move = cell.state.chess.history({ verbose: true })[
cell.state.historyIndex - 1
]
window.ground?.set({
fen: move ? move.after : DEFAULT_POSITION,
lastMove: move ? [move.from, move.to] : undefined,
turnColor: move?.color == 'w' ? 'white' : 'black',
})
if (cell.state.historyIndex == cell.state.history.length) {
// Back to current game state. Set movable color to current turn
window.ground?.set({
turnColor: cell.state.chess.turn() == 'w' ? 'white' : 'black',
movable: {
color: cell.state.chess.turn() == 'w' ? 'white' : 'black',
dests: toDests(cell.state.chess),
},
})
} else {
// Disable moving pieces while viewing history
// TODO: Implement variation navigation
window.ground?.set({
movable: {
color: undefined,
dests: {},
},
})
}
},
},
HistoryService,
// },
],

Expand Down
1 change: 1 addition & 0 deletions src/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const Board = cell =>
highlight: {
check: true,
},
check: cell.state.chess.inCheck(),
events: {
move: (orig, dest) => {
let state = cell.getState()
Expand Down
50 changes: 50 additions & 0 deletions src/Services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { State } from './State'
import { toDests } from './utils'

export const DEFAULT_POSITION =
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

export const HistoryService = {
// Watch for changes to historyIndex and update the board
onchange: (state: State) => state.historyIndex,
run: cell => {
console.log('historyIndex', cell.state.historyIndex)
if (cell.state.historyIndex < 0) {
cell.update({ historyIndex: 0 })
return
}
if (cell.state.historyIndex > cell.state.history.length) {
cell.update({ historyIndex: cell.state.history.length })
return
}
let move = cell.state.chess.history({ verbose: true })[
cell.state.historyIndex - 1
]
window.ground?.set({
fen: move ? move.after : DEFAULT_POSITION,
lastMove: move ? [move.from, move.to] : undefined,
turnColor: move?.color == 'w' ? 'white' : 'black',
check: cell.state.chess.inCheck(),
})
if (cell.state.historyIndex == cell.state.history.length) {
// Back to current game state. Set movable color to current turn
window.ground?.set({
turnColor: cell.state.chess.turn() == 'w' ? 'white' : 'black',
movable: {
color: cell.state.chess.turn() == 'w' ? 'white' : 'black',
dests: toDests(cell.state.chess),
},
check: cell.state.chess.inCheck(),
})
} else {
// Disable moving pieces while viewing history
// TODO: Implement variation navigation
window.ground?.set({
movable: {
color: undefined,
dests: {},
},
})
}
},
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ let cell = cells()
// Handle left and right arrow key presses
document.removeEventListener('keydown', cell.state.onkeydown)
document.addEventListener('keydown', function (event) {
switch(event.key) {
switch (event.key) {
case 'ArrowLeft':
cell.update({ historyIndex: cell.getState().historyIndex - 1 })
event.preventDefault()
break
case 'ArrowRight':
cell.update({ historyIndex: cell.getState().historyIndex + 1 })
event.preventDefault()
break
case 'ArrowUp':
cell.update({ historyIndex: 0 })
event.preventDefault()
break
case 'ArrowDown':
cell.update({ historyIndex: cell.getState().history.length })
event.preventDefault()
break
}
})


// Debug

meiosisTracer({
Expand Down
2 changes: 1 addition & 1 deletion src/meiosis.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:root {
--primary: #7b00ff;
--secondary: #6c757d;
--highlight: rgb(149, 149, 149);
--highlight: #adadad;
--success: #28a745;
--info: #17a2b8;
--warning: #ffc107;
Expand Down
65 changes: 33 additions & 32 deletions src/meiosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import '../node_modules/chessground/assets/chessground.brown.css'
import '../node_modules/chessground/assets/chessground.cburnett.css'

import '@fortawesome/fontawesome-free/css/all.css'
import { notify } from 'alertifyjs'
import alertify, { notify } from 'alertifyjs'
import 'alertifyjs/build/css/alertify.css'


import 'alertifyjs/build/css/themes/semantic.css'
alertify.defaults.transition = 'zoom'
// alertify.defaults.theme.ok = 'ui positive button'
// alertify.defaults.theme.cancel = 'ui black button'
export const Toolbar = ({ state, update }) =>
m('#toolbar.component', {}, [
m('', {}, state.page),
Expand All @@ -24,36 +26,35 @@ export const Toolbar = ({ state, update }) =>
),
])


export const History = cell =>
m(
'table.history',
{},
Array.from(
{ length: Math.ceil(cell.state.history?.length / 2) },
(_, i) => i
).map(index =>
m('tr', {}, [
m('td.move-number', {}, index + 1),
m(
'td.move',
{
class:
2 * index + 1 === cell.state.historyIndex ? 'highlight' : '',
},
cell.state.history[2 * index]
),
m(
'td.move',
{
class:
2 * index + 2 === cell.state.historyIndex ? 'highlight' : '',
},
cell.state.history[2 * index + 1]
),
])
)
export const History = cell =>
m(
'table.history',
{},
Array.from(
{ length: Math.ceil(cell.state.history?.length / 2) },
(_, i) => i
).map(index =>
m('tr', {}, [
m('td.move-number', {}, index + 1),
m(
'td.move',
{
class: 2 * index + 1 === cell.state.historyIndex ? 'highlight' : '',
onclick: () => cell.update({ historyIndex: 2 * index + 1 }),
},
cell.state.history[2 * index]
),
m(
'td.move',
{
class: 2 * index + 2 === cell.state.historyIndex ? 'highlight' : '',
onclick: () => cell.update({ historyIndex: 2 * index + 2 }),
},
cell.state.history[2 * index + 1]
),
])
)
)

export const Menu = cell =>
m('#toolbar.component', {}, [
Expand Down

0 comments on commit 5ff9d28

Please sign in to comment.