diff --git a/src/components/Board/index.tsx b/src/components/Board/index.tsx index 14d6da1..14c4eb2 100644 --- a/src/components/Board/index.tsx +++ b/src/components/Board/index.tsx @@ -11,13 +11,14 @@ import { BoardStyles } from './styles'; import Board from '../../game/board'; import { store } from '../../redux/store'; -import { updateGame, updateHistory } from '../../redux/action'; +import { updateGame, updateHistory, updateRecords } from '../../redux/action'; import { keygen } from '../../utils/keygen'; import { Direction } from '../../utils/enums'; -import { GameConfig } from '../../utils/types'; +import { GameConfig, RecordType } from '../../utils/types'; interface ReduxProps { - game: GameConfig + game: GameConfig, + records: Array } class BoardView extends React.Component { @@ -38,10 +39,17 @@ class BoardView extends React.Component { onNewGame = () => { store.dispatch(updateGame({ ...new Board(4) })); + store.dispatch(updateHistory(null)); this.setState({ open: false }); } onSaveGame = () => { + let records = [...this.props.records, { highestTile: this.getHighestTile(), score: this.props.game.score }]; + records.sort((a, b) => (b.highestTile + b.score) - (a.highestTile + a.score)); + + store.dispatch(updateRecords(records)); + store.dispatch(updateGame({ ...new Board(4) })); + store.dispatch(updateHistory(null)); this.setState({ open: false }); } @@ -100,6 +108,7 @@ class BoardView extends React.Component { const mapStateToProps = state => ({ game: state.game, + records: state.records, }); export default connect(mapStateToProps)(BoardView); diff --git a/src/utils/types.ts b/src/utils/types.ts index 6037739..4a97959 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -48,6 +48,6 @@ export interface TileColorSchemeType { // game records export interface RecordType { - topTile: number, + highestTile: number, score: number, }