162 lines
3.8 KiB
GDScript
162 lines
3.8 KiB
GDScript
extends Control
|
|
|
|
@export var popup: PanelContainer
|
|
@export var search_button: Button
|
|
@export var abort_button: Button
|
|
@export var accept_button: Button
|
|
@export var decline_button: Button
|
|
@export var time_bar: ProgressBar
|
|
@export var login: Login
|
|
|
|
var searching: bool = false
|
|
var sent_accept_message: bool = false
|
|
var found_match: MatchFoundMessage
|
|
|
|
|
|
func _ready() -> void:
|
|
if !export_valid():
|
|
return
|
|
login.connect("login_successful", on_login)
|
|
|
|
# Websockets
|
|
(
|
|
ConnectionChannel
|
|
. connect(
|
|
"on_channel_token_received",
|
|
on_channel_token_received,
|
|
)
|
|
)
|
|
(
|
|
MatchmakingChannel
|
|
. connect(
|
|
"on_match_set_search_state",
|
|
on_match_set_search_state,
|
|
)
|
|
)
|
|
MatchmakingChannel.connect("on_match_found", on_match_found)
|
|
MatchmakingChannel.connect("on_match_aborted", on_match_aborted)
|
|
MatchmakingChannel.connect("on_match_established", on_match_established)
|
|
|
|
# UI
|
|
search_button.connect("pressed", on_search_pressed)
|
|
abort_button.connect("pressed", on_abort_pressed)
|
|
accept_button.connect("pressed", on_accept_pressed)
|
|
decline_button.connect("pressed", on_decline_pressed)
|
|
popup.visible = false
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if found_match:
|
|
var now := int(Time.get_unix_time_from_system() * 1000)
|
|
time_bar.max_value = found_match.ttl
|
|
time_bar.value = found_match.ttl - (now - found_match.created)
|
|
|
|
|
|
# gdlint:ignore = max-public-methods
|
|
func export_valid() -> bool:
|
|
if not popup:
|
|
push_error("Popup not Set")
|
|
return false
|
|
if not search_button:
|
|
push_error("Search Button not Set")
|
|
return false
|
|
if not abort_button:
|
|
push_error("Abort Button not Set")
|
|
return false
|
|
if not accept_button:
|
|
push_error("Accept Button not Set")
|
|
return false
|
|
if not decline_button:
|
|
push_error("Decline Button not Set")
|
|
return false
|
|
if not time_bar:
|
|
push_error("Time Bar not Set")
|
|
return false
|
|
if not login:
|
|
push_error("No login configured")
|
|
return false
|
|
return true
|
|
|
|
|
|
func on_login(_session: PlayerLoginSession) -> void:
|
|
ConnectionChannel.request_channel_token(Message.Channels.MATCHMAKING)
|
|
|
|
|
|
##############################
|
|
# Websocket Interactions #
|
|
##############################
|
|
|
|
|
|
func on_channel_token_received(msg: ProvidedConnectionTokenMessage) -> void:
|
|
if msg.channel != Message.Channels.MATCHMAKING:
|
|
return
|
|
MatchmakingChannel.connect_socket(msg.token)
|
|
|
|
|
|
func on_match_set_search_state(msg: MatchSetSearchStateMessage) -> void:
|
|
set_searching_ui(msg.searching)
|
|
|
|
|
|
func on_match_found(msg: MatchFoundMessage) -> void:
|
|
set_match_accptance_ui(true)
|
|
set_accaptence_buttons_enabled(true)
|
|
found_match = msg
|
|
|
|
|
|
func on_match_aborted() -> void:
|
|
set_match_accptance_ui(false)
|
|
set_searching_ui(false)
|
|
found_match = null
|
|
|
|
|
|
func on_match_established(msg: MatchEstablishedMessage) -> void:
|
|
CurrentMatch.start_game(msg)
|
|
|
|
|
|
#############################
|
|
# UI Interactions #
|
|
#############################
|
|
|
|
|
|
func on_search_pressed() -> void:
|
|
if searching:
|
|
return
|
|
set_searching_ui(true)
|
|
MatchmakingChannel.send_search_state(true)
|
|
|
|
|
|
func on_abort_pressed() -> void:
|
|
if not searching:
|
|
return
|
|
set_searching_ui(false)
|
|
MatchmakingChannel.send_search_state(false)
|
|
|
|
|
|
func on_accept_pressed() -> void:
|
|
set_accaptence_buttons_enabled(false)
|
|
MatchmakingChannel.send_match_accepted(true, found_match.matchId)
|
|
|
|
|
|
func on_decline_pressed() -> void:
|
|
set_accaptence_buttons_enabled(false)
|
|
MatchmakingChannel.send_match_accepted(false, found_match.matchId)
|
|
|
|
|
|
#############################
|
|
# UI Management #
|
|
#############################
|
|
|
|
|
|
func set_searching_ui(new_searching: bool) -> void:
|
|
searching = new_searching
|
|
search_button.visible = !new_searching
|
|
abort_button.visible = new_searching
|
|
|
|
|
|
func set_accaptence_buttons_enabled(enabled: bool) -> void:
|
|
accept_button.disabled = !enabled
|
|
decline_button.disabled = !enabled
|
|
|
|
|
|
func set_match_accptance_ui(vissible: bool) -> void:
|
|
popup.visible = vissible
|