mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 08:33:12 +09:00
refactor(minimap): use discriminated union for PlayerPositionRecord and extract readSignedInt24
This commit is contained in:
1 parent
245ceb14fa
commit
c3136532b8
1 file changed
+40
-19
@@ -12,17 +12,31 @@ export interface ParsedPlayerPositions {
|
|||||||
positions: PlayerPositionRecord[];
|
positions: PlayerPositionRecord[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PlayerPositionRecord {
|
export type PlayerPositionRecord =
|
||||||
type: number;
|
| PlayerPositionRecordType0
|
||||||
|
| PlayerPositionRecordType1
|
||||||
|
| PlayerPositionRecordType2;
|
||||||
|
|
||||||
|
export interface PlayerPositionRecordType0 {
|
||||||
|
type: 0;
|
||||||
playerId: number;
|
playerId: number;
|
||||||
// Position record fields
|
tSec: number;
|
||||||
tSec?: number;
|
sec: number;
|
||||||
sec?: number;
|
xTiles: number;
|
||||||
xTiles?: number;
|
yTiles: number;
|
||||||
yTiles?: number;
|
}
|
||||||
// Session record fields
|
|
||||||
tMs?: number;
|
export interface PlayerPositionRecordType1 {
|
||||||
playerName?: string;
|
type: 1;
|
||||||
|
playerId: number;
|
||||||
|
tMs: number;
|
||||||
|
playerName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PlayerPositionRecordType2 {
|
||||||
|
type: 2;
|
||||||
|
playerId: number;
|
||||||
|
tMs: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ParsedPlayerPos {
|
export interface ParsedPlayerPos {
|
||||||
@@ -32,6 +46,17 @@ export interface ParsedPlayerPos {
|
|||||||
sec: number;
|
sec: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a signed 24-bit big-endian integer from binary data
|
||||||
|
* @param buf The binary data buffer
|
||||||
|
* @param offset Offset in the buffer to read from
|
||||||
|
* @returns Signed 24-bit integer value
|
||||||
|
*/
|
||||||
|
function readSignedInt24(buf: BinaryData, offset: number): number {
|
||||||
|
const unsigned = (readUInt8(buf, offset) << 16) | (readUInt8(buf, offset + 1) << 8) | readUInt8(buf, offset + 2);
|
||||||
|
return unsigned > 0x7FFFFF ? unsigned - 0x1000000 : unsigned;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse binary player position file data
|
* Parse binary player position file data
|
||||||
* @param buf Binary player position data
|
* @param buf Binary player position data
|
||||||
@@ -54,7 +79,7 @@ export function parsePlayerPositionsBinary(buf: BinaryData): ParsedPlayerPositio
|
|||||||
const result = parsePositionRecord(buf, offset);
|
const result = parsePositionRecord(buf, offset);
|
||||||
if (!result.success) { break; }
|
if (!result.success) { break; }
|
||||||
|
|
||||||
const record: PlayerPositionRecord = {
|
const record: PlayerPositionRecordType0 = {
|
||||||
type: 0,
|
type: 0,
|
||||||
playerId: result.playerId,
|
playerId: result.playerId,
|
||||||
tSec: result.tSec,
|
tSec: result.tSec,
|
||||||
@@ -74,7 +99,7 @@ export function parsePlayerPositionsBinary(buf: BinaryData): ParsedPlayerPositio
|
|||||||
const result = parseSessionStartRecord(buf, offset);
|
const result = parseSessionStartRecord(buf, offset);
|
||||||
if (!result.success) { break; }
|
if (!result.success) { break; }
|
||||||
|
|
||||||
const record: PlayerPositionRecord = {
|
const record: PlayerPositionRecordType1 = {
|
||||||
type: 1,
|
type: 1,
|
||||||
playerId: result.playerId,
|
playerId: result.playerId,
|
||||||
tMs: result.tMs,
|
tMs: result.tMs,
|
||||||
@@ -93,7 +118,7 @@ export function parsePlayerPositionsBinary(buf: BinaryData): ParsedPlayerPositio
|
|||||||
const result = parseSessionEndRecord(buf, offset);
|
const result = parseSessionEndRecord(buf, offset);
|
||||||
if (!result.success) { break; }
|
if (!result.success) { break; }
|
||||||
|
|
||||||
const record: PlayerPositionRecord = {
|
const record: PlayerPositionRecordType2 = {
|
||||||
type: 2,
|
type: 2,
|
||||||
playerId: result.playerId,
|
playerId: result.playerId,
|
||||||
tMs: result.tMs,
|
tMs: result.tMs,
|
||||||
@@ -139,14 +164,10 @@ function parsePositionRecord(buf: BinaryData, offset: number): {
|
|||||||
const sec = readUInt32BE(buf, offset);
|
const sec = readUInt32BE(buf, offset);
|
||||||
offset += 4;
|
offset += 4;
|
||||||
|
|
||||||
// Read x_tiles (3 bytes, signed 24-bit)
|
const xTiles = readSignedInt24(buf, offset);
|
||||||
const xBytes = (readUInt8(buf, offset) << 16) | (readUInt8(buf, offset + 1) << 8) | readUInt8(buf, offset + 2);
|
|
||||||
const xTiles = xBytes > 0x7FFFFF ? xBytes - 0x1000000 : xBytes; // Convert to signed
|
|
||||||
offset += 3;
|
offset += 3;
|
||||||
|
|
||||||
// Read y_tiles (3 bytes, signed 24-bit)
|
const yTiles = readSignedInt24(buf, offset);
|
||||||
const yBytes = (readUInt8(buf, offset) << 16) | (readUInt8(buf, offset + 1) << 8) | readUInt8(buf, offset + 2);
|
|
||||||
const yTiles = yBytes > 0x7FFFFF ? yBytes - 0x1000000 : yBytes; // Convert to signed
|
|
||||||
offset += 3;
|
offset += 3;
|
||||||
|
|
||||||
const playerId = readUInt16BE(buf, offset);
|
const playerId = readUInt16BE(buf, offset);
|
||||||
|
|||||||
Reference in new issue
Block a user