Skip to content

Commit

Permalink
Split app into multiple files and add routing
Browse files Browse the repository at this point in the history
Needed to allow for multiple pages in same app.
  • Loading branch information
follesoe committed Apr 12, 2020
1 parent 10a767c commit eeab93e
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 93 deletions.
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@testing-library/user-event": "^7.2.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}
</style>
</head>
<body class="background-map">
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
Expand Down
97 changes: 9 additions & 88 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import BattleGoalsDeck from './BattleGoalsDeck';
import Card from './Card';
import './App.css';

class App extends React.Component {
class ItemsApp extends React.Component {
constructor(props) {
super(props);
this.state = { itemsDeckTo: 14 };
Expand All @@ -13,77 +15,20 @@ class App extends React.Component {
}
render() {
return (
<div className="App">
<header className="m-8">
<div class="background-map h-screen">
<header>
<label>
Available items:
Available items:
<input type="number" min="1" max="164" value={this.state.itemsDeckTo} onChange={this.handleChange} />
</label>
</header>
<ItemsDeck to={this.state.itemsDeckTo} />
<BattleGoals />
<BattleGoalsDeck />
</div>
);
}
}

class BattleGoals extends React.Component {
constructor(props) {
super(props);
this.state = {
deck: [],
shuffledDeck: [],
drawnDeck: []
};
this.drawCard = this.drawCard.bind(this);
}
componentDidMount() {
fetch(process.env.PUBLIC_URL + '/gloomhaven/data/battle-goals.js')
.then(res => res.json())
.then(
(result) => {
const deck = result.filter(c => c.name !== 'battlegoal-back');
this.setState({
deck: deck,
shuffledDeck: shuffleDeck(deck),
drawnDeck: []
});
},
(error) => {
console.error(error);
}
)
}
drawCard() {
const shuffledDeck = this.state.shuffledDeck.length > 0 ?
this.state.shuffledDeck : shuffleDeck(this.state.deck);

const drawnDeck = this.state.shuffledDeck.length > 0 ?
this.state.drawnDeck : [];

this.setState({
deck: this.state.deck,
drawnDeck: [shuffledDeck[0], ...drawnDeck],
shuffledDeck: [...shuffledDeck.slice(1)]
});
}
render() {
const drawnCards = this.state.drawnDeck.map((card) =>
<Card key={card.name} image={process.env.PUBLIC_URL + '/gloomhaven/images/' + card.image } name={card.name} />
);

return (
<div className="flex flex-wrap flex-row m-6">
<Card
name="Draw Battle Card"
onClick={this.drawCard}
image={process.env.PUBLIC_URL + '/gloomhaven/images/battle-goals/battlegoal-back.png' } />
{drawnCards}
</div>
)
}
}

class ItemsDeck extends React.Component {
constructor(props) {
super(props);
Expand All @@ -105,7 +50,7 @@ class ItemsDeck extends React.Component {
image: process.env.PUBLIC_URL + '/gloomhaven/images/' + card.image
}
});
this.setState({
this.setState({
deck: cards
});
},
Expand All @@ -126,28 +71,4 @@ class ItemsDeck extends React.Component {
}
}

function Card(props) {
function handleClick(e) {
e.preventDefault();
if (props.onClick) {
props.onClick(props.name);
}
}
return (
<div className="m-2 Card" onClick={handleClick}>
<img className="Card-face" src={props.image} alt={props.name} />
</div>
);
}

// Fisher–Yates shuffle
function shuffleDeck(deck) {
var newDeck = [...deck];
for (let i = newDeck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newDeck[i], newDeck[j]] = [newDeck[j], newDeck[i]];
}
return newDeck;
}

export default App;
export default ItemsApp;
17 changes: 17 additions & 0 deletions src/AttackModifierDeck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import BattleGoalsDeck from './BattleGoalsDeck';

class AttackModifierDeck extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div className="bg-greenscreen h-screen">
<BattleGoalsDeck />
</div>
)
}
}

export default AttackModifierDeck;
72 changes: 72 additions & 0 deletions src/BattleGoalsDeck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';
import Card from './Card';
import './App.css';

class BattleGoalsDeck extends React.Component {
constructor(props) {
super(props);
this.state = {
deck: [],
shuffledDeck: [],
drawnDeck: []
};
this.drawCard = this.drawCard.bind(this);
}
componentDidMount() {
fetch(process.env.PUBLIC_URL + '/gloomhaven/data/battle-goals.js')
.then(res => res.json())
.then(
(result) => {
const deck = result.filter(c => c.name !== 'battlegoal-back');
this.setState({
deck: deck,
shuffledDeck: shuffleDeck(deck),
drawnDeck: []
});
},
(error) => {
console.error(error);
}
)
}
drawCard() {
const shuffledDeck = this.state.shuffledDeck.length > 0 ?
this.state.shuffledDeck : shuffleDeck(this.state.deck);

const drawnDeck = this.state.shuffledDeck.length > 0 ?
this.state.drawnDeck : [];

this.setState({
deck: this.state.deck,
drawnDeck: [shuffledDeck[0], ...drawnDeck],
shuffledDeck: [...shuffledDeck.slice(1)]
});
}
render() {
const drawnCards = this.state.drawnDeck.map((card) =>
<Card key={card.name} image={process.env.PUBLIC_URL + '/gloomhaven/images/' + card.image } name={card.name} />
);

return (
<div className="flex flex-wrap flex-row">
<Card
name="Draw Battle Card"
onClick={this.drawCard}
image={process.env.PUBLIC_URL + '/gloomhaven/images/battle-goals/battlegoal-back.png' } />
{drawnCards}
</div>
)
}
}

// Fisher–Yates shuffle
function shuffleDeck(deck) {
var newDeck = [...deck];
for (let i = newDeck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newDeck[i], newDeck[j]] = [newDeck[j], newDeck[i]];
}
return newDeck;
}

export default BattleGoalsDeck;
Loading

0 comments on commit eeab93e

Please sign in to comment.