Skip to content

Commit

Permalink
Version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gpedro committed Feb 22, 2015
0 parents commit 702050c
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
npm-debug.log
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"node": true
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
48 changes: 48 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
module.exports = function (grunt) {
// Show elapsed time at the end
require('time-grunt')(grunt);
// Load all grunt tasks
require('load-grunt-tasks')(grunt);

grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
gruntfile: {
src: ['Gruntfile.js']
},
js: {
src: ['*.js']
},
test: {
src: ['test/**/*.js']
}
},
mochacli: {
options: {
reporter: 'nyan',
bail: true
},
all: ['test/*.js']
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
js: {
files: '<%= jshint.js.src %>',
tasks: ['jshint:js', 'mochacli']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'mochacli']
}
}
});

grunt.registerTask('default', ['jshint', 'mochacli']);
};
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-url]][daviddm-image]

> Catch information like players online, owner, motd, map weight, monsters, npcs, etc. passing IP and PORT from Tibia Server

## Install

```sh
$ npm install --save tibia-status
```


## Usage

```js
var ServerStatus = require('tibia-status');

var otserv = new ServerStatus('underwar.org');
otserv
.then(function(data) {
console.log(data);
})
.fail(function(err) {
console.error(err);
});

/*
{ server:
{ uptime: 82139,
ip: '192.99.19.223',
servername: 'UnderWar',
port: 7171,
location: 'Brazil',
url: 'http://undewar.org',
server: 'UnderWar~W.G',
version: '0.5.0',
client: '8.60' },
owner: { name: 'TGC', email: '[email protected]' },
players: { online: 329, max: 1200, peak: 518 },
monsters: 50704,
map: { name: '', author: '', width: 65000, height: 65000 },
rates: { experience: 1, magic: 1, skill: 1, loot: 1, spawn: 3 },
motd: 'Welcome to the UnderWar Server! TeamSpeak 3: ts3.underwar.org' }
*/
```

## License

The MIT License (MIT)

Copyright (c) 2015 TibiaJS

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[npm-url]: https://npmjs.org/package/serverstatus
[npm-image]: https://badge.fury.io/js/serverstatus.svg
[travis-url]: https://travis-ci.org/TibiaJS/serverstatus
[travis-image]: https://travis-ci.org/TibiaJS/serverstatus.svg?branch=master
[daviddm-url]: https://david-dm.org/TibiaJS/serverstatus.svg?theme=shields.io
[daviddm-image]: https://david-dm.org/TibiaJS/serverstatus
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./src');
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "tibia-status",
"version": "0.0.1",
"description": "Catch information like players online, owner, motd, map weight, monsters, npcs, etc. passing IP and PORT from Tibia Server",
"homepage": "https://github.com/TibiaJS/serverstatus",
"author": {
"name": "Gabriel Pedro",
"email": "[email protected]",
"url": "https://gpedro.net"
},
"contributors": [{
"name": "Ranieri Althoff",
"email": "[email protected]",
"url": "http://otserv.com.br"
}],
"repository": "TibiaJS/serverstatus",
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"otserv",
"tibia",
"cipsoft",
"server",
"status"
],
"dependencies": {
"q": "^1.2.0",
"xml2js": "^0.4.5"
},
"devDependencies": {
"grunt-cli": "^0.1.13",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-nodeunit": "^0.4.1",
"grunt-contrib-watch": "^0.6.1",
"load-grunt-tasks": "^1.0.0",
"time-grunt": "^1.0.0",
"grunt-mocha-cli": "^1.11.0",
"jshint-stylish": "^1.0.0"
},
"scripts": {
"test": "grunt"
}
}
60 changes: 60 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';
var Q = require('q');

function ServerStatus(host, port) {
this.host = host;
this.port = port || 7171;
this.deferred = Q.defer();

return this.execute();
}

ServerStatus.prototype.getMessage = function() {
return new Buffer([0x06, 0x00, 0xFF, 0xFF, 0x69, 0x6E, 0x66, 0x6F]);
};

ServerStatus.prototype.execute = function() {
var net = require('net')
, client = new net.Socket()
, status = undefined
, self = this;

client.connect(self.port, self.host, function() {
client.write(self.getMessage());
});

client.on('data', function(data) {
var xml = require('xml2js')
, parser = xml.Parser()
, Status = require('./status');

parser.parseString(data.toString('utf8'), function(err, data) {
if(err) {
self.deferred.reject(new Error(err));
}

status = new Status(data);
self.deferred.resolve(status);
});

client.destroy();

return self.deferred.promise;
});

client.on('timeout', function() {
self.deferred.reject(new Error('Server Timeout'));
});

client.on('error', function(err) {
if(err.code == 'ENOTFOUND') {
self.deferred.reject(new Error('Server not found'));
} else {
self.deffered.reject(err);
}
});

return self.deferred.promise;
};

module.exports = ServerStatus;
10 changes: 10 additions & 0 deletions src/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

function Map(obj) {
this.name = obj.name;
this.author = obj.author;
this.width = parseInt(obj.width);
this.height = parseInt(obj.height);
}

module.exports = Map;
8 changes: 8 additions & 0 deletions src/owner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

function Owner(obj) {
this.name = obj.name;
this.email = obj.email;
}

module.exports = Owner;
9 changes: 9 additions & 0 deletions src/players.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function Players(obj) {
this.online = parseInt(obj.online);
this.max = parseInt(obj.max);
this.peak = parseInt(obj.peak);
}

module.exports = Players;
11 changes: 11 additions & 0 deletions src/rates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

function Rates(obj) {
this.experience = parseInt(obj.experience);
this.magic = parseInt(obj.magic);
this.skill = parseInt(obj.skill);
this.loot = parseInt(obj.loot);
this.spawn = parseInt(obj.spawn);
}

module.exports = Rates;
15 changes: 15 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

function Server(obj) {
this.uptime = parseInt(obj.uptime);
this.ip = obj.ip;
this.servername = obj.servername;
this.port = parseInt(obj.port);
this.location = obj.location;
this.url = obj.url;
this.server = obj.server;
this.version = obj.version;
this.client = obj.client;
}

module.exports = Server;
21 changes: 21 additions & 0 deletions src/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var Server = require('./server')
, Owner = require('./owner')
, Players = require('./players')
, Map = require('./map')
, Rates = require('./rates');

function ServerInfo(obj) {
obj = obj.tsqp;

this.server = new Server(obj.serverinfo[0].$);
this.owner = new Owner(obj.owner[0].$);
this.players = new Players(obj.players[0].$);
this.monsters = parseInt(obj.monsters[0].$.total);
this.map = new Map(obj.map[0].$);
this.rates = new Rates(obj.rates[0].$);
this.motd = obj.motd[0];
}

module.exports = ServerInfo;
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*global describe, it */
'use strict';
var assert = require('assert');
var ServerStatus = require('../');

describe('Server Status', function () {
it('existing server', function () {
var otserv = new ServerStatus('underwar.org');
otserv.then(function(data){
assert(data.server.servername, 'UnderWar');
});
});

it('unknown server', function() {
var otserv = new ServerStatus('underwax.org');
otserv
.then(function(data) {
assert(data, !undefined);
})
.fail(function(err) {
assert(err, new Error('Server not found'));
});
});
});

0 comments on commit 702050c

Please sign in to comment.