-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (98 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
$(function () {
'use strict';
/* Character selection */
var character = 'frlg_boy';
$('#nav a').click(function (e) {
e.preventDefault();
$(this).tab('show');
})
// Init character previews
$.ajaxSetup({ async: false });
$.get('/json/characters.json', function(data) {
for (var i in data.characters) {
for (var j in data.characters[i]) {
$('.character-list:eq(' + i + ')')
.append('<li style="background-image:url(\'img/characters/' + data.characters[i][j] + '.gif\')" class="character-thumb"></li>');
}
}
});
function refreshIcon() {
$('.character-thumb').each(function() {
if ($(this).css('background-image').match('img/characters/' + character + '.gif') !== null) {
$(this).append('<span class="glyphicon glyphicon-ok"></span>');
}
else {
$(this).empty();
}
});
}
refreshIcon();
// Character click event
$('.character-list li').click(function(event) {
character = $(event.target).css('background-image').match(/(\w+).gif/)[1];
refreshIcon();
});
// Character mouseover animation interval
var interval_id;
// Character mouseover event
$('.character-list li').mouseover(function(event) {
var elt = $(event.target);
interval_id = window.setInterval(function() {
switch (elt.css('background-position-x')) {
case '0%':
elt.css('background-position-x', '528px');
break;
case '528px':
elt.css('background-position-x', '0px');
break;
case '0px':
elt.css('background-position-x', '480px');
break;
case '480px':
elt.css('background-position-x', '0%');
break;
}
}, 150);
});
// Character mouseout event
$('.character-list li').mouseout(function(event) {
window.clearInterval(interval_id);
$(event.target).css('background-position-x', '0%');
});
/* Submit */
$('form').submit(function(e) {
e.preventDefault();
var usernameInput = $('input#username');
var username = usernameInput.val();
// Remove errors
usernameInput.parent().removeClass('has-error');
usernameInput.siblings().remove();
// Check lengths
if (username.length == 0) {
usernameInput.focus().parent()
.addClass('has-error')
.append('<label class="control-label" for="username">Please enter your username.</label>');
}
else {
// Join the game !
$.ajax({
type: 'POST',
url: '/join',
data: { username: username, character: character }
})
.done(function(msg) {
console.log(msg);
switch (msg) {
case 'not available':
usernameInput.focus().parent()
.addClass('has-error')
.append('<label class="control-label" for="username">This username is not available.</label>');
break;
case 'ok':
window.location.href = 'game.html';
break;
}
});
}
});
})