Skip to content

Instantly share code, notes, and snippets.

@aoxu
Created July 27, 2013 07:00
Show Gist options
  • Save aoxu/6094048 to your computer and use it in GitHub Desktop.
Save aoxu/6094048 to your computer and use it in GitHub Desktop.
local userArch =
{
[1] = {
id = 1,
[1] = { index = 1, level = 1, pos = { x = 100, y = 100 }, finishedTime = 0
}
},
[2] = {
id = 2,
[1] = { index = 1, level = 3, pos = { x = 150, y = 150 }, finishedTime = 18948788493
},
[2] = { index = 2, level = 3, pos = { x = 200, y = 200 }, finishedTime = 0
},
[3] = { index = 3, level = 4, pos = { x = 150, y = 150 }, finishedTime = 18948788493
},
[4] = { index = 4, level = 4, pos = { x = 200, y = 200 }, finishedTime = 18948788493
},
[5] = { index = 5, level = 3, pos = { x = 300, y = 300 }, finishedTime = 18948788493
}
}
}
local t = {
id = 2,
[1] = { index = 1, level = 3, pos = { x = 150, y = 150 }, finishedTime = 18948788493
},
[2] = { index = 2, level = 3, pos = { x = 200, y = 200 }, finishedTime = 0
},
[3] = { index = 3, level = 4, pos = { x = 150, y = 150 }, finishedTime = 18948788493
},
[4] = { index = 4, level = 4, pos = { x = 200, y = 200 }, finishedTime = 18948788493
},
[5] = { index = 5, level = 3, pos = { x = 300, y = 300 }, finishedTime = 18948788493
}
}
local function addIsBuilding(userArch)
local t = userArch
local function addIsBuildingForIndex()
t.isBuilding = os.time() < t.finishedTime
if t.isBuilding then
t.leftTime = t.finishedTime - os.time()
else
t.leftTime = 0
end
end
local function addIsBuildingForId()
for k, v in pairs(t) do
t = v
if k ~= "id" then
addIsBuildingForIndex()
end
end
end
if userArch.index ~= nil then
return addIsBuildingForIndex()
end
if userArch.id ~= nil then
return addIsBuildingForId()
end
for k, v in pairs(userArch) do
t = v
return addIsBuildingForId()
end
end
addIsBuilding(userArch)
print(userArch[1][1].isBuilding, userArch[1][1].leftTime)
addIsBuilding(t)
print(t[1].isBuilding, t[1].leftTime)
print(t[2].isBuilding, t[2].leftTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment