概要
前回のつづき。
Dynmapを導入しているサーバーからjavascriptを用いてデータを取得する。
今回はJSONデータを直接読み込む。
便宜上dynmapのあるwebページを {dynmap} と表記する。また、取得先のワールド名を{world}と表記する。
このワールド名は{dynmap}/up/configurationで列挙されているものを使えばよい。
手法
どこでもいいので以下のjsファイルを保存。
custom.js
var _dynmap = '{dynmap} ';
var _worldName = '{world}';
function test() {
var me = this;
me.init();
}
test.prototype = {
lasttimestamp:0,
init : function(){
var me = this;
me.update();
},
update : function(){
var me = this;
setTimeout( function(){ me.update(); } , 500 );
$.getJSON( _dynmap + '/up/world/' + _worldName + '/' + me.lasttimestamp,function(update){
me.lasttimestamp=update.timestamp;
console.log(update.timestamp);
console.log(update);
}
);
return;
}
}
var a = new test();
これでコンソールに変更通知のJSONが送られてくる。
内容は
return.json
{
"confighash": -82987328,
"currentcount": 2,
"hasStorm": false,
"isThundering": false,
"players": [
{
"account": "player1",
"armor": 8,
"health": 20,
"name": "player1",
"sort": 0,
"type": "player",
"world": "world",
"x": -1156.0,
"y": 20.0,
"z": 1526.0
},
{
"account": "wanwan",
"armor": 2,
"health": 20,
"name": "wanwan",
"sort": 0,
"type": "player",
"world": "world",
"x": -708.0,
"y": 58.0,
"z": 400.0
}
],
"servertime": 20112,
"timestamp": 1425229751386,
"updates": [
{
"name": "flat/0_-1/21_-1.png",
"timestamp": 1425229739013,
"type": "tile"
},
{
"name": "t/-1_-1/-29_-4.png",
"timestamp": 1425229740117,
"type": "tile"
}
]
}
のようになっている。
これで、取得時のtimestamp、timestamp時のプレイヤーデータ群、更新されたマップ群が取得できた。
注意
・このコードが正しいかどうかはわかりません。
・これはdynmapのソースコードを私的使用のために改変したものです。