2022-06-29 13:29:11 -04:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
2022-07-13 21:14:30 -04:00
|
|
|
export var monitor_enabled: bool = false
|
2022-09-01 11:30:01 -04:00
|
|
|
export var debug: bool = false
|
2022-06-29 13:29:11 -04:00
|
|
|
export var development_url: String = "http://localhost:4050/api/v1"
|
|
|
|
var url_real: String = "https://alai.cromer.cl/api/v1"
|
|
|
|
export var use_development_url: bool = false
|
|
|
|
onready var url: String = development_url if use_development_url else url_real
|
|
|
|
|
2022-07-04 11:12:08 -04:00
|
|
|
var start_time: int = 0
|
|
|
|
var started: bool = false
|
|
|
|
|
2022-06-29 13:29:11 -04:00
|
|
|
var player: Dictionary = {}
|
|
|
|
var os_id: int = 0
|
|
|
|
var godot_version: Dictionary = Engine.get_version_info()
|
|
|
|
var processor_count: int = OS.get_processor_count()
|
|
|
|
var screen_count: int = OS.get_screen_count()
|
|
|
|
var screen_dpi: int = OS.get_screen_dpi()
|
|
|
|
var screen_size: Vector2 = OS.get_screen_size()
|
|
|
|
var machine_id: String = OS.get_unique_id()
|
|
|
|
var locale: String = OS.get_locale()
|
|
|
|
var game_version: String
|
|
|
|
var frames: Array = []
|
|
|
|
|
|
|
|
var coins: int = 0
|
|
|
|
var points: int = 0
|
|
|
|
var objects: Array = []
|
|
|
|
|
|
|
|
const empty_object: Dictionary = {
|
|
|
|
"name": "Object Name",
|
|
|
|
"state": "Object State",
|
|
|
|
"position_x": 0,
|
|
|
|
"position_y": 0,
|
|
|
|
"velocity_x": 0,
|
|
|
|
"velocity_y": 0
|
|
|
|
}
|
|
|
|
const empty_frame: Dictionary = {
|
|
|
|
"coins": 0,
|
|
|
|
"points": 0,
|
|
|
|
"fps": 0,
|
|
|
|
"elapsed_time": 0,
|
|
|
|
"objects": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
# The game dictionary holds all data to be sent to the server
|
|
|
|
var game: Dictionary = {}
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2022-08-27 18:10:36 -04:00
|
|
|
Event.connect("object_created", self, "_object_created")
|
|
|
|
Event.connect("object_updated", self, "_object_updated")
|
|
|
|
Event.connect("object_removed", self, "_object_removed")
|
2022-09-01 11:30:01 -04:00
|
|
|
Event.connect("game_started", self, "_on_game_started")
|
|
|
|
Event.connect("player_died", self, "_on_game_over")
|
|
|
|
Event.connect("player_won", self, "_on_game_won")
|
2022-08-27 22:34:18 -04:00
|
|
|
Event.connect("coin_collected", self, "_on_coin_update")
|
2022-09-01 11:30:01 -04:00
|
|
|
|
2022-06-29 13:29:11 -04:00
|
|
|
game_version = get_parent().game_version
|
|
|
|
|
2022-07-08 00:57:46 -04:00
|
|
|
player["rut"] = ""
|
|
|
|
player["name"] = ""
|
|
|
|
player["email"] = ""
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
var os_name = OS.get_name()
|
|
|
|
if os_name == "Android":
|
|
|
|
os_id = 1
|
|
|
|
elif os_name == "iOS":
|
|
|
|
os_id = 2
|
|
|
|
elif os_name == "HTML5":
|
|
|
|
os_id = 3
|
|
|
|
elif os_name == "OSX":
|
|
|
|
os_id = 4
|
|
|
|
elif os_name == "Server":
|
|
|
|
os_id = 5
|
|
|
|
elif os_name == "Windows":
|
|
|
|
os_id = 6
|
|
|
|
elif os_name == "UWP":
|
|
|
|
os_id = 7
|
|
|
|
elif os_name == "X11":
|
|
|
|
os_id = 8
|
|
|
|
else:
|
|
|
|
os_id = 0
|
|
|
|
|
|
|
|
game["player"] = player
|
2022-07-07 13:01:47 -04:00
|
|
|
game["level_id"] = 0
|
2022-06-29 13:29:11 -04:00
|
|
|
game["os_id"] = os_id
|
|
|
|
game["godot_version"] = godot_version
|
|
|
|
game["processor_count"] = processor_count
|
|
|
|
game["machine_id"] = machine_id
|
|
|
|
game["locale"] = locale
|
|
|
|
game["screen_count"] = screen_count
|
|
|
|
game["screen_dpi"] = screen_dpi
|
|
|
|
game["screen_size"] = screen_size
|
|
|
|
game["game_version"] = game_version
|
2022-07-07 13:01:47 -04:00
|
|
|
game["won"] = false
|
2022-08-05 13:23:45 -04:00
|
|
|
game["timestamp"] = int(Time.get_unix_time_from_system())
|
2022-06-29 13:29:11 -04:00
|
|
|
game["frames"] = frames
|
|
|
|
|
|
|
|
var err = $HTTPRequest.connect("request_completed", self, "_on_request_completed")
|
|
|
|
if err != OK:
|
|
|
|
print(err)
|
|
|
|
|
2022-07-08 00:57:46 -04:00
|
|
|
err = $MonitorGUI.find_node("Button").connect("input_validated", self, "_on_input_validated")
|
|
|
|
if err != OK:
|
|
|
|
print(err)
|
|
|
|
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
func _physics_process(_delta: float) -> void:
|
2022-07-13 21:14:30 -04:00
|
|
|
if monitor_enabled:
|
2022-07-23 13:27:21 -04:00
|
|
|
if has_node("MonitorGUI") and not $MonitorGUI.visible:
|
2022-07-21 00:14:14 -04:00
|
|
|
$MonitorGUI.visible = true
|
2022-09-01 11:30:01 -04:00
|
|
|
Event.emit_signal("monitor_loaded")
|
2022-07-23 13:27:21 -04:00
|
|
|
|
2022-07-13 21:14:30 -04:00
|
|
|
if started and not get_tree().paused:
|
2022-07-07 13:01:47 -04:00
|
|
|
var frame = empty_frame.duplicate(true)
|
|
|
|
frame["coins"] = coins
|
|
|
|
frame["points"] = points
|
|
|
|
frame["fps"] = Engine.get_frames_per_second()
|
2022-08-05 13:23:45 -04:00
|
|
|
frame["elapsed_time"] = Time.get_ticks_msec() - start_time
|
2022-06-29 13:29:11 -04:00
|
|
|
|
2022-07-07 13:01:47 -04:00
|
|
|
var frame_objects = objects.duplicate()
|
|
|
|
frame["objects"] = frame_objects
|
2022-06-29 13:29:11 -04:00
|
|
|
|
2022-07-07 13:01:47 -04:00
|
|
|
frames.append(frame)
|
2022-06-29 13:29:11 -04:00
|
|
|
|
2022-07-07 13:01:47 -04:00
|
|
|
if Input.is_action_just_pressed("Send"):
|
|
|
|
stop_monitor()
|
|
|
|
send_data()
|
|
|
|
else:
|
|
|
|
if Input.is_action_just_pressed("Send"):
|
|
|
|
start_monitor()
|
2022-07-13 21:14:30 -04:00
|
|
|
else:
|
|
|
|
get_tree().paused = false
|
2022-09-01 11:30:01 -04:00
|
|
|
Event.emit_signal("monitor_loaded")
|
2022-07-13 21:14:30 -04:00
|
|
|
queue_free()
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
2022-07-08 00:57:46 -04:00
|
|
|
func _on_input_validated(validated_player: Dictionary) -> void:
|
|
|
|
$MonitorGUI.queue_free()
|
|
|
|
get_tree().paused = false
|
2022-09-01 11:30:01 -04:00
|
|
|
Event.emit_signal("game_started")
|
2022-07-08 00:57:46 -04:00
|
|
|
player = validated_player.duplicate(true)
|
2022-08-14 12:42:49 -04:00
|
|
|
game["player"] = player
|
2022-07-08 00:57:46 -04:00
|
|
|
|
|
|
|
|
2022-06-29 13:29:11 -04:00
|
|
|
func _object_created(name: String, state: String, position: Vector2, velocity: Vector2) -> void:
|
2022-08-14 16:53:44 -04:00
|
|
|
if monitor_enabled:
|
2022-07-04 11:12:08 -04:00
|
|
|
add_object(name, state, position, velocity)
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
func _object_updated(name: String, state: String, position: Vector2, velocity: Vector2) -> void:
|
2022-08-14 16:53:44 -04:00
|
|
|
if monitor_enabled:
|
2022-07-04 11:12:08 -04:00
|
|
|
remove_object(name)
|
|
|
|
add_object(name, state, position, velocity)
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
func _object_removed(name: String) -> void:
|
2022-08-14 16:53:44 -04:00
|
|
|
if monitor_enabled:
|
2022-07-04 11:12:08 -04:00
|
|
|
remove_object(name)
|
|
|
|
|
|
|
|
|
|
|
|
func start_monitor() -> void:
|
2022-07-07 13:01:47 -04:00
|
|
|
frames.clear()
|
|
|
|
game["level_id"] = 2 # PrototypeR
|
|
|
|
game["won"] = false
|
2022-08-29 21:44:08 -04:00
|
|
|
coins = 0
|
|
|
|
points = 0
|
2022-08-05 13:23:45 -04:00
|
|
|
game["timestamp"] = int(Time.get_unix_time_from_system())
|
|
|
|
start_time = Time.get_ticks_msec()
|
2022-07-04 11:12:08 -04:00
|
|
|
started = true
|
|
|
|
|
|
|
|
|
|
|
|
func stop_monitor() -> void:
|
|
|
|
started = false
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
func add_object(name: String, state: String, position: Vector2, velocity: Vector2) -> void:
|
|
|
|
var object = empty_object.duplicate(true)
|
|
|
|
object["name"] = name
|
|
|
|
object["state"] = state
|
|
|
|
object["position_x"] = position.x
|
|
|
|
object["position_y"] = position.y
|
|
|
|
object["velocity_x"] = velocity.x
|
|
|
|
object["velocity_y"] = velocity.y
|
|
|
|
|
|
|
|
objects.append(object)
|
|
|
|
|
|
|
|
|
|
|
|
func remove_object(name: String) -> void:
|
|
|
|
for i in range(0, objects.size()):
|
2022-08-14 16:53:44 -04:00
|
|
|
if objects[i]["name"] == name:
|
|
|
|
objects.remove(i)
|
2022-09-01 11:30:01 -04:00
|
|
|
return
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
func _on_coin_update(amount: int) -> void:
|
|
|
|
coins = coins + amount
|
|
|
|
|
|
|
|
|
|
|
|
func _on_request_completed(result: int, response_code: int, headers: PoolStringArray, body: PoolByteArray) -> void:
|
|
|
|
if result != HTTPRequest.RESULT_SUCCESS:
|
|
|
|
print_debug("Error: Failed to connect with error code: " + str(result))
|
|
|
|
return
|
|
|
|
|
|
|
|
if response_code != HTTPClient.RESPONSE_OK:
|
|
|
|
print_debug("Error: Failed response with error code: " + str(response_code))
|
|
|
|
|
|
|
|
print_debug(headers)
|
|
|
|
if body.size() > 0:
|
|
|
|
var json = JSON.parse(body.get_string_from_utf8())
|
|
|
|
print_debug(JSON.print(json.result, "\t"))
|
|
|
|
|
|
|
|
|
|
|
|
func send_data() -> void:
|
|
|
|
var json = JSON.print(game)
|
|
|
|
var headers = ["Content-Type: application/json", "Content-Encoding: gzip", "Content-Transfer-Encoding: base64"]
|
|
|
|
|
|
|
|
var body = compress_payload(json)
|
|
|
|
|
|
|
|
print("JSON B: " + String(json.length()))
|
|
|
|
print("JSON MB: " + String(json.length() / pow(2, 20)))
|
|
|
|
print("Body B: " + String(body.length()))
|
|
|
|
print("Body MB: " + String(body.length() / pow(2, 20)))
|
|
|
|
|
2022-09-01 11:30:01 -04:00
|
|
|
if not debug:
|
|
|
|
$HTTPRequest.request(url + "/game", headers, false, HTTPClient.METHOD_POST, body)
|
|
|
|
else:
|
|
|
|
var file = File.new()
|
|
|
|
if file.open("user://game.json", File.WRITE) != 0:
|
|
|
|
print_debug("Could not open game.json for writing!")
|
|
|
|
return
|
|
|
|
|
|
|
|
file.store_string(json)
|
|
|
|
file.close()
|
2022-06-29 13:29:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
func compress_payload(payload: String) -> String:
|
|
|
|
var bytes = payload.to_utf8()
|
|
|
|
var compressed = bytes.compress(File.COMPRESSION_GZIP)
|
|
|
|
|
|
|
|
var new_payload = Marshalls.raw_to_base64(compressed)
|
|
|
|
|
|
|
|
return new_payload
|
|
|
|
|
|
|
|
|
2022-09-01 11:30:01 -04:00
|
|
|
func _on_game_started() -> void:
|
|
|
|
print_debug("started game")
|
|
|
|
if not started:
|
|
|
|
start_monitor()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_game_over() -> void:
|
|
|
|
if started:
|
|
|
|
stop_monitor()
|
|
|
|
game["won"] = false
|
|
|
|
send_data()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_game_won() -> void:
|
|
|
|
if started:
|
|
|
|
stop_monitor()
|
|
|
|
game["won"] = true
|
|
|
|
send_data()
|