ensl_gathers/app/javascripts/components/gather.js

993 lines
24 KiB
JavaScript
Raw Normal View History

2016-01-26 10:44:34 +00:00
import {AssumeUserIdButton} from "javascripts/components/user";
2016-01-22 10:12:23 +00:00
const React = require("react");
2016-01-22 21:39:03 +00:00
const helper = require("javascripts/helper");
const enslUrl = helper.enslUrl;
const rankVotes = helper.rankeVotes;
const hiveUrl = helper.hiveUrl;
2015-07-28 15:54:29 +00:00
2016-01-26 10:44:34 +00:00
const SelectPlayerButton = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
gatherer: React.PropTypes.object.isRequired
},
2015-08-11 00:19:04 +00:00
selectPlayer(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:select", {
2015-07-28 23:32:50 +00:00
player: parseInt(e.target.value, 10)
2016-01-26 10:44:34 +00:00
});
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-09-16 15:16:00 +00:00
let button;
2015-07-28 23:32:50 +00:00
if (this.props.gatherer.leader) {
2015-09-16 15:16:00 +00:00
button = <button
2015-09-20 14:47:43 +00:00
className="btn btn-xs btn-default team-label"
2015-09-16 15:16:00 +00:00
data-disabled="true">Leader</button>;
2015-08-08 17:13:17 +00:00
} else if (this.props.gatherer.team !== "lobby") {
2015-09-16 15:16:00 +00:00
button = <button
data-disabled="true"
2015-10-02 14:53:11 +00:00
className="btn btn-xs btn-default team-label">
{_.capitalize(this.props.gatherer.team)}
2015-09-16 15:16:00 +00:00
</button>;
2015-07-28 15:54:29 +00:00
} else {
2015-09-16 15:16:00 +00:00
button = <button
2015-07-28 15:54:29 +00:00
onClick={this.selectPlayer}
2015-07-28 23:32:50 +00:00
value={this.props.gatherer.id}
2015-09-20 14:47:43 +00:00
className="btn btn-xs btn-primary team-label"> Select
2015-09-16 15:16:00 +00:00
</button>;
2015-07-28 15:54:29 +00:00
}
2015-09-16 15:16:00 +00:00
return button;
2015-07-28 15:54:29 +00:00
}
2015-07-28 23:32:50 +00:00
});
2015-07-28 15:54:29 +00:00
2016-01-26 10:44:34 +00:00
const GathererList = React.createClass({
2015-08-11 00:19:04 +00:00
memberList() {
2016-01-26 10:44:34 +00:00
const self = this;
2015-08-11 00:19:04 +00:00
return this.props.gather.gatherers
.filter(gatherer => gatherer.team === self.props.team)
.sort(gatherer => { return gatherer.leader ? 1 : -1 });
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
const extractGatherer = gatherer => {
let image;
2015-07-28 15:54:29 +00:00
if (gatherer.leader) {
image = <i className="fa fa-star add-right"></i>;
2015-07-28 15:54:29 +00:00
}
return (
<tr key={gatherer.id}>
<td className="col-md-12">
{image}{gatherer.user.username}
<span className="pull-right">
<LifeformIcons gatherer={gatherer} />
</span>
</td>
2015-07-28 15:54:29 +00:00
</tr>
);
2016-01-26 10:44:34 +00:00
};
const members = this.memberList()
2015-09-17 11:33:28 +00:00
.map(extractGatherer);
2015-08-08 19:14:16 +00:00
return (
<table className="table">
<tbody>
{members}
</tbody>
</table>
);
}
});
2016-01-26 10:44:34 +00:00
const GatherTeams = React.createClass({
2015-08-11 00:19:04 +00:00
render() {
2015-07-28 15:54:29 +00:00
return (
2015-09-05 17:22:23 +00:00
<div className="row add-top">
2015-09-16 15:16:00 +00:00
<div className="col-sm-6">
<div className="panel panel-primary panel-light-background">
2015-09-05 17:22:23 +00:00
<div className="panel-heading">
2015-09-17 11:47:37 +00:00
Marines
2015-07-28 15:54:29 +00:00
</div>
2015-09-17 11:47:37 +00:00
<GathererList gather={this.props.gather} team="marine" />
2015-07-28 15:54:29 +00:00
</div>
2015-09-05 17:22:23 +00:00
</div>
2015-09-16 15:16:00 +00:00
<div className="col-sm-6">
<div className="panel panel-primary panel-light-background">
2015-09-05 17:22:23 +00:00
<div className="panel-heading">
2015-09-17 11:47:37 +00:00
Aliens
2015-07-28 15:54:29 +00:00
</div>
2015-09-17 11:47:37 +00:00
<GathererList gather={this.props.gather} team="alien" />
2015-07-28 15:54:29 +00:00
</div>
</div>
</div>
);
}
2015-08-08 19:14:16 +00:00
});
2015-07-28 15:54:29 +00:00
2016-01-26 10:44:34 +00:00
const ElectionProgressBar = React.createClass({
2015-08-11 00:19:04 +00:00
componentDidMount() {
2016-01-26 10:44:34 +00:00
const 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() {
2016-01-26 10:44:34 +00:00
const interval = this.props.gather.election.interval;
const startTime = (new Date(this.props.gather.election.startTime)).getTime();
const msTranspired = Math.floor((new Date()).getTime() - startTime);
2015-07-31 15:54:08 +00:00
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()} />);
}
});
2016-01-26 10:44:34 +00:00
const ProgressBar = React.createClass({
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
const progress = this.props.progress;
const style = {
2015-10-02 14:53:11 +00:00
width: Math.round((progress.num / progress.den * 100)) + "%"
2015-07-31 15:54:08 +00:00
};
2016-01-26 10:44:34 +00:00
const barMessage = progress.barMessage || "";
2015-07-31 15:54:08 +00:00
return (
<div className="progress">
2015-09-05 17:22:23 +00:00
<div className="progress-bar progress-bar-striped active"
data-role="progressbar"
2015-10-02 14:53:11 +00:00
data-aria-valuenow={progress.num}
2015-09-05 17:22:23 +00:00
data-aria-valuemin="0"
2015-10-02 14:53:11 +00:00
data-aria-valuemax={progress.den}
2015-09-05 17:22:23 +00:00
style={style}>{barMessage}
</div>
</div>
2015-07-31 15:54:08 +00:00
);
}
});
2016-01-26 10:44:34 +00:00
const 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() {
2016-01-26 10:44:34 +00:00
const num = this.props.gather.gatherers.length;
const den = 12;
const remaining = den - num;
const message = (remaining === 1) ?
2015-10-02 14:53:11 +00:00
"Waiting for last player" : `Waiting for ${remaining} more players`;
2015-07-28 15:54:29 +00:00
return {
num: num,
den: den,
message: message
};
},
2015-08-11 00:19:04 +00:00
electionProgress() {
2016-01-26 10:44:34 +00:00
const num = this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-28 15:54:29 +00:00
if (gatherer.leaderVote) acc++;
return acc;
}, 0);
2016-01-26 10:44:34 +00:00
const den = 12;
2015-07-28 15:54:29 +00:00
return {
num: num,
den: den,
message: den - num + " more votes required"
};
},
2015-08-11 00:19:04 +00:00
selectionProgress() {
2016-01-26 10:44:34 +00:00
const num = this.props.gather.gatherers.reduce((acc, gatherer) => {
2015-07-28 15:54:29 +00:00
if (gatherer.team !== "lobby") acc++;
return acc;
}, 0);
2016-01-26 10:44:34 +00:00
const den = 12;
2015-07-28 15:54:29 +00:00
return {
num: num,
den: den,
2015-09-16 15:16:00 +00:00
message: `${num} out of ${den} players assigned. Waiting
on ${_.capitalize(this.props.gather.pickingTurn)}s to pick next...`
2015-07-28 15:54:29 +00:00
};
},
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
let progress, progressBar;
const gatherState = this.props.gather.state;
2015-07-28 15:54:29 +00:00
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 (
2015-09-05 17:22:23 +00:00
<div className="no-bottom">
2015-07-31 15:54:08 +00:00
<p><strong>{this.stateDescription()}</strong> {progress.message}</p>
{progressBar}
</div>
);
2015-07-28 15:54:29 +00:00
}
});
2016-01-26 10:44:34 +00:00
const JoinGatherButton = React.createClass({
propTypes: {
thisGatherer: React.PropTypes.object,
user: React.PropTypes.object.isRequired,
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired
2015-10-20 16:36:21 +00:00
},
2015-08-27 11:57:35 +00:00
2015-09-28 13:30:13 +00:00
componentDidMount() {
2016-01-26 10:44:34 +00:00
const self = this;
2015-09-28 13:30:13 +00:00
this.timer = setInterval(() => {
self.forceUpdate();
}, 30000);
},
componentWillUnmount() {
clearInterval(this.timer);
},
2015-08-11 00:19:04 +00:00
joinGather(e) {
2015-07-29 14:35:58 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:join");
2015-07-29 14:35:58 +00:00
},
2015-08-11 00:19:04 +00:00
leaveGather(e) {
2015-07-28 15:54:29 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:leave");
2015-07-28 23:32:50 +00:00
},
2015-08-11 00:19:04 +00:00
2015-09-28 13:30:13 +00:00
cooldownTime() {
let user = this.props.user;
if (!user) return false;
let cooloffTime = this.props.gather.cooldown[user.id];
if (!cooloffTime) return false;
let timeRemaining = new Date(cooloffTime) - new Date();
return timeRemaining > 0 ? timeRemaining : false;
},
render() {
2015-10-02 14:53:11 +00:00
let gather = this.props.gather;
let thisGatherer = this.props.thisGatherer;
if (thisGatherer) {
2015-09-28 13:30:13 +00:00
return <button
onClick={this.leaveGather}
className="btn btn-danger">Leave Gather</button>;
}
2015-10-02 14:53:11 +00:00
if (gather.state === 'gathering') {
2015-09-28 13:30:13 +00:00
let cooldownTime = this.cooldownTime();
if (cooldownTime) {
return <CooloffButton timeRemaining={cooldownTime} />;
} else {
return <button
onClick={this.joinGather}
className="btn btn-success">Join Gather</button>;
}
}
return false;
}
});
2016-01-26 10:44:34 +00:00
const CooloffButton = React.createClass({
propTypes: {
timeRemaining: React.PropTypes.number.isRequired
},
2015-09-28 13:30:13 +00:00
timeRemaining() {
return `${Math.floor(this.props.timeRemaining / 60000) + 1} minutes remaining`;
},
render() {
return <button
disabled="true"
className="btn btn-success">
Leaver Cooloff ({this.timeRemaining()})
</button>
}
})
2016-01-26 10:44:34 +00:00
const GatherActions = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired,
thisGatherer: React.PropTypes.object
},
2015-09-17 13:56:50 +00:00
voteRegather(e) {
e.preventDefault(e);
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:vote", {
2015-09-17 13:56:50 +00:00
regather: (e.target.value === "true")
});
},
regatherVotes() {
2015-10-02 14:53:11 +00:00
let gather = this.props.gather;
if (!gather) return 0;
return gather.gatherers.reduce((acc, gatherer) => {
2015-09-17 13:56:50 +00:00
if (gatherer.regatherVote) acc++;
return acc;
}, 0);
2015-07-28 15:54:29 +00:00
},
2015-08-11 00:19:04 +00:00
render() {
2015-09-28 13:30:13 +00:00
let regatherButton;
2016-01-26 10:44:34 +00:00
const user = this.props.user;
const gather = this.props.gather;
const socket = this.props.socket;
const thisGatherer = this.props.thisGatherer;
2015-10-02 14:53:11 +00:00
if (thisGatherer) {
2015-09-17 13:56:50 +00:00
let regatherVotes = this.regatherVotes();
2015-10-02 14:53:11 +00:00
if (thisGatherer.regatherVote) {
regatherButton = <button value="false" onClick={this.voteRegather}
className="btn btn-danger">
{`Voted Regather (${regatherVotes}/8)`}
</button>;
2015-07-28 23:32:50 +00:00
} else {
2015-10-02 14:53:11 +00:00
regatherButton = <button value="true" onClick={this.voteRegather}
className="btn btn-danger">
{`Vote Regather (${regatherVotes}/8)`}
</button>;
2015-07-28 23:32:50 +00:00
}
}
return (
2015-10-20 16:36:21 +00:00
<div>
<div className="text-right">
<ul className="list-inline no-bottom">
<li>
{regatherButton}
</li>
<li>
<JoinGatherButton gather={gather} thisGatherer={thisGatherer}
2016-01-26 10:44:34 +00:00
user={user} socket={socket} />
2015-10-20 16:36:21 +00:00
</li>
</ul>
2015-09-14 22:14:36 +00:00
</div>
2015-07-28 23:32:50 +00:00
</div>
);
}
});
2016-01-26 10:44:34 +00:00
const VoteButton = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
candidate: React.PropTypes.object.isRequired,
thisGatherer: React.PropTypes.object
},
2015-08-27 14:53:11 +00:00
cancelVote(e) {
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:vote", {
2015-08-27 14:53:11 +00:00
leader: {
candidate: null
}
});
},
vote(e) {
2015-07-29 13:50:39 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:vote", {
2015-08-27 14:53:11 +00:00
leader: {
candidate: parseInt(e.target.value, 10)
2015-07-29 13:50:39 +00:00
}
});
},
2015-08-11 00:19:04 +00:00
stopGatherMusic() {
soundController.stop();
},
2015-08-27 14:53:11 +00:00
render() {
2015-10-02 14:53:11 +00:00
let candidate = this.props.candidate;
let thisGatherer = this.props.thisGatherer;
if (thisGatherer === null) {
2015-08-27 14:53:11 +00:00
return false;
}
2015-10-02 14:53:11 +00:00
if (thisGatherer.leaderVote === candidate.id) {
2015-08-27 14:53:11 +00:00
return (
<button
onClick={this.cancelVote}
2015-09-20 13:50:59 +00:00
className="btn btn-xs btn-success vote-button">Voted
2015-08-27 14:53:11 +00:00
</button>
);
} else {
return (
<button
onClick={this.vote}
2015-09-20 13:50:59 +00:00
className="btn btn-xs btn-primary vote-button"
2015-10-02 14:53:11 +00:00
value={candidate.id}>Vote
2015-08-27 14:53:11 +00:00
</button>
);
}
}
});
2016-01-26 10:44:34 +00:00
const ServerVoting = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired,
thisGatherer: React.PropTypes.object,
servers: React.PropTypes.array.isRequired,
},
2015-08-27 14:53:11 +00:00
voteHandler(serverId) {
2016-01-26 10:44:34 +00:00
return e => {
2015-08-27 14:53:11 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:vote", {
2015-08-27 14:53:11 +00:00
server: {
id: serverId
}
});
}
},
2015-08-11 00:19:04 +00:00
votesForServer(server) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
if (gatherer.serverVote.some(voteId => voteId === server.id)) acc++;
2015-07-29 13:50:39 +00:00
return acc;
}, 0);
},
2015-08-11 00:19:04 +00:00
render() {
2015-10-02 14:53:11 +00:00
let self = this;
let thisGatherer = self.props.thisGatherer;
let servers = self.props.servers.sort((a, b) => {
2016-01-26 10:44:34 +00:00
const aVotes = self.votesForServer(a);
const bVotes = self.votesForServer(b);
2015-10-02 14:53:11 +00:00
return bVotes - aVotes;
}).map(server => {
2015-08-27 14:53:11 +00:00
let votes = self.votesForServer(server);
let style = thisGatherer.serverVote.some(voteId => voteId === server.id) ?
"list-group-item list-group-item-success" : "list-group-item";
return (
<a href="#"
className={style}
2015-12-25 08:03:06 +00:00
onClick={self.voteHandler(server.id)}
key={server.id}>
<span className="badge">{votes}</span>
{server.name || server.description}
</a>
);
2015-07-29 13:50:39 +00:00
});
2015-08-27 14:53:11 +00:00
let votes = thisGatherer.serverVote.length;
2015-08-27 14:53:11 +00:00
2015-07-29 13:50:39 +00:00
return (
2015-09-21 20:15:27 +00:00
<div className="panel panel-primary">
2015-07-29 13:50:39 +00:00
<div className="panel-heading">
{votes === 2 ? "Server Votes" :
`Please Vote for a Server. ${2 - votes} votes remaining` }
2015-07-29 13:50:39 +00:00
</div>
2015-08-27 14:53:11 +00:00
<div className="list-group gather-voting">
2015-07-29 13:50:39 +00:00
{servers}
2015-08-27 14:53:11 +00:00
</div>
2015-07-29 13:50:39 +00:00
</div>
);
}
})
2016-01-26 10:44:34 +00:00
const MapVoting = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired,
thisGatherer: React.PropTypes.object,
maps: React.PropTypes.array.isRequired,
},
2015-08-27 14:53:11 +00:00
voteHandler(mapId) {
2016-01-26 10:44:34 +00:00
return e => {
2015-08-27 14:53:11 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:vote", {
2015-08-27 14:53:11 +00:00
map: {
id: mapId
}
});
}
2015-07-29 13:50:39 +00:00
},
2015-08-11 00:19:04 +00:00
votesForMap(map) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
if (gatherer.mapVote.some(voteId => voteId === map.id)) acc++;
2015-07-29 13:50:39 +00:00
return acc;
}, 0);
},
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
const self = this;
2015-10-02 14:53:11 +00:00
let thisGatherer = self.props.thisGatherer
let maps = self.props.maps.sort((a, b) => {
2016-01-26 10:44:34 +00:00
const aVotes = self.votesForMap(a);
const bVotes = self.votesForMap(b);
2015-10-02 14:53:11 +00:00
return bVotes - aVotes;
}).map(map => {
let votes = self.votesForMap(map);
2015-12-25 07:50:27 +00:00
let style = thisGatherer.mapVote.some(voteId => voteId === map.id) ?
"list-group-item list-group-item-success" : "list-group-item";
return (
<a href="#"
key={map.id}
onClick={self.voteHandler(map.id)}
className={style}>
<span className="badge">{votes}</span>
{map.name}
</a>
);
2015-10-02 14:53:11 +00:00
});
2015-08-27 15:11:54 +00:00
let votes = thisGatherer.mapVote.length;
2015-08-27 15:11:54 +00:00
2015-07-29 13:50:39 +00:00
return (
2015-09-21 20:15:27 +00:00
<div className="panel panel-primary">
2015-07-29 13:50:39 +00:00
<div className="panel-heading">
{votes === 2 ? "Map Votes" :
`Please Vote for a Map. ${2 - votes} votes remaining` }
2015-07-29 13:50:39 +00:00
</div>
2015-08-27 14:53:11 +00:00
<div className="list-group gather-voting">
2015-07-29 13:50:39 +00:00
{maps}
2015-08-27 14:53:11 +00:00
</div>
2015-07-29 13:50:39 +00:00
</div>
);
}
})
2016-01-26 10:44:34 +00:00
const Gather = exports.Gather = React.createClass({
propTypes: {
thisGatherer: React.PropTypes.object,
maps: React.PropTypes.array.isRequired,
servers: React.PropTypes.array.isRequired,
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired
},
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
const socket = this.props.socket;
const gather = this.props.gather;
const thisGatherer = this.props.thisGatherer;
const servers = this.props.servers;
const maps = this.props.maps;
const user = this.props.user;
if (gather === null) return <div></div>;
2015-07-28 23:32:50 +00:00
2015-10-02 14:53:11 +00:00
let voting;
if (thisGatherer) {
let state = gather.state;
2015-09-05 17:37:43 +00:00
if (state === 'gathering' || state === 'election') {
voting = (
<div className="row add-top">
2015-09-16 15:16:00 +00:00
<div className="col-sm-6">
2015-10-02 14:53:11 +00:00
<MapVoting gather={gather} maps={maps}
2016-01-26 10:44:34 +00:00
socket={socket} thisGatherer={thisGatherer} />
2015-09-05 17:37:43 +00:00
</div>
2015-09-16 15:16:00 +00:00
<div className="col-sm-6">
2015-10-02 14:53:11 +00:00
<ServerVoting gather={gather} servers={servers}
2016-01-26 10:44:34 +00:00
socket={socket} thisGatherer={thisGatherer} />
2015-09-05 17:37:43 +00:00
</div>
2015-07-29 13:50:39 +00:00
</div>
2015-09-05 17:37:43 +00:00
);
} else {
2015-10-02 14:53:11 +00:00
voting = <GatherVotingResults gather={gather}
servers={servers}
maps={maps} />;
2015-09-05 17:37:43 +00:00
}
2015-07-29 13:50:39 +00:00
}
2015-10-02 14:53:11 +00:00
let gatherTeams;
if (gather.state === 'selection') {
gatherTeams = <GatherTeams gather={gather} />;
2015-07-28 15:54:29 +00:00
}
2015-10-20 22:49:21 +00:00
if (gather.gatherers.length > 0) {
return (
<div>
<div className="panel panel-primary add-bottom">
<div className="panel-heading">Current Gather</div>
<div className="panel-body">
<GatherProgress gather={gather} />
2016-01-26 10:44:34 +00:00
<GatherActions gather={gather} user={user} thisGatherer={thisGatherer}
socket={socket} />
2015-10-20 22:49:21 +00:00
</div>
2015-09-14 22:14:36 +00:00
</div>
<Gatherers gather={gather} user={user}
soundController={this.props.soundController}
2016-01-26 10:44:34 +00:00
thisGatherer={thisGatherer} socket={socket} />
2015-10-20 22:49:21 +00:00
{gatherTeams}
{voting}
2015-09-14 22:14:36 +00:00
</div>
2015-10-20 22:49:21 +00:00
);
} else {
return (
<div>
<div className="panel panel-primary add-bottom">
<div className="panel-heading">Current Gather</div>
</div>
2016-01-26 10:44:34 +00:00
<Gatherers gather={gather} user={user} thisGatherer={thisGatherer}
socket={socket} />
2015-10-20 22:49:21 +00:00
</div>
);
}
2015-07-28 15:54:29 +00:00
}
});
2016-01-26 10:44:34 +00:00
const LifeformIcons = exports.LifeformIcons = React.createClass({
availableLifeforms() {
return ["skulk", "gorge", "lerk", "fade", "onos", "commander"];
},
gathererLifeforms() {
let lifeforms = [];
let gatherer = this.props.gatherer;
2015-10-02 14:53:11 +00:00
let abilities = gatherer.user.profile.abilities;
for (let attr in abilities) {
if (abilities[attr]) lifeforms.push(_.capitalize(attr));
}
return lifeforms;
},
render() {
let lifeforms = this.gathererLifeforms();
let availableLifeforms = this.availableLifeforms();
let icons = availableLifeforms.map(lifeform => {
let containsAbility = lifeforms.some(gathererLifeform => {
return gathererLifeform.toLowerCase() === lifeform.toLowerCase()
});
if (containsAbility) {
return <img
className="lifeform-icon"
2015-09-27 11:55:00 +00:00
key={lifeform}
2016-01-22 10:12:23 +00:00
src={`/${lifeform.toLowerCase()}.png`} />
} else {
return <img
className="lifeform-icon"
2015-09-27 11:55:00 +00:00
key={lifeform}
2016-01-22 10:12:23 +00:00
src={`/blank.gif`} />
}
});
return <span className="add-right hidden-xs">{icons}</span>
}
});
2016-01-26 10:44:34 +00:00
const Gatherers = React.createClass({
propTypes: {
user: React.PropTypes.object,
thisGatherer: React.PropTypes.object,
socket: React.PropTypes.object.isRequired,
gather: React.PropTypes.object.isRequired
},
2015-08-11 00:19:04 +00:00
joinGather(e) {
2015-07-29 14:35:58 +00:00
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:join");
2015-07-29 14:35:58 +00:00
},
2015-08-11 00:19:04 +00:00
2015-09-17 12:36:18 +00:00
bootGatherer(e) {
e.preventDefault();
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:leave", {
2015-09-17 12:36:18 +00:00
gatherer: parseInt(e.target.value, 10) || null
});
},
2015-08-11 00:19:04 +00:00
render() {
2016-01-26 10:44:34 +00:00
const self = this;
const user = this.props.user;
const socket = this.props.socket;
const gather = this.props.gather;
const thisGatherer = this.props.thisGatherer;
const admin = (user && user.admin) || (user && user.moderator);
const gatherers = gather.gatherers
2015-09-17 11:37:16 +00:00
.sort((a, b) => {
return (b.user.hive.skill || 1000) - (a.user.hive.skill || 1000);
})
.map(gatherer => {
2016-01-26 10:44:34 +00:00
let country;
2015-08-08 17:13:17 +00:00
if (gatherer.user.country) {
2016-01-26 10:44:34 +00:00
country = (
2016-01-22 10:12:23 +00:00
<img src="/blank.gif"
2015-11-15 10:14:13 +00:00
className={"flag flag-" + gatherer.user.country.toLowerCase()}
2015-10-02 14:53:11 +00:00
alt={gatherer.user.country} />
);
2015-08-08 17:13:17 +00:00
};
2015-07-28 15:54:29 +00:00
2015-09-20 13:50:59 +00:00
let skill = gatherer.user.profile.skill || "Not Available";
2015-08-28 14:17:08 +00:00
2015-09-20 13:50:59 +00:00
let hiveStats = [];
2015-09-14 16:50:00 +00:00
if (gatherer.user.hive.skill) hiveStats.push(`${gatherer.user.hive.skill} ELO`);
2015-08-31 14:55:42 +00:00
if (gatherer.user.hive.playTime) {
2015-09-14 16:50:00 +00:00
hiveStats.push(`${Math.floor(gatherer.user.hive.playTime / 3600)} Hours`);
2015-08-31 14:55:42 +00:00
}
2015-09-20 13:50:59 +00:00
let hive = (hiveStats.length) ? hiveStats.join(", ") : "Not Available";
2015-09-05 17:22:23 +00:00
2015-09-20 13:50:59 +00:00
let team = (gatherer.user.team) ? gatherer.user.team.name : "None";
2015-07-28 23:32:50 +00:00
2015-09-20 13:50:59 +00:00
let action;
2015-10-02 14:53:11 +00:00
if (gather.state === "election") {
let votes = gather.gatherers.reduce((acc, voter) => {
2015-07-28 15:54:29 +00:00
if (voter.leaderVote === gatherer.id) acc++;
return acc;
}, 0)
action = (
<span>
<span className="badge add-right">{votes + " votes"}</span>
2015-10-02 14:53:11 +00:00
<VoteButton
thisGatherer={thisGatherer}
soundController={this.props.soundController}
2015-10-02 14:53:11 +00:00
candidate={gatherer} />
2015-07-28 15:54:29 +00:00
</span>
);
}
2015-10-02 14:53:11 +00:00
if (gather.state === 'selection') {
if (thisGatherer &&
thisGatherer.leader &&
thisGatherer.team === gather.pickingTurn) {
2015-08-08 17:13:17 +00:00
action = (
<span>
<SelectPlayerButton gatherer={gatherer} />
</span>
);
} else {
2015-09-16 15:16:00 +00:00
if (gatherer.leader) {
2015-10-02 14:53:11 +00:00
action = (<span className={`label label-padding
label-${gatherer.team}
team-label`}>Leader</span>);
2015-09-16 15:16:00 +00:00
} else if (gatherer.team !== "lobby") {
2015-10-02 14:53:11 +00:00
action = (<span className={`label label-padding
label-${gatherer.team}
team-label`}>{_.capitalize(gatherer.team)}</span>);
2015-09-20 14:47:43 +00:00
} else {
2015-10-02 14:53:11 +00:00
action = (<span className="label label-padding label-default team-label">
Lobby</span>);
2015-08-08 17:13:17 +00:00
}
}
2015-07-28 23:32:50 +00:00
}
2015-10-02 14:53:11 +00:00
let adminOptions;
2015-09-17 12:36:18 +00:00
if (admin) {
adminOptions = [
2015-10-03 15:36:58 +00:00
<hr />,
2015-09-17 12:36:18 +00:00
<dt>Admin</dt>,
<dd>
<button
className="btn btn-xs btn-danger"
value={gatherer.user.id}
onClick={this.bootGatherer}>
Boot from Gather
2015-09-27 11:55:00 +00:00
</button>&nbsp;
2016-01-26 10:44:34 +00:00
<AssumeUserIdButton socket={socket}
gatherer={gatherer} currentUser={user} />
2015-09-17 12:36:18 +00:00
</dd>
]
}
2015-09-27 13:18:32 +00:00
let tabColor = gatherer.team !== "lobby" ? `panel-${gatherer.team}` : "panel-info";
2015-07-28 15:54:29 +00:00
return (
2015-10-02 14:53:11 +00:00
<div className={`panel ${tabColor} gatherer-panel`}
key={gatherer.user.id} data-userid={gatherer.user.id}>
2015-09-05 17:22:23 +00:00
<div className="panel-heading">
<h4 className="panel-title">
2015-09-17 20:40:54 +00:00
{country} {gatherer.user.username}
2015-09-05 17:22:23 +00:00
<span className="pull-right">
2015-09-17 20:40:54 +00:00
<a data-toggle="collapse"
href={"#"+gatherer.user.id.toString() + "-collapse"}
aria-expanded="false"
2015-09-18 15:13:52 +00:00
className="btn btn-xs btn-primary add-right"
2015-09-17 20:40:54 +00:00
aria-controls={gatherer.user.id.toString() + "-collapse"}>
Info <span className="caret"></span></a>
<LifeformIcons gatherer={gatherer} />
2015-09-05 17:22:23 +00:00
{action}
</span>
</h4>
</div>
<div id={gatherer.user.id.toString() + "-collapse"}
className="panel-collapse collapse out" >
<div className="panel-body">
<dl className="dl-horizontal">
<dt>Skill Level</dt>
<dd>{skill}</dd>
<dt>Team</dt>
<dd>{team}</dd>
<dt>Hive Stats</dt>
<dd>{hive}</dd>
2015-10-03 15:36:58 +00:00
<dt>Links</dt>
<dd>
2015-10-18 15:23:39 +00:00
<a href={enslUrl(gatherer)}
2015-10-03 15:36:58 +00:00
className="btn btn-xs btn-primary"
target="_blank">ENSL Profile</a>&nbsp;
2015-10-18 15:23:39 +00:00
<a href={hiveUrl(gatherer)}
2015-10-03 15:36:58 +00:00
className="btn btn-xs btn-primary"
target="_blank">Hive Profile</a>
</dd>
2015-09-17 12:36:18 +00:00
{adminOptions}
2015-09-05 17:22:23 +00:00
</dl>
</div>
</div>
</div>
2015-07-28 15:54:29 +00:00
);
})
2015-10-02 14:53:11 +00:00
if (gather.gatherers.length) {
2015-07-28 15:54:29 +00:00
return (
2015-10-02 14:53:11 +00:00
<div class="panel-group"
role="tablist"
aria-multiselectable="true"
id="gatherers-panel">
2015-09-05 17:22:23 +00:00
{gatherers}
2015-07-28 15:54:29 +00:00
</div>
);
} else {
2015-07-29 10:46:30 +00:00
return (
2015-09-21 20:15:27 +00:00
<div className="panel panel-primary add-bottom">
2015-09-14 23:02:52 +00:00
<div className="panel-body text-center join-hero">
<button
onClick={this.joinGather}
className="btn btn-success btn-lg">Start a Gather</button>
</div>
2015-07-29 10:46:30 +00:00
</div>
);
2015-07-28 15:54:29 +00:00
}
}
});
2016-01-26 10:44:34 +00:00
const CompletedGather = React.createClass({
completionDate() {
let d = new Date(this.props.gather.done.time);
if (d) {
return d.toLocaleTimeString();
} else {
return "Completed Gather"
}
},
2015-09-27 11:55:00 +00:00
getInitialState() {
return {
show: !!this.props.show
};
},
toggleGatherInfo() {
let newState = !this.state.show;
this.setState({
show: newState
});
},
2015-09-05 17:37:43 +00:00
render() {
2015-09-27 11:55:00 +00:00
let gatherInfo = [];
2015-10-02 14:53:11 +00:00
let gather = this.props.gather;
let maps = this.props.maps;
let servers = this.props.servers;
2015-09-27 11:55:00 +00:00
if (this.state.show) {
2015-10-02 14:53:11 +00:00
gatherInfo.push(<GatherTeams gather={gather} />);
gatherInfo.push(<GatherVotingResults gather={gather}
maps={maps}
servers={servers}/>);
2015-09-27 11:55:00 +00:00
}
2015-09-05 17:37:43 +00:00
return (
2015-09-27 11:55:00 +00:00
<div>
<div className="panel panel-success add-bottom pointer"
onClick={this.toggleGatherInfo}>
<div className="panel-heading"><strong>{this.completionDate()}</strong></div>
2015-09-14 23:10:06 +00:00
</div>
2015-09-27 11:55:00 +00:00
{gatherInfo}
2015-09-05 17:37:43 +00:00
</div>
);
}
});
2016-01-26 10:44:34 +00:00
const GatherVotingResults = React.createClass({
2015-10-22 09:34:40 +00:00
// Returns an array of ids voted for e.g. [1,2,5,1,1,3,2]
2015-08-11 00:19:04 +00:00
countVotes(voteType) {
return this.props.gather.gatherers.reduce((acc, gatherer) => {
let votes = gatherer[voteType];
2015-10-04 12:25:06 +00:00
// Temporary fix because some mapvotes are ints and not arrays
if (!Array.isArray(votes)) votes = [votes];
if (votes.length > 0) votes.forEach(vote => acc.push(vote));
2015-08-08 19:14:16 +00:00
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-09-14 17:27:50 +00:00
let maps = this.selectedMaps();
let server = this.selectedServer().pop();
let password;
if (server.password) {
password = [
<dt>Password</dt>,
<dd>{server.password}</dd>
];
}
2015-07-29 14:35:58 +00:00
return (
2015-09-21 21:33:05 +00:00
<div className="panel panel-primary">
2015-09-14 17:27:50 +00:00
<div className="panel-heading">
Server
</div>
<div className="panel-body">
<dl className="dl-horizontal">
<dt>Maps</dt>
<dd>{maps.map(map => map.name).join(" & ")}</dd>
<dt>Server</dt>
<dd>{server.name}</dd>
<dt>Address</dt>
<dd>{server.ip}:{server.port}</dd>
{password}
</dl>
<p>
2015-10-20 16:48:44 +00:00
<a href={`steam://run/4920/connect+%20${server.ip}:${server.port}%20+password%20${server.password}`}
2015-09-21 21:33:05 +00:00
className="btn btn-primary max-width">Join Server</a>
2015-09-14 17:27:50 +00:00
</p>
</div>
2015-07-29 14:35:58 +00:00
</div>
);
}
});
2015-09-27 11:55:00 +00:00
2016-01-26 10:44:34 +00:00
const ArchivedGathers = exports.ArchivedGathers = React.createClass({
propTypes: {
archive: React.PropTypes.array.isRequired,
servers: React.PropTypes.array.isRequired,
maps: React.PropTypes.array.isRequired
},
2015-09-27 11:55:00 +00:00
render() {
let archive = this.props.archive
.sort((a, b) => {
return new Date(b.createdAt) - new Date(a.createdAt);
})
.map((archivedGather, index) => {
2015-09-27 11:55:00 +00:00
return <CompletedGather
2015-10-02 14:53:11 +00:00
id={archivedGather.gather.done.time}
show={(index === 0) ? true : false}
2015-09-27 11:55:00 +00:00
gather={archivedGather.gather}
maps={this.props.maps}
servers={this.props.servers} />
});
return (
<div className="panel panel-primary">
<div className="panel-heading">Archived Gathers</div>
<div className="panel-body">
{archive}
</div>
</div>
);
}
});