Skip to content

Commit

Permalink
got basic functions back together and more streamlined
Browse files Browse the repository at this point in the history
  • Loading branch information
cmavelis committed Mar 11, 2019
1 parent cafbd61 commit 3bc5c92
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 193 deletions.
66 changes: 31 additions & 35 deletions src/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,39 @@ class Game extends React.Component {
}

componentDidMount() {
this.getWordData();
console.log('game mounted');
const { wordFiles, setSeed } = this.props;
Math.seedrandom(Date.now());
const randomAdjective = this.getRandomWord(wordFiles.seedAdjectives);
const randomNoun = this.getRandomWord(wordFiles.seedNouns);
const newSeed = `${randomAdjective} ${randomNoun}`;
setSeed(newSeed);
this.seedNewGame(newSeed);
}

componentDidUpdate() {
const { wordFiles, randomSeed } = this.state;

if (!randomSeed) {
if (!Object.entries(wordFiles).some(obj => obj[1].isLoading)) {
Math.seedrandom(Date.now());
const randomAdjective = this.getRandomWord(wordFiles.seedAdjectives);
const randomNoun = this.getRandomWord(wordFiles.seedNouns);
const newSeed = `${randomAdjective} ${randomNoun}`;
this.setState(
{ randomSeed: newSeed },
);
this.seedNewGame(newSeed);
}
componentDidUpdate(prevProps) {
const { randomSeed } = this.props;
console.log('game updated');
if (prevProps.randomSeed !== randomSeed) {
this.seedNewGame(randomSeed);
}
}


// const { randomSeed } = this.props;
// if (randomSeed) { this.seedNewGame(randomSeed); }

// if (!randomSeed) {
// if (!Object.entries(wordFiles).some(obj => obj[1].isLoading)) {
// Math.seedrandom(Date.now());
// const randomAdjective = this.getRandomWord(wordFiles.seedAdjectives);
// const randomNoun = this.getRandomWord(wordFiles.seedNouns);
// const newSeed = `${randomAdjective} ${randomNoun}`;
// this.setState(
// { randomSeed: newSeed },
// );
// this.seedNewGame(newSeed);
// }
// }
}

showModal = (cardID) => {
this.setState({
Expand All @@ -66,21 +78,14 @@ class Game extends React.Component {
this.setState({ modalShown: false, cardClicked: undefined });
};

handleInputChange = (e) => {
const { name, value } = e.target;
const newSeed = value.toLowerCase();
this.setState({ [name]: newSeed });
this.seedNewGame(newSeed);
};

getRandomWord = (wordObject) => {
const { wordList, listLength } = wordObject;
return wordList[Math.floor(Math.random() * listLength)];
};

seedNewGame = (newSeed) => {
let randomSeed = newSeed;
const { wordFiles } = this.state;
const { wordFiles } = this.props;
const { wordList, listLength } = wordFiles.cardsClassic;
const today = new Date();
const todayValue = today.getUTCFullYear().toString()
Expand Down Expand Up @@ -121,14 +126,7 @@ class Game extends React.Component {
words: wordsSelected,
leaderMode: false,
});
this.updateBoard(newCardColors);
};

toggleHeaderHide = () => {
const { headerIsHidden } = this.state;
this.setState({
headerIsHidden: !headerIsHidden,
});
// this.updateBoard(newCardColors);
};

updateBoard = () => {
Expand Down Expand Up @@ -185,8 +183,6 @@ class Game extends React.Component {
modalShown,
cardClicked,
counts,
randomSeed,
headerIsHidden,
cardLeaderMarks,
} = this.state;

Expand Down
154 changes: 0 additions & 154 deletions src/GameContext.js

This file was deleted.

126 changes: 126 additions & 0 deletions src/GameLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import React from 'react';
import axios from 'axios';
import Game from './Game';
import SetupMenu from './components/SetupMenu';
import './GameContext.css';


// const loadedGame = (props) => {
// const { wordFiles } = props;
// if (!Object.entries(wordFiles).some(obj => obj[1].isLoading) {
// return (
// <Game />
// )
// }
// }

class GameLoader extends React.Component {
constructor(props) {
super(props);
this.setSeed = this.setSeed.bind(this);
this.state = {
randomSeed: null,
headerIsHidden: false,
leaderMode: false,
wordFiles: {
cardsClassic: {
fileName: 'words_classic.csv',
isLoading: true,
},
cardsSimple: {
fileName: 'words_simple.csv',
isLoading: true,
},
seedAdjectives: {
fileName: 'seed_adjectives.csv',
isLoading: true,
},
seedNouns: {
fileName: 'seed_nouns.csv',
isLoading: true,
},
},
};
}

componentDidMount() {
this.getWordData();
}

getWordData = () => {
const { wordFiles } = this.state;
Object.entries(wordFiles)
.forEach(([objTitle, objContent]) => {
const { fileName } = objContent;
axios.get(fileName)
.then((wordsReceived) => {
const wordList = wordsReceived.data.split(/\r?\n/);
const listLength = wordList.length - 1;
return {
wordList,
listLength,
isLoading: false,
};
})
.then((wordData) => {
this.setState(prevState => ({
wordFiles: {
...prevState.wordFiles,
[objTitle]:
wordData,
},
}));
})
.catch(error => this.setState({ error }));
});
};

setSeed = (newSeed) => {
this.setState({ randomSeed: newSeed.toLowerCase() });
};

handleInputChange = (e) => {
const { value } = e.target;
this.setSeed(value);
};

toggleHeaderHide = () => {
this.setState(prevState => ({ headerIsHidden: !prevState.headerIsHidden }));
};

render() {
const {
randomSeed,
headerIsHidden,
wordFiles,
} = this.state;

return (
<div>
{Object.entries(wordFiles).some(obj => obj[1].isLoading)
? (
<p> Loading... </p>
)
: (
<div>
<SetupMenu
randomSeed={randomSeed}
headerIsHidden={headerIsHidden}
toggleHeaderHide={this.toggleHeaderHide}
handleInputChange={this.handleInputChange}
showModal={this.showModal}
/>
<Game
randomSeed={randomSeed}
wordFiles={wordFiles}
setSeed={this.setSeed}
/>
</div>
)
}
</div>
);
}
}

export default GameLoader;
Loading

0 comments on commit 3bc5c92

Please sign in to comment.