should comp updates

This commit is contained in:
Mashy 2019-10-31 11:50:27 +10:00
parent d52efc6c01
commit b554464802
5 changed files with 138 additions and 118 deletions

View File

@ -1,34 +1,25 @@
const preact = require('preact'); const preact = require('preact');
const { connect } = require('preact-redux'); const { connect } = require('preact-redux');
const actions = require('../actions');
const { ConstructAvatar } = require('./construct'); const { ConstructAvatar } = require('./construct');
const Controls = require('./controls');
const addState = connect( const addState = connect(
function receiveState(state) { function receiveState(state) {
const { const {
ws,
instance, instance,
account, account,
} = state; } = state;
function sendInstanceReady() {
return ws.sendInstanceReady(instance.id);
}
return { return {
instance, instance,
account, account,
sendInstanceReady,
}; };
}, }
); );
function FaceoffConstruct(args) { function FaceoffConstruct(args) {
const { const {
construct construct,
} = args; } = args;
return ( return (
@ -39,75 +30,82 @@ function FaceoffConstruct(args) {
<ConstructAvatar construct={construct} /> <ConstructAvatar construct={construct} />
</div> </div>
</div> </div>
)
}
function Faceoff(props) {
const {
instance,
account,
sendInstanceReady,
} = props;
if (!instance) return <div>...</div>;
const otherTeam = instance.players.find(t => t.id !== account.id);
const playerTeam = instance.players.find(t => t.id === account.id);
function PlayerTeam(team) {
const constructs = team.constructs.map((c, i) =>
<FaceoffConstruct key={c.id} construct={c}/>);
const winner = instance.winner === team.id;
const classes = `team player ${winner ? 'winner' : team.ready ? 'ready' : ''}`
return (
<div class={classes}>
{constructs}
</div>
);
}
function OpponentTeam(team) {
const constructs = team.constructs.map((c, i) =>
<FaceoffConstruct key={c.id} construct={c}/>);
const winner = instance.winner === team.id;
const classes = `team opponent ${winner ? 'winner' : team.ready ? 'ready' : ''}`
return (
<div class={classes}>
{constructs}
</div>
);
}
function faceoffText() {
if (!instance.winner) {
return (
<div class="faceoff-text">
<div class="opponent-text"> {otherTeam.name} </div>
<div class="vs"> vs </div>
<div class="player-text"> {playerTeam.name} </div>
</div>
);
}
const winner = instance.winner === playerTeam.id ? playerTeam : otherTeam;
return (
<div class="faceoff-text winner">
<div> {winner.name} </div>
<div> wins </div>
</div>
)
}
return (
<main class="faceoff">
{OpponentTeam(otherTeam)}
{faceoffText()}
{PlayerTeam(playerTeam)}
</main>
); );
} }
class Faceoff extends preact.Component {
shouldComponentUpdate(newProps) {
if (newProps.instance !== this.props.instance) return true;
if (newProps.account !== this.props.account) return true;
return false;
}
render(props) {
const {
instance,
account,
} = props;
if (!instance) return <div>...</div>;
const otherTeam = instance.players.find(t => t.id !== account.id);
const playerTeam = instance.players.find(t => t.id === account.id);
function PlayerTeam(team) {
const constructs = team.constructs.map((c, i) =>
<FaceoffConstruct key={c.id} construct={c}/>);
const winner = instance.winner === team.id;
const classes = `team player ${winner ? 'winner' : team.ready ? 'ready' : ''}`;
return (
<div class={classes}>
{constructs}
</div>
);
}
function OpponentTeam(team) {
const constructs = team.constructs.map((c, i) =>
<FaceoffConstruct key={c.id} construct={c}/>);
const winner = instance.winner === team.id;
const classes = `team opponent ${winner ? 'winner' : team.ready ? 'ready' : ''}`;
return (
<div class={classes}>
{constructs}
</div>
);
}
function faceoffText() {
if (!instance.winner) {
return (
<div class="faceoff-text">
<div class="opponent-text"> {otherTeam.name} </div>
<div class="vs"> vs </div>
<div class="player-text"> {playerTeam.name} </div>
</div>
);
}
const winner = instance.winner === playerTeam.id ? playerTeam : otherTeam;
return (
<div class="faceoff-text winner">
<div> {winner.name} </div>
<div> wins </div>
</div>
)
}
return (
<main class="faceoff">
{OpponentTeam(otherTeam)}
{faceoffText()}
{PlayerTeam(playerTeam)}
</main>
);
}
}
module.exports = addState(Faceoff); module.exports = addState(Faceoff);

View File

@ -3,7 +3,7 @@ const reactStringReplace = require('react-string-replace');
const specThresholds = require('./info.thresholds'); const specThresholds = require('./info.thresholds');
const { INFO } = require('./../constants'); const { INFO } = require('./../constants');
const { convertItem, removeTier, formatInfo } = require('../utils'); const { convertItem, removeTier } = require('../utils');
const { tutorialStage } = require('../tutorial.utils'); const { tutorialStage } = require('../tutorial.utils');
const shapes = require('./shapes'); const shapes = require('./shapes');
@ -75,8 +75,8 @@ class InfoComponent extends preact.Component {
}; };
if (isSkill || isSpec) { if (isSkill || isSpec) {
let infoText = info; let infoName = info;
while (infoText.includes('Plus')) infoText = infoText.replace('Plus', '+'); while (infoName.includes('Plus')) infoName = infoName.replace('Plus', '+');
const header = isSkill ? <h3> SKILL </h3> : <h3> SPEC </h3>; const header = isSkill ? <h3> SKILL </h3> : <h3> SPEC </h3>;
@ -101,7 +101,7 @@ class InfoComponent extends preact.Component {
return ( return (
<div class={isSkill ? 'info-skill' : 'info-spec'}> <div class={isSkill ? 'info-skill' : 'info-spec'}>
<h2>{infoText} {fullInfo.cost}b</h2> <h2>{infoName} {fullInfo.cost}b</h2>
{header} {header}
{itemSourceInfo} {itemSourceInfo}
{cooldown} {cooldown}

View File

@ -13,11 +13,13 @@ const actions = require('../actions');
const addState = connect( const addState = connect(
function receiveState(state) { function receiveState(state) {
const { instance, const {
instance,
nav, nav,
navInstance, navInstance,
} = state; } = state;
return { instance, return {
instance,
nav, nav,
navInstance, navInstance,
}; };
@ -52,7 +54,7 @@ const addState = connect(
); );
class Instance extends Component { class Instance extends Component {
componentShouldUpdate(newProps) { shouldComponentUpdate(newProps) {
if (newProps.instance !== this.props.instance) return true; if (newProps.instance !== this.props.instance) return true;
return false; return false;
} }

View File

@ -16,35 +16,45 @@ const addState = connect(
} }
); );
function Main(props) { class Main extends preact.Component {
const { shouldComponentUpdate(newProps) {
game, if (newProps.game !== this.props.game) return true;
instance, if (newProps.instance !== this.props.instance) return true;
account, if (newProps.account !== this.props.account) return true;
nav, if (newProps.nav !== this.props.nav) return true;
} = props; return false;
if (!account) {
return <Welcome />;
} }
if (game) { render(props) {
return <Game />; const {
game,
instance,
account,
nav,
} = props;
if (!account) {
return <Welcome />;
}
if (game) {
return <Game />;
}
if (instance) {
return <Instance />;
}
if (nav === 'transition') return false;
return (
<main class="menu">
<Header />
<Top />
<Bottom />
</main>
);
} }
if (instance) {
return <Instance />;
}
if (nav === 'transition') return false;
return (
<main class="menu">
<Header />
<Top />
<Bottom />
</main>
);
} }
module.exports = addState(Main); module.exports = addState(Main);

View File

@ -10,11 +10,21 @@ const addState = connect(
state => ({ showNav: state.showNav }) state => ({ showNav: state.showNav })
); );
const Mnml = ({ showNav }) => class Mnml extends preact.Component {
<div id="mnml" class={showNav ? 'nav-visible' : ''}> shouldComponentUpdate(newProps) {
<Main /> if (newProps.showNav !== this.props.showNav) return true;
<Controls /> return false;
<Footer /> }
</div>;
render(args) {
return (
<div id="mnml" class={args.showNav ? 'nav-visible' : ''}>
<Main />
<Controls />
<Footer />
</div>
);
}
}
module.exports = addState(Mnml); module.exports = addState(Mnml);