ensl_gathers/app/javascripts/components/user.js

394 lines
9.8 KiB
JavaScript
Raw Normal View History

2016-01-26 10:44:34 +00:00
import {LifeformIcons} from "javascripts/components/gather";
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 hiveUrl = helper.hiveUrl;
const modalId = helper.modalId;
2016-02-03 16:12:08 +00:00
const Ps = require('perfect-scrollbar');
2015-07-28 15:54:29 +00:00
2016-01-22 10:12:23 +00:00
const DisconnectUserButton = React.createClass({
2016-01-26 10:44:34 +00:00
propTypes: {
socket: React.PropTypes.object.isRequired,
id: React.PropTypes.number.isRequired
},
2015-11-15 16:26:32 +00:00
getDefaultProps() {
return {
id: null
};
},
disconnectUser() {
2016-01-26 10:44:34 +00:00
this.props.socket.emit("users:disconnect", {
2015-11-15 16:26:32 +00:00
id: this.props.id
});
},
render() {
return <button
className="btn btn-danger"
onClick={this.disconnectUser}>
Disconnect User</button>
}
});
2016-01-22 10:12:23 +00:00
const UserModal = React.createClass({
2016-01-26 10:44:34 +00:00
propTypes: {
user: React.PropTypes.object.isRequired,
socket: React.PropTypes.object.isRequired,
2016-02-03 16:12:08 +00:00
currentUser: React.PropTypes.object.isRequired,
close: React.PropTypes.func.isRequired
2016-01-26 10:44:34 +00:00
},
2015-10-18 15:23:39 +00:00
render() {
2015-11-15 16:26:32 +00:00
const currentUser = this.props.currentUser;
const user = this.props.user;
2015-10-18 15:23:39 +00:00
let hiveStats;
if (user.hive.id) {
hiveStats = [
2016-01-26 10:44:34 +00:00
<tr key="stats"><td><strong>Hive Stats</strong></td><td></td></tr>,
<tr key="elo">
2015-10-18 15:23:39 +00:00
<td>ELO</td>
<td>{user.hive.skill}</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="hours">
2015-10-18 15:23:39 +00:00
<td>Hours Played</td>
<td>{Math.round(user.hive.playTime / 3600)}</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="wins">
2015-10-18 15:23:39 +00:00
<td>Wins</td>
<td>{user.hive.wins}</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="losses">
2015-10-18 15:23:39 +00:00
<td>Losses</td>
<td>{user.hive.loses}</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="kills">
2015-10-18 15:23:39 +00:00
<td>Kills (/min)</td>
<td>{user.hive.kills} ({_.round(user.hive.kills / (user.hive.playTime / 60), 1)})</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="assists">
2015-10-18 15:23:39 +00:00
<td>Assists (/min)</td>
<td>{user.hive.assists} ({_.round(user.hive.assists / (user.hive.playTime / 60), 1)})</td>
</tr>,
2016-01-26 10:44:34 +00:00
<tr key="deaths">
2015-10-18 15:23:39 +00:00
<td>Deaths (/min)</td>
<td>{user.hive.deaths} ({_.round(user.hive.deaths / (user.hive.playTime / 60), 1)})</td>
</tr>
]
}
2015-11-15 16:26:32 +00:00
let adminOptions;
if (currentUser.admin) {
2016-01-26 10:44:34 +00:00
adminOptions = <DisconnectUserButton id={user.id} socket={this.props.socket} />;
2015-11-15 16:26:32 +00:00
}
2016-02-03 16:12:08 +00:00
2015-10-18 15:23:39 +00:00
return (
2016-02-03 16:55:34 +00:00
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" onClick={this.props.close}
aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 className="modal-title">
<img src="blank.gif"
className={"flag flag-" + ((user.country === null) ? "eu" :
user.country.toLowerCase()) }
alt={user.country} />&nbsp;
{user.username}
</h4>
</div>
<div className="modal-body">
<div className="text-center">
<img
src={user.avatar}
alt="User Avatar" />
2015-10-18 15:23:39 +00:00
</div>
2016-02-03 16:55:34 +00:00
<table className="table">
<tbody>
<tr>
<td>Lifeforms</td>
<td><LifeformIcons gatherer={{user: user}} /></td>
</tr>
<tr>
<td>Links</td>
<td>
<a href={enslUrl(user)}
className="btn btn-xs btn-primary"
target="_blank">ENSL Profile</a>&nbsp;
<a href={hiveUrl({user: user})}
className="btn btn-xs btn-primary"
target="_blank">Hive Profile</a>
</td>
</tr>
{hiveStats}
</tbody>
</table>
</div>
<div className="modal-footer">
{adminOptions}
<button type="button"
className="btn btn-default"
onClick={this.props.close}
>Close</button>
2015-10-18 15:23:39 +00:00
</div>
</div>
</div>
);
}
2015-07-28 15:54:29 +00:00
})
2016-01-22 10:12:23 +00:00
const UserItem = React.createClass({
2016-01-26 10:44:34 +00:00
propTypes: {
user: React.PropTypes.object.isRequired,
socket: React.PropTypes.object.isRequired,
2016-02-03 16:12:08 +00:00
currentUser: React.PropTypes.object.isRequired,
mountModal: React.PropTypes.func.isRequired
},
2016-02-03 16:55:34 +00:00
openModal(e) {
e.preventDefault();
2016-02-03 16:12:08 +00:00
this.props.mountModal({
component: UserModal,
props: {
user: this.props.user,
currentUser: this.props.currentUser,
socket: this.props.socket
}
});
2016-01-26 10:44:34 +00:00
},
2015-10-18 15:23:39 +00:00
render() {
2015-11-15 16:26:32 +00:00
const user = this.props.user;
const currentUser = this.props.currentUser;
2015-10-18 15:23:39 +00:00
return (
2016-02-03 16:12:08 +00:00
<li className="users-list-group-item">
<a href="#" onClick={this.openModal}>{user.username}</a>
2015-10-18 15:23:39 +00:00
</li>
);
}
});
2016-01-26 10:44:34 +00:00
const UserMenu = exports.UserMenu = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
2016-02-03 16:12:08 +00:00
users: React.PropTypes.array.isRequired,
mountModal: React.PropTypes.func.isRequired
},
componentDidMount() {
2016-04-18 22:23:50 +00:00
2016-01-26 10:44:34 +00:00
},
2015-08-11 00:25:25 +00:00
render() {
2015-11-15 16:26:32 +00:00
const users = this.props.users
2015-10-22 22:05:53 +00:00
.sort((a, b) => (a.username.toLowerCase() > b.username.toLowerCase()) ? 1 : -1)
2015-10-18 15:44:59 +00:00
.map(user => {
2015-11-15 16:26:32 +00:00
return <UserItem user={user} key={user.id}
2016-02-03 16:12:08 +00:00
currentUser={this.props.user} socket={this.props.socket}
mountModal={this.props.mountModal} />
2015-07-28 15:54:29 +00:00
});
return (
<div>
2016-02-03 16:12:08 +00:00
<ul className="users-list-group" id="user-list">
{users}
</ul>
2015-09-14 22:27:45 +00:00
</div>
2015-07-28 15:54:29 +00:00
);
}
});
2016-01-26 10:44:34 +00:00
const ProfileModal = exports.ProfileModal = React.createClass({
propTypes: {
2016-02-03 16:55:34 +00:00
user: React.PropTypes.object.isRequired,
socket: React.PropTypes.object.isRequired,
close: React.PropTypes.func.isRequired
},
getInitialState() {
const user = this.props.user;
console.log(user.profile);
return {
abilities: {
skulk: user.profile.abilities.skulk,
lerk: user.profile.abilities.lerk,
gorge: user.profile.abilities.gorge,
fade: user.profile.abilities.fade,
onos: user.profile.abilities.onos,
commander: user.profile.abilities.commander
},
skill: user.profile.skill
};
2016-01-26 10:44:34 +00:00
},
2015-08-28 20:28:50 +00:00
handleUserUpdate(e) {
e.preventDefault();
2016-02-03 16:55:34 +00:00
this.props.socket.emit("users:update:profile", {
2015-08-28 20:28:50 +00:00
id: this.props.user.id,
profile: {
2016-02-03 16:55:34 +00:00
abilities: this.state.abilities,
skill: this.state.skill
2015-08-28 20:28:50 +00:00
}
});
2016-02-03 16:55:34 +00:00
this.props.close();
},
handleAbilityChange(e) {
let abilities = this.state.abilities;
abilities[e.target.name] = e.target.checked;
this.setState({abilities: abilities});
},
handleSkillChange(e) {
this.setState({ skill: e.target.value });
2015-08-28 20:28:50 +00:00
},
2015-10-01 10:22:23 +00:00
2015-08-28 20:28:50 +00:00
render() {
2016-02-03 16:55:34 +00:00
const user = this.props.user;
if (!user) return false;
const abilities = this.state.abilities;
2015-08-28 20:28:50 +00:00
let abilitiesForm = [];
for (let lifeform in abilities) {
abilitiesForm.push(
<div key={lifeform} className="checkbox">
<label className="checkbox-inline">
2016-02-03 16:55:34 +00:00
<input type="checkbox" name={lifeform}
checked={abilities[lifeform]} onChange={this.handleAbilityChange} />
{_.capitalize(lifeform)}
2015-08-28 20:28:50 +00:00
</label>
</div>
);
}
2016-02-03 16:55:34 +00:00
let skillLevel = user.profile.skill;
2015-10-01 13:12:27 +00:00
let skillLevels = _.uniq(["Low Skill", "Medium Skill", "High Skill", skillLevel])
2015-09-19 23:11:43 +00:00
.filter(skill => { return typeof skill === 'string' })
2016-02-03 16:55:34 +00:00
.map(skill => { return <option key={skill} value={skill}>{skill}</option>});
2015-08-28 20:28:50 +00:00
return (
2016-02-03 16:55:34 +00:00
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" aria-label="Close"
onClick={this.props.close}>
<span aria-hidden="true">&times;</span>
</button>
<h4 className="modal-title">Profile</h4>
</div>
<div className="modal-body" id="profile-panel">
<form>
<div className="form-group">
<label>Player Skill</label><br />
<select
value={skillLevel}
className="form-control"
onChange={this.handleSkillChange}>
{skillLevels}
</select>
<p className="add-top"><small>
Try to give an accurate representation of your skill to raise
the quality of your gathers
</small></p>
</div>
<hr />
<div className="form-group">
<label>Preferred Lifeforms</label><br />
{abilitiesForm}
<p><small>
Specify which lifeforms you'd like to play in the gather
</small></p>
</div>
<hr />
<p className="small">
You will need to rejoin the gather to see your updated profile
</p>
<div className="form-group">
<button
type="submit"
className="btn btn-primary"
onClick={this.handleUserUpdate}>
Update &amp; Close</button>
</div>
</form>
2015-10-18 15:23:39 +00:00
</div>
</div>
2015-10-01 10:22:23 +00:00
</div>
2015-08-28 20:28:50 +00:00
);
}
});
2016-01-26 10:44:34 +00:00
const CurrentUser = exports.CurrentUser = React.createClass({
2015-08-11 00:25:25 +00:00
render() {
2015-07-28 15:54:29 +00:00
if (this.props.user) {
2016-01-22 10:12:23 +00:00
let adminOptions;
2015-10-22 21:59:47 +00:00
if (this.props.user.admin || this.props.user.moderator) {
2015-08-18 11:02:27 +00:00
adminOptions = (
<li>
2015-10-18 15:23:39 +00:00
<a href="#" data-toggle="modal" data-target="#adminmodal">
<i className="fa fa-magic fa-fw"></i> Administration
</a>
2015-08-18 11:02:27 +00:00
</li>
)
}
2015-07-28 15:54:29 +00:00
return (
2015-08-08 19:14:16 +00:00
<li className="dropdown">
<a className="dropdown-toggle" data-toggle="dropdown" href="#">
{this.props.user.username} &nbsp;<img src={this.props.user.avatar}
alt="User Avatar"
height="20"
width="20" /> <i className="fa fa-caret-down"></i>
</a>
<ul className="dropdown-menu dropdown-user">
<li>
2015-08-28 20:28:50 +00:00
<a data-toggle="modal"
data-target="#profilemodal"
2015-10-29 19:50:38 +00:00
href="#"><i className="fa fa-user fa-fw"></i> Profile</a>
</li>
<li>
<a data-toggle="modal"
data-target="#settingsmodal"
href="#"><i className="fa fa-gear fa-fw"></i> Settings</a>
</li>
2015-08-18 11:02:27 +00:00
{adminOptions}
</ul>
</li>
2015-07-28 15:54:29 +00:00
);
} else {
return false;
}
}
});
2015-08-11 00:37:43 +00:00
2016-01-26 10:44:34 +00:00
var AssumeUserIdButton = exports.AssumeUserIdButton = React.createClass({
propTypes: {
socket: React.PropTypes.object.isRequired,
gatherer: React.PropTypes.object.isRequired,
currentUser: React.PropTypes.object.isRequired,
},
2015-09-26 11:15:24 +00:00
assumeId(e) {
e.preventDefault();
if (this.props.gatherer) {
2016-01-26 10:44:34 +00:00
this.props.socket.emit("users:authorize", {
2015-09-26 11:15:24 +00:00
id: this.props.gatherer.id
});
// Refresh Gather list
setTimeout(() => {
2016-01-26 10:44:34 +00:00
this.props.socket.emit("gather:refresh");
2015-09-26 11:15:24 +00:00
}, 5000);
}
},
render() {
let currentUser = this.props.currentUser;
let gatherer = this.props.gatherer;
if (currentUser && gatherer) {
return <button
className="btn btn-xs btn-danger"
onClick={this.assumeId}>Assume User ID</button>
}
}
2015-10-01 10:22:23 +00:00
});