Message component work.

master
Ben Burlingham 9 years ago
parent c45a2bdf5a
commit a660ff6b78
  1. 7
      actions/board/grid.actions.js
  2. 17
      actions/board/message.actions.js
  3. 13
      actions/board/scorebar.actions.js
  4. 38
      components/board/board.component.js
  5. 94
      components/board/message.component.js
  6. 4
      index.js
  7. 14
      reducers/board/grid.reducer.js
  8. 29
      reducers/board/message.reducer.js
  9. 5
      reducers/board/muncher.reducer.js
  10. 4
      reducers/board/scorebar.reducer.js
  11. 3
      reducers/welcome/new-game.reducer.js

@ -10,10 +10,9 @@ export const generateValues = (count, level) => ({
level: level level: level
}); });
export const updateValues = (x, y, level) => ({ export const updateValues = (index, value) => ({
type: GRID_ACTION, type: GRID_ACTION,
action: UPDATE, action: UPDATE,
x: x, index: index,
y: y, value: value
level: level
}); });

@ -0,0 +1,17 @@
// Message component actions and action creators.
export const MESSAGE_ACTION = 'MESSAGE_ACTION';
export const DISPLAY = 'DISPLAY';
export const HIDE = 'HIDE';
export const display = (msg1, msg2, hidden) => ({
type: MESSAGE_ACTION,
action: DISPLAY,
message1: msg1,
message2: msg2,
hidden: hidden
});
export const hide = () => ({
type: MESSAGE_ACTION,
action: HIDE
});

@ -1,9 +1,14 @@
// Scorebar component actions and action creators. // Scorebar component actions and action creators.
export const SCOREBAR_ACTION = 'SCOREBAR_ACTION'; export const SCOREBAR_ACTION = 'SCOREBAR_ACTION';
export const MUNCH = 'MUNCH'; export const MUNCH_SUCCEEDED = 'MUNCH_SUCCEEDED';
export const MUNCH_FAILED = 'MUNCH_FAILED';
export const munch = (success) => ({ export const munchSucceeded = () => ({
type: SCOREBAR_ACTION, type: SCOREBAR_ACTION,
action: MUNCH, action: MUNCH_SUCCEEDED,
success: success });
export const munchFailed = () => ({
type: SCOREBAR_ACTION,
action: MUNCH_FAILED
}); });

@ -6,31 +6,51 @@ import { getState } from 'react-redux';
import Scorebar from './scorebar.component'; import Scorebar from './scorebar.component';
import Titlebar from './titlebar.component'; import Titlebar from './titlebar.component';
import Grid from './grid.component'; import Grid from './grid.component';
// import Message from './Message'; import Message from './message.component';
import Muncher from './muncher.component'; import Muncher from './muncher.component';
import Values from '../../reducers/Values';
import { SETTINGS } from '../../App';
import * as GridActions from '../../actions/board/grid.actions'; import * as GridActions from '../../actions/board/grid.actions';
import * as MuncherActions from '../../actions/board/muncher.actions'; import * as MuncherActions from '../../actions/board/muncher.actions';
import * as ScorebarActions from '../../actions/board/scorebar.actions'; import * as ScorebarActions from '../../actions/board/scorebar.actions';
import * as MessageActions from '../../actions/board/message.actions';
const countEmptyStrings = (acc, curr) => { return acc + (curr === ''); }; var exclamations = [
'Congratulations!',
'Yippee!',
'Woohoo!',
'Nice work!',
'Great job!',
'Boom!',
'All finished!',
'Shazam!'
];
export default class Board extends Component { export default class Board extends Component {
munch(x, y) { munch(x, y) {
// Before/after grid validation reveals successful munch.
const pre = this.props.grid.reduce(countEmptyStrings, 0);
this.props.dispatch(GridActions.updateValues(x, y, 0));
const post = this.props.grid.reduce(countEmptyStrings, 0);
this.props.dispatch(ScorebarActions.munch(pre !== post));
this.props.dispatch(MuncherActions.munch()); this.props.dispatch(MuncherActions.munch());
const index = y * SETTINGS.GRID_HEIGHT + x
const valid = Values.validate(this.props.grid[index], 0);
if (valid) {
const msg = exclamations[Math.floor(Math.random() * exclamations.length)];
this.props.dispatch(GridActions.updateValues(index, ''));
this.props.dispatch(ScorebarActions.munchSucceeded());
this.props.dispatch(MessageActions.display(msg, 'Press Spacebar to Continue'));
}
else {
this.props.dispatch(ScorebarActions.munchFailed());
}
}; };
render() { render() {
// <Message />
return (<div className='board'> return (<div className='board'>
<Scorebar /> <Scorebar />
<Titlebar /> <Titlebar />
<Message />
<Grid /> <Grid />
<Muncher munch={this.munch.bind(this)} /> <Muncher munch={this.munch.bind(this)} />
</div>); </div>);

@ -1,9 +1,10 @@
require('../../sass/board/message.scss'); require('../../sass/board/message.scss');
import { Component } from 'react'; import { Component } from 'react';
// var Values = require('./Values'); import { connect } from 'react-redux';
import * as MessageActions from '../../actions/board/message.actions';
var exclamations = [ const exclamations = [
'Congratulations!', 'Congratulations!',
'Yippee!', 'Yippee!',
'Woohoo!', 'Woohoo!',
@ -14,66 +15,63 @@ var exclamations = [
'Shazam!' 'Shazam!'
]; ];
export default class Message extends Component { let listener = null;
// getInitialState() {
// return {
// message1: 'Congratulations!',
// message2: 'Press spacebar to continue.',
// hidden: true
// };
// },
componentDidMount() { export class Message extends Component {
// State.subscribe('level/complete', this.levelComplete); keydown(e) {
// State.subscribe('level/next', this.levelNext); if (e.keyCode === 32) {
// State.subscribe('munch/failed', this.munchFailed); this.props.dispatch(MessageActions.hide());
}
}; };
munchFailed(value) { componentDidMount() {
var self = this; listener = this.keydown.bind(this);
window.addEventListener('keydown', listener);
function keydown(e) {
if (e.keyCode === 32) {
window.removeEventListener('keydown', keydown);
// self.setState({ hidden: true });
}
};
// var msg = Values.getError(value, State.level);
// this.setState({ hidden: false, message1: msg });
window.addEventListener('keydown', keydown);
}; };
levelComplete() { componentWillUnmount() {
function keydown(e) { window.removeEventListener('keydown', listener);
if (e.keyCode === 32) {
window.removeEventListener('keydown', keydown);
// State.publish('level/next');
}
};
var msg = exclamations[Math.floor(Math.random() * exclamations.length)];
// this.setState({ hidden: false, message1: msg });
window.addEventListener('keydown', keydown);
}; };
levelNext() { // munchFailed(value) {
// this.setState({ hidden: true }); // var self = this;
}; //
// // var msg = Values.getError(value, State.level);
// // this.setState({ hidden: false, message1: msg });
// };
//
// levelComplete() {
// function keydown(e) {
// if (e.keyCode === 32) {
// window.removeEventListener('keydown', keydown);
// // State.publish('level/next');
// }
// };
//
// var msg = exclamations[Math.floor(Math.random() * exclamations.length)];
// // this.setState({ hidden: false, message1: msg });
// window.addEventListener('keydown', keydown);
// };
render() { render() {
var classname = ['message', 'hidden']; var classname = ['message'];
const state = { message1: 'foo', message2: 'bar' };
// if (this.state.hidden === true) { if (this.props.hidden === true) {
// classname.push('hidden'); classname.push('hidden');
// } }
return ( return (
<div className={classname.join(' ')}> <div className={classname.join(' ')}>
{state.message1} {this.props.message1}
<br /> <br />
{state.message2} {this.props.message2}
</div> </div>
); );
}; };
}; };
const select = (state) => {
return state.message;
};
export default connect(select)(Message);

@ -10,13 +10,15 @@ import newgameReducer from './reducers/welcome/new-game.reducer';
import gridReducer from './reducers/board/grid.reducer'; import gridReducer from './reducers/board/grid.reducer';
import muncherReducer from './reducers/board/muncher.reducer'; import muncherReducer from './reducers/board/muncher.reducer';
import scorebarReducer from './reducers/board/scorebar.reducer'; import scorebarReducer from './reducers/board/scorebar.reducer';
import messageReducer from './reducers/board/message.reducer';
const reducers = combineReducers({ const reducers = combineReducers({
mode: modeReducer, mode: modeReducer,
newgame: newgameReducer, newgame: newgameReducer,
muncher: muncherReducer, muncher: muncherReducer,
scorebar: scorebarReducer, scorebar: scorebarReducer,
grid: gridReducer grid: gridReducer,
message: messageReducer
}); });
const store = createStore(reducers); const store = createStore(reducers);

@ -1,10 +1,11 @@
const Immutable = require('immutable'); const Immutable = require('immutable');
import * as GridActions from '../../actions/board/grid.actions'; import * as GridActions from '../../actions/board/grid.actions';
import { SETTINGS } from '../../App';
import Values from '../Values'; import Values from '../Values';
const reducer = (state = [], action) => { const initial = [];
const reducer = (state = initial, action) => {
if (action.type !== GridActions.GRID_ACTION) { if (action.type !== GridActions.GRID_ACTION) {
return state; return state;
} }
@ -14,14 +15,7 @@ const reducer = (state = [], action) => {
return Values.generate(action.count, action.level); return Values.generate(action.count, action.level);
case GridActions.UPDATE: case GridActions.UPDATE:
const results = Immutable.List(state); return Immutable.List(state).set(action.index, action.value).toArray();
const index = action.y * SETTINGS.GRID_HEIGHT + action.x;
const valid = Values.validate(state[index], action.level);
if (valid) {
return results.set(index, '').toArray();
}
break;
} }
return state; return state;

@ -0,0 +1,29 @@
const Immutable = require('immutable');
import * as MessageActions from '../../actions/board/message.actions';
const initial = { message1: '', message2: '', hidden: true };
const reducer = (state = initial, action) => {
if (action.type !== MessageActions.MESSAGE_ACTION) {
return state;
}
switch (action.action) {
case MessageActions.DISPLAY:
return Immutable.Map(state)
.set('message1', action.message1)
.set('message2', action.message2)
.set('hidden', false)
.toObject();
case MessageActions.HIDE:
return Immutable.Map(state)
.set('hidden', true)
.toObject();
}
return state;
};
export default reducer;

@ -1,7 +1,9 @@
import * as MuncherActions from '../../actions/board/muncher.actions'; import * as MuncherActions from '../../actions/board/muncher.actions';
import { SETTINGS } from '../../App'; import { SETTINGS } from '../../App';
const reducer = (state = { x: 0, y: 0 }, action) => { const initial = { x: 0, y: 0 };
const reducer = (state = initial, action) => {
if (action.type !== MuncherActions.MUNCHER_ACTION) { if (action.type !== MuncherActions.MUNCHER_ACTION) {
return state; return state;
} }
@ -32,7 +34,6 @@ const reducer = (state = { x: 0, y: 0 }, action) => {
return { x: state.x, y: state.y + 1 }; return { x: state.x, y: state.y + 1 };
case MuncherActions.MUNCH: case MuncherActions.MUNCH:
console.log("Muncher's mouth moved!");
return state; return state;
} }

@ -4,7 +4,9 @@ import * as ScorebarActions from '../../actions/board/scorebar.actions';
import Values from '../Values'; import Values from '../Values';
import { SETTINGS } from '../../App'; import { SETTINGS } from '../../App';
const reducer = (state = { current: 0, high: 999, lives: SETTINGS.LIVES }, action) => { const initial = { current: 0, high: 999, lives: SETTINGS.LIVES };
const reducer = (state = initial, action) => {
if (action.type !== ScorebarActions.SCOREBAR_ACTION) { if (action.type !== ScorebarActions.SCOREBAR_ACTION) {
return state; return state;
} }

@ -1,6 +1,7 @@
import * as NewGameActions from '../../actions/welcome/new-game.actions'; import * as NewGameActions from '../../actions/welcome/new-game.actions';
const reducer = (state = { hidden: false }, action) => { const initial = { hidden: false };
const reducer = (state = initial, action) => {
if (action.type !== NewGameActions.NEWGAME_ACTION) { if (action.type !== NewGameActions.NEWGAME_ACTION) {
return state; return state;
} }

Loading…
Cancel
Save