initial commit of poker game
This commit is contained in:
28
web-server/public/js/views/AlertConfirmView.js
Normal file
28
web-server/public/js/views/AlertConfirmView.js
Normal file
@@ -0,0 +1,28 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
initialize: function(){
|
||||
this.setElement('#confirm-alert');
|
||||
$(this.el).modal({
|
||||
show : false,
|
||||
keyboard : true,
|
||||
backdrop : true
|
||||
});
|
||||
},
|
||||
events: {
|
||||
"click button.submit": "doConfirm"
|
||||
},
|
||||
display: function(vars, callback){
|
||||
if(vars && typeof callback === typeof Function){
|
||||
$(this.el).modal('show');
|
||||
$(this.el).find('.modal-title').html(vars.title);
|
||||
$(this.el).find('.modal-body').html(vars.content);
|
||||
this.callback = callback;
|
||||
}
|
||||
},
|
||||
doConfirm: function(vars){
|
||||
$(this.el).modal('hide');
|
||||
this.callback();
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
26
web-server/public/js/views/AlertErrorView.js
Normal file
26
web-server/public/js/views/AlertErrorView.js
Normal file
@@ -0,0 +1,26 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
initialize: function(){
|
||||
this.setElement('#error-alert');
|
||||
$(this.el).modal({
|
||||
show : false,
|
||||
keyboard : true,
|
||||
backdrop : true
|
||||
});
|
||||
},
|
||||
display: function(vars, failback, onClose){
|
||||
if(vars){
|
||||
$(this.el).modal('show');
|
||||
$(this.el).find('.modal-title').html(vars.title);
|
||||
$(this.el).find('.modal-body').html(vars.content);
|
||||
}
|
||||
if(typeof failback == typeof Function){
|
||||
failback();
|
||||
}
|
||||
if(typeof onClose == typeof Function){
|
||||
$(this.el).unbind('hidden').on('hidden', onClose);
|
||||
}
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
38
web-server/public/js/views/AlertGeneralView.js
Normal file
38
web-server/public/js/views/AlertGeneralView.js
Normal file
@@ -0,0 +1,38 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
initialize: function(){
|
||||
this.setElement('#general-alert');
|
||||
$(this.el).modal({
|
||||
show : false,
|
||||
keyboard : true,
|
||||
backdrop : true
|
||||
});
|
||||
},
|
||||
display: function(vars, url, timeout){
|
||||
var me = this;
|
||||
me.redirected = false;
|
||||
if(vars){
|
||||
$(me.el).modal('show');
|
||||
$(me.el).find('.modal-title').html(vars.title);
|
||||
$(me.el).find('.modal-body').html(vars.content);
|
||||
if(url){
|
||||
$('.modal-alert button').click(function(){
|
||||
$(this).unbind('click');
|
||||
me.redirected = true;
|
||||
Backbone.history.navigate(url);
|
||||
});
|
||||
me.autoRedirect(url, timeout);
|
||||
}
|
||||
}
|
||||
},
|
||||
autoRedirect: function(url, timeout){
|
||||
var me = this;
|
||||
setTimeout(function(){
|
||||
if(!me.redirected){
|
||||
Backbone.history.navigate(url);
|
||||
}
|
||||
}, timeout || 3000);
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
233
web-server/public/js/views/GlobalView.js
Normal file
233
web-server/public/js/views/GlobalView.js
Normal file
@@ -0,0 +1,233 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
el: 'body',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
me.bindGlobalEvents();
|
||||
me.selectedFriend = undefined;
|
||||
me.messageContainer = $('#messenger-input-container');
|
||||
me.friendModal = $('#add-friend-alert');
|
||||
me.friendList = $('#friend-list');
|
||||
me.messageArea = $('#messenger-area');
|
||||
me.msgs = {};
|
||||
me.options.eventPubSub.bind('getFriendList', function(){
|
||||
me.messageArea.html('');
|
||||
me.getFriends();
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click .goBack': 'goBack',
|
||||
'click #nav-logout-btn': 'logout',
|
||||
'click #friend-list li': 'selectChatFriend',
|
||||
'keypress #messenger-chatbox': 'submitFriendMessage',
|
||||
'click #show-add-friend-modal-btn': 'showAddFriendModal',
|
||||
'click #search-friends-btn': 'findFriend',
|
||||
'click #add-friend-btn': 'addFriend',
|
||||
'click #sync-friend-list': 'getFriends'
|
||||
},
|
||||
goBack: function(e){
|
||||
e.preventDefault();
|
||||
window.history.back();
|
||||
},
|
||||
logout: function(){
|
||||
this.options.game.disconnect();
|
||||
$('.username-placeholder').html('');
|
||||
$('.chips-placeholder').html('');
|
||||
$('.authed-section').hide();
|
||||
$('.unauthed-section').show();
|
||||
Backbone.history.navigate('#/login');
|
||||
},
|
||||
bindGlobalEvents: function(){
|
||||
var me = this;
|
||||
pomelo.on('onUpdateMyself', function(data){
|
||||
console.log('onUpdateMyself', data.user);
|
||||
me.options.game.session.user.chips = data.user.chips || me.options.game.session.user.chips;
|
||||
me.options.game.session.user.username = data.user.username || me.options.game.session.user.username;
|
||||
$('.username-placeholder').text(me.options.game.session.user.username);
|
||||
$('.chips-placeholder').text(me.options.game.session.user.chips);
|
||||
});
|
||||
pomelo.on('onUserChat', function(res){
|
||||
console.log('onUserChat', res);
|
||||
me.addMessage(res.username, res.username, res.msg);
|
||||
});
|
||||
},
|
||||
selectChatFriend: function(e){
|
||||
var item = $(e.currentTarget);
|
||||
if(!item.attr('did')){
|
||||
return false;
|
||||
}
|
||||
$('#friend-list li').removeClass('active');
|
||||
item.addClass('active');
|
||||
this.selectedFriend = $.trim(item.text());
|
||||
this.messageContainer.show();
|
||||
this.renderMessages();
|
||||
},
|
||||
renderMessages: function(){
|
||||
this.msgs[this.selectedFriend] = this.msgs[this.selectedFriend] || [];
|
||||
this.messageArea.html(_.template($('#ChatMessageList').html(), {
|
||||
items : this.msgs[this.selectedFriend]
|
||||
}));
|
||||
},
|
||||
submitFriendMessage: function(e){
|
||||
if(e.keyCode != 13) return;
|
||||
var me = this;
|
||||
var msg = $(e.currentTarget);
|
||||
if($.trim(msg.val()).length > 0 && me.selectedFriend){
|
||||
me.addMessage(me.selectedFriend, 'Me', msg.val());
|
||||
me.options.game.sendMessage(msg.val(), me.selectedFriend, function(e){
|
||||
if(e){
|
||||
me.addMessage(me.selectedFriend, 'System', (e == 'user-not-online' ? 'user is not online.' : e), null, 'error');
|
||||
}
|
||||
msg.scrollTop(msg[0].scrollHeight);
|
||||
msg.val('');
|
||||
});
|
||||
}
|
||||
},
|
||||
addMessage: function(context, username, text, time, type){
|
||||
if(time == null){
|
||||
time = new Date();
|
||||
}else if((time instanceof Date) === false){
|
||||
time = new Date(time);
|
||||
}
|
||||
var message = {
|
||||
date : utils.timeString(time),
|
||||
user : username,
|
||||
msg : utils.toStaticHTML(text),
|
||||
type : type || ''
|
||||
};
|
||||
this.msgs[context] = this.msgs[context] || [];
|
||||
this.msgs[context].push(message);
|
||||
this.messageArea.append(_.template($('#ChatMessageItem').html(), message));
|
||||
this.messageArea.scrollTop(this.messageArea[0].scrollHeight);
|
||||
},
|
||||
showAddFriendModal: function(){
|
||||
this.friendModal.find('input').val('');
|
||||
this.friendModal.find('#friend-results-list').html('');
|
||||
this.friendModal.modal('show');
|
||||
},
|
||||
findFriend: function(){
|
||||
var me = this;
|
||||
var search = this.friendModal.find('input');
|
||||
var friendList = this.friendModal.find('#friend-results-list');
|
||||
friendList.html('');
|
||||
pomelo.request('game.userHandler.getUsers', {
|
||||
name : 'username',
|
||||
val : search.val()
|
||||
}, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('userHandler.getUsers', res);
|
||||
me.renderUserList(friendList, res.matches, true);
|
||||
}
|
||||
});
|
||||
},
|
||||
renderUserList: function(dom, friends, showCheckbox){
|
||||
dom.html(_.template($('#FriendListTmpl').html(), {
|
||||
friends : friends,
|
||||
showCheckbox : showCheckbox
|
||||
}));
|
||||
},
|
||||
addFriend: function(){
|
||||
var me = this;
|
||||
var friendList = this.friendModal.find('#friend-results-list');
|
||||
var fid = friendList.find('input:checked').closest('li').attr('did');
|
||||
pomelo.request('game.userHandler.addFriend', {
|
||||
friend : fid
|
||||
}, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('chatHandler.addFriend', res);
|
||||
me.friendModal.modal('hide');
|
||||
}
|
||||
});
|
||||
},
|
||||
getFriends: function(){
|
||||
var me = this;
|
||||
pomelo.request('chat.chatHandler.getFriends', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('getFriends', res);
|
||||
me.renderUserList(me.friendList, res.friends);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
||||
|
||||
function GameClient(){
|
||||
this.connection = {};
|
||||
this.session = {};
|
||||
this.table = {};
|
||||
}
|
||||
/* gate handling */
|
||||
GameClient.prototype.getEntry = function(cb){
|
||||
var me = this;
|
||||
pomelo.init({
|
||||
host : window.location.hostname,
|
||||
port : 3014,
|
||||
log : true
|
||||
}, function(){
|
||||
pomelo.request('gate.gateHandler.queryEntry', {}, function(data){
|
||||
console.log('queryEntry', data);
|
||||
me.connection = data;
|
||||
pomelo.disconnect();
|
||||
cb(data);
|
||||
});
|
||||
});
|
||||
};
|
||||
/* user management */
|
||||
GameClient.prototype.register = function(userObj, cb, fb){
|
||||
var me = this;
|
||||
me.init(function(){
|
||||
pomelo.request('connector.entryHandler.register', userObj, function(res){
|
||||
if(res.code == 500){
|
||||
fb(res.error);
|
||||
}else{
|
||||
console.log('register', res);
|
||||
cb();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
GameClient.prototype.connect = function(userObj, cb){
|
||||
var me = this;
|
||||
me.init(function(){
|
||||
pomelo.request('connector.entryHandler.connect', userObj, function(res){
|
||||
if(res.code != 200){
|
||||
cb(res.error);
|
||||
}else{
|
||||
console.log('connect', res);
|
||||
me.session.token = res.token;
|
||||
me.session.user = res.user;
|
||||
cb();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
GameClient.prototype.disconnect = function(){
|
||||
this.session = {};
|
||||
pomelo.disconnect();
|
||||
};
|
||||
GameClient.prototype.init = function(cb){
|
||||
pomelo.init({
|
||||
host : this.connection.host,
|
||||
port : this.connection.port,
|
||||
log : true
|
||||
}, function(socket){
|
||||
console.log('init', socket);
|
||||
cb(socket);
|
||||
});
|
||||
};
|
||||
GameClient.prototype.sendMessage = function(msg, target, cb, fb){
|
||||
pomelo.request('chat.chatHandler.sendMessage', {
|
||||
content : msg,
|
||||
target : target
|
||||
}, function(res){
|
||||
cb(res.error);
|
||||
});
|
||||
};
|
40
web-server/public/js/views/LoginView.js
Normal file
40
web-server/public/js/views/LoginView.js
Normal file
@@ -0,0 +1,40 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
el: '#login',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
options.eventPubSub.bind('initLoginView', function(){
|
||||
$('.app-view').hide();
|
||||
me.$el.show('fast');
|
||||
me.formDom = me.$el.find('form');
|
||||
me.formDom.val('');
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click #login-btn': 'login'
|
||||
},
|
||||
login: function(e){
|
||||
e.preventDefault();
|
||||
var me = this;
|
||||
var obj = utils.collect(me.formDom);
|
||||
me.options.game.connect(obj, function(e){
|
||||
if(e){
|
||||
var content = e;
|
||||
if(e == 'invalid-user')
|
||||
content = 'The credentials you specified were invalid. Please try again.';
|
||||
else if(e == 'duplicate-session')
|
||||
content = 'You are already logged in from another machine.';
|
||||
Alerts.Error.display({
|
||||
title : 'Could Not Login',
|
||||
content : content
|
||||
});
|
||||
return;
|
||||
}
|
||||
me.formDom.find('input').val('');
|
||||
Backbone.history.navigate('#/tables');
|
||||
});
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
82
web-server/public/js/views/ProfileView.js
Normal file
82
web-server/public/js/views/ProfileView.js
Normal file
@@ -0,0 +1,82 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
el: '#profile',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
options.eventPubSub.bind('initProfileView', function(){
|
||||
$('.app-view').hide();
|
||||
me.$el.show('fast');
|
||||
me.getPlayerInfo(function(user){
|
||||
me.renderProfile(user);
|
||||
me.renderStats(user);
|
||||
})
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click #save-profile-btn': 'saveProfile',
|
||||
'click #update-password-btn': 'updatePassword'
|
||||
},
|
||||
getPlayerInfo: function(cb){
|
||||
pomelo.request('game.userHandler.getUsers', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('userHandler.getUsers', res);
|
||||
cb(res.matches[0]);
|
||||
}
|
||||
});
|
||||
},
|
||||
renderProfile: function(user){
|
||||
$('#profile-container').html(_.template($('#ProfileUpdateTmpl').html(), {
|
||||
user : user
|
||||
}));
|
||||
},
|
||||
renderStats: function(user){
|
||||
$('#user-stats-container').html(_.template($('#UserStatsTmpl').html(), {
|
||||
user : user
|
||||
}));
|
||||
},
|
||||
saveProfile: function(e){
|
||||
e.preventDefault();
|
||||
var form = $('#profile-form');
|
||||
var obj = utils.collect(form);
|
||||
pomelo.request('game.userHandler.setProfile', obj, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('userHandler.setProfile', res);
|
||||
Alerts.General.display({
|
||||
title : 'Profile Updated',
|
||||
content : 'Your profile information has been updated.'
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
updatePassword: function(e){
|
||||
e.preventDefault();
|
||||
var form = $('#password-form');
|
||||
var obj = utils.collect(form);
|
||||
if(obj.password !== obj.password2){
|
||||
Alerts.General.display({
|
||||
title : 'Password Update Failed',
|
||||
content : 'The confirmation password did not match the password you specified.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
pomelo.request('game.userHandler.setPassword', obj, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('userHandler.setPassword', res);
|
||||
form.find('input').val('');
|
||||
Alerts.General.display({
|
||||
title : 'Profile Updated',
|
||||
content : 'Your profile information has been updated.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
39
web-server/public/js/views/RegisterView.js
Normal file
39
web-server/public/js/views/RegisterView.js
Normal file
@@ -0,0 +1,39 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
el: '#register',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
options.eventPubSub.bind('initRegisterView', function(){
|
||||
$('.app-view').hide();
|
||||
me.$el.show('fast');
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click #register-btn': 'register'
|
||||
},
|
||||
register: function(e){
|
||||
e.preventDefault();
|
||||
var me = this;
|
||||
var form = this.$el.find('form');
|
||||
var obj = utils.collect(form);
|
||||
me.options.game.register(obj, function(){
|
||||
Alerts.General.display({
|
||||
title : 'User Created',
|
||||
content : 'Your username "'+obj.username+'" has been created. You will now be redirected to the login page.'
|
||||
});
|
||||
form.find('input').val('');
|
||||
Backbone.history.navigate('#/login');
|
||||
}, function(err){
|
||||
var content = '';
|
||||
if(err == 'user-exists')
|
||||
content = 'The username "'+obj.username+'" you specified has already been taken. Please try another username.';
|
||||
Alerts.Error.display({
|
||||
title : 'Could Not Register',
|
||||
content : content
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
63
web-server/public/js/views/TableListView.js
Normal file
63
web-server/public/js/views/TableListView.js
Normal file
@@ -0,0 +1,63 @@
|
||||
define(['jquery', 'backbone'], function($, Backbone){
|
||||
var View = Backbone.View.extend({
|
||||
el: '#tables',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
options.eventPubSub.bind('initTableListView', function(){
|
||||
$('.app-view').hide();
|
||||
me.$el.show('fast');
|
||||
me.tableDom = $('#table-list');
|
||||
me.getTables();
|
||||
me.options.eventPubSub.trigger('getFriendList');
|
||||
me.newTableContainer = $('#create-table-container');
|
||||
$('#messenger-area').html('');
|
||||
me.renderNewTable();
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click #register-btn': 'register',
|
||||
'click #create-table-btn': 'createTable',
|
||||
'click .join-table-btn': 'joinTable',
|
||||
'click #sync-table-list': 'getTables'
|
||||
},
|
||||
getTables: function(){
|
||||
var me = this;
|
||||
pomelo.request('game.tableHandler.getTables', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('getTables', res);
|
||||
me.renderTables(res.tables);
|
||||
}
|
||||
});
|
||||
},
|
||||
renderTables: function(tables){
|
||||
this.tableDom.html(_.template($('#TableListTmpl').html(), tables));
|
||||
},
|
||||
renderNewTable: function(){
|
||||
this.newTableContainer.html(_.template($('#newTableTmpl').html()));
|
||||
},
|
||||
createTable: function(e){
|
||||
e.preventDefault();
|
||||
var me = this;
|
||||
pomelo.request('game.tableHandler.createTable', utils.collect(this.newTableContainer), function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('createTable', res);
|
||||
me.getTables();
|
||||
// Backbone.history.navigate('#/table');
|
||||
}
|
||||
});
|
||||
},
|
||||
joinTable: function(e){
|
||||
e.preventDefault();
|
||||
var tid = $(e.currentTarget).closest('tr').attr('did');
|
||||
if(tid){
|
||||
Backbone.history.navigate('#/table/'+tid);
|
||||
}
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
312
web-server/public/js/views/TableView.js
Normal file
312
web-server/public/js/views/TableView.js
Normal file
@@ -0,0 +1,312 @@
|
||||
define(['jquery', 'backbone', 'collections/UserCollection', 'models/UserModel'], function($, Backbone, UserCollection, UserModel){
|
||||
var View = Backbone.View.extend({
|
||||
el: '#dashboard',
|
||||
initialize: function(options){
|
||||
var me = this;
|
||||
me.options = options;
|
||||
me.userlist = me.$el.find('.list-group');
|
||||
me.chatarea = me.$el.find('#chat-area');
|
||||
me.historyarea = me.$el.find('#history-area');
|
||||
me.countdownDom = $('#countdown-placeholder');
|
||||
me.timeout = {};
|
||||
me.bindEvents();
|
||||
me.channel = new UserCollection();
|
||||
options.eventPubSub.bind('initTableView', function(params){
|
||||
if(!params || !params.tid)
|
||||
Backbone.history.navigate('#/tables');
|
||||
$('.app-view').hide();
|
||||
me.$el.show('fast');
|
||||
me.$el.find('textarea').val('');
|
||||
me.gameSession = undefined;
|
||||
me.resetCountDown();
|
||||
me.historyarea.html('');
|
||||
me.actionIndex = 0;
|
||||
me.joinTable(params.tid, function(){
|
||||
me.renderGame();
|
||||
});
|
||||
});
|
||||
},
|
||||
events: {
|
||||
'click #login-btn': 'login',
|
||||
'click .list-group-item': 'showMemberProfile',
|
||||
'keypress #chat-container textarea': 'submitMessage',
|
||||
'click #join-game-btn': 'joinGame',
|
||||
'click #start-game-btn': 'startGame',
|
||||
'click .game-action': 'doAction',
|
||||
'click #create-new-game-btn': 'resetGame',
|
||||
'click #leave-table-btn': 'leaveTable',
|
||||
'click #remove-bots-btn': 'removeBots'
|
||||
},
|
||||
showMemberProfile: function(e){
|
||||
e.preventDefault();
|
||||
var dom = $(e.currentTarget);
|
||||
if(dom.attr('data-original-title')){
|
||||
return;
|
||||
}
|
||||
pomelo.request('game.userHandler.getUsers', {
|
||||
name : 'id',
|
||||
val : dom.attr('did')
|
||||
}, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('userHandler.getUsers', res);
|
||||
dom.popover({
|
||||
html : true,
|
||||
trigger : 'click',
|
||||
content : function(){
|
||||
return _.template($('#UserStatsTmpl').html(), {
|
||||
user : res.matches[0] || {}
|
||||
});
|
||||
}
|
||||
});
|
||||
dom.popover('toggle');
|
||||
}
|
||||
});
|
||||
},
|
||||
goBack: function(e){
|
||||
e.preventDefault();
|
||||
window.history.back();
|
||||
},
|
||||
buildUserList: function(){
|
||||
this.userlist.html(_.template($('#UserListItem').html(), {
|
||||
items : this.channel.models,
|
||||
userId : this.options.game.session.user.id
|
||||
}));
|
||||
},
|
||||
bindEvents: function(){
|
||||
var me = this;
|
||||
pomelo.on('onUpdateUsers', function(data){
|
||||
console.log('onUpdateUsers', data.members);
|
||||
me.channel = new UserCollection(data.members);
|
||||
me.buildUserList();
|
||||
});
|
||||
pomelo.on('onChat', function(data){
|
||||
console.log('onChat', data);
|
||||
me.addMessage(data.username, data.msg);
|
||||
});
|
||||
pomelo.on('onAdd', function(data){
|
||||
console.log('onAdd', data);
|
||||
me.channel.add(data.user);
|
||||
me.buildUserList();
|
||||
me.updateHistory('<b>'+data.user.username+'</b> joined table');
|
||||
});
|
||||
pomelo.on('onLeave', function(data){
|
||||
console.log('onLeave', data);
|
||||
me.channel.remove(data.user.id);
|
||||
me.removePlayerFromGame(data.user.id);
|
||||
me.buildUserList();
|
||||
me.renderGame();
|
||||
me.updateHistory('<b>'+data.user.username+'</b> left table');
|
||||
});
|
||||
pomelo.on('disconnect', function(reason){
|
||||
console.log('disconnect', reason);
|
||||
});
|
||||
pomelo.on('onTableJoin', function(data){
|
||||
console.log('onTableJoin', data);
|
||||
me.gameSession.playersToAdd.push(data.msg);
|
||||
me.renderGame();
|
||||
me.updateHistory('<b>'+data.msg.playerName+'</b> sat down');
|
||||
});
|
||||
pomelo.on('onTableEvent', function(data){
|
||||
console.log('onTableEvent', data);
|
||||
me.gameSession = data.msg;
|
||||
me.handleTimeout();
|
||||
me.renderGame();
|
||||
if(me.gameSession.actions.length == 0)
|
||||
me.actionIndex = 0;
|
||||
while(me.actionIndex < me.gameSession.actions.length){
|
||||
var action = me.gameSession.actions[me.actionIndex];
|
||||
me.updateHistory('<b>'+action.playerName+'</b> performed <b>'+action.action
|
||||
+'</b>'+(action.amount ? (' for <b>'+action.amount+'</b> chips') : ''));
|
||||
++me.actionIndex;
|
||||
}
|
||||
if(me.gameSession.gameWinners.length){
|
||||
for(var i=0;i<me.gameSession.gameWinners.length;++i){
|
||||
var winner = me.gameSession.gameWinners[i];
|
||||
me.updateHistory('<b>'+winner.playerName+'</b> wins <b>'+winner.amount+'</b> chips');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
handleTimeout: function(){
|
||||
var me = this;
|
||||
if(me.gameSession.state == 'JOIN')
|
||||
me.resetCountDown();
|
||||
if(me.gameSession.state == 'IN_PROGRESS' && (me.timeout.gid !== me.gameSession.id || me.timeout.tid !== me.gameSession.tid || me.timeout.player !== me.gameSession.currentPlayer)){
|
||||
me.resetCountDown();
|
||||
me.timeout = {
|
||||
player : me.gameSession.currentPlayer,
|
||||
gid : me.gameSession.id,
|
||||
tid : me.gameSession.tid,
|
||||
count : me.gameSession.gameMode == 'normal' ? 30 : 15,
|
||||
ref : me.initCountdown()
|
||||
};
|
||||
}
|
||||
},
|
||||
initCountdown: function(){
|
||||
var me = this;
|
||||
me.resetCountDown();
|
||||
return setInterval(function(){
|
||||
if(typeof me.gameSession.currentPlayer == 'number'){
|
||||
me.countdownDom.html(--me.timeout.count+' seconds left for <b>'+me.gameSession.players[me.gameSession.currentPlayer].playerName+'</b>');
|
||||
}
|
||||
if(me.timeout.count <= 0 || typeof me.gameSession.currentPlayer != 'number')
|
||||
me.resetCountDown();
|
||||
}, 1000);
|
||||
},
|
||||
resetCountDown: function(){
|
||||
this.countdownDom.html('');
|
||||
clearInterval(this.timeout.ref);
|
||||
},
|
||||
removePlayerFromGame: function(uid){
|
||||
var i;
|
||||
if(this.gameSession){
|
||||
for(i in this.gameSession.players)
|
||||
if(this.gameSession.players[i].id === uid)
|
||||
this.gameSession.playersToRemove.push(i);
|
||||
for(i in this.gameSession.playersToAdd)
|
||||
if(this.gameSession.playersToAdd[i].id === uid)
|
||||
this.gameSession.playersToAdd.splice(i, 1);
|
||||
}
|
||||
},
|
||||
submitMessage: function(e){
|
||||
var me = this;
|
||||
if(e.keyCode != 13) return;
|
||||
var msg = $(e.currentTarget);
|
||||
if($.trim(msg.val()).length > 0){
|
||||
this.options.game.sendMessage(msg.val(), 'table', function(e){
|
||||
if(e){
|
||||
me.addMessage('System', (e == 'user-not-online' ? 'user is not online.' : e), null, 'error');
|
||||
}
|
||||
msg.scrollTop(msg[0].scrollHeight);
|
||||
msg.val('');
|
||||
});
|
||||
}
|
||||
},
|
||||
addMessage: function(username, text, time){
|
||||
if(time == null){
|
||||
time = new Date();
|
||||
}else if((time instanceof Date) === false){
|
||||
time = new Date(time);
|
||||
}
|
||||
this.chatarea.append(_.template($('#ChatMessageItem').html(), {
|
||||
date : utils.timeString(time),
|
||||
user : username,
|
||||
msg : utils.toStaticHTML(text)
|
||||
}));
|
||||
this.chatarea.scrollTop(this.chatarea[0].scrollHeight);
|
||||
},
|
||||
renderGame: function(){
|
||||
$('#poker-container .panel-body').html(_.template($('#CurrentGameView').html(), {
|
||||
gameSession : this.gameSession,
|
||||
user : this.options.game.session.user,
|
||||
convertCard : this.convertCard,
|
||||
timeout : this.timeout
|
||||
}));
|
||||
},
|
||||
joinTable: function(tid){
|
||||
var me = this;
|
||||
pomelo.request('game.tableHandler.joinTable', {
|
||||
tid : tid
|
||||
}, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('joinTable', res);
|
||||
me.options.game.session.tid = res.tid;
|
||||
}
|
||||
});
|
||||
},
|
||||
leaveTable: function(e){
|
||||
e.preventDefault();
|
||||
var me = this;
|
||||
pomelo.request('game.tableHandler.leaveTable', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('leaveTable', res);
|
||||
delete me.options.game.session.tid;
|
||||
me.channel.reset();
|
||||
Backbone.history.navigate('#/tables');
|
||||
}
|
||||
});
|
||||
},
|
||||
resetGame: function(){
|
||||
delete this.gameSession;
|
||||
this.renderGame();
|
||||
},
|
||||
joinGame: function(e){
|
||||
e.preventDefault();
|
||||
pomelo.request('game.tableHandler.joinGame', {
|
||||
buyIn : this.$el.find('input[name="user-buyin-input"]').val()
|
||||
}, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('joinGame', res);
|
||||
}
|
||||
});
|
||||
},
|
||||
startGame: function(e){
|
||||
e.preventDefault();
|
||||
pomelo.request('game.tableHandler.startGame', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('startGame', res);
|
||||
}
|
||||
});
|
||||
},
|
||||
doAction: function(e){
|
||||
var me = this;
|
||||
var params = {
|
||||
action : $(e.currentTarget).attr('did')
|
||||
};
|
||||
if(params.action == 'bet')
|
||||
params.amt = me.$el.find('input[name="betAmount"]').val();
|
||||
pomelo.request('game.tableHandler.execute', params, function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('execute', res);
|
||||
if(params.action == 'bet')
|
||||
me.$el.find('input[name="betAmount"]').val('');
|
||||
}
|
||||
});
|
||||
},
|
||||
convertCard: function(card){
|
||||
var str = '';
|
||||
switch(card[0]){
|
||||
case 'J': str += 'jack'; break;
|
||||
case 'Q': str += 'queen'; break;
|
||||
case 'K': str += 'king'; break;
|
||||
case 'A': str += 'ace'; break;
|
||||
case 'T': str += '10'; break;
|
||||
default : str += card[0]; break;
|
||||
}
|
||||
str += '_of_';
|
||||
switch(card[1]){
|
||||
case 'D': str += 'diamonds'; break;
|
||||
case 'S': str += 'spades'; break;
|
||||
case 'C': str += 'clubs'; break;
|
||||
case 'H': str += 'hearts'; break;
|
||||
}
|
||||
return str += '.png';
|
||||
},
|
||||
updateHistory: function(msg){
|
||||
this.historyarea.append('<div>'+msg+'</div>');
|
||||
this.historyarea.scrollTop(this.historyarea[0].scrollHeight);
|
||||
},
|
||||
removeBots: function(){
|
||||
pomelo.request('game.tableHandler.removeBots', '', function(res){
|
||||
if(res.code != 200){
|
||||
console.log('error', res.error);
|
||||
}else{
|
||||
console.log('removed');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return View;
|
||||
});
|
Reference in New Issue
Block a user