Objectify the event polling
This commit is contained in:
@@ -109,7 +109,6 @@ class ServerEventHandler(BaseHandler):
|
||||
def update(self, request):
|
||||
events = json.loads(request.POST['events'])['events']
|
||||
for evt in events:
|
||||
print repr(evt)
|
||||
if evt['type'] == 'chat':
|
||||
chat(evt['payload']['sender'], evt['payload']['message'])
|
||||
if evt['type'] == 'player-death':
|
||||
@@ -142,12 +141,14 @@ class PollHandler(BaseHandler):
|
||||
serverInfo = cache.get('caminus-server-info')
|
||||
if serverInfo == None:
|
||||
cachePlayerList()
|
||||
pollData = {'server-info': {}, 'user-info': {}}
|
||||
pollData['server-info'] = cache.get('caminus-server-info')
|
||||
pollData = {}
|
||||
info = {'server': {}, 'user': {}}
|
||||
info['server'] = cache.get('caminus-server-info')
|
||||
pollData['is-live'] = settings.CAMINUS_USE_BEANSTALKD
|
||||
if not request.user.is_anonymous():
|
||||
pollData['user-info']['balance'] = request.user.minecraftprofile.currencyaccount.balance
|
||||
info['user']['balance'] = request.user.minecraftprofile.currencyaccount.balance
|
||||
pollData['events'] = []
|
||||
pollData['info'] = info
|
||||
pollData['poll-id'] = timestamp
|
||||
if timestamp == "0" and settings.CAMINUS_USE_BEANSTALKD:
|
||||
pollData['poll-id'] = time.time()
|
||||
@@ -198,6 +199,5 @@ class VaultHandler(BaseHandler):
|
||||
slot.data = stack['data']
|
||||
updated = True
|
||||
if updated:
|
||||
print "Saving slot %s(%s): %s"%(slot.position, slot.id, slot.item)
|
||||
slot.save()
|
||||
return {'success': True}
|
||||
|
@@ -1,3 +1,36 @@
|
||||
function chatEvent(evt, payload) {
|
||||
$('#chat-display').append("<li>"+payload['sender']+": "+payload['message']);
|
||||
}
|
||||
|
||||
function joinEvent(evt, payload) {
|
||||
$('#chat-display').append("<li><em>"+payload['player']+" has joined</em></li>");
|
||||
}
|
||||
|
||||
function quitEvent(evt, payload) {
|
||||
$('#chat-display').append("<li><em>"+payload['player']+" has quit</em></li>");
|
||||
}
|
||||
|
||||
function broadcastEvent(evt, payload) {
|
||||
$('#chat-display').append("<li><strong>"+payload['message']+"</strong></li>");
|
||||
}
|
||||
|
||||
function deathEvent(evt, payload) {
|
||||
$('#chat-display').append("<li><em>"+payload['player']+" died.</em></li>");
|
||||
}
|
||||
|
||||
function heartbeatEvent(evt, payload) {
|
||||
var cls = "night";
|
||||
var t = new Date(payload['time']*1000);
|
||||
$('body').removeClass("morning night");
|
||||
if (t.getHours() > 8 && t.getHours() < 17) {
|
||||
cls = "morning";
|
||||
$('body').addClass("morning");
|
||||
} else {
|
||||
$('body').addClass("night");
|
||||
}
|
||||
$('#time-display').html(t.toLocaleTimeString());
|
||||
}
|
||||
|
||||
function sendChat(message) {
|
||||
$.post('/api/chat', {'message': message}, function(data) {
|
||||
$('#chat-line').val('');
|
||||
@@ -6,6 +39,14 @@ function sendChat(message) {
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
Caminus.eventPoll.onEvent('chat', chatEvent);
|
||||
Caminus.eventPoll.onEvent('join', joinEvent);
|
||||
Caminus.eventPoll.onEvent('quit', quitEvent);
|
||||
Caminus.eventPoll.onEvent('broadcast', broadcastEvent);
|
||||
Caminus.eventPoll.onEvent('player-death', deathEvent);
|
||||
Caminus.eventPoll.onEvent('server-heartbeat', heartbeatEvent);
|
||||
Caminus.eventPoll.onEvent('#connected', function() {$('#chat-display').html('');});
|
||||
|
||||
$('#server-interaction .drawer').each(function() {
|
||||
var canvas = $(this).children('.canvas');
|
||||
$(this).children('.drawer-label').click(function() {
|
||||
@@ -18,4 +59,8 @@ $(document).ready(function() {
|
||||
sendChat($('#chat-line').val());
|
||||
}
|
||||
});
|
||||
|
||||
Caminus.eventPoll.onEvent('#poll', function() {
|
||||
$('#balance-display').html(Caminus.eventPoll.info['user']['balance']);
|
||||
});
|
||||
});
|
||||
|
@@ -1,46 +1,97 @@
|
||||
function pollMessages(id) {
|
||||
$.get('/api/poll/'+id, function(data) {
|
||||
if (id == 0)
|
||||
$('#chat-display').html('');
|
||||
$('#balance-display').html(data['user-info']['balance']);
|
||||
$(data['events']).each(function(idx, evt) {
|
||||
console.log(evt['type']);
|
||||
if (evt['type'] == "chat") {
|
||||
$('#chat-display').append("<li>"+evt['payload']['sender']+": "+evt['payload']['message']);
|
||||
} else if (evt['type'] == 'join') {
|
||||
$('#chat-display').append("<li><em>"+evt['payload']['player']+" has joined</em></li>");
|
||||
} else if (evt['type'] == 'quit') {
|
||||
$('#chat-display').append("<li><em>"+evt['payload']['player']+" has quit</em></li>");
|
||||
} else if (evt['type'] == 'broadcast') {
|
||||
$('#chat-display').append("<li><strong>"+evt['payload']['message']+"</strong></li>");
|
||||
} else if (evt['type'] == 'player-death') {
|
||||
$('#chat-display').append("<li><em>"+evt['payload']['player']+" died.</em></li>");
|
||||
} else if (evt['type'] == 'server-heartbeat') {
|
||||
cls = "night";
|
||||
console.log(evt['payload']['time']);
|
||||
t = new Date(evt['payload']['time']*1000);
|
||||
$('body').removeClass("morning night");
|
||||
if (t.getHours() > 8 && t.getHours() < 17) {
|
||||
cls = "morning";
|
||||
$('body').addClass("morning");
|
||||
} else {
|
||||
$('body').addClass("night");
|
||||
}
|
||||
$('#time-display').html(t.toLocaleTimeString());
|
||||
}
|
||||
});
|
||||
if (data['is-live']) {
|
||||
window.setTimeout(function() {pollMessages(data['poll-id'])}, 1);
|
||||
} else {
|
||||
window.setTimeout(function() {pollMessages(data['poll-id'])}, 100000);
|
||||
}
|
||||
});
|
||||
var EventPoller = function() {
|
||||
this.id = 0;
|
||||
this.handlers = {};
|
||||
}
|
||||
|
||||
function poll() {
|
||||
pollMessages(0);
|
||||
EventPoller.prototype.onEvent = function(event_type, callback) {
|
||||
if (!(event_type in this.handlers)) {
|
||||
this.handlers[event_type] = new Array();
|
||||
}
|
||||
this.handlers[event_type].push(callback);
|
||||
}
|
||||
|
||||
EventPoller.prototype.start = function() {
|
||||
this.id = 0;
|
||||
this.dispatchEvent({
|
||||
type: '#start',
|
||||
payload: {}
|
||||
});
|
||||
this.poll();
|
||||
};
|
||||
|
||||
EventPoller.prototype.dispatchEvent = function(evt) {
|
||||
console.log(evt['type']);
|
||||
if (evt['type'] in this.handlers) {
|
||||
this.handlers[evt['type']].forEach(function (callback, idx, handlers) {
|
||||
callback(evt, evt['payload']);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
EventPoller.prototype.poll = function() {
|
||||
$.ajax({
|
||||
url: '/api/poll/'+this.id,
|
||||
success: this._successfulPoll,
|
||||
error: this._failedPoll,
|
||||
context: this,
|
||||
});
|
||||
};
|
||||
|
||||
EventPoller.prototype._failedPoll = function(data) {
|
||||
this.dispatchEvent({
|
||||
type: '#error',
|
||||
payload: {}
|
||||
});
|
||||
|
||||
if (this.id != 0) {
|
||||
this.dispatchEvent({
|
||||
type: '#disconnected',
|
||||
payload: {}
|
||||
});
|
||||
}
|
||||
this.id = 0;
|
||||
}
|
||||
|
||||
EventPoller.prototype._successfulPoll = function(data) {
|
||||
if (this.id != data['poll-id']) {
|
||||
var oldID = this.id;
|
||||
this.id = data['poll-id'];
|
||||
this.dispatchEvent({
|
||||
type: '#id-update',
|
||||
payload: {'old': oldID, 'new': this.id}
|
||||
});
|
||||
|
||||
if (oldID == 0) {
|
||||
this.dispatchEvent({
|
||||
type: '#connected',
|
||||
payload: {}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
this.info = data['info'];
|
||||
|
||||
this.dispatchEvent({
|
||||
type: '#poll',
|
||||
payload: {}
|
||||
});
|
||||
|
||||
var that = this;
|
||||
$(data['events']).each(function(idx, evt) {
|
||||
that.dispatchEvent(evt);
|
||||
});
|
||||
|
||||
if (data['is-live']) {
|
||||
window.setTimeout(this.poll.bind(this), 1);
|
||||
} else {
|
||||
window.setTimeout(this.poll.bind(this), 100000);
|
||||
}
|
||||
};
|
||||
|
||||
var Caminus = {
|
||||
eventPoll: new EventPoller(),
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
poll();
|
||||
Caminus.eventPoll.start();
|
||||
});
|
||||
|
Reference in New Issue
Block a user