2017-08-09 11:05:55 +02:00
|
|
|
const async = require('async');
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
class Samp extends require('./core') {
|
|
|
|
run(state) {
|
2014-10-29 08:02:03 +01:00
|
|
|
async.series([
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendPacket('i',(reader) => {
|
2014-10-29 08:02:03 +01:00
|
|
|
state.password = !!reader.uint(1);
|
|
|
|
state.raw.numplayers = reader.uint(2);
|
|
|
|
state.maxplayers = reader.uint(2);
|
2017-08-09 11:05:55 +02:00
|
|
|
state.name = this.readString(reader,4);
|
|
|
|
state.raw.gamemode = this.readString(reader,4);
|
|
|
|
this.map = this.readString(reader,4);
|
2014-10-29 08:02:03 +01:00
|
|
|
c();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendPacket('r',(reader) => {
|
|
|
|
const ruleCount = reader.uint(2);
|
2014-10-29 08:02:03 +01:00
|
|
|
state.raw.rules = {};
|
2017-08-09 11:05:55 +02:00
|
|
|
for(let i = 0; i < ruleCount; i++) {
|
|
|
|
const key = this.readString(reader,1);
|
|
|
|
const value = this.readString(reader,1);
|
2014-10-29 08:02:03 +01:00
|
|
|
state.raw.rules[key] = value;
|
|
|
|
}
|
|
|
|
if('mapname' in state.raw.rules)
|
|
|
|
state.map = state.raw.rules.mapname;
|
|
|
|
c();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendPacket('d',(reader) => {
|
|
|
|
const playerCount = reader.uint(2);
|
|
|
|
for(let i = 0; i < playerCount; i++) {
|
|
|
|
const player = {};
|
2014-10-29 08:02:03 +01:00
|
|
|
player.id = reader.uint(1);
|
2017-08-09 11:05:55 +02:00
|
|
|
player.name = this.readString(reader,1);
|
2014-10-29 08:02:03 +01:00
|
|
|
player.score = reader.int(4);
|
|
|
|
player.ping = reader.uint(4);
|
|
|
|
state.players.push(player);
|
|
|
|
}
|
|
|
|
c();
|
2017-08-09 11:05:55 +02:00
|
|
|
},() => {
|
|
|
|
for(let i = 0; i < state.raw.numplayers; i++) {
|
2014-10-29 08:02:03 +01:00
|
|
|
state.players.push({});
|
|
|
|
}
|
2015-03-29 22:43:58 +02:00
|
|
|
c();
|
2014-10-29 08:02:03 +01:00
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.finish(state);
|
2014-10-29 08:02:03 +01:00
|
|
|
}
|
|
|
|
]);
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
readString(reader,lenBytes) {
|
|
|
|
const length = reader.uint(lenBytes);
|
2014-10-29 08:02:03 +01:00
|
|
|
if(!length) return '';
|
2017-08-09 11:05:55 +02:00
|
|
|
const string = reader.string({length:length});
|
2014-10-29 08:02:03 +01:00
|
|
|
return string;
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
sendPacket(type,onresponse,ontimeout) {
|
|
|
|
const outbuffer = Buffer.alloc(11);
|
2014-10-29 08:02:03 +01:00
|
|
|
outbuffer.writeUInt32BE(0x53414D50,0);
|
2017-08-09 11:05:55 +02:00
|
|
|
const ipSplit = this.options.address.split('.');
|
2014-10-29 08:02:03 +01:00
|
|
|
outbuffer.writeUInt8(parseInt(ipSplit[0]),4);
|
|
|
|
outbuffer.writeUInt8(parseInt(ipSplit[1]),5);
|
|
|
|
outbuffer.writeUInt8(parseInt(ipSplit[2]),6);
|
|
|
|
outbuffer.writeUInt8(parseInt(ipSplit[3]),7);
|
2017-08-09 11:05:55 +02:00
|
|
|
outbuffer.writeUInt16LE(this.options.port,8);
|
2014-10-29 08:02:03 +01:00
|
|
|
outbuffer.writeUInt8(type.charCodeAt(0),10);
|
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
this.udpSend(outbuffer,(buffer) => {
|
|
|
|
const reader = this.reader(buffer);
|
|
|
|
for(let i = 0; i < outbuffer.length; i++) {
|
2014-10-29 08:02:03 +01:00
|
|
|
if(outbuffer.readUInt8(i) !== reader.uint(1)) return;
|
|
|
|
}
|
|
|
|
onresponse(reader);
|
|
|
|
return true;
|
2017-08-09 11:05:55 +02:00
|
|
|
},() => {
|
2014-10-29 08:02:03 +01:00
|
|
|
if(ontimeout) {
|
|
|
|
ontimeout();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Samp;
|