-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevel.gd
169 lines (127 loc) · 4.17 KB
/
Level.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
extends Node2D
onready var nav_map = $Navigation2D
var show_tutorial = false
const StaticServer = preload("res://StaticServer.tscn")
const DynamicServer = preload("res://DynamicServer.tscn")
const LoadBalancer = preload("res://LoadBalancer.tscn")
const Database = preload("res://Database.tscn")
const Firewall = preload("res://Firewall.tscn")
const User = preload("res://User.tscn")
const Request = preload("res://Request.tscn")
var iptable = {}
export var product_cost_base = 30
export var product_cost: float
export var money = 250 setget set_money
export var month_income = 0
export var game_over = true
export var server_costs = 0
export var month = 0
var messages = []
var months = {}
var objects = {}
var request_logs = []
export var dns_record: String
var month_timer: Timer
var request_types = ['static', 'dynamic']
var servers = {}
var last_created_object: Node2D
func _ready():
get_tree().paused = false
randomize()
connect_signals()
objects = load("res://src/objects.gd")
objects = JSON.parse(objects.json).result
months = objects['months']
month_timer = $MonthTimer
month_timer.wait_time = objects['month']['duration']
$Splash.visible = true
func _process(_delta):
server_costs = calculate_server_cost()
func connect_signals():
$HUD.connect("user_request", self, "new_user_request")
$HUD.connect("new_month", self, "new_month")
$HUD.connect('dns_change', self, 'set_dns_record')
$HUD.connect('new_server', self, 'new_instance')
$HUD.connect('clear', self, 'clear')
$HUD.connect('tutorial_complete', $Tutorial, 'tutorial_complete')
func clear():
var all = get_tree().get_root().get_child(0).get_children()
for i in all:
if i.is_class("User"):
i.queue_free()
if i.is_class("Request"):
i.queue_free()
func new_instance(obj_name):
var obj = get(obj_name)
var new = obj.instance()
add_child(new)
new.init2()
new.connect("tutorial_complete", $Tutorial, 'tutorial_complete')
return new
func set_dns_record():
dns_record = $HUD.dns_record
#todo: move this under user
func new_user_request(is_malicious:bool, speed:String="slow"):
var user = new_instance("User")
var request = new_instance("Request")
var req_speed:int = user.speeds[speed]
request.type = request_types[randi()%2]
var potential_urls = objects['request_urls'][request.type]
request.url = potential_urls[randi()%len(potential_urls)]
if request.type == 'static':
request.method = 'GET'
else:
var methods = ['POST', 'PUT', 'PATCH']
request.method = methods[randi() % len(methods)]
request.data = request.method + " " + request.url
request.set_origin(user)
request.route.append(user.eth0.ip)
request.route.append(dns_record)
request.is_malicious = is_malicious
request.send(req_speed)
func add_log(msg, is_error=false):
$HUD.add_log(msg, is_error)
func set_money(val):
if val < 0:
game_over = true
#get_tree().paused = true
$HUD/GameOver.blocking_popup_centered()
if money < val:
month_income += val - money
money = val
func new_month():
month += 1
month_income = 0
product_cost = product_cost_base * month
server_costs = calculate_server_cost()
var spread:float = 1/(log(month+1)*log(month+1))
while month_timer.time_left:
var is_malicious = false
if randf() < objects['requests']['malicious'] and month>=3:
is_malicious = true
new_user_request(is_malicious)
yield(get_tree().create_timer(spread), "timeout")
func calculate_server_cost():
server_costs = 0
for s in iptable.values():
if s.properties['monthly_cost']:
server_costs += s.properties['monthly_cost']*s.cpu
return server_costs
func _on_MonthTimer_timeout():
money -= server_costs + product_cost
if !$MonthTimer.one_shot:
new_month()
func add_highlight(node):
var highlight = CanvasItemMaterial.new()
highlight.blend_mode = BLEND_MODE_ADD
get_tree().get_root().get_node("Level0/HUD/Panel/new_StaticServer").material = highlight
func remove_highlight(node):
var empty_material = CanvasItemMaterial.new()
get_tree().get_root().get_node("Level0/HUD/Panel/new_StaticServer").material = empty_material
func _on_start_game_pressed():
show_tutorial = $Splash/checkbox.pressed
$Splash.visible = false
if show_tutorial:
money = 1000
$Tutorial.visible = true
$Tutorial.start()