From 962c5ab6f97018c5c4a7a224fddaed800ef7933e Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 13 Jul 2022 21:14:30 -0400 Subject: [PATCH] validate information and send it to the monitor --- godot/Main.tscn | 1 - godot/monitor/EnterButton.gd | 104 ++++++++++++++++++- godot/monitor/GUI.tscn | 187 +++++++++++++---------------------- godot/monitor/Monitor.gd | 130 ++---------------------- godot/monitor/Monitor.tscn | 2 + godot/monitor/Rut.gd | 74 ++++++++++++-- godot/monitor/Text.gd | 14 --- 7 files changed, 251 insertions(+), 261 deletions(-) delete mode 100644 godot/monitor/Text.gd diff --git a/godot/Main.tscn b/godot/Main.tscn index 0896dfd..24b4abe 100644 --- a/godot/Main.tscn +++ b/godot/Main.tscn @@ -9,4 +9,3 @@ script = ExtResource( 1 ) level = ExtResource( 2 ) [node name="Monitor" parent="." instance=ExtResource( 3 )] -enabled = true diff --git a/godot/monitor/EnterButton.gd b/godot/monitor/EnterButton.gd index 2430822..f66c90e 100644 --- a/godot/monitor/EnterButton.gd +++ b/godot/monitor/EnterButton.gd @@ -4,13 +4,115 @@ extends Button signal input_validated(player) +func is_valid_name(name: String) -> bool: + if name.strip_edges() == "": + print_debug("Name is empty!") + return false + if name.split(" ").size() == 1: + print_debug("Doesn't contain at least a first and last name!") + return false + return true + + +func is_valid_rut(rut: String) -> bool: + var rut_node = get_node("%Rut") + rut = rut_node.clean_rut(rut) + if rut.length() < 8 or rut.length() > 9: + print_debug("RUT length is invalid!") + return false + + var rut_temp: String = rut.substr(0, rut.length() - 1) + var verifier: String = rut.substr(rut.length() - 1, 1) + + if not rut_temp.is_valid_integer(): + print_debug("RUT isn't a valid integer!") + return false + + if rut_temp.to_int() > 50000000: + print_debug("RUT is too large, that is a company!") + return false + + if verifier != generate_verifier(rut): + return false + + return true + + +func generate_verifier(rut: String) -> String: + if not rut.is_valid_integer(): + print_debug("RUT isn't a valid integer!") + return "" + + var multiplier: int = 2 + var sum: int = 0 + var remainder: int = 0 + var division: int = 0 + var rut_length: int = rut.length() + + var i: int = rut_length - 1 + while i >= 0: + sum = sum + rut.substr(i, i + 1).to_int() * multiplier + multiplier = multiplier + 1 + if multiplier == 8: + multiplier = 2 + i = i - 1 + + var tempSum: float = int(sum) + division = int(floor(tempSum / 11)) + division = division * 11 + remainder = sum - division + + if remainder != 0: + remainder = 11 - remainder + + if remainder == 10: + return "k" + else: + return String(remainder) + + +func is_valid_email(email: String) -> bool: + var regex = RegEx.new() + regex.compile("\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+") + if regex.search(email) == null: + print_debug("Isn't a valid email address!") + return false + return true + func _on_Button_pressed() -> void: var name = get_node("%Name") var rut = get_node("%Rut") var email = get_node("%Email") var player: Dictionary = { "name" : name.text, - "rut" : rut.text, + "rut" : get_node("%Rut").clean_rut(rut.text), "email" : email.text } + + if not is_valid_name(player.name): + show_error_message("Ingresa un nombre completo valido por favor!") + return + + if not is_valid_rut(player.rut): + show_error_message("Ingresa un RUT valido por favor!") + return + + if not is_valid_email(player.email): + show_error_message("Ingresa un email valido por favor!") + return + emit_signal("input_validated", player) + + +func show_error_message(message: String) -> void: + var popup = get_node("%PopupDialog") + popup.get_node("ErrorMessage").text = message + popup.popup() + popup.focus_mode = Control.FOCUS_ALL + popup.grab_focus() + + +func _on_PopupDialog_gui_input(event: InputEvent) -> void: + if event.is_pressed(): + var popup = get_node("%PopupDialog") + popup.hide() diff --git a/godot/monitor/GUI.tscn b/godot/monitor/GUI.tscn index 2290c25..2fb72ed 100644 --- a/godot/monitor/GUI.tscn +++ b/godot/monitor/GUI.tscn @@ -1,63 +1,22 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://assets/fonts/data/PixelOperator.tres" type="DynamicFontData" id=1] [ext_resource path="res://assets/fonts/data/PixelOperator-Bold.tres" type="DynamicFontData" id=2] [ext_resource path="res://monitor/Rut.gd" type="Script" id=3] -[ext_resource path="res://monitor/Text.gd" type="Script" id=4] [ext_resource path="res://monitor/EnterButton.gd" type="Script" id=5] [sub_resource type="DynamicFont" id=1] font_data = ExtResource( 1 ) -[sub_resource type="DynamicFont" id=2] -size = 12 -font_data = ExtResource( 2 ) - -[sub_resource type="DynamicFont" id=3] -size = 12 -font_data = ExtResource( 2 ) - -[sub_resource type="DynamicFont" id=4] -size = 12 -font_data = ExtResource( 2 ) - -[sub_resource type="DynamicFont" id=9] +[sub_resource type="DynamicFont" id=14] size = 8 -extra_spacing_top = 2 font_data = ExtResource( 1 ) -[sub_resource type="StyleBoxFlat" id=5] -content_margin_left = 5.0 -content_margin_right = 5.0 -bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) -border_blend = true -corner_radius_top_left = 5 -corner_radius_top_right = 5 -corner_radius_bottom_right = 5 -corner_radius_bottom_left = 5 - -[sub_resource type="DynamicFont" id=10] -size = 8 -extra_spacing_top = 2 -font_data = ExtResource( 1 ) - -[sub_resource type="StyleBoxFlat" id=6] -content_margin_left = 5.0 -content_margin_right = 5.0 -bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) -corner_radius_top_left = 5 -corner_radius_top_right = 5 -corner_radius_bottom_right = 5 -corner_radius_bottom_left = 5 - -[sub_resource type="DynamicFont" id=8] -size = 8 -extra_spacing_top = 2 -font_data = ExtResource( 1 ) - -[sub_resource type="StyleBoxFlat" id=7] +[sub_resource type="StyleBoxFlat" id=13] content_margin_left = 5.0 content_margin_right = 5.0 +content_margin_top = 2.0 +content_margin_bottom = 2.0 bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) corner_radius_top_left = 5 corner_radius_top_right = 5 @@ -83,6 +42,20 @@ corner_radius_top_right = 5 corner_radius_bottom_right = 5 corner_radius_bottom_left = 5 +[sub_resource type="StyleBoxFlat" id=15] +bg_color = Color( 0.239216, 0.239216, 0.239216, 1 ) +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 +expand_margin_left = 5.0 +expand_margin_right = 5.0 +expand_margin_top = 5.0 +expand_margin_bottom = 5.0 + +[sub_resource type="DynamicFont" id=16] +font_data = ExtResource( 2 ) + [node name="MonitorGUI" type="CanvasLayer"] pause_mode = 2 @@ -102,6 +75,7 @@ anchor_right = 1.0 anchor_bottom = 1.0 size_flags_horizontal = 3 size_flags_vertical = 3 +custom_constants/separation = 0 [node name="CenterContainer" type="CenterContainer" parent="GUI/VBoxContainer"] margin_right = 512.0 @@ -120,98 +94,60 @@ align = 1 autowrap = true [node name="CenterContainer2" type="CenterContainer" parent="GUI/VBoxContainer"] -margin_top = 100.0 +margin_top = 96.0 margin_right = 512.0 -margin_bottom = 196.0 +margin_bottom = 192.0 rect_min_size = Vector2( 0, 96 ) size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="HBoxContainer" type="HBoxContainer" parent="GUI/VBoxContainer/CenterContainer2"] -margin_left = 6.0 +[node name="VBoxContainer2" type="VBoxContainer" parent="GUI/VBoxContainer/CenterContainer2"] +margin_left = 16.0 margin_top = 14.0 -margin_right = 506.0 +margin_right = 496.0 margin_bottom = 82.0 -rect_min_size = Vector2( 500, 0 ) -size_flags_horizontal = 13 -size_flags_vertical = 13 - -[node name="VBoxContainer" type="VBoxContainer" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer"] -margin_right = 74.0 -margin_bottom = 68.0 +rect_min_size = Vector2( 480, 0 ) +size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="Label" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] -margin_top = 3.0 -margin_right = 74.0 -margin_bottom = 16.0 -size_flags_vertical = 6 -custom_fonts/font = SubResource( 2 ) -text = "Nombre" -align = 2 - -[node name="Label2" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] -margin_top = 27.0 -margin_right = 74.0 -margin_bottom = 40.0 -size_flags_vertical = 6 -custom_fonts/font = SubResource( 3 ) -text = "RUT" -align = 2 - -[node name="Label3" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] -margin_top = 51.0 -margin_right = 74.0 -margin_bottom = 64.0 -size_flags_vertical = 6 -custom_fonts/font = SubResource( 4 ) -text = "Email" -align = 2 - -[node name="VBoxContainer2" type="VBoxContainer" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer"] -margin_left = 78.0 -margin_right = 500.0 -margin_bottom = 68.0 -size_flags_horizontal = 3 - -[node name="Name" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +[node name="Name" type="LineEdit" parent="GUI/VBoxContainer/CenterContainer2/VBoxContainer2"] unique_name_in_owner = true -margin_right = 422.0 +margin_right = 480.0 margin_bottom = 20.0 -rect_min_size = Vector2( 300, 20 ) -size_flags_horizontal = 3 -size_flags_vertical = 3 -custom_fonts/font = SubResource( 9 ) -custom_styles/normal = SubResource( 5 ) +custom_fonts/font = SubResource( 14 ) +custom_styles/normal = SubResource( 13 ) +context_menu_enabled = false +clear_button_enabled = true +placeholder_text = "Nombre Completo" caret_blink = true -script = ExtResource( 4 ) -[node name="Rut" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +[node name="Rut" type="LineEdit" parent="GUI/VBoxContainer/CenterContainer2/VBoxContainer2"] unique_name_in_owner = true margin_top = 24.0 -margin_right = 422.0 +margin_right = 480.0 margin_bottom = 44.0 -rect_min_size = Vector2( 0, 20 ) -custom_fonts/font = SubResource( 10 ) -custom_styles/normal = SubResource( 6 ) +custom_fonts/font = SubResource( 14 ) +custom_styles/normal = SubResource( 13 ) +clear_button_enabled = true +placeholder_text = "RUT" caret_blink = true script = ExtResource( 3 ) -[node name="Email" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +[node name="Email" type="LineEdit" parent="GUI/VBoxContainer/CenterContainer2/VBoxContainer2"] unique_name_in_owner = true margin_top = 48.0 -margin_right = 422.0 +margin_right = 480.0 margin_bottom = 68.0 -rect_min_size = Vector2( 0, 20 ) -custom_fonts/font = SubResource( 8 ) -custom_styles/normal = SubResource( 7 ) +custom_fonts/font = SubResource( 14 ) +custom_styles/normal = SubResource( 13 ) +clear_button_enabled = true +placeholder_text = "Email" caret_blink = true -script = ExtResource( 4 ) [node name="CenterContainer3" type="CenterContainer" parent="GUI/VBoxContainer"] -margin_top = 200.0 +margin_top = 192.0 margin_right = 512.0 -margin_bottom = 296.0 +margin_bottom = 288.0 rect_min_size = Vector2( 0, 96 ) size_flags_horizontal = 3 size_flags_vertical = 3 @@ -228,8 +164,27 @@ enabled_focus_mode = 0 text = "Ingresar" script = ExtResource( 5 ) -[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Name" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Name" method="_on_text_changed"] -[connection signal="focus_exited" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" method="_on_Rut_focus_exited"] -[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" method="_on_Rut_text_changed"] -[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Email" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Email" method="_on_text_changed"] +[node name="PopupDialog" type="PopupDialog" parent="."] +unique_name_in_owner = true +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 96.0 +margin_top = 96.0 +margin_right = -96.0 +margin_bottom = -96.0 +custom_styles/panel = SubResource( 15 ) + +[node name="ErrorMessage" type="Label" parent="PopupDialog"] +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 7 +custom_fonts/font = SubResource( 16 ) +text = "Error Message" +align = 1 +valign = 1 +autowrap = true + +[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/VBoxContainer2/Rut" to="GUI/VBoxContainer/CenterContainer2/VBoxContainer2/Rut" method="_on_Rut_text_changed"] [connection signal="pressed" from="GUI/VBoxContainer/CenterContainer3/Button" to="GUI/VBoxContainer/CenterContainer3/Button" method="_on_Button_pressed"] +[connection signal="gui_input" from="PopupDialog" to="GUI/VBoxContainer/CenterContainer3/Button" method="_on_PopupDialog_gui_input"] diff --git a/godot/monitor/Monitor.gd b/godot/monitor/Monitor.gd index 9a4e6f8..b9628a1 100644 --- a/godot/monitor/Monitor.gd +++ b/godot/monitor/Monitor.gd @@ -1,7 +1,7 @@ extends Node -export var enabled: bool = false +export var monitor_enabled: bool = false 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 @@ -73,8 +73,6 @@ func _ready() -> void: else: os_id = 0 - player["rut"] = clean_rut(player["rut"]) - game["player"] = player game["level_id"] = 0 game["os_id"] = os_id @@ -100,8 +98,8 @@ func _ready() -> void: func _physics_process(_delta: float) -> void: - if enabled: - if started: + if monitor_enabled: + if started and not get_tree().paused: var frame = empty_frame.duplicate(true) frame["coins"] = coins frame["points"] = points @@ -119,6 +117,9 @@ func _physics_process(_delta: float) -> void: else: if Input.is_action_just_pressed("Send"): start_monitor() + else: + get_tree().paused = false + queue_free() func _on_input_validated(validated_player: Dictionary) -> void: @@ -128,18 +129,18 @@ func _on_input_validated(validated_player: Dictionary) -> void: func _object_created(name: String, state: String, position: Vector2, velocity: Vector2) -> void: - if enabled and started: + if monitor_enabled and started: add_object(name, state, position, velocity) func _object_updated(name: String, state: String, position: Vector2, velocity: Vector2) -> void: - if enabled and started: + if monitor_enabled and started: remove_object(name) add_object(name, state, position, velocity) func _object_removed(name: String) -> void: - if enabled and started: + if monitor_enabled and started: remove_object(name) @@ -215,116 +216,3 @@ func compress_payload(payload: String) -> String: return new_payload -func clean_rut(rut: String) -> String: - rut = rut.strip_escapes() - rut = rut.strip_edges(true, true) - rut = rut.to_lower() - rut = rut.replace(".", "") - rut = rut.replace("-", "") - - var rutTemp: String = rut.substr(0, rut.length() - 1) - var verifier: String = rut.substr(rut.length() - 1, 1) - - var regex = RegEx.new() - regex.compile("\\D") - rutTemp = regex.sub(rutTemp, "", true) - - regex.compile("[^kK\\d]") - verifier = regex.sub(verifier, "", true) - - rut = rutTemp + verifier - - return rut - - -func pretty_rut(rut: String) -> String: - rut = clean_rut(rut) - - var rutTemp: String = rut.substr(0, rut.length() - 1) - var verifier: String = rut.substr(rut.length() - 1, 1) - - var regex = RegEx.new() - regex.compile("[^kK\\d]") - verifier = regex.sub(verifier, "", true) - - var byteArray = rutTemp.to_utf8() - byteArray.invert() - - var newByteArray: PoolByteArray = PoolByteArray() - var i = 1 - for symbol in byteArray: - newByteArray.append(symbol) - if i == 3: - newByteArray.append(".".to_utf8()[0]) - i = 0 - i = i + 1 - if newByteArray.size() > 0 and newByteArray[newByteArray.size() - 1] == ".".to_utf8()[0]: - newByteArray.resize(newByteArray.size() - 1) - - newByteArray.invert() - rutTemp = newByteArray.get_string_from_utf8() - - if rutTemp.length() == 0 and verifier.length() > 0: - rutTemp = verifier - elif rutTemp.length() > 0 and verifier.length() > 0: - rutTemp = rutTemp + "-" + verifier - - return rutTemp - - -func is_rut_valid(rut: String) -> bool: - rut = clean_rut(rut) - if rut.length() < 8 or rut.length() > 9: - print_debug("RUT length is invalid!") - return false - - var rutTemp: String = rut.substr(0, rut.length() - 1) - var verifier: String = rut.substr(rut.length() - 1, 1) - print("Rut: " + rutTemp) - print("Verifier: " + verifier) - - if not rutTemp.is_valid_integer(): - print_debug("RUT isn't a valid integer!") - return false - - if rutTemp.to_int() > 50000000: - print_debug("RUT is too large, that is a company!") - return false - - if verifier != generate_verifier(rut): - return false - - return true - - -func generate_verifier(rut: String) -> String: - if not rut.is_valid_integer(): - print_debug("RUT isn't a valid integer!") - return "" - - var multiplier: int = 2 - var sum: int = 0 - var remainder: int = 0 - var division: int = 0 - var rutLength: int = rut.length() - - var i: int = rutLength - 1 - while i >= 0: - sum = sum + rut.substr(i, i + 1).to_int() * multiplier - multiplier = multiplier + 1 - if multiplier == 8: - multiplier = 2 - i = i - 1 - - var tempSum: float = int(sum) - division = int(floor(tempSum / 11)) - division = division * 11 - remainder = sum - division - - if remainder != 0: - remainder = 11 - remainder - - if remainder == 10: - return "k" - else: - return String(remainder) diff --git a/godot/monitor/Monitor.tscn b/godot/monitor/Monitor.tscn index a50b176..46070db 100644 --- a/godot/monitor/Monitor.tscn +++ b/godot/monitor/Monitor.tscn @@ -4,7 +4,9 @@ [ext_resource path="res://monitor/GUI.tscn" type="PackedScene" id=3] [node name="Monitor" type="Node"] +pause_mode = 2 script = ExtResource( 1 ) +monitor_enabled = true use_development_url = true [node name="MonitorGUI" parent="." instance=ExtResource( 3 )] diff --git a/godot/monitor/Rut.gd b/godot/monitor/Rut.gd index ea7339c..85b66d0 100644 --- a/godot/monitor/Rut.gd +++ b/godot/monitor/Rut.gd @@ -1,13 +1,71 @@ -extends TextEdit +extends LineEdit -func _on_Rut_text_changed() -> void: - _on_Rut_focus_exited() +var previous_text: String = "" -func _on_Rut_focus_exited() -> void: - var monitor = get_tree().get_current_scene().get_node("Monitor") +func _on_Rut_text_changed(_new_text: String) -> void: + var old_pos = caret_position if text.length() > 12: - text = text.substr(0, 12) - text = monitor.pretty_rut(text) - cursor_set_column(text.length()) + text = previous_text + old_pos = old_pos - 2 + text = pretty_rut(text) + caret_position = old_pos + 1 + previous_text = text + + +func clean_rut(rut: String) -> String: + rut = rut.strip_escapes() + rut = rut.strip_edges(true, true) + rut = rut.to_lower() + rut = rut.replace(".", "") + rut = rut.replace("-", "") + + var rut_temp: String = rut.substr(0, rut.length() - 1) + var verifier: String = rut.substr(rut.length() - 1, 1) + + var regex = RegEx.new() + regex.compile("\\D") + rut_temp = regex.sub(rut_temp, "", true) + + regex.compile("[^kK\\d]") + verifier = regex.sub(verifier, "", true) + + rut = rut_temp + verifier + + return rut + + +func pretty_rut(rut: String) -> String: + rut = clean_rut(rut) + + var rut_temp: String = rut.substr(0, rut.length() - 1) + var verifier: String = rut.substr(rut.length() - 1, 1) + + var regex = RegEx.new() + regex.compile("[^kK\\d]") + verifier = regex.sub(verifier, "", true) + + var byte_array = rut_temp.to_utf8() + byte_array.invert() + + var new_byte_array: PoolByteArray = PoolByteArray() + var i = 1 + for symbol in byte_array: + new_byte_array.append(symbol) + if i == 3: + new_byte_array.append(".".to_utf8()[0]) + i = 0 + i = i + 1 + if new_byte_array.size() > 0 and new_byte_array[new_byte_array.size() - 1] == ".".to_utf8()[0]: + new_byte_array.resize(new_byte_array.size() - 1) + + new_byte_array.invert() + rut_temp = new_byte_array.get_string_from_utf8() + + if rut_temp.length() == 0 and verifier.length() > 0: + rut_temp = verifier + elif rut_temp.length() > 0 and verifier.length() > 0: + rut_temp = rut_temp + "-" + verifier + + return rut_temp diff --git a/godot/monitor/Text.gd b/godot/monitor/Text.gd deleted file mode 100644 index 7109c78..0000000 --- a/godot/monitor/Text.gd +++ /dev/null @@ -1,14 +0,0 @@ -extends TextEdit - - -onready var previousText: String = text - - -func _on_text_changed() -> void: - var col = cursor_get_column() - # if a scroll bar appears, reset to the previous good string - if text.length() > 50: - text = previousText - col = col - 1 - previousText = text - cursor_set_column(col)