erinx icon

TibiaChar RPC Server

erinx | PRO | 06/05/17 02:44:27 PM UTC | 0 ⭐ | 480 👁️ | Never ⏰ | []
JavaScript |

3.68 KB

|

None

|

0 👍

/

0 👎

/*
 
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('<td>Sex:</td><td>') > 1){
          data['sex'] = body.split('<td>Sex:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['sex'] = null;
        }        
        if(body.indexOf('<td>Vocation:</td><td>') > 1){
          data['vocation'] = body.split('<td>Vocation:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['vocation'] = null;
        }   
        if(body.indexOf('Level:</td><td>') > 1){
          data['level'] = body.split('Level:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['level'] = null;
        }       
        if(body.indexOf('<nobr>Achievement Points:</nobr></td><td>') > 1){
          data['points'] = body.split('<nobr>Achievement Points:</nobr></td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['points'] = null;
        }       
        if(body.indexOf('<td>World:</td><td>') > 1){
          data['world'] = body.split('><td>World:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['world'] = null;
        }        
        if(body.indexOf('<td>Residence:</td><td>') > 1){
          data['home'] = body.split('<td>Residence:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['home'] = null;
        }
        if(body.indexOf('<td>Guild&#160;Membership:</td><td>') > 1){
          data['guild'] = body.split('<td>Guild&#160;Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['guild'] = null;
        }       
        if(body.indexOf('<td>Last Login:</td><td>') > 1){
          data['last'] = body.split('<td>Last Login:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['last'] = null;
        }   
        if(body.indexOf('<td valign=top>Comment:</td><td>') > 1){
          data['comment'] = body.split('<td valign=top>Comment:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
        }else{
          data['comment'] = null;
        } 
        if(body.indexOf('<td>Account&#160;Status:</td><td>') > 1){
          data['status'] = body.split('<td>Account&#160;Status:</td><td>')[1].split('</td>')[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 + '.');
});

Comments