mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 10:00:44 +00:00
make terrain map load async
This commit is contained in:
+10
-8
@@ -18,7 +18,7 @@ class Client {
|
||||
private hasJoined = false
|
||||
|
||||
private socket: WebSocket | null = null;
|
||||
private terrainMap: TerrainMap
|
||||
private terrainMap: Promise<TerrainMap>
|
||||
private game: ClientGame
|
||||
|
||||
private lobbiesContainer: HTMLElement | null;
|
||||
@@ -115,13 +115,15 @@ class Client {
|
||||
if (this.game != null) {
|
||||
return;
|
||||
}
|
||||
this.game = createClientGame(getUsername(), new PseudoRandom(Date.now()).nextID(), lobby.id, getConfig(), this.terrainMap);
|
||||
this.game.join();
|
||||
const g = this.game;
|
||||
window.addEventListener('beforeunload', function (event) {
|
||||
console.log('Browser is closing');
|
||||
g.stop();
|
||||
});
|
||||
this.terrainMap.then(tm => {
|
||||
this.game = createClientGame(getUsername(), new PseudoRandom(Date.now()).nextID(), lobby.id, getConfig(), tm);
|
||||
this.game.join();
|
||||
const g = this.game;
|
||||
window.addEventListener('beforeunload', function (event) {
|
||||
console.log('Browser is closing');
|
||||
g.stop();
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,8 +29,11 @@ export class Terrain {
|
||||
constructor(public type: TerrainType) { }
|
||||
}
|
||||
|
||||
export function loadTerrainMap(): TerrainMap {
|
||||
const fileData = binAsString;
|
||||
export async function loadTerrainMap(): Promise<TerrainMap> {
|
||||
// Simulate an asynchronous file load
|
||||
const fileData = await new Promise<string>((resolve) => {
|
||||
setTimeout(() => resolve(binAsString), 100);
|
||||
});
|
||||
|
||||
console.log(`Loaded data length: ${fileData.length} bytes`);
|
||||
|
||||
@@ -40,9 +43,6 @@ export function loadTerrainMap(): TerrainMap {
|
||||
|
||||
console.log(`Decoded dimensions: ${width}x${height}`);
|
||||
|
||||
// Log the first 100 bytes of data (including the width and height bytes)
|
||||
// logBinaryAsAscii(fileData, 100);
|
||||
|
||||
// Check if the data length matches the expected size
|
||||
if (fileData.length != width * height + 4) { // +4 for the width and height bytes
|
||||
throw new Error(`Invalid data: buffer size ${fileData.length} incorrect for ${width}x${height} terrain plus 4 bytes for dimensions.`);
|
||||
|
||||
Reference in New Issue
Block a user