/*
TibiaChar RPC Server.
Provies Tibia character profile data via a JSON API.
Hint: Hash the name with a seed, have users put it in their comment section,
and you have a confirmed method of linking tibia profiles to other profiles?
love, Erin Steph 2017
(See me on refugia!)
*/
/*------ installing ------//
Express: $ npm install express --save
Request: $ npm install request
//------------------------*/
// Config:
const port = 9980; //set the port for the RPC server to listen on.
//This is the code.
function tibiachar(){
this.request = require('request');
}
tibiachar.prototype = {
get : function(name, collo){
this.request('https://secure.tibia.com/community/?subtopic=characters&name=' + name, function(error, response, body){
var data = {};
data.name = name.replace(/[^A-Za-z]/g, ' ');
if(body.indexOf('
Sex: | ') > 1){
data['sex'] = body.split(' | Sex: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['sex'] = null;
}
if(body.indexOf('Vocation: | ') > 1){
data['vocation'] = body.split(' | Vocation: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['vocation'] = null;
}
if(body.indexOf('Level:') > 1){
data['level'] = body.split('Level: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['level'] = null;
}
if(body.indexOf('Achievement Points:') > 1){
data['points'] = body.split('Achievement Points: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['points'] = null;
}
if(body.indexOf('World: | ') > 1){
data['world'] = body.split('> | World: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['world'] = null;
}
if(body.indexOf('Residence: | ') > 1){
data['home'] = body.split(' | Residence: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['home'] = null;
}
if(body.indexOf('Guild Membership: | ') > 1){
data['guild'] = body.split(' | Guild Membership: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['guild'] = null;
}
if(body.indexOf('Last Login: | ') > 1){
data['last'] = body.split(' | Last Login: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['last'] = null;
}
if(body.indexOf('Comment: | ') > 1){
data['comment'] = body.split(' | Comment: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['comment'] = null;
}
if(body.indexOf('Account Status: | ') > 1){
data['status'] = body.split(' | Account Status: | ')[1].split(' | ')[0].replace(/[ ]+$/g, '');
}else{
data['status'] = null;
}
collo(data);
});
}
};
var tib = new tibiachar();
var rpc = require('express')();
rpc.use(function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Content-Type", "application/json");
next();
});
rpc.get('/:data', function(req, res){
tib.get(req.params.data, function(i){
res.send(JSON.stringify(i));
});
});
rpc.listen(port, function(){
console.log('> TibiaChar RPC Server listening on port ' + port + '.');
});