Skip to content

Commit

Permalink
[FACTORIO 2.0] Renamed global into storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hgschmie committed Oct 23, 2024
1 parent 93f75e4 commit 0deb061
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 186 deletions.
2 changes: 1 addition & 1 deletion spec/area/chunk_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Chunk Spec', function()
end)

it('should verify getting and setting data', function()
_G.global = {}
_G.storage = {}
_G.game = { surfaces = { nauvis = { index = 1, __self = 'userdata', valid = true } } }

local chunk_pos = { x = 4, y = -6 }
Expand Down
2 changes: 1 addition & 1 deletion spec/area/tile_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Tile Spec', function()
end)

it('should verify getting and setting data', function()
_G.global = {}
_G.storage = {}
_G.game = { surfaces = { nauvis = { index = 1, __self = 'userdata', valid = true } } }

local tile_pos = { x = 4, y = -6 }
Expand Down
6 changes: 3 additions & 3 deletions spec/entity/entity_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Entity', function()
end)

it('should verify getting and setting data', function()
_G['global'] = {}
_G['storage'] = {}
local entity = { name = 'fast-inserter', valid = true }
assert.is_nil(Entity.get_data(entity))

Expand All @@ -76,7 +76,7 @@ describe('Entity', function()
end)

it('should verify getting and setting data with unit_numbers', function()
_G['global'] = {}
_G['storage'] = {}
local entity = { name = 'fast-inserter', valid = true, unit_number = 13}
assert.is_nil(Entity.get_data(entity))

Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Entity', function()
end)

it('should verify data can be deleted', function()
_G['global'] = {}
_G['storage'] = {}
local entity = { name = 'fast-inserter', valid = true }
assert.is_nil(Entity.get_data(entity))

Expand Down
86 changes: 43 additions & 43 deletions spec/event/force_bak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ describe("Force",
end
}
_G.game = { forces = { } }
_G.global = { forces = { }}
_G.storage = { forces = { }}

setmetatable(game.forces, _mt)
setmetatable(global.forces, _mt)
setmetatable(storage.forces, _mt)
end
)

Expand Down Expand Up @@ -60,65 +60,65 @@ describe("Force",

it("should load forces into the global object on init",
function()
_G.global = {}
_G.storage = {}
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
end
require('__stdlib__/stdlib/event/force').register_events()
Event.dispatch({name = Event.core_events.init})
for _, force_name in ipairs(global.forces) do
assert.same(game.forces[force_name].name, global.forces[force_name].name)
for _, force_name in ipairs(storage.forces) do
assert.same(game.forces[force_name].name, storage.forces[force_name].name)
end
end
)

it("should load forces into the global object on configuration changed",
function()
_G.global = {}
_G.storage = {}
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
end
require('__stdlib__/stdlib/event/force').register_events()
Event.dispatch({name = Event.core_events.configuration_changed, test = "TEST"})
for _, force_name in ipairs(global.forces) do
assert.same(game.forces[force_name].name, global.forces[force_name].name)
for _, force_name in ipairs(storage.forces) do
assert.same(game.forces[force_name].name, storage.forces[force_name].name)
end
end
)

it("should load forces into the global object when forces are created in the game object",
function()
_G.global = {}
_G.storage = {}
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
Event.dispatch({name = defines.events.on_force_created})
assert.same(game.forces[force_name].name, global.forces[force_name].name)
assert.same(game.forces[force_name].name, storage.forces[force_name].name)
end
end
)

it(".get should retrieve forces from game.forces and global.forces",
it(".get should retrieve forces from game.forces and storage.forces",
function()
local Force = require('__stdlib__/stdlib/event/force').register_events()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
global.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
storage.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
end
for _, force_name in ipairs(force_names) do
local force_game, force_global = Force.get(force_name)
assert.same({index = force_name, name = force_name}, force_game)
assert.same({index = force_name, name = force_name, data = "Data" .. force_name}, force_global)
assert.equal(force_game.index, force_global.index)
assert.equal(force_game.name, force_global.name)
assert.equal(force_game.index, force_storage.index)
assert.equal(force_game.name, force_storage.name)
end
end
)

it(".get should add a force into global.forces if the force is in game.forces but does not exist in global.forces",
it(".get should add a force into storage.forces if the force is in game.forces but does not exist in storage.forces",
function()
local Force = require('__stdlib__/stdlib/event/force').register_events()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
Expand All @@ -129,41 +129,41 @@ describe("Force",
local force_game, force_global = Force.get(force_name)
assert.same({index = force_name, name = force_name, valid = true}, force_game)
assert.same({index = force_name, name = force_name}, force_global)
assert.equal(force_game.index, force_global.index)
assert.equal(force_game.name, force_global.name)
assert.equal(force_game.index, force_storage.index)
assert.equal(force_game.name, force_storage.name)
end
end
)

it(".add_data_all should merge a copy of the passed data to all forces in global.forces",
it(".add_data_all should merge a copy of the passed data to all forces in storage.forces",
function()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
global.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
storage.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
end
local Force = require('__stdlib__/stdlib/event/force').register_events()
local data = {a = "abc", b = "def"}
Force.add_data_all(data)
for _, force_name in ipairs(force_names) do
assert.equal(data.a, global.forces[force_name].a)
assert.equal(data.b, global.forces[force_name].b)
assert.equal(data.a, storage.forces[force_name].a)
assert.equal(data.b, storage.forces[force_name].b)
end
end
)

it(".init should initialize global.forces",
it(".init should initialize storage.forces",
function()
local Force = require('__stdlib__/stdlib/event/force').register_events()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
end
assert.is_same({}, global.forces)
assert.is_same({}, storage.forces)
for _, force_name in ipairs(force_names) do
Force.init({index = force_name})
assert.same({index = force_name, name = game.forces[force_name].name}, global.forces[force_name])
assert.same({index = force_name, name = game.forces[force_name].name}, storage.forces[force_name])
end
assert.is_equal(#game.forces, #global.forces)
assert.is_equal(#game.forces, #storage.forces)
end
)

Expand All @@ -173,11 +173,11 @@ describe("Force",
local force_names = {"ForceOne", "ForceTwo", "ForceThree", "ForceFour"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
global.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
storage.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
end

for i, force_name in ipairs(force_names) do
assert.is_not_nil(global.forces[force_name].data)
assert.is_not_nil(storage.forces[force_name].data)

if i == 1 then
Force.init(game.forces[force_name], true)
Expand All @@ -189,50 +189,50 @@ describe("Force",
Force.init(force_name, true)
end

assert.is_nil(global.forces[force_name].data)
assert.same({index = force_name, name = game.forces[force_name].name}, global.forces[force_name])
assert.is_nil(storage.forces[force_name].data)
assert.same({index = force_name, name = game.forces[force_name].name}, storage.forces[force_name])
end
end
)

it(".init should iterate all game.forces[index] and initialize global.forces[index] when nil is passed",
it(".init should iterate all game.forces[index] and initialize storage.forces[index] when nil is passed",
function()
local Force = require('__stdlib__/stdlib/event/force').register_events()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
end
assert.same({}, global.forces)
assert.same({}, storage.forces)
Force.init(nil)
assert.equal(#game.forces, #global.forces)
assert.equal(#game.forces, #storage.forces)
for _, force_name in ipairs(force_names) do
assert.same({index = game.forces[force_name].index, name = game.forces[force_name].name}, global.forces[force_name])
assert.same({index = game.forces[force_name].index, name = game.forces[force_name].name}, storage.forces[force_name])
end
end
)

it(".init should iterate all game.forces[index] and re-init global.forces[index] when event is nil and overwrite is true",
it(".init should iterate all game.forces[index] and re-init storage.forces[index] when event is nil and overwrite is true",
function()
local Force = require('__stdlib__/stdlib/event/force').register_events()
local force_names = {"ForceOne", "ForceTwo", "ForceThree"}
for _, force_name in ipairs(force_names) do
game.forces[force_name] = { index = force_name, name = force_name }
global.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
storage.forces[force_name] = { index = force_name, name = force_name, data = "Data" .. force_name }
end
assert.equal(#game.forces, #global.forces)
assert.equal(#game.forces, #storage.forces)
for _, force_name in ipairs(force_names) do
assert.is_not_nil(global.forces[force_name].data)
assert.is_not_nil(storage.forces[force_name].data)
end
Force.init(nil, true)
assert.equal(#game.forces, #global.forces)
assert.equal(#game.forces, #storage.forces)
for _, force_name in ipairs(force_names) do
assert.is_nil(global.forces[force_name].data)
assert.same({index = force_name, name = game.forces[force_name].name}, global.forces[force_name])
assert.is_nil(storage.forces[force_name].data)
assert.same({index = force_name, name = game.forces[force_name].name}, storage.forces[force_name])
end
end
)

it(".init should initialize global.forces for all existing game.forces even if a single game.forces[index] is not a valid force",
it(".init should initialize storage.forces for all existing game.forces even if a single game.forces[index] is not a valid force",
--If a force isn"t valid then it won"t add it to global table
--Additionally game.forces won"t return invalid forces (TBD)
function()
Expand All @@ -243,8 +243,8 @@ describe("Force",
end
Force.init({force = "fake"})
for _, force_name in ipairs(force_names) do
assert.is_not_nil(global.forces[force_name])
assert.same({index = force_name, name = game.forces[force_name].name}, global.forces[force_name])
assert.is_not_nil(storage.forces[force_name])
assert.same({index = force_name, name = game.forces[force_name].name}, storage.forces[force_name])
end
end
)
Expand Down
Loading

0 comments on commit 0deb061

Please sign in to comment.