Skip to content

Instantly share code, notes, and snippets.

@cimnine
Created December 4, 2012 08:14
Show Gist options
  • Save cimnine/4201769 to your computer and use it in GitHub Desktop.
Save cimnine/4201769 to your computer and use it in GitHub Desktop.
Little lib to access the HSR IntTe'12 "Testat2" chat server
(function(window) {
"use strict";
var Yack = function(playerToken) {
if(playerToken) {
this.playerToken = playerToken;
} else {
var o = this;
callSrv('Connect', {}, function(res) {
o.playerToken = res;
});
}
}
function callSrv(action, data, cb) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://sifsv-80018.hsr.ch/Service/ChatService.asmx/" + action,
dataType: "json",
data: data ? JSON.stringify(data) : null
}).done(function(data) {
if(cb) {
cb(data.d);
}
}).fail(console.error);
}
Yack.prototype.printToken = function() {
console.log(this.playerToken);
}
Yack.prototype.getChannels = function(cb) {
callSrv('GetChats', null, cb);
}
Yack.prototype.joinChannel = function(chatid, username, cb) {
callSrv('JoinChat', {
'playerToken': this.playerToken,
'chatId': chatid,
'userName': username
}, cb);
}
Yack.prototype.createChannel = function(channelName, cb) {
callSrv('CreateChannel', {
'playerToken': this.playerToken,
'channelName': channelName
}, cb);
}
Yack.prototype.getChannel = function(chatid, cb) {
callSrv('GetChat', {
'chatId': chatid
}, cb);
}
Yack.prototype.getLinesFrom = function(cb) {
callSrv('GetLinesFrom', {
'playerToken': this.playerToken
}, cb);
}
Yack.prototype.getUsers = function(chatid, cb) {
callSrv('GetPlayers', {
'chatId': chatid
}, cb);
}
Yack.prototype.isNameUnique = function(name, cb) {
callSrv('IsNameUnique', {
'name': name
}, cb);
}
Yack.prototype.leaveChannel = function(cb) {
callSrv('LeaveChat', {
'playerToken': this.playerToken
}, cb);
}
Yack.prototype.writeLine = function(text, cb) {
callSrv('WriteLine', {
'playerToken': this.playerToken,
'text': text
}, cb);
}
window.Yack = Yack;
})(window);
/*
* for debug purpose some helpers
*/
/*
var lcb = function() {
console.log(arguments)
};
var x = new Yack();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment