ensl_gathers/lib/react/gather.jsx

665 lines
15 KiB
React
Raw Normal View History

2015-07-28 15:54:29 +00:00
"use strict";
var VoteButton = React.createClass({
2015-08-11 00:19:04 +00:00
cancelVote(e) {
2015-07-28 15:54:29 +00:00
socket.emit("gather:vote", {
leader: {
candidate: null
}
});
},
2015-08-11 00:19:04 +00:00
vote(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
socket.emit("gather:vote", {
leader: {
candidate: parseInt(e.target.value, 10)
}
});
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 15:54:29 +00:00
if (this.props.currentGatherer === null) {
return false;
}
if (this.props.currentGatherer.leaderVote === this.props.candidate.id) {
return (
<button
onClick={this.cancelVote}
className="btn btn-xs btn-success">Voted
</button>
);
} else {
return (
<button
onClick={this.vote}
className="btn btn-xs btn-default"
value={this.props.candidate.id}>Vote
</button>
);
}
}
});
var SelectPlayerButton = React.createClass({
2015-08-11 00:19:04 +00:00
selectPlayer(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2015-07-28 23:32:50 +00:00
socket.emit("gather:select", {
player: parseInt(e.target.value, 10)
})
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 23:32:50 +00:00
if (this.props.gatherer.leader) {
return (<button
className="btn btn-xs btn-default"
data-disabled="true">Leader</button>);
2015-08-08 17:13:17 +00:00
} else if (this.props.gatherer.team !== "lobby") {
return (<button
onClick={this.selectPlayer}
value={this.props.gatherer.id}
className="btn btn-xs btn-default"> Reselect
</button>
);
2015-07-28 15:54:29 +00:00
} else {
return (<button
onClick={this.selectPlayer}
2015-07-28 23:32:50 +00:00
value={this.props.gatherer.id}
2015-07-28 15:54:29 +00:00
className="btn btn-xs btn-primary"> Select
</button>
);
}
}
2015-07-28 23:32:50 +00:00
});
2015-07-28 15:54:29 +00:00
2015-08-08 19:14:16 +00:00
var GathererList = React.createClass({
2015-08-11 00:19:04 +00:00
memberList() {
2015-08-08 19:14:16 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
return this.props.gather.gatherers
.filter(gatherer => gatherer.team === self.props.team )
.sort(gatherer => gatherer.leader ? 1 : -1);
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
var extractGatherer = gatherer => {
2015-07-28 15:54:29 +00:00
var image;
if (gatherer.leader) {
image = (<img src="/images/commander.png"
alt="Commander"
height="20"
width="20" />);
}
return (
<tr key={gatherer.id}>
<td className="col-md-1">{image}</td>
<td className="col-md-11">{gatherer.user.username}</td>
</tr>
);
}
2015-08-08 19:14:16 +00:00
var members = this.memberList().map(extractGatherer);
return (
<table className="table">
<tbody>
{members}
</tbody>
</table>
);
}
});
var GatherTeams = React.createClass({
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 15:54:29 +00:00
return (
<div className="panel-body">
<div className="row">
<div className="col-md-6">
<div className="panel panel-default">
<div className="panel-heading">
Aliens
</div>
2015-08-08 19:14:16 +00:00
<GathererList gather={this.props.gather} team="alien" />
2015-07-28 15:54:29 +00:00
</div>
</div>
<div className="col-md-6">
<div className="panel panel-default">
<div className="panel-heading">
Marines
</div>
2015-08-08 19:14:16 +00:00
<GathererList gather={this.props.gather} team="marine" />
2015-07-28 15:54:29 +00:00
</div>
</div>
</div>
</div>
);
}
2015-08-08 19:14:16 +00:00
});
2015-07-28 15:54:29 +00:00
2015-07-31 15:54:08 +00:00
var ElectionProgressBar = React.createClass({
2015-08-11 00:19:04 +00:00
componentDidMount() {
2015-07-31 15:54:08 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
this.timer = setInterval(() => {
2015-07-31 15:54:08 +00:00
self.forceUpdate();
}, 900);
},
2015-08-11 00:19:04 +00:00
progress() {
2015-07-31 15:54:08 +00:00
var interval = this.props.gather.election.interval;
var startTime = (new Date(this.props.gather.election.startTime)).getTime();
var msTranspired = Math.floor((new Date()).getTime() - startTime);
return {
num: msTranspired,
den: interval,
barMessage: Math.floor((interval - msTranspired) / 1000) + "s remaining"
2015-07-31 15:54:08 +00:00
}
},
2015-08-11 00:19:04 +00:00
componentWillUnmount() {
2015-07-31 15:54:08 +00:00
clearInterval(this.timer);
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-31 15:54:08 +00:00
return (<ProgressBar progress={this.progress()} />);
}
});
var ProgressBar = React.createClass({
2015-08-11 00:19:04 +00:00
render() {
2015-07-31 15:54:08 +00:00
var style = {
width: Math.round((this.props.progress.num / this.props.progress.den * 100)) + "%"
};
var barMessage = this.props.progress.barMessage || "";
return (
<div className="progress">
<div className="progress-bar progress-bar-striped active"
data-role="progressbar"
data-aria-valuenow={this.props.progress.num}
data-aria-valuemin="0"
data-aria-valuemax={this.props.progress.den}
style={style}>{barMessage}
</div>
</div>
);
}
});
2015-07-28 15:54:29 +00:00
var GatherProgress = React.createClass({
2015-08-11 00:19:04 +00:00
stateDescription() {
2015-07-28 15:54:29 +00:00
switch(this.props.gather.state) {
case "gathering":
return "Waiting for more gatherers.";
case "election":
return "Currently voting for team leaders.";
case "selection":
2015-07-31 16:34:27 +00:00
return "Waiting for leaders to pick teams.";
2015-07-28 15:54:29 +00:00
case "done":
return "Gather completed.";
default:
return "Initialising gather.";
}
},
2015-08-11 00:19:04 +00:00
gatheringProgress() {
2015-07-28 15:54:29 +00:00
var num = this.props.gather.gatherers.length;
var den = 12;
var remaining = den - num;
var message = (remaining === 1) ? "Waiting for last player" : "Waiting for " + remaining + " more players";
return {
num: num,
den: den,
message: message
};
},
2015-08-11 00:19:04 +00:00
electionProgress() {
var num = this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-28 15:54:29 +00:00
if (gatherer.leaderVote) acc++;
return acc;
}, 0);
var den = 12;
return {
num: num,
den: den,
message: den - num + " more votes required"
};
},
2015-08-11 00:19:04 +00:00
selectionProgress() {
var num = this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-28 15:54:29 +00:00
if (gatherer.team !== "lobby") acc++;
return acc;
}, 0);
var den = 12;
return {
num: num,
den: den,
message: num + " out of " + den + " players assigned"
};
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-31 15:54:08 +00:00
var progress, progressBar;
2015-07-28 15:54:29 +00:00
var gatherState = this.props.gather.state;
if (gatherState === 'gathering' && this.props.gather.gatherers.length) {
progress = this.gatheringProgress();
2015-07-31 15:54:08 +00:00
progressBar = (<ProgressBar progress={progress} />);
2015-07-28 15:54:29 +00:00
} else if (gatherState === 'election') {
progress = this.electionProgress();
2015-07-31 15:54:08 +00:00
progressBar = (<ElectionProgressBar {...this.props} progress={progress} />);
2015-07-28 15:54:29 +00:00
} else if (gatherState === 'selection') {
progress = this.selectionProgress();
2015-07-31 15:54:08 +00:00
progressBar = (<ProgressBar progress={progress} />);
2015-07-28 15:54:29 +00:00
}
2015-07-31 15:54:08 +00:00
if (!progress) return false;
return (
<div className="panel-body no-bottom">
<p><strong>{this.stateDescription()}</strong> {progress.message}</p>
{progressBar}
</div>
);
2015-07-28 15:54:29 +00:00
}
});
2015-07-28 23:32:50 +00:00
var GatherActions = React.createClass({
2015-08-11 00:19:04 +00:00
joinGather(e) {
2015-07-29 14:35:58 +00:00
e.preventDefault();
socket.emit("gather:join");
},
2015-08-11 00:19:04 +00:00
leaveGather(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2015-07-28 23:32:50 +00:00
socket.emit("gather:leave");
},
2015-08-11 00:19:04 +00:00
confirmTeam(e) {
2015-07-28 23:32:50 +00:00
e.preventDefault();
socket.emit("gather:select:confirm");
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
inviteToGather(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2015-07-29 14:35:58 +00:00
alert("Boop!");
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 15:54:29 +00:00
var joinButton;
2015-07-28 23:32:50 +00:00
if (this.props.currentGatherer) {
2015-07-28 15:54:29 +00:00
joinButton = (<li><button
onClick={this.leaveGather}
className="btn btn-danger">Leave Gather</button></li>);
2015-07-29 14:35:58 +00:00
} else if (this.props.gather.state === 'gathering') {
joinButton = (
<button
onClick={this.joinGather}
className="btn btn-success">Join Gather</button>
);
2015-07-28 15:54:29 +00:00
}
2015-07-28 23:32:50 +00:00
var confirmTeam;
if (this.props.currentGatherer &&
2015-08-11 00:19:04 +00:00
this.props.currentGatherer.leader &&
this.props.gather.state === 'selection' &&
this.props.gather.gatherers.every(gatherer => gatherer.team !== 'lobby')) {
2015-07-28 23:32:50 +00:00
if (this.props.currentGatherer.confirm) {
confirmTeam = (
<li>
<button
className="btn btn-default"
data-disabled="true"
>
Confirmed
</button>
</li>
);
} else {
confirmTeam = (
<li>
<button
2015-07-29 10:46:30 +00:00
className="btn btn-success"
2015-07-28 23:32:50 +00:00
onClick={this.confirmTeam}
>
Confirm Team
</button>
</li>
);
}
}
2015-07-28 15:54:29 +00:00
var inviteButton;
if (this.props.gather.state === 'gathering') {
inviteButton = (<li><button
onClick={this.inviteToGather}
className="btn btn-primary">Invite to Gather</button></li>);
}
2015-07-28 23:32:50 +00:00
return (
<div className="panel-footer text-right">
2015-07-29 10:46:30 +00:00
<ul className="list-inline no-bottom">
2015-07-28 23:32:50 +00:00
{confirmTeam}
{inviteButton}
{joinButton}
</ul>
</div>
);
}
});
2015-07-29 13:50:39 +00:00
var ServerVoting = React.createClass({
2015-08-11 00:19:04 +00:00
handleServerVote(e) {
2015-07-29 13:50:39 +00:00
e.preventDefault();
socket.emit("gather:vote", {
server: {
id: parseInt(e.target.value, 10)
}
});
},
2015-08-11 00:19:04 +00:00
votesForServer(server) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-29 13:50:39 +00:00
if (server.id === gatherer.serverVote) acc++;
return acc;
}, 0);
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-29 13:50:39 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
var servers = self.props.servers.map(server => {
2015-07-29 13:50:39 +00:00
var voteButton;
if (self.props.currentGatherer.serverVote === server.id) {
voteButton = (<button
data-disabled="true"
className="btn btn-xs btn-success">
Voted</button>)
} else {
voteButton = (<button
onClick={self.handleServerVote}
value={server.id}
className="btn btn-xs btn-primary">
Vote</button>);
}
return (
2015-07-31 11:23:55 +00:00
<tr key={server.id}>
2015-08-10 22:38:05 +00:00
<td className="col-md-6">{server.description || server.dns}</td>
2015-07-31 14:39:59 +00:00
<td className="col-md-6 text-right">
{self.votesForServer(server)} Votes&nbsp;
2015-07-29 13:50:39 +00:00
{voteButton}
</td>
</tr>
);
});
return (
<div className="panel panel-default">
<div className="panel-heading">
Server Voting
</div>
<table id="serverVoteTable" className="table table-condensed table-hover voting-table">
{servers}
</table>
</div>
);
}
})
var MapVoting = React.createClass({
2015-08-11 00:19:04 +00:00
handleMapVote(e) {
2015-07-29 13:50:39 +00:00
e.preventDefault();
socket.emit("gather:vote", {
map: {
id: parseInt(e.target.value, 10)
}
});
},
2015-08-11 00:19:04 +00:00
votesForMap(map) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-29 13:50:39 +00:00
if (map.id === gatherer.mapVote) acc++;
return acc;
}, 0);
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-29 13:50:39 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
var maps = self.props.maps.map(map => {
2015-07-29 13:50:39 +00:00
var voteButton;
if (self.props.currentGatherer.mapVote === map.id) {
voteButton = (<button
data-disabled="true"
className="btn btn-xs btn-success">
Voted</button>)
} else {
voteButton = (<button
onClick={self.handleMapVote}
value={map.id}
className="btn btn-xs btn-primary">
Vote</button>);
}
return (
2015-07-31 11:23:55 +00:00
<tr key={map.id}>
2015-07-29 13:50:39 +00:00
<td className="col-md-6">{map.name}</td>
2015-07-31 14:39:59 +00:00
<td className="col-md-6 text-right">
{self.votesForMap(map)} Votes&nbsp;
2015-07-29 13:50:39 +00:00
{voteButton}
</td>
</tr>
);
});
return (
<div className="panel panel-default">
<div className="panel-heading">
Map Voting
</div>
<table className="table table-condensed table-hover voting-table">
{maps}
</table>
</div>
);
}
})
2015-07-28 23:32:50 +00:00
var Gather = React.createClass({
2015-08-11 00:19:04 +00:00
getDefaultProps() {
2015-07-28 23:32:50 +00:00
return {
gather: {
gatherers: []
}
}
},
2015-08-11 00:19:04 +00:00
componentDidMount() {
2015-07-28 23:32:50 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
socket.on("gather:refresh", data => self.setProps(data));
2015-07-28 23:32:50 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 23:32:50 +00:00
if (this.props.gather.state === 'done') {
2015-07-29 14:35:58 +00:00
return (<CompletedGather {...this.props} />);
2015-07-28 23:32:50 +00:00
}
2015-07-29 13:50:39 +00:00
var voting;
if (this.props.currentGatherer) {
voting = (
<div className="panel-body">
<div className="row">
<div className="col-md-6">
<MapVoting {...this.props} />
</div>
<div className="col-md-6">
<ServerVoting {...this.props} />
</div>
</div>
</div>
);
}
2015-07-28 15:54:29 +00:00
var gatherTeams;
if (this.props.gather.state === 'selection') {
gatherTeams = <GatherTeams gather={this.props.gather} />
}
var previousGather;
if (this.props.previousGather) {
previousGather = (<CompletedGather {...this.props} gather={this.props.previousGather} />);
}
2015-07-28 15:54:29 +00:00
return (
<div>
2015-07-28 15:54:29 +00:00
<div className="panel panel-default">
<div className="panel-heading">
2015-08-04 23:57:54 +00:00
<strong>Current Gather</strong>
2015-07-28 15:54:29 +00:00
<span className="badge add-left">{this.props.gather.gatherers.length}</span>
</div>
2015-07-31 15:54:08 +00:00
<GatherProgress {...this.props} />
<Gatherers {...this.props} />
2015-07-28 15:54:29 +00:00
{gatherTeams}
2015-07-29 14:35:58 +00:00
{voting}
2015-07-28 23:32:50 +00:00
<GatherActions {...this.props} />
</div>
{previousGather}
</div>
2015-07-28 15:54:29 +00:00
);
}
});
var Gatherers = React.createClass({
2015-08-11 00:19:04 +00:00
joinGather(e) {
2015-07-29 14:35:58 +00:00
e.preventDefault();
socket.emit("gather:join");
},
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 15:54:29 +00:00
var self = this;
2015-08-11 00:19:04 +00:00
var gatherers = this.props.gather.gatherers.map(gatherer => {
2015-08-08 17:13:17 +00:00
// Country
var country;
if (gatherer.user.country) {
country = (<img src="images/blank.gif"
className={"flag flag-" + gatherer.user.country.toLowerCase()}
alt={gatherer.user.country} />);
};
2015-07-28 15:54:29 +00:00
var division = (<span className="label label-primary">{gatherer.user.ability.division}</span>);
2015-08-05 00:06:09 +00:00
var lifeform = (
2015-08-11 00:19:04 +00:00
gatherer.user.ability.lifeforms.map(lifeform => {
2015-08-05 00:06:09 +00:00
return (<span className="label label-default"
key={[lifeform, gatherer.id].join("-")}>{lifeform}</span>);
})
);
2015-08-08 15:27:17 +00:00
var team;
if (gatherer.user.team) {
team = (<span className="label label-primary">{gatherer.user.team.name}</span>);
}
2015-07-28 23:32:50 +00:00
2015-08-05 00:06:09 +00:00
var action;
2015-07-28 23:32:50 +00:00
2015-07-28 15:54:29 +00:00
if (self.props.gather.state === "election") {
2015-08-11 00:19:04 +00:00
var votes = self.props.gather.gatherers.reduce((acc, voter) => {
2015-07-28 15:54:29 +00:00
if (voter.leaderVote === gatherer.id) acc++;
return acc;
}, 0)
action = (
<span>
<small>{votes + " votes"} &nbsp;</small>
<VoteButton currentGatherer={self.props.currentGatherer} candidate={gatherer} />
</span>
);
}
2015-07-28 23:32:50 +00:00
if (self.props.gather.state === 'selection') {
2015-08-08 17:13:17 +00:00
if (self.props.currentGatherer && self.props.currentGatherer.leader) {
action = (
<span>
<SelectPlayerButton gatherer={gatherer} />
</span>
);
} else {
if (gatherer.team !== "lobby") {
action = (<span className="label label-success">{gatherer.team}</span>);
}
}
2015-07-28 23:32:50 +00:00
}
2015-07-28 15:54:29 +00:00
return (
2015-08-18 09:56:35 +00:00
<tr key={gatherer.user.id} data-userid={gatherer.user.id}>
<td className="col-md-5">{country} {gatherer.user.username}&nbsp;</td>
2015-08-08 17:13:17 +00:00
<td className="col-md-5">
{lifeform} {division} {team}&nbsp;
</td>
<td className="col-md-2 text-right">{action}&nbsp;</td>
2015-07-28 15:54:29 +00:00
</tr>
);
})
if (this.props.gather.gatherers.length) {
return (
<div className="panel-body">
<div className="panel panel-default">
<table className="table roster-table">
<tbody>
{gatherers}
</tbody>
</table>
</div>
</div>
);
} else {
2015-07-29 10:46:30 +00:00
return (
<div className="panel-body text-center join-hero">
2015-07-29 14:35:58 +00:00
<button
onClick={this.joinGather}
className="btn btn-success btn-lg">Start a Gather</button>
2015-07-29 10:46:30 +00:00
</div>
);
2015-07-28 15:54:29 +00:00
}
}
});
2015-07-29 14:35:58 +00:00
var CompletedGather = React.createClass({
2015-08-11 00:19:04 +00:00
countVotes(voteType) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-08-08 19:14:16 +00:00
if (gatherer[voteType] !== null) acc.push(gatherer[voteType]);
return acc;
}, []);
2015-07-29 14:35:58 +00:00
},
2015-08-11 00:19:04 +00:00
selectedMaps() {
2015-08-08 19:14:16 +00:00
return rankVotes(this.countVotes('mapVote'), this.props.maps).slice(0, 2)
},
2015-08-11 00:19:04 +00:00
selectedServer() {
2015-08-08 19:14:16 +00:00
return rankVotes(this.countVotes('serverVote'), this.props.servers).slice(0, 1);
2015-07-29 14:35:58 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-08-08 19:14:16 +00:00
var maps = this.selectedMaps();
var server = this.selectedServer().pop();
2015-07-29 14:35:58 +00:00
return (
<div className="panel panel-default">
<div className="panel-heading">
<strong>Previous Gather</strong>
2015-07-29 14:35:58 +00:00
</div>
2015-08-08 19:14:16 +00:00
<GatherTeams gather={this.props.gather} />
2015-07-29 14:35:58 +00:00
<div className="panel-body">
2015-08-08 19:14:16 +00:00
<dl className="dl-horizontal">
<dt>Maps</dt>
2015-08-11 00:19:04 +00:00
<dd>{maps.map(map => map.name).join(" & ")}</dd>
2015-08-08 19:14:16 +00:00
<dt>Server</dt>
<dd>{server.name}</dd>
<dt>Address</dt>
<dd>{server.ip}:{server.port}</dd>
<dt>Password</dt>
<dd>{server.password}</dd>
<br />
<dt>&nbsp;</dt>
<dd><a href={["steam://run/4920/connect", server.ip +":"+server.port, server.password].join("/")}
className="btn btn-primary">Click to Join</a></dd>
</dl>
2015-07-29 14:35:58 +00:00
</div>
</div>
);
}
});