-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.gd
190 lines (153 loc) · 5.06 KB
/
Server.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
extends KinematicBody2D
const Request = preload("res://Request.tscn")
const Interface = preload("src/Interface.gd")
var can_grab = false
var grabbed_offset = Vector2()
var eth0: Interface
var level: Node2D
var callback: Object
var ports = {}
var sysload: float
var type: String
var properties = {}
var server_name: String
var cpu = 1
const cpu_max = 64
var instance_family = "m4-standard"
var instance_size: String
var config_warning: bool = false
var sizeup_animation_scale = 0.01
var accepted_content_types
signal tutorial_complete
func init2():
instance_size = instance_family + "-" + str(cpu)
$CollisionShape2D/AnimatedSprite.animation = type
level = get_parent()
var margin = level.servers.size()*50
position = Vector2(200+margin, 200+margin)
eth0 = Interface.new()
#todo: this should be in interface
while eth0.ip in level.iptable:
eth0 = Interface.new()
level.iptable[eth0.ip] = self
level.servers[eth0.ip] = self
level.last_created_object = self
server_name = eth0.ip
OS.set_clipboard(eth0.ip)
$Meta/ConfigWindow/Info/Name.text = server_name
$Meta/ConfigWindow/Info/IP.text = server_name
$Meta/ConfigWindow/Info/InstanceSize.text = instance_size
$ClipBoardAnimation.play()
func _ready():
$Meta/ConfigWindow.visible = false
var objects = load("res://src/objects.gd")
objects = JSON.parse(objects.json).result
properties = objects['entities'][type]
func _input_event(_viewport, event, _shape_idx):
if event is InputEventMouseButton:
if event.button_index == 1:
can_grab = event.pressed
grabbed_offset = position - get_global_mouse_position()
if event.button_index == 2 and !event.is_pressed():
level.iptable.erase(eth0.ip)
level.servers.erase(eth0.ip)
queue_free()
func _process(_delta):
$Meta/DNSNameLabel.text = server_name
$Meta/SysLoadBar.value = sysload
$Meta/ConfigWindow/Info/InstanceSize.text = instance_size
if Input.is_mouse_button_pressed(BUTTON_LEFT) and can_grab:
position = get_global_mouse_position() + grabbed_offset
if sysload <= 0.5:
$CollisionShape2D.modulate = Color(1,1,1,1)
if sysload > 0.5 and sysload < 0.9:
$CollisionShape2D.modulate = Color(1,0.5,0)
if sysload >= 0.9:
$CollisionShape2D.modulate = Color(1,0,0)
$Meta/ConfigWindow/Info/Cost.text = str(properties['monthly_cost']*cpu)
if config_warning:
$Meta/ToggleConfig.modulate = Color(1,0.2,0.2,1)
else:
$Meta/ToggleConfig.modulate = $Meta/ToggleConfig.self_modulate
func get_response(req):
request_animation()
var response = yield(process_request(req), "completed")
return_response(req, response)
func check_content_type(req_type):
if req_type in accepted_content_types:
return true
else:
return false
func request_animation():
$CollisionShape2D.scale.x += sizeup_animation_scale
$CollisionShape2D.scale.y += sizeup_animation_scale
var timer = get_tree().create_timer(0.1)
timer.connect('timeout',self, "_request_animation_end")
func _request_animation_end():
$CollisionShape2D.scale.x -= sizeup_animation_scale
$CollisionShape2D.scale.y -= sizeup_animation_scale
func return_response(req, response):
if response: #temp bug workaround
req.response = req.response + response
if check_content_type(req.type):
req.status_code = 200
else:
req.status_code = 404
req.send()
func generate_system_load(wait):
var amount = properties['load_per_request'] / cpu
var duration = (properties['load_per_request'] / cpu)*5 + properties['load_per_request']
sysload = sysload + amount
duration *= (1+sysload)
var timer = get_tree().create_timer(wait+duration)
timer.connect('timeout',self, "end_system_load", [amount])
func end_system_load(amount):
sysload = sysload - amount
func calculate_response_time():
var wait
if sysload < 0.9:
wait = 0.5*sysload
else:
wait = 5
generate_system_load(wait)
yield(get_tree().create_timer(wait), "timeout")
#should yield
func process_request(req):
yield(get_tree(), "idle_frame")
req.route.pop_back()
req.money += properties['revenue']
yield(calculate_response_time(), "completed")
return("200 OK")
func upgrade():
cpu *= 2
var upgrade_cost = properties['monthly_cost']*cpu
var new = $CollisionShape2D/AnimatedSprite.duplicate()
new.name = "cpu"+str(cpu)
new.position[1] = -25*(log(cpu)/log(2))
$CollisionShape2D.add_child(new)
instance_size = instance_family + "-" + str(cpu)
if cpu == cpu_max:
$Meta/ConfigWindow/Info/UpgradeInstance.disabled = true
func _on_ToggleConfig_pressed():
emit_signal("tutorial_complete", "config_window_open")
if $Meta/ConfigWindow.visible:
hide_config()
else:
for server in level.iptable.values():
#server.hide_config() #Don't hide other config windows
pass
show_config()
func hide_config():
$CollisionShape2D.z_index = 0
$Meta.z_index = 0
$Meta/ConfigWindow.visible = false
$Meta/ToggleConfig/Icon.icon_name = "wrench"
$Meta/ToggleConfig/Icon.modulate = self_modulate
func show_config():
$CollisionShape2D.z_index = 10
$Meta.z_index = 10
$Meta/ConfigWindow.visible = true
$Meta/ToggleConfig/Icon.icon_name = "window-close"
$Meta/ToggleConfig/Icon.modulate = Color(1,0,0,0.8)
func _on_Name_text_changed(text):
server_name = text