diff --git a/ElementsMetatable/FuelTank.lua b/ElementsMetatable/FuelTank.lua new file mode 100644 index 0000000..681e3a1 --- /dev/null +++ b/ElementsMetatable/FuelTank.lua @@ -0,0 +1,67 @@ +--[[ + Fuel Tanks Metatable By Jericho + Version: 1.0.0 + Source available at: +]] +FuelTank = { + __index = { + name = '', + percentage = 0, + timeLeft = 0, + lastRefresh = 0, + fuelType = 'rocket', + refresh = function(self) + local time = system.getUtcTime() + if lastRefresh ~= time then --refresh only once by seconds + local class = self.getClass():lower() + if class:find('atmo') then + self.fuelType = "atmo" + elseif class:find('space') then + self.fuelType = "space" + end + local jsondata = self.getWidgetData() + local data = json.decode(jsondata) + self.name = data.name + self.percentage = data.percentage + self.timeLeft = tonumber(data.timeLeft) + self.lastRefresh = time + end + end, + getName = function(self) + self:refresh() + return self.name + end, + getPercentFilled = function(self) + self:refresh() + return self.percentage + end, + getSecondsLeft = function(self) + self:refresh() + return self.timeLeft + end, + getFuelType = function(self) + self:refresh() + return self.fuelType + end, + getTimeLeftString = function (self) + local seconds = self:getSecondsLeft() + if seconds == nil or seconds <= 0 then + return "-"; + end + days = string.format("%2.f", math.floor(seconds/(3600*24))); + hours = string.format("%2.f", math.floor(seconds/3600 - (days*24))); + mins = string.format("%2.f", math.floor(seconds/60 - (hours*60) - (days*24*60))); + secs = string.format("%2.f", math.floor(seconds - hours*3600 - (days*24*60*60) - mins *60)); + str = "" + if tonumber(days) > 0 then str = str .. days.."d " end + if tonumber(hours) > 0 then str = str .. hours.."h " end + if tonumber(mins) > 0 then str = str .. mins.."m " end + if tonumber(secs) > 0 then str = str .. secs .."s" end + return str + end, + dump = function(self) + self:refresh() + system.print('{name="' .. self.name .. '",percentage=' .. self.percentage ..',timeLeft=' .. self.timeLeft .. ',fuelType="'.. self.fuelType ..'"}') + end + } +} \ No newline at end of file diff --git a/ElementsMetatable/FuelTank.min.lua b/ElementsMetatable/FuelTank.min.lua new file mode 100644 index 0000000..f5ed7ea --- /dev/null +++ b/ElementsMetatable/FuelTank.min.lua @@ -0,0 +1,6 @@ +--[[ + Fuel Tanks Metatable By Jericho + Version: 1.0.0 + Unminified Source available at: https://github.com/Jericho1060/DualUniverse/blob/master/ElementMetatable/FuelTank.lua +]] +FuelTank={__index={name='',percentage=0,timeLeft=0,lastRefresh=0,fuelType='rocket',refresh=function(self)local a=system.getUtcTime()if lastRefresh~=a then local b=self.getClass():lower()if b:find('atmo')then self.fuelType="atmo"elseif b:find('space')then self.fuelType="space"end;local c=self.getWidgetData()local d=json.decode(c)self.name=d.name;self.percentage=d.percentage;self.timeLeft=tonumber(d.timeLeft)self.lastRefresh=a end end,getName=function(self)self:refresh()return self.name end,getPercentFilled=function(self)self:refresh()return self.percentage end,getSecondsLeft=function(self)self:refresh()return self.timeLeft end,getFuelType=function(self)self:refresh()return self.fuelType end,getTimeLeftString=function(self)local e=self:getSecondsLeft()if e==nil or e<=0 then return"-"end;days=string.format("%2.f",math.floor(e/(3600*24)))hours=string.format("%2.f",math.floor(e/3600-days*24))mins=string.format("%2.f",math.floor(e/60-hours*60-days*24*60))secs=string.format("%2.f",math.floor(e-hours*3600-days*24*60*60-mins*60))str=""if tonumber(days)>0 then str=str..days.."d "end;if tonumber(hours)>0 then str=str..hours.."h "end;if tonumber(mins)>0 then str=str..mins.."m "end;if tonumber(secs)>0 then str=str..secs.."s"end;return str end,dump=function(self)self:refresh()system.print('{name="'..self.name..'",percentage='..self.percentage..',timeLeft='..self.timeLeft..',fuelType="'..self.fuelType..'"}')end}} \ No newline at end of file