Handle all packets

This commit is contained in:
Emily 2019-08-01 01:14:28 +02:00
parent fbae43c542
commit 5c93244934

View File

@ -70,10 +70,23 @@ class FLYSocket {
}
handle_packet3(data) { // TODO: implement (Whole data / history)
// Ixs
// I: Length
// xs
// s: DataStream
return ret = [];
let ret = {};
ret["uuid"] = data.getUint32(this.pointer); this.pointer += 4;
ret["maxFuel"] = data.getFloat32(this.pointer); this.pointer += 4;
ret["model"] = data.getUint16(this.pointer); this.pointer += 2;
let playernameLength = data.getUint8(this.pointer++);
let playername = "";
for (let i = 0; i < playernameLength; i++)
playername += String.fromCharCode( data.getUint8(this.pointer++) );
ret["playername"] = playername;
let timeline = [];
while (this.pointer < data.byteLength)
timeline.push( this.parseFrame(data) );
ret["timeline"] = timeline;
return ret;
}
handle_packet4(data) {
@ -86,10 +99,14 @@ class FLYSocket {
return ret;
}
handle_packet5(data) { // TODO: implement (One frame)
handle_packet5(data) {
// Ixs
// I: Length
// I: FlightUUID
// s: DataStream
let ret = [];
ret.push( data.getUint32(this.pointer) ); this.pointer += 4;
ret.push( this.parseFrame(data) );
return ret;
}
handle_packet255(data) {
@ -108,6 +125,43 @@ class FLYSocket {
throw TypeError(`Invalid DATA: ${Array.from( this.buffer2HexArray(data, --this.pointer) ).join(" ")}`);
}
parseFrame(data) {
// IBxs
// I: Timestamp
// B: Flag
// s: FrameStream
let ret = {};
ret["timestamp"] = data.getUint32(this.pointer); this.pointer += 4;
let flags = data.getUint8(this.pointer++);
if (flags & (1<<0)) {
let statusFlag = data.getUint8(this.pointer++);
ret["paused"] = !!( statusFlag & (1<<0) );
ret["engine"] = !!( statusFlag & (1<<1) );
}
if (flags & (1<<1)) {
ret["satisfaction"] = data.getUint8(this.pointer++);
}
if (flags & (1<<2)) {
ret["speed"] = data.getUint16(this.pointer); this.pointer += 2;
}
if (flags & (1<<3)) {
ret["altitude"] = data.getInt16(this.pointer); this.pointer += 2;
}
if (flags & (1<<4)) {
ret["health"] = data.getUint16(this.pointer); this.pointer += 2;
}
if (flags & (1<<5)) {
ret["fuel"] = data.getFloat32(this.pointer); this.pointer += 4;
}
if (flags & (1<<6)) {
ret["x"] = data.getFloat32(this.pointer); this.pointer += 4;
}
if (flags & (1<<7)) {
ret["y"] = data.getFloat32(this.pointer); this.pointer += 4;
}
return ret;
}
*buffer2HexArray(buff, start = 0, end = buff.byteLength) {
for (let i = start; i < end; i++) {
yield hex.push( buff.getUint8(i).toString(16) );
@ -117,11 +171,4 @@ class FLYSocket {
callbackDummy(_) {}
}
class Flight {
constructor(uuid) {
this.uuid = uuid;
this.playername;
}
}
console.log("OK") // TODO: please remove...