Commit abc9e436 authored by DoNaTio s.r.o.'s avatar DoNaTio s.r.o.
Browse files

client.js

parent 912fc98d
$(function () {
// Initialize varibles
var $window = $(window);
var $loginPage = $('.login-page');
var $gamePage = $('.game-page');
var log = new Log($('.messages'));
// Prompt for setting a username
var user = new ClientPlayer($('.pages'));
user.setLoginCallback(function () {
$loginPage.fadeOut();
$gamePage.fadeIn({
duration: 2000
});
$loginPage.off('click');
socket.emit('add user', user.getName());
});
user.init();
var connected = false;
var socket = io();
$(document).mousemove(function (event) {
var d = {
id: user.getId(),
x: event.pageX,
y: event.pageY
};
drawRacket(d)
socket.emit('move racket', d);
});
// Whenever the server emits 'login', log the login message
socket.on('login', function (data) {
connected = true;
log.add("Joined: " + data.numUsers);
log.add("Your name: " + data.username);
user.setId(data.id);
});
socket.on('show racket', function (data) {
drawRacket(data);
})
// Whenever the server emits 'user joined', log it in the chat body
socket.on('user joined', function (data) {
log.add(data.username + ' joined');
});
// Whenever the server emits 'user left', log it in the chat body
socket.on('user left', function (data) {
log.add(data.username + ' left');
});
function drawRacket(data) {
$('div.racket' + data.id).css({left: data.x + 20, top: data.y + 20});
}
$('.logout').click(function () {
socket.emit('disconnectUser', user);
$gamePage.fadeOut();
$loginPage.fadeIn();
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment