Skip to content

Commit

Permalink
Add 'status'
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosnt committed May 21, 2017
1 parent 1e652b2 commit 2789d10
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -12,6 +12,9 @@ Displays the number of online players of your Minecraft Server in your site with

In HTML, should be prefixed with `data-playercounter-`. E.g (`data-playercounter-ip`)

You can also display the server status by adding the attribute `data-playercounter-status`. It will display "online" or "offline".
See [example](examples/index.html#L11)

## Demo
- https://cdn.rawgit.com/leonardosnt/mc-player-counter/master/examples/index.html

Expand Down
22 changes: 17 additions & 5 deletions dist/mc-player-counter.js
Expand Up @@ -52,11 +52,23 @@ var PlayerCounter = function () {

var FORMAT_REGEX = /{\b(online|max)\b}/ig;
var data = JSON.parse(request.responseText);
var text = _this.format.replace(FORMAT_REGEX, function (match, group) {
return data.players[group];
});

_this.element.innerHTML = text;
var displayStatus = _this.element.getAttribute('data-playercounter-status');

// Display server status.
// offline/online
if (displayStatus !== null) {
_this.element.innerText = data.status ? 'online' : 'offline';
return;
}

// Display online players
// Make sure server is online
if (data.status) {
var text = _this.format.replace(FORMAT_REGEX, function (match, group) {
return data.players[group];
});
_this.element.innerHTML = text;
}
};
request.open('GET', 'https://mcapi.ca/query/' + this.ip + '/players');
request.send();
Expand Down
2 changes: 1 addition & 1 deletion dist/mc-player-counter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/index.html
Expand Up @@ -9,5 +9,6 @@
<body>
<p>Players on hypixel.net: <b><span data-playercounter-ip="mc.hypixel.net">0</span></b></p>
<p>Players on play.cubecraft.net: <b><span data-playercounter-ip="play.cubecraft.net" data-playercounter-format="{online}/{max}">0</span></b></p>
<p>Mineplex is currently <span data-playercounter-ip="us.mineplex.com" data-playercounter-status>...</span></p>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mc-player-counter",
"version": "1.0.0",
"version": "1.1.0",
"author": "leonardosnt",
"license": "MIT",
"repository": {
Expand Down
16 changes: 14 additions & 2 deletions src/mc-player-counter.js
Expand Up @@ -34,9 +34,21 @@ class PlayerCounter {

const FORMAT_REGEX = /{\b(online|max)\b}/ig;
const data = JSON.parse(request.responseText);
const text = this.format.replace(FORMAT_REGEX, (match, group) => data.players[group]);
const displayStatus = this.element.getAttribute('data-playercounter-status');

this.element.innerHTML = text;
// Display server status.
// offline/online
if (displayStatus !== null) {
this.element.innerText = data.status ? 'online' : 'offline';
return;
}

// Display online players
// Make sure server is online
if (data.status) {
const text = this.format.replace(FORMAT_REGEX, (match, group) => data.players[group]);
this.element.innerHTML = text;
}
};
request.open('GET', `https://mcapi.ca/query/${this.ip}/players`);
request.send();
Expand Down

0 comments on commit 2789d10

Please sign in to comment.