Files
kubeinvaders/main/spaceship.script
Eugenio Marzo 4cc7b43eb7 fix pod log
2020-05-10 11:37:01 +02:00

235 lines
6.8 KiB
Plaintext

current_pods = {}
conf = {}
check_current_pods = false
endpoint = ""
token = ""
namespace = ""
namespace_list = {}
namespace_cnt = 0
namespace_total = 0
automatic = false
last_namespace_change = false
last_hit_time = os.clock()
alien_proximity_factor = 15
hit_cpu_time_rate_limit = 0
pod_update_time = 0.5
kubernetes_pods = {}
last_pod_log = false
last_pod_log_time = false
local COLLISION_RESPONSE = hash("collision_response")
local timer = require "main.mymodules.timer"
local spaceship = require "main.mymodules.spaceship"
local help = require "main.mymodules.help"
local pod_mod = require "main.mymodules.pod"
local pod_api = require "main.mymodules.pod_api"
function reverse(tbl)
for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp
end
end
function init(self)
msg.post("ui#gui", "mode",{ mode = "You are using the manual mode. Press \"a\" to use switch to automatic"})
local path = os.getenv("HOME") .. "/.KubeInv.json"
local contents = ""
conf = {}
local file = io.open( path, "r" )
if file then
local contents = file:read( "*a" )
conf = json.decode(contents);
io.close(file)
else
conf["token"] = os.getenv("TOKEN")
conf["endpoint"] = os.getenv("ENDPOINT")
conf["namespace"] = os.getenv("NAMESPACE")
if os.getenv("ALIENPROXIMITY") ~= nil then
alien_proximity_factor = tonumber(os.getenv("ALIENPROXIMITY"))
end
if os.getenv("HITSLIMIT") ~= nil then
hit_cpu_time_rate_limit = tonumber(os.getenv("HITSLIMIT"))
end
if os.getenv("UPDATETIME") ~= nil then
pod_update_time = tonumber(os.getenv("UPDATETIME"))
end
end
if conf["token"] == nil or conf["endpoint"] == nil or conf["namespace"] == nil then
msg.post("ui#gui", "howto",{ howto_message = "Please create .KubeInv.json in " .. os.getenv("HOME") .. "\nThe file .KubeInv.json should be like this \n{\"endpoint\": \"http://ocmaster39\",\"token\": \"xxxx\",\"namespace\": \"foobar\"}"})
else
print( "token: ", conf["token"] )
print( "endpoint: ", conf["endpoint"] )
print( "namespace: ", conf["namespace"] )
endpoint = conf["endpoint"]
token = conf["token"]
namespace_cnt = 0
for i in string.gmatch(conf["namespace"], "[^,]+") do
namespace_list[namespace_cnt] = i
namespace_cnt = namespace_cnt + 1
end
namespace_total = table.getn(namespace_list)
namespace_cnt = 0
namespace = namespace_list[0]
info = "Endpoint: " .. conf["endpoint"] .. "\n\n" .. "Namespace: " .. namespace
msg.post("ui#gui", "info",{ infomessage = info })
msg.post(".", "acquire_input_focus")
headers = {
["Accept"] = "application/json",
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. token
}
timer.repeat_seconds(pod_update_time, function()
pod_api.update_pod()
pod_mod.swap_pod(kubernetes_pods)
end)
timer.repeat_seconds(1, function()
msg.post("ui#gui","pod_name",{ pod_name = false, position = go.get_position()})
end)
timer.repeat_seconds(1, function()
spaceship.auto()
end)
pod_api.set_pods()
end
end
function update(self, dt)
timer.update(dt)
end
function on_input(self, action_id, action)
local p = go.get_position()
if action_id == hash("up") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.y < 700 then
p.y = p.y + 4
go.set_position(p)
end
elseif action_id == hash("down") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.y > 1 then
p.y = p.y - 4
go.set_position(p)
end
elseif action_id == hash("left") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.x > 1 then
p.x = p.x - 4
go.set_position(p)
end
elseif action_id == hash("right") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.x < 1280 then
p.x = p.x + 4
go.set_position(p)
end
elseif action_id == hash("infopod") then
pod_mod.print_pod_name()
elseif action_id == hash("refresh_pod_log") then
last_pod_log = false
elseif action_id == hash("help") then
print "help!"
help.get_help()
elseif action_id == hash("quit_help") then
help.quit_help()
elseif action_id == hash("automatic") then
automatic = true
msg.post("ui#gui", "mode",{ mode = "You are using the automatic mode. Press \"m\" to use switch to manual"})
elseif action_id == hash("manual") then
print ("[change_mode] manual")
automatic = false
msg.post("ui#gui", "mode",{ mode = "You are using the manual mode. Press \"a\" to use switch to automatic"})
elseif action_id == hash("namespace") then
print ("[change_namespace] change namespaces requested by user")
msg.post("ui#gui", "error",{ errormessage = "changing namespace!" })
-- Returns an approximation of the amount in seconds of CPU time used by the program.
current_time = os.clock()
if last_namespace_change ~= false then
diff_time = current_time - last_namespace_change
print ("[change_namespace] diff_time ".. diff_time)
else
diff_time = 99
end
if diff_time > 0.1 then
for i,value in ipairs(current_pods) do
go.delete(value["id"])
end
current_pods = {}
if namespace_cnt > (namespace_total - 1) then
print ("[change_namespace] return to first namespace")
namespace = namespace_list[0]
namespace_cnt = 0
else
namespace_cnt = namespace_cnt + 1
print ("[change_namespace] go to namespace " .. namespace_cnt)
namespace = namespace_list[namespace_cnt]
end
end
last_namespace_change = os.clock()
info = "Endpoint: " .. conf["endpoint"] .. "\n\n" .. "Namespace: " .. namespace
msg.post("ui#gui", "info",{ infomessage = info })
elseif action_id == hash("space") and action.pressed then
msg.post("ui#gui", "error",{ errormessage = "" })
-- Returns an approximation of the amount in seconds of CPU time used by the program.
current_hit_time = os.clock()
if automatic == false then
shot = true
if ((current_hit_time - last_hit_time) < hit_cpu_time_rate_limit) then
shot = false
end
if shot == true then
local pos = go.get_position()
local to = pos.y + 2 * 64
local bullet = factory.create("/bullet#bulletfactory", pos)
go.animate(bullet, "position.y", go.PLAYBACK_ONCE_FORWARD, 700, go.EASING_INQUAD, 1,0,function()
go.delete(bullet)
end)
last_hit_time = os.clock()
end
end
end
end
function on_message(self, message_id, message, sender)
current_time = os.clock()
if message_id == COLLISION_RESPONSE then
for i,value in ipairs(current_pods) do
if (message["other_id"] == value["id"]) then
pod_name = value["pod_name"]
end
end
if last_pod_log_time ~= false then
diff_time = current_time - last_pod_log_time
else
diff_time = 99
end
if last_pod_log ~= pod_name and diff_time > 0.2 then
last_pod_log = pod_mod.print_pod_log(message["other_id"])
print("last_pod_log " .. last_pod_log)
print("pod_name " .. pod_name)
last_pod_log_time = os.clock()
end
end
end