Files
kubeinvaders/main/spaceship.script
2019-09-11 22:35:10 +02:00

362 lines
11 KiB
Plaintext

current_pods = {}
check_current_pods = false
endpoint = ""
token = ""
namespace = ""
automatic = false
local timer = require ("main.timer")
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 swap_pod(items)
for i,value in ipairs(current_pods) do
if value["collision"] == true and value["color"] == "red" then
current_pods[i]["id"] = factory.create("/pod_not_running#podfactory", value["old_position"])
current_pods[i]["color"] = "red"
current_pods[i]["collision"] = false
delete_request_pod()
return
end
end
kubeinvaders_pod_not_running = 0 -- Pods not running in Kubeinvaders
kubeinvaders_pod_running = 0 -- Pods running in Kubeinvaders
kubernetes_pod_not_running = 0 -- Pods not running in Kubernetes
kubernetes_pod_running = 0 -- Pods running in Kubernetes
global_kubernetes_pod_size = table.getn(items) -- All pods in Kubernetes
global_kubeinvaders_pod_size = table.getn(current_pods) -- All pods in Kubeinvaders
for i,value in ipairs(items) do
if value['status']['phase'] ~= "Running" or value['metadata']['deletionTimestamp'] ~= nil then
print("[swap_pod] phase of " .. value['metadata']['name'] .. " is " ..value['status']['phase'])
print("[swap_pod] " .. value['metadata']['name'] .. " is not running")
kubernetes_pod_not_running = kubernetes_pod_not_running + 1
else
kubernetes_pod_running = kubernetes_pod_running + 1
end
end
print ("[swap_pod]: pods not running pods in Kubernetes: " .. kubernetes_pod_not_running)
print ("[swap_pod]: pods running pods in Kubernetes: " .. kubernetes_pod_running)
msg.post("ui#gui", "hello_gui",{ pod_running = kubernetes_pod_running })
for i,value in ipairs(current_pods) do
if value["color"] == "red" then
kubeinvaders_pod_not_running = kubeinvaders_pod_not_running + 1
elseif value["color"] == "white" then
kubeinvaders_pod_running = kubeinvaders_pod_running + 1
end
end
print ("[swap_pod]: pods not running pods in KubeInvaders: " .. kubeinvaders_pod_not_running)
print ("[swap_pod]: pods running pods in KubeInvaders: " .. kubeinvaders_pod_running)
if kubernetes_pod_running < kubeinvaders_pod_running then
print ("[swap_pod] there are less pod running in kubernetes than kubeinvaders")
i = global_kubeinvaders_pod_size - 1
deleted_pods = 0
while ( i > 0) do
if current_pods[i] ~= nil and current_pods[i]["color"] == "white" then
go.delete(current_pods[i]["id"])
table.remove(current_pods,i)
deleted_pods = deleted_pods + 1
print ("[swap_pod] delete kubeinvaders pod at position " .. tostring(i) .. " of kubeinvaders global pods array")
end
i = i - 1
end
msg.post("ui#gui", "error",{ errormessage = "Synchronizing alien ships with Kubernetes pods.." })
return
end
if (kubernetes_pod_running > kubeinvaders_pod_running) then
for i,value in ipairs(current_pods) do
go.delete(value["id"])
end
current_pods = {}
set_pods()
end
if kubeinvaders_pod_not_running > kubernetes_pod_not_running then
print ("[swap_pod] there are more pod not running in KubeInvaders than Kubernetes")
diff = kubeinvaders_pod_not_running - kubernetes_pod_not_running
a = 0
while ( a < diff ) do
for i,value in ipairs(current_pods) do
if value["color"] == "red" then
go.delete(value["id"])
table.remove(current_pods,i)
end
end
a = a + 1
end
msg.post("ui#gui", "error",{ errormessage = "Synchronizing alien ships with Kubernetes pods.." })
end
end
function http_update_pod_result(self, _, response)
kubernetes_pods = {}
if response.status == 0 then
msg.post("ui#gui", "error",{ errormessage = "Error connecting to " .. conf['endpoint'] })
end
pods = json.decode(response.response)
pod_items_size = table.getn(pods["items"])
pod_items = pods["items"]
i = 0
while i <= pod_items_size do
if pod_items[i] ~= nil then
table.insert(kubernetes_pods,pod_items[i])
end
i = i + 1
end
swap_pod(kubernetes_pods)
end
function ai()
launch_bullet = false
msg.post("ui#gui", "error",{ errormessage = "" })
if automatic == false then
return
end
for i=1,1 do
rand_pos = math.random(100, 1100)
end
pos = go.get_position()
pos.y = 220
go.set_position(pos)
pos.x = rand_pos
for i,value in ipairs(current_pods) do
if value["color"] == "white" then
position_of_pod = go.get_position(value["id"])
if (pos.x > position_of_pod.x and pos.x - position_of_pod.x < 30) then
launch_bullet = true
elseif (position_of_pod.x > pos.x and position_of_pod.x - pos.x < 30) then
launch_bullet = true
end
end
end
if launch_bullet == true then
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_INQUAD, 1,0, function()
pos = go.get_position()
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)
end)
else
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_INQUAD, 1,0)
end
end
function update_pod()
http.request(endpoint .. "/api/v1/namespaces/" .. namespace .. "/pods", "GET", http_update_pod_result,headers)
end
function delete_pod(pod_name)
print "[delete_pod] delete pods of Kubernetes"
http.request(endpoint .. "/api/v1/namespaces/" .. namespace .. "/pods/" .. pod_name, "DELETE", http_pod_delete_result,headers)
end
function delete_request_pod()
http.request(endpoint .. "/api/v1/namespaces/".. namespace .. "/pods", "GET", http_pod_delete_request_result,headers)
end
function http_pod_delete_result(self, _, response)
print "[http_pod_delete_result] Request for delete pod on kubernetes has been done"
print(response.status)
print(response.response)
print(response.headers)
if string.match(response.status, '4.*') or string.match(response.status, '5.*') then
msg.post("ui#gui", "error",{ errormessage = "Status code: " .. response.status .. "\n" .. response.response })
end
end
function http_pod_delete_request_result(self, _, response)
pods = {}
pods = json.decode(response.response)
pod_items_size = table.getn(pods["items"])
a = 0
while a <= pod_items_size do
if pod_items[a] ~= nil then
if pod_items[a]['status']['phase'] == "Running" and pod_items[a]['metadata']['deletionTimestamp'] == nil then
pod_to_delete = pod_items[a]['metadata']['name']
break
end
end
a = a + 1
end
delete_pod(pod_to_delete)
end
function http_pod_result(self, _, response)
print(response.status)
print(response.response)
print(response.headers)
if string.match(response.status, '4.*') or string.match(response.status, '5.*') then
msg.post("ui#gui", "error",{ errormessage = "Status code: " .. response.status .. "\n" .. response.response })
end
pods = json.decode(response.response)
pod_items_size = table.getn(pods["items"])
pod_items = pods["items"]
check_above = false
a = 0
distance_between_pods = 1000 / pod_items_size
y_pos = 600
space_between_factor = 0
if pod_items_size <= 15 then
space_between_factor = pod_items_size
else
space_between_factor = 15
end
while( a <= pod_items_size )
do
if pod_items[a] ~= nil then
this_pod = pod_items[a]
phase = this_pod["status"]["phase"]
local pos = go.get_position()
if a <= 15 then
pos.x = 100 + ( a * (1000 / space_between_factor) )
end
if (a % 15 == 0) then
y_pos = y_pos - (( a / 15 ) * 50)
check_above = true
end
if check_above == true then
pos_of_pod_above = go.get_position(current_pods[a - 14]['id'])
pos.x = pos_of_pod_above.x
end
pos.y = y_pos
if phase == "Running" and this_pod['metadata']['deletionTimestamp'] == nil then
local pod = factory.create("/pod#podfactory", pos)
table.insert(current_pods, { id = pod , color = "white" })
check_current_pods = true
else
local pod = factory.create("/pod_not_running#podfactory", pos)
table.insert(current_pods, { id = pod , color = "red" })
check_current_pods = true
end
end
a = a+1
end
end
function set_pods()
http.request(endpoint .. "/api/v1/namespaces/".. namespace .. "/pods", "GET", http_pod_result,headers)
end
function init(self)
msg.post("ui#gui", "mode",{ mode = "You are using the manual mode. Press \"a\" to use switch to automatic"})
conf = {}
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")
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"]
namespace = conf["namespace"]
token = conf["token"]
info = "Endpoint: " .. conf["endpoint"] .. "\n\n" .. "Namespace: " .. conf["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(0.3, function()
update_pod()
end)
timer.repeat_seconds(1, function()
ai()
end)
set_pods()
end
end
function update(self, dt)
timer.update(dt)
end
function on_input(self, action_id, action)
if action_id == hash("up") then
local p = go.get_position()
p.y = p.y + 4
go.set_position(p)
elseif action_id == hash("down") then
local p = go.get_position()
p.y = p.y - 4
go.set_position(p)
elseif action_id == hash("left") then
local p = go.get_position()
p.x = p.x - 4
go.set_position(p)
elseif action_id == hash("right") then
local p = go.get_position()
p.x = p.x + 4
go.set_position(p)
elseif action_id == hash("click") and action.pressed then
print(action_id)
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
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("space") and action.pressed then
msg.post("ui#gui", "error",{ errormessage = "" })
if automatic == false 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)
end
end
end
function on_message(self, message_id, message, sender)
end