diff --git a/main/bullet.script b/main/bullet.script index 985ad06..70ea8ec 100644 --- a/main/bullet.script +++ b/main/bullet.script @@ -1,17 +1,11 @@ local COLLISION_RESPONSE = hash("collision_response") function refresh_pods() - print "refresh pods" http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods", "GET", http_pod_result,headers) end -function delete_request_pod() - print "request for deleting pods" - http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods", "GET", http_pod_delete_request_result,headers) -end - function init(self) - print "init bullet" + msg.post(".", "acquire_input_focus") token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJmb29iYXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiYm90LXRva2VuLTZiNndkIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImJvdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjRmZTZkOWUwLWRlNjEtMTFlOC1hOTIzLTIyM2Q5NTVhNzBlOSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpmb29iYXI6Ym90In0.GwuazgmPxWIGgFNuVQxpNz5kXweuPjUHMnsemX1YJAINYgzvZ2FMaTCqJJ1pN3Vdk05KA9YD69lJLYv3kBDSeTdWOCuIYSGyq0EPhUu5eGdFvDxg1Hqj58XWTfNS79Yb3jPTTETGLqDPLJIMqKbmvIERxVZlVT8cGWo6xVhDrUaA0YrGD6SFdvh35pqGV-hmyyzhqU27p51zUG4cYv9KVk1TVVJst2qDIoB2vCnab04qaaPuWB3GmVQLXEG09qqGWvYuil88gb76iMNv7vfX3FMKuENxD175et5hhIbEjWg2jdxqGzZ6wj8Qi19WJr-1fREk_loefi-1ipC-zc5uLw" headers = { ["Authorization"] = "Bearer " .. token , @@ -21,20 +15,18 @@ function init(self) end function on_message(self, message_id, message, sender) - print "start on_message function for bullets" - print(self.id) + print "[on_message] bullet object received a message" if message_id == COLLISION_RESPONSE then - print 'collision detected!' - for i,id in ipairs(current_pods) do - print ("delete " .. id ) - if id ~= "reset" then - go.delete(id) + print "[on_message] collision detected" + for i,value in ipairs(current_pods) do + print("[on_message] pod of colision is " .. value["color"] .. " " .. "id is " .. message["other_id"] ) + if value["color"] == "white" and value["id"] == message["other_id"] then + old_pos = go.get_position(value["id"]) + print ("on_message] position of pod to be deleted is " .. old_pos) + current_pods[i] = { id = value["id"] , color = "red", collision = true, old_position = old_pos} + go.delete(value["id"]) end end - current_pods={} - print "set pods.." - delete_request_pod() - set_pods() end - print 'end on_message function for bullets' + print "[on_message] end of on_message function" end \ No newline at end of file diff --git a/main/spaceship.script b/main/spaceship.script index 7e63f97..4a8bcc5 100644 --- a/main/spaceship.script +++ b/main/spaceship.script @@ -2,24 +2,101 @@ current_pods = {} check_current_pods = false local timer = require ("main.timer") +function swap_pod(items) + current_pod_not_running = 0 + kube_pod_not_running = 0 + pod_items_size = table.getn(items) + + for i,value in ipairs(current_pods) do + if value["collision"] ~= nil and value["color"] == "red" then + print("[swap_pod] creating " .. value["id"] .. " in red. Pos: " .. " " .. value["old_position"]) + current_pods[i]["id"] = factory.create("/pod_not_running#podfactory", value["old_position"]) + current_pods[i]["color"] = "red" + current_pods[i]["collision"] = nil + delete_request_pod() + return + end + end + + for i,value in ipairs(current_pods) do + if value["id"] ~= "reset" and value["color"] == "red" then + current_pod_not_running = current_pod_not_running + 1 + --print ("[swap_pod]:" .. value["id"] .. " is not running in KubeInvaders") + end + end + for i,value in ipairs(items) do + if value['status']['phase'] ~= "Running" then + kube_pod_not_running = kube_pod_not_running + 1 + --print ("[swap_pod]:" .. value['metadata']['name'] .. " is not running on Kubernetes cluster") + end + end + --print ("[swap_pod]: current pods on Kubernetes: " .. pod_items_size) + --print ("[swap_pod]: not running pods Kubernetes: " .. kube_pod_not_running) + --print ("[swap_pod]: not running pods in KubeInvaders: " .. current_pod_not_running) + + if (current_pod_not_running > 0) and (kube_pod_not_running > current_pod_not_running )then + diff = kube_pod_not_running - current_pod_not_running + a = 0 + while ( a < diff ) do + for i,value in ipairs(current_pods) do + if value["color"] == "white" then + go.delete(value["id"]) + local pod = factory.create("/pod_not_running#podfactory", go.get_position(value["id"])) + current_pods[i] = { id = pod , color = "red" } + end + end + a = a + 1 + end + + elseif current_pod_not_running > kube_pod_not_running then + diff = current_pod_not_running - kube_pod_not_running + a = 0 + while ( a < diff ) do + for i,value in ipairs(current_pods) do + if value["color"] == "red" then + pos = go.get_position() + go.delete(value["id"]) + local pod = factory.create("/pod#podfactory", go.get_position(value["id"])) + current_pods[i] = { id = pod , color = "white" } + end + end + a = a + 1 + end + end +end + +function http_update_pod_result(self, _, response) + pods = json.decode(response.response) + pod_items_size = table.getn(pods["items"]) + pod_items = pods["items"] + swap_pod(pod_items) +end + +function update_pod() + http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods", "GET", http_update_pod_result,headers) +end + function delete_pod(pod_name) - print "delete pods on Kubernetes/Openshift" + print "[delete_pod] delete pods of Kubernetes" http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods/" .. pod_name, "DELETE", http_pod_delete_result,headers) end +function delete_request_pod() + http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods", "GET", http_pod_delete_request_result,headers) +end + function http_pod_delete_result(self, _, response) - print "print http_pod_delete_result" - print(response.status) - print(response.response) - print(response.headers) + print "[http_pod_delete_result] Request for delete pod on kubernetes has been done" + --print(response.status) + --print(response.response) + --print(response.headers) end function http_pod_delete_request_result(self, _, response) - print "getting http_pod_delete_result" pods = {} - print(response.status) - print(response.response) - print(response.headers) + --print(response.status) + --print(response.response) + --print(response.headers) pods = json.decode(response.response) pod_items_size = table.getn(pods["items"]) a = 0 @@ -35,9 +112,9 @@ function http_pod_delete_request_result(self, _, response) end function http_pod_result(self, _, response) - print(response.status) - print(response.response) - print(response.headers) + --print(response.status) + --print(response.response) + --print(response.headers) pods = json.decode(response.response) pod_items_size = table.getn(pods["items"]) pod_items = pods["items"] @@ -54,11 +131,11 @@ function http_pod_result(self, _, response) pos.y = 600 if phase == "Running" then local pod = factory.create("/pod#podfactory", pos) - table.insert(current_pods, pod) + 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, pod) + table.insert(current_pods, { id = pod , color = "red" }) check_current_pods = true end end @@ -67,12 +144,10 @@ function http_pod_result(self, _, response) end function set_pods() - print "create pods" http.request("https://ocmaster39:8443/api/v1/namespaces/foobar/pods", "GET", http_pod_result,headers) end function init(self) - print "init spaceship" msg.post(".", "acquire_input_focus") token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJmb29iYXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiYm90LXRva2VuLTZiNndkIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImJvdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjRmZTZkOWUwLWRlNjEtMTFlOC1hOTIzLTIyM2Q5NTVhNzBlOSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpmb29iYXI6Ym90In0.GwuazgmPxWIGgFNuVQxpNz5kXweuPjUHMnsemX1YJAINYgzvZ2FMaTCqJJ1pN3Vdk05KA9YD69lJLYv3kBDSeTdWOCuIYSGyq0EPhUu5eGdFvDxg1Hqj58XWTfNS79Yb3jPTTETGLqDPLJIMqKbmvIERxVZlVT8cGWo6xVhDrUaA0YrGD6SFdvh35pqGV-hmyyzhqU27p51zUG4cYv9KVk1TVVJst2qDIoB2vCnab04qaaPuWB3GmVQLXEG09qqGWvYuil88gb76iMNv7vfX3FMKuENxD175et5hhIbEjWg2jdxqGzZ6wj8Qi19WJr-1fREk_loefi-1ipC-zc5uLw" headers = { @@ -80,20 +155,24 @@ function init(self) ["Accept"] = "application/json", ["Content-Type"] = "application/json" } + timer.repeat_seconds(1, function() + update_pod() + end) set_pods() end function update(self, dt) - if rawequal(next(current_pods), nil) and check_current_pods then - for i,id in ipairs(current_pods) do - print ("delete " .. id ) - if id ~= "reset" then - go.delete(id) - end - end - set_pods() - current_pods = {"reset"} - end + --if rawequal(next(current_pods), nil) and check_current_pods then + -- for i,value in ipairs(current_pods) do + -- print ("delete " .. value["id"] ) + -- if value["id"] ~= "reset" then + -- go.delete(id["id"]) + -- end + -- end + --set_pods() + --current_pods = { id = "reset"} + --end + timer.update(dt) end function on_input(self, action_id, action) @@ -116,14 +195,10 @@ function on_input(self, action_id, action) go.set_position(p) elseif action_id == hash("click") and action.pressed then print(action_id) - print(hash("click")) - print("CLICK!") elseif action_id == hash("space") and action.pressed then - print("SPACE!") local pos = go.get_position() local to = pos.y + 2 * 64 local bullet = factory.create("/bullet#bulletfactory", pos) - print "animate and delete bullet" go.animate(bullet, "position.y", go.PLAYBACK_ONCE_FORWARD, 700, go.EASING_INQUAD, 1,0,function() go.delete(bullet) end) @@ -131,5 +206,5 @@ function on_input(self, action_id, action) end function on_message(self, message_id, message, sender) - print "ciaoooo" + end \ No newline at end of file diff --git a/main/timer.lua b/main/timer.lua index b270e81..3c3632f 100644 --- a/main/timer.lua +++ b/main/timer.lua @@ -85,5 +85,11 @@ function M.update(dt) end end +function M.repeat_seconds(seconds, callback) + M.seconds(seconds, function() + callback() + M.repeat_seconds(seconds, callback) + end) +end return M \ No newline at end of file