summaryrefslogtreecommitdiff
path: root/public/js
diff options
context:
space:
mode:
Diffstat (limited to 'public/js')
-rw-r--r--public/js/auth.js38
-rw-r--r--public/js/chat.js4
-rw-r--r--public/js/game.js4
-rw-r--r--public/js/util.js3
4 files changed, 48 insertions, 1 deletions
diff --git a/public/js/auth.js b/public/js/auth.js
new file mode 100644
index 0000000..6cdc4c0
--- /dev/null
+++ b/public/js/auth.js
@@ -0,0 +1,38 @@
+var Auth = {
+ init: function(){
+ Auth.bind();
+ if (Auth.loggedIn()) {
+ }
+ else {
+ $("#login").show();
+ }
+ },
+ bind: function(){
+ $("#login-go").click(Auth.login);
+ $("#username").keydown(function(e){
+ switch (e.keyCode) {
+ case 13: // enter
+ Auth.login();
+ break
+ }
+ });
+ },
+ loggedIn: function(){
+ if (localStorage && localStorage.nick) {
+ Game.nick = strip(localStorage.nick);
+ if (Game.nick.length == 0) return false;
+ return true;
+ }
+ return false;
+ },
+ login: function(){
+ Game.nick = strip( $("#username").val() );
+ if (Game.nick.length == 0) return;
+ localStorage.nick = Game.nick;
+ $("#username").val("");
+ $("#login").hide();
+ },
+ logout: function(){
+ $("#login").show();
+ }
+}
diff --git a/public/js/chat.js b/public/js/chat.js
index 67b6255..752ed67 100644
--- a/public/js/chat.js
+++ b/public/js/chat.js
@@ -2,6 +2,10 @@ var Chat = {
height: 300,
init: function(){
+ Chat.bind();
+ },
+
+ bind: function(){
$("#chat-message").on("keydown", Chat.keys);
},
diff --git a/public/js/game.js b/public/js/game.js
index 3b0a71b..af05ad9 100644
--- a/public/js/game.js
+++ b/public/js/game.js
@@ -1,13 +1,14 @@
var socket = io.connect(window.location.hostname);
var Game = {
- nick: "ryz",
+ nick: null,
init: function(){
// Bind events from the server
socket.on('event-join', Events.receive.join);
socket.on('event-message', Events.receive.message);
+ Auth.init();
Chat.init();
}
}
@@ -15,6 +16,7 @@ var Game = {
var Events = {
receive: {
+
join: function(data){
for (var i in data.messages) {
Chat.add(data.messages[i]);
diff --git a/public/js/util.js b/public/js/util.js
index 12a331a..e51b844 100644
--- a/public/js/util.js
+++ b/public/js/util.js
@@ -1,6 +1,9 @@
function trim (s) {
return s.replace(/^\s+/, "").replace(/\s+$/, "");
}
+function strip (s) {
+ return trim(s).replace(/\W+/, "");
+}
function sanitize (s) {
return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
}