mirror of
https://github.com/Steffo99/swear-jar.git
synced 2024-11-21 15:24:18 +00:00
Add high score submission
This commit is contained in:
parent
90328652ec
commit
e39cc481e2
8 changed files with 222 additions and 3 deletions
|
@ -119,7 +119,7 @@ permissions/install_location_provider=false
|
|||
permissions/install_packages=false
|
||||
permissions/install_shortcut=false
|
||||
permissions/internal_system_window=false
|
||||
permissions/internet=false
|
||||
permissions/internet=true
|
||||
permissions/kill_background_processes=false
|
||||
permissions/location_hardware=false
|
||||
permissions/manage_accounts=false
|
||||
|
|
|
@ -7,6 +7,7 @@ class_name CustomUI
|
|||
|
||||
@onready var game_safe_ui: MarginContainer = $GameSafeUI
|
||||
@onready var shop_safe_ui: MarginContainer = $ShopSafeUI
|
||||
@onready var score_safe_ui: MarginContainer = $ScoreSafeUI
|
||||
@onready var game_camera: GameCamera = $GameViewport/Viewport/GameCamera
|
||||
|
||||
|
||||
|
@ -27,4 +28,5 @@ func _on_viewport_size_changed():
|
|||
get_window().set_content_scale_factor(scaling_factor)
|
||||
game_safe_ui.set_safe_margins(scaling_factor)
|
||||
shop_safe_ui.set_safe_margins(scaling_factor)
|
||||
score_safe_ui.set_safe_margins(scaling_factor)
|
||||
game_camera.set_camera_position(scaling_factor)
|
||||
|
|
|
@ -43,5 +43,6 @@ size_flags_horizontal = 8
|
|||
text = "Shop"
|
||||
alignment = 2
|
||||
|
||||
[connection signal="pressed" from="ScoreButton" to="." method="_on_score_button_pressed"]
|
||||
[connection signal="pressed" from="SpawnButton" to="." method="_on_spawn_button_pressed"]
|
||||
[connection signal="pressed" from="ShopButton" to="." method="_on_shop_button_pressed"]
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
extends Button
|
||||
class_name ScoreButton
|
||||
|
||||
var score: int = 0
|
||||
|
||||
|
||||
func set_score(total: int):
|
||||
score = total
|
||||
if total >= 1000:
|
||||
add_theme_font_size_override("font_size", 8)
|
||||
text = "$%0.2f" % (float(total) / 100)
|
||||
|
|
67
interface/score_ui.gd
Normal file
67
interface/score_ui.gd
Normal file
|
@ -0,0 +1,67 @@
|
|||
extends Panel
|
||||
class_name ScoreUI
|
||||
|
||||
@export var board: String
|
||||
@export var secret: String
|
||||
|
||||
@onready var input_name: LineEdit = $Rows/HBoxContainer/Container/NameTextEdit
|
||||
@onready var score_button: ScoreButton = $Rows/UpperButtons/ScoreButton
|
||||
@onready var submit_button: Button = $Rows/HBoxContainer/Container/SubmitButton
|
||||
@onready var request: HTTPRequest = $SubmissionRequest
|
||||
|
||||
|
||||
func _on_game_score_changed(total: int):
|
||||
score_button.set_score(total)
|
||||
|
||||
|
||||
func lock():
|
||||
score_button.disabled = true
|
||||
submit_button.disabled = true
|
||||
submit_button.text = "Submitting..."
|
||||
|
||||
|
||||
func unlock():
|
||||
score_button.disabled = false
|
||||
submit_button.disabled = false
|
||||
submit_button.text = "Submit"
|
||||
|
||||
|
||||
func _on_submit_button_pressed():
|
||||
var player = input_name.text
|
||||
lock()
|
||||
request.request(
|
||||
"https://arcade.steffo.eu/score/?board=%s&player=%s" % [board, player],
|
||||
[
|
||||
"Authorization: Bearer %s" % secret,
|
||||
"Content-Type: application/json",
|
||||
"User-Agent: eu.steffo.ld54"
|
||||
],
|
||||
HTTPClient.METHOD_PUT,
|
||||
JSON.stringify(float(score_button.score) / 100.0)
|
||||
)
|
||||
|
||||
|
||||
signal score_submission_success
|
||||
signal score_submission_error
|
||||
|
||||
|
||||
func _on_submission_request_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray):
|
||||
if response_code in [200, 201]:
|
||||
score_submission_success.emit()
|
||||
print("[ScoreUI] Score submission successful!")
|
||||
else:
|
||||
push_error("Score submission failed. Printing details to output.")
|
||||
score_submission_error.emit()
|
||||
print("[ScoreUI] Score submission failed.")
|
||||
print("[ScoreUI] Result: ", result)
|
||||
print("[ScoreUI] Code: ", response_code)
|
||||
print("[ScoreUI] Headers: ", headers)
|
||||
print("[ScoreUI] Body: ", body)
|
||||
unlock()
|
||||
|
||||
|
||||
signal score_button_pressed
|
||||
|
||||
func _on_score_button_pressed():
|
||||
score_button_pressed.emit()
|
||||
|
111
interface/score_ui.tscn
Normal file
111
interface/score_ui.tscn
Normal file
|
@ -0,0 +1,111 @@
|
|||
[gd_scene load_steps=5 format=3 uid="uid://b1gum13jilhgy"]
|
||||
|
||||
[ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="1_lcp0l"]
|
||||
[ext_resource type="PackedScene" uid="uid://bey5lilvteg32" path="res://interface/score_button.tscn" id="2_cggjg"]
|
||||
[ext_resource type="Script" path="res://interface/score_ui.gd" id="2_yg102"]
|
||||
[ext_resource type="PackedScene" uid="uid://wodgj6rp2ewm" path="res://interface/purchasable_item_padding.tscn" id="3_y87mu"]
|
||||
|
||||
[node name="ScoreUI" type="Panel"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme = ExtResource("1_lcp0l")
|
||||
script = ExtResource("2_yg102")
|
||||
|
||||
[node name="Rows" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="UpperButtons" type="Control" parent="Rows"]
|
||||
custom_minimum_size = Vector2(0, 54)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="ScoreButton" parent="Rows/UpperButtons" instance=ExtResource("2_cggjg")]
|
||||
custom_minimum_size = Vector2(88, 0)
|
||||
layout_mode = 1
|
||||
offset_right = 88.0
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="Rows"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="PaddingLeft" type="Control" parent="Rows/HBoxContainer"]
|
||||
custom_minimum_size = Vector2(2, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 0
|
||||
|
||||
[node name="Container" type="VBoxContainer" parent="Rows/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
alignment = 1
|
||||
|
||||
[node name="PurchasableItemPadding5" parent="Rows/HBoxContainer/Container" instance=ExtResource("3_y87mu")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SubmitScoreLabel" type="Label" parent="Rows/HBoxContainer/Container"]
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 24
|
||||
text = "Submit your current score!"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
autowrap_mode = 2
|
||||
uppercase = true
|
||||
|
||||
[node name="PurchasableItemPadding6" parent="Rows/HBoxContainer/Container" instance=ExtResource("3_y87mu")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="DescriptionLabel" type="Label" parent="Rows/HBoxContainer/Container"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
mouse_filter = 1
|
||||
theme_override_font_sizes/font_size = 8
|
||||
text = "Add your score to the leaderboards to let both us and other people know what you've managed to achieve in the game!
|
||||
|
||||
Find a link to the leaderboards in the game page."
|
||||
autowrap_mode = 2
|
||||
|
||||
[node name="PurchasableItemPadding7" parent="Rows/HBoxContainer/Container" instance=ExtResource("3_y87mu")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="NameTextEdit" type="LineEdit" parent="Rows/HBoxContainer/Container"]
|
||||
custom_minimum_size = Vector2(0, 24)
|
||||
layout_mode = 2
|
||||
placeholder_text = "Your name"
|
||||
alignment = 1
|
||||
|
||||
[node name="PurchasableItemPadding8" parent="Rows/HBoxContainer/Container" instance=ExtResource("3_y87mu")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SubmitButton" type="Button" parent="Rows/HBoxContainer/Container"]
|
||||
texture_filter = 1
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 16
|
||||
text = "Submit"
|
||||
|
||||
[node name="PurchasableItemPadding9" parent="Rows/HBoxContainer/Container" instance=ExtResource("3_y87mu")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="DescriptionLabel2" type="Label" parent="Rows/HBoxContainer/Container"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
mouse_filter = 1
|
||||
theme_override_font_sizes/font_size = 8
|
||||
text = "Got a better score? Submit it again with the same name!"
|
||||
autowrap_mode = 2
|
||||
|
||||
[node name="PaddingRight" type="Control" parent="Rows/HBoxContainer"]
|
||||
custom_minimum_size = Vector2(2, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 8
|
||||
|
||||
[node name="SubmissionRequest" type="HTTPRequest" parent="."]
|
||||
timeout = 8.0
|
||||
|
||||
[connection signal="pressed" from="Rows/UpperButtons/ScoreButton" to="." method="_on_score_button_pressed"]
|
||||
[connection signal="pressed" from="Rows/HBoxContainer/Container/SubmitButton" to="." method="_on_submit_button_pressed"]
|
||||
[connection signal="request_completed" from="SubmissionRequest" to="." method="_on_submission_request_request_completed"]
|
19
main.gd
19
main.gd
|
@ -7,6 +7,8 @@ class_name Main
|
|||
@onready var game_ui: GameUI = $CustomUI/GameSafeUI/GameUI
|
||||
@onready var shop_safe_ui: MarginContainer = $CustomUI/ShopSafeUI
|
||||
@onready var shop_ui: ShopUI = $CustomUI/ShopSafeUI/ShopUI
|
||||
@onready var score_safe_ui: MarginContainer = $CustomUI/ScoreSafeUI
|
||||
@onready var score_ui: ScoreUI = $CustomUI/ScoreSafeUI/ScoreUI
|
||||
|
||||
|
||||
enum UIState {
|
||||
|
@ -24,14 +26,23 @@ enum UIState {
|
|||
tree.paused = false
|
||||
game_safe_ui.show()
|
||||
shop_safe_ui.hide()
|
||||
score_safe_ui.hide()
|
||||
shop_safe_ui.process_mode = Node.PROCESS_MODE_DISABLED
|
||||
score_safe_ui.process_mode = Node.PROCESS_MODE_DISABLED
|
||||
UIState.SHOP:
|
||||
tree.paused = true
|
||||
game_safe_ui.hide()
|
||||
shop_safe_ui.show()
|
||||
score_safe_ui.hide()
|
||||
shop_safe_ui.process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
score_safe_ui.process_mode = Node.PROCESS_MODE_DISABLED
|
||||
UIState.SCORE:
|
||||
pass
|
||||
tree.paused = true
|
||||
game_safe_ui.hide()
|
||||
shop_safe_ui.hide()
|
||||
score_safe_ui.show()
|
||||
shop_safe_ui.process_mode = Node.PROCESS_MODE_DISABLED
|
||||
score_safe_ui.process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
|
||||
|
||||
func _on_game_ui_score_button_pressed():
|
||||
|
@ -57,3 +68,9 @@ func _on_shop_ui_delete_begin():
|
|||
|
||||
func _on_shop_ui_delete_cancel():
|
||||
ui_state = UIState.GAME
|
||||
|
||||
func _on_score_ui_score_button_pressed():
|
||||
ui_state = UIState.GAME
|
||||
|
||||
func _on_score_ui_score_submission_success():
|
||||
ui_state = UIState.GAME
|
||||
|
|
20
main.tscn
20
main.tscn
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=10 format=3 uid="uid://dqm0h5h0baqcg"]
|
||||
[gd_scene load_steps=11 format=3 uid="uid://dqm0h5h0baqcg"]
|
||||
|
||||
[ext_resource type="Script" path="res://main.gd" id="1_fqt34"]
|
||||
[ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="1_je7w1"]
|
||||
|
@ -8,6 +8,7 @@
|
|||
[ext_resource type="PackedScene" uid="uid://bo5unrhqpoyim" path="res://interface/game_ui.tscn" id="4_siim3"]
|
||||
[ext_resource type="PackedScene" uid="uid://bpvx3nkkbluip" path="res://safe_ui.tscn" id="5_2uxda"]
|
||||
[ext_resource type="Script" path="res://game_camera.gd" id="5_xxjtg"]
|
||||
[ext_resource type="PackedScene" uid="uid://b1gum13jilhgy" path="res://interface/score_ui.tscn" id="9_ghurm"]
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_kyj65"]
|
||||
script/source = "extends SubViewport
|
||||
|
@ -76,8 +77,23 @@ apply_margin_bottom = true
|
|||
[node name="ShopUI" parent="CustomUI/ShopSafeUI" instance=ExtResource("2_qj86l")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="ScoreSafeUI" parent="CustomUI" instance=ExtResource("5_2uxda")]
|
||||
process_mode = 4
|
||||
visible = false
|
||||
layout_mode = 1
|
||||
apply_margin_left = true
|
||||
apply_margin_right = true
|
||||
apply_margin_top = true
|
||||
apply_margin_bottom = true
|
||||
|
||||
[node name="ScoreUI" parent="CustomUI/ScoreSafeUI" instance=ExtResource("9_ghurm")]
|
||||
layout_mode = 2
|
||||
board = "ld54"
|
||||
secret = "TXES816qEXynzWi5"
|
||||
|
||||
[connection signal="score_changed" from="CustomUI/GameViewport/Viewport/Game" to="CustomUI/GameSafeUI/GameUI" method="_on_game_score_changed"]
|
||||
[connection signal="score_changed" from="CustomUI/GameViewport/Viewport/Game" to="CustomUI/ShopSafeUI/ShopUI" method="_on_game_score_changed"]
|
||||
[connection signal="score_changed" from="CustomUI/GameViewport/Viewport/Game" to="CustomUI/ScoreSafeUI/ScoreUI" method="_on_game_score_changed"]
|
||||
[connection signal="score_button_pressed" from="CustomUI/GameSafeUI/GameUI" to="." method="_on_game_ui_score_button_pressed"]
|
||||
[connection signal="shop_button_pressed" from="CustomUI/GameSafeUI/GameUI" to="." method="_on_game_ui_shop_button_pressed"]
|
||||
[connection signal="spawn_button_pressed" from="CustomUI/GameSafeUI/GameUI" to="CustomUI/GameViewport/Viewport/Game" method="trigger_spawn"]
|
||||
|
@ -101,3 +117,5 @@ layout_mode = 2
|
|||
[connection signal="score_button_pressed" from="CustomUI/ShopSafeUI/ShopUI" to="." method="_on_shop_ui_score_button_pressed"]
|
||||
[connection signal="upgraded_auto_spawn" from="CustomUI/ShopSafeUI/ShopUI" to="CustomUI/GameViewport/Viewport/Game" method="_on_upgraded_auto_spawn"]
|
||||
[connection signal="upgraded_manual_spawn" from="CustomUI/ShopSafeUI/ShopUI" to="CustomUI/GameViewport/Viewport/Game" method="_on_upgraded_manual_spawn"]
|
||||
[connection signal="score_button_pressed" from="CustomUI/ScoreSafeUI/ScoreUI" to="." method="_on_score_ui_score_button_pressed"]
|
||||
[connection signal="score_submission_success" from="CustomUI/ScoreSafeUI/ScoreUI" to="." method="_on_score_ui_score_submission_success"]
|
||||
|
|
Loading…
Reference in a new issue