'use strict';

class GameWorld {
    constructor() {
        this.stepCount = 0;
        this.objects = {};
        this.playerCount = 0;
        this.idCount = 0;
    }

    // TODO: remove this function
    static deserialize(gameEngine, serializer, worldData) {

        var world = new GameWorld();

        var worldDataDV = new DataView(worldData);
        world.stepCount = worldDataDV.getInt32(0);
        var byteOffset = Int32Array.BYTES_PER_ELEMENT;

        world.lastHandledInput = worldDataDV.getInt16(byteOffset);
        byteOffset += Int16Array.BYTES_PER_ELEMENT;

        // go ever the buffer and deserialize items
        while (byteOffset < worldData.byteLength) {
            let object = serializer.deserialize(worldData, byteOffset);
            var objectByteSize = serializer.getNetSchemeBufferSizeByClass(object.class);

            world.objects[object.id] = object;
            byteOffset += objectByteSize;
        }

        return world;
    }

    /**
     * Return the primary game object for a specific player
     *
     * @param {String} playerId the player ID
     * @return {Object} game object for this player
     */
    getPlayerObject(playerId) {
        for (let objId in this.world.objects) {
            let o = this.world.objects[objId];
            if (o.playerId === playerId)
                return o;
        }
    }

    /**
     * Return an array of all the game objects owned by a specific player
     *
     * @param {String} playerId the player ID
     * @return {Array} game objects owned by this player
     */
    getOwnedObject(playerId) {
        let owned = [];
        for (let objId in this.world.objects) {
            let o = this.world.objects[objId];
            if (o.ownerId === playerId)
                owned.push(o);
        }
        return owned;
    }

}

module.exports = GameWorld;

@@ -1202,37 +1202,43 @@
  (let objId 
-in this.world
+of Object.keys(this
 .objects) %7B%0A
@@ -1226,32 +1226,33 @@
 ys(this.objects)
+)
  %7B%0A            l
@@ -1255,38 +1255,32 @@
    let o = this.
-world.
 objects%5BobjId%5D;%0A
@@ -1645,21 +1645,27 @@
 jId 
-in this.world
+of Object.keys(this
 .obj
@@ -1669,16 +1669,17 @@
 objects)
+)
  %7B%0A     
@@ -1698,22 +1698,16 @@
  = this.
-world.
 objects%5B

fix GameWorld convenience methods