Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Commit

Permalink
record saving
Browse files Browse the repository at this point in the history
  • Loading branch information
lochungtin committed May 31, 2021
1 parent 2586a41 commit 6a0ed05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/components/Board/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecordType>
}

class BoardView extends React.Component<ReduxProps> {
Expand All @@ -38,10 +39,17 @@ class BoardView extends React.Component<ReduxProps> {

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 });
}

Expand Down Expand Up @@ -100,6 +108,7 @@ class BoardView extends React.Component<ReduxProps> {

const mapStateToProps = state => ({
game: state.game,
records: state.records,
});

export default connect(mapStateToProps)(BoardView);
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export interface TileColorSchemeType {

// game records
export interface RecordType {
topTile: number,
highestTile: number,
score: number,
}

0 comments on commit 6a0ed05

Please sign in to comment.