Skip to content

Commit 51a7c1b

Browse files
committed
add ability to change player names
1 parent 4522fe9 commit 51a7c1b

File tree

11 files changed

+235
-31
lines changed

11 files changed

+235
-31
lines changed

assets/js/classes/game/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Game {
3838
*/
3939
bindEventHandlers() {
4040
this.vent.sub('save', () => this.save());
41+
this.vent.sub('player:nameChanged', () => {
42+
this.render();
43+
this.save();
44+
});
4145
this.vent.sub('render', () => this.render());
4246
this.vent.sub('drawCard', () => this.onDrawCard());
4347
this.vent.sub('error', (error) => this.error(error));
@@ -73,11 +77,10 @@ class Game {
7377
}
7478
});
7579

76-
$(document).on('click', '.alert-link', function(event) {
80+
$(document).on('click', '.modal-link--alert', function(event) {
7781
event.preventDefault();
7882
vex.dialog.alert($(this).attr('data-alert-content'));
7983
});
80-
8184
}
8285

8386
/**

assets/js/classes/player/index.js

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import $ from 'jquery';
22
import roles from '../../modules/roles';
33
import exporter from '../../modules/exporter';
4+
import vex from '../../plugins/vex';
45

56
const ACTIVE_CLASS = 'player--active';
67
const SECOND_PLAYER_ACTIVE = 'body--player2-active';
78
const HIGHLIGHT_SCORE_CLASS = 'player--highlight-score';
8-
9+
const MAX_LENGTH_NAME = 16;
910
/**
1011
* Each game has players.
1112
*/
@@ -24,6 +25,37 @@ class Player {
2425
this.guessCount = opts.guessCount || 0;
2526
this.guess = opts.guess || null;
2627
this.$el = $('#' + this.id);
28+
this.bindEventHandlers();
29+
}
30+
31+
/**
32+
* Bind event handlers for this player.
33+
*/
34+
bindEventHandlers() {
35+
this.$el.on('click', '.player__name', (event) => {
36+
event.preventDefault();
37+
this.showChangeNamePrompt();
38+
});
39+
}
40+
41+
/**
42+
* Show modal to change name
43+
*/
44+
showChangeNamePrompt() {
45+
let input = "<input name='vex' type='text' class='vex-dialog-prompt-input' placeholder='Enter new name' maxlength='" + MAX_LENGTH_NAME +"' />"
46+
vex.dialog.prompt({
47+
message: 'Change your name, ' + this.name + ':',
48+
input: input,
49+
callback: (value) => {
50+
//escape html entities with use of .text()
51+
value = $('<div>').html($.trim(value)).text();
52+
//false is returned on hitting esc and it's tranformed to a string above.
53+
//this has caveat of preventing user from setting their username to 'false' but...oh well. improve if ever needed.
54+
if (value && value !== 'false') {
55+
this.setName(value);
56+
}
57+
}
58+
});
2759
}
2860

2961
/**
@@ -70,7 +102,7 @@ class Player {
70102
$points.find('em').html(this.score === 1 ? 'point' : 'points');
71103

72104
this.$el.find('.player__role').html(this.getRoleHtml());
73-
this.$el.find('.player__name').html(this.name);
105+
this.$el.find('.player__name a').html(this.name);
74106
return this;
75107
}
76108

@@ -92,6 +124,20 @@ class Player {
92124
return this;
93125
}
94126

127+
/**
128+
* Set name
129+
* @param {string}
130+
*/
131+
setName(name) {
132+
if (name.length > MAX_LENGTH_NAME) {
133+
name = name.substr(0, MAX_LENGTH_NAME);
134+
}
135+
this.name = name;
136+
this.render();
137+
this.vent.pub('player:nameChanged');
138+
return this;
139+
}
140+
95141
/**
96142
* @param {number} num
97143
*/
@@ -174,14 +220,14 @@ class Player {
174220
buildAlertHintLink(linkText) {
175221
let alert = "When you have three correct guesses in a row, you will be allowed to pass.";
176222
alert += " It is to your advantage to pass, so you can avoid gaining points.";
177-
return "<a href='#' class='alert-link' data-alert-content='" + alert + "'>" + linkText + "</a>";
223+
return "<a href='#' class='modal-link modal-link--alert' data-alert-content='" + alert + "'>" + linkText + "</a>";
178224
}
179225

180226
/**
181227
* Export deck
182228
* @return {object}
183229
*/
184-
export() {
230+
export () {
185231
return exporter.exportObj(this, ['$el', 'vent']);
186232
}
187233
};

assets/scss/main.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@import 'modules/_base.scss';
88

99

10-
.alert-link {
10+
.modal-link {
1111
text-decoration: none;
1212
border-bottom: 1px dashed #999;
1313
}

assets/scss/modules/_players.scss

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818
font-size: 1.3em;
1919
margin: 0.5em 0;
2020
padding: 0;
21+
22+
//TODO: fix the jumpiness.
23+
a {
24+
&:hover {
25+
&:after {
26+
display: inline-block;
27+
}
28+
}
29+
30+
&:after {
31+
display: none;
32+
position: relative;
33+
top: -5px;
34+
left: 7px;
35+
content: '\f040'; //edit icon. fa-pencil.
36+
font: normal normal normal 14px/1 FontAwesome;
37+
38+
}
39+
}
2140
}
2241

2342
.player__role {

public/css/main.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/main.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/app.bundle.js

+75-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/views/partials/deck.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
</div>
1414
<label class="points-otl show-media-non-lg-only">
1515
<span class="points-otl__point-value">0</span>
16-
<span class="points-otl__text">points</span> <a href="#" class="alert-link" data-alert-content="The points on the line will increase until the Guesser gets a wrong answer. Then, all points on the line go to the Guesser. Remember: the goal is to end the game with as few points as possible.">on the line</a>
16+
<span class="points-otl__text">points</span> <a href="#" class="modal-link modal-link--alert" data-alert-content="The points on the line will increase until the Guesser gets a wrong answer. Then, all points on the line go to the Guesser. Remember: the goal is to end the game with as few points as possible.">on the line</a>
1717
</label>
1818
</div>
1919
<div class="deck__item">
2020
<div class="points-otl points-otl--lg show-media-lg-only">
2121
<span class="points-otl__point-value">0</span>
22-
<span class="points-otl__text">points</span> <a href="#" class="alert-link" data-alert-content="The points on the line will increase until the Guesser gets a wrong answer. Then, all points on the line go to the Guesser. Remember: the goal is to end the game with as few points as possible.">on the line</a>
22+
<span class="points-otl__text">points</span> <a href="#" class="modal-link modal-link--alert" data-alert-content="The points on the line will increase until the Guesser gets a wrong answer. Then, all points on the line go to the Guesser. Remember: the goal is to end the game with as few points as possible.">on the line</a>
2323
</div>
2424
</div>
2525
</div>

public/views/partials/players.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<em></em>
1010
</div>
1111
</div>
12-
<h3 class="player__name"></h3>
12+
<h3 class="player__name">
13+
<a href="#" class="modal-link" title="Change name">
14+
</a>
15+
</h3>
1316
<h4 class="player__role"></h4>
1417
<label class="player__hint"></label>
1518
</div>

public/views/partials/rules.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
</ul>
2424
<hr />
2525
<h2>Protips:</h2>
26-
<p>You can also use your keyboard to make moves when it's your turn:</p>
26+
<p>Use your keyboard to make moves when it's your turn:</p>
2727
<ul>
2828
<li><b>Enter: </b> Draw a card.</li>
2929
<li><b>Up arrow: </b> Guess high.</li>
3030
<li><b>Down arrow: </b> Guess low.</li>
3131
<li><b>Right arrow: </b> Pass.</li>
3232
</ul>
33+
<p>Click your name to change it anytime during the game.</p>
3334
<p>It's also worth mentioning that there's a cheat mode. If you can <a href="https://github.com/tublitzed/hi-lo">find it</a>, you can use it. It'll allow you to draw more cards at a time.</p>
3435
</section>

tests/test.bundle.js

+75-9
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)