Hook score submission into new server and move into the results screen.
This commit is contained in:
parent
29d84b0c80
commit
155c86c013
9 changed files with 853 additions and 9 deletions
|
@ -37,6 +37,7 @@ var time_bonus: int = 0
|
|||
var life_bonus: int = 0
|
||||
var perfect_bonus: int = 0
|
||||
var final_score: int = 0
|
||||
var old_high_score: int = 0
|
||||
#== state ==#
|
||||
var current_sector := Vector2.ZERO
|
||||
var respawn_point := Vector2(32,166)
|
||||
|
@ -60,9 +61,6 @@ var marathon_shards: int = 0
|
|||
var marathon_deaths: int = 0
|
||||
|
||||
|
||||
func _ready():
|
||||
pause_mode = Node.PAUSE_MODE_PROCESS
|
||||
|
||||
func _get_stars() -> int:
|
||||
return stars_collected.count(true)
|
||||
|
||||
|
@ -197,6 +195,8 @@ func save():
|
|||
var save_id = LevelData.levels[current_level].save_id
|
||||
var save_data: Save.LevelSaveData = Save.current_file.levels[save_id]
|
||||
|
||||
old_high_score = max(save_data.score_100, save_data.score_any)
|
||||
|
||||
# save score and time depending on completion
|
||||
if _get_shards() >= 5 && keys >= 50:
|
||||
save_data.score_100 = max(save_data.score_100, final_score) as int
|
||||
|
|
|
@ -18,6 +18,8 @@ const TRANS_SPEEDS := [0.8, 0.4, 0.2, 0.0000001]
|
|||
#Game
|
||||
var rumble: int = RumbleMode.FULL
|
||||
var gore: int = Gore.FULL
|
||||
var scoreboard_name: String = ""
|
||||
var scoreboard_id: int = -1
|
||||
|
||||
#Video
|
||||
var fullscreen: bool = false setget _set_fullscreen
|
||||
|
@ -42,6 +44,7 @@ var defaults = null
|
|||
|
||||
func _ready() -> void:
|
||||
pause_mode = PAUSE_MODE_PROCESS
|
||||
|
||||
# clone self into defaults before loading stored values
|
||||
if defaults == null:
|
||||
defaults = duplicate()
|
||||
|
@ -58,6 +61,9 @@ func load_options() -> void:
|
|||
# game
|
||||
rumble = file.get_value("game", "rumble", defaults.rumble)
|
||||
gore = file.get_value("game", "gore", defaults.gore)
|
||||
scoreboard_name = file.get_value("game", "scoreboard_name", "")
|
||||
randomize()
|
||||
scoreboard_id = file.get_value("game", "scoreboard_id", randi())
|
||||
# video
|
||||
_set_fullscreen(file.get_value("video", "fullscreen", defaults.fullscreen))
|
||||
_set_window_size(file.get_value("video", "window_size", defaults.window_size))
|
||||
|
@ -78,6 +84,8 @@ func load_defaults(section: int = Section.ALL) -> void:
|
|||
Section.GAME, Section.ALL:
|
||||
rumble = defaults.rumble
|
||||
gore = defaults.gore
|
||||
scoreboard_name = defaults.scoreboard_name
|
||||
scoreboard_id = randi()
|
||||
Section.VIDEO, Section.ALL:
|
||||
fullscreen = defaults.fullscreen
|
||||
window_size = defaults.window_size
|
||||
|
@ -96,6 +104,8 @@ func save_options() -> void:
|
|||
#Game
|
||||
file.set_value("game", "rumble", rumble)
|
||||
file.set_value("game", "gore", gore)
|
||||
file.set_value("game", "scoreboard_name", scoreboard_name)
|
||||
file.set_value("game", "scoreboard_id", scoreboard_id)
|
||||
#Video
|
||||
file.set_value("video", "fullscreen", fullscreen)
|
||||
file.set_value("video", "window_size", window_size)
|
||||
|
|
123
autoloads/scoreboard.gd
Normal file
123
autoloads/scoreboard.gd
Normal file
|
@ -0,0 +1,123 @@
|
|||
extends Node
|
||||
|
||||
|
||||
signal _response_received
|
||||
|
||||
|
||||
const CHECK_INTERVAL: float = 60.0
|
||||
const POLL_INTERVAL: float = 0.5
|
||||
|
||||
|
||||
var server_host: String = ""
|
||||
var http: HTTPRequest = null
|
||||
## True if there is currently a request processing.
|
||||
var waiting_for_response: bool = false
|
||||
## True if the last request failed and the result is not valid.
|
||||
var errored: bool = false
|
||||
|
||||
var _response_code: int = -1
|
||||
var _response_body: String = ""
|
||||
|
||||
|
||||
class ScoreEntry:
|
||||
var score: int
|
||||
var time: int
|
||||
var difficulty: int
|
||||
|
||||
func _init(score: int, time: int, difficulty: int) -> void:
|
||||
self.score = score
|
||||
self.time = time
|
||||
self.difficulty = difficulty
|
||||
|
||||
static func from_dict(dict: Dictionary) -> ScoreEntry:
|
||||
return ScoreEntry.new(dict.score, dict.time, dict.difficulty)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var file = File.new()
|
||||
file.open("res://scoreboard_host.txt", File.READ)
|
||||
server_host = file.get_line()
|
||||
file.close()
|
||||
|
||||
http = HTTPRequest.new()
|
||||
http.download_chunk_size = 4096
|
||||
http.timeout = 10.0
|
||||
http.use_threads = true
|
||||
http.connect("request_completed", self, "_on_request_completed")
|
||||
add_child(http)
|
||||
|
||||
func _on_request_completed(result: int, response_code: int, headers: PoolStringArray, body: PoolByteArray) -> void:
|
||||
if result != OK:
|
||||
errored = true
|
||||
else:
|
||||
errored = false
|
||||
_response_code = response_code
|
||||
_response_body = body.get_string_from_utf8()
|
||||
waiting_for_response = false
|
||||
emit_signal("_response_received")
|
||||
|
||||
|
||||
func is_name_free(player_name: String, id: int) -> bool:
|
||||
if waiting_for_response:
|
||||
yield(self, "_response_received")
|
||||
var err = http.request(server_host + "/api/players/" + player_name)
|
||||
if err != OK:
|
||||
errored = true
|
||||
return false
|
||||
waiting_for_response = true
|
||||
|
||||
yield(self, "_response_received")
|
||||
if _response_code == HTTPClient.RESPONSE_NOT_FOUND or _response_body.to_int() == id:
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func touch_name(player_name: String, id: int) -> void:
|
||||
if waiting_for_response:
|
||||
yield(self, "_response_received")
|
||||
var err = http.request(
|
||||
server_host + "/api/players/%s/%d" % [player_name, id],
|
||||
[], true, HTTPClient.METHOD_POST
|
||||
)
|
||||
if err != OK:
|
||||
errored = true
|
||||
return
|
||||
waiting_for_response = true
|
||||
|
||||
|
||||
func submit_score(level: String, entry: ScoreEntry) -> bool:
|
||||
if waiting_for_response:
|
||||
yield(self, "_response_received")
|
||||
var err = http.request(
|
||||
server_host + "/api/scores/%s/%s/%d/%d/%d" % [
|
||||
level, Options.scoreboard_name,
|
||||
entry.score, entry.time, entry.difficulty
|
||||
],
|
||||
[], true, HTTPClient.METHOD_POST
|
||||
)
|
||||
if err != OK:
|
||||
errored = true
|
||||
return false
|
||||
waiting_for_response = true
|
||||
yield(self, "_response_received")
|
||||
if errored or _response_code != HTTPClient.RESPONSE_OK:
|
||||
return false
|
||||
return true
|
||||
|
||||
|
||||
func get_scores(level: String) -> Dictionary:
|
||||
if waiting_for_response:
|
||||
yield(self, "_response_received")
|
||||
var err = http.request(server_host + "/api/scores/%s" % level)
|
||||
if err != OK:
|
||||
errored = true
|
||||
return {}
|
||||
waiting_for_response = true
|
||||
yield(self, "_response_received")
|
||||
if not errored and _response_code == HTTPClient.RESPONSE_OK:
|
||||
var dict = parse_json(_response_body)
|
||||
var scores = {}
|
||||
for player in dict.keys():
|
||||
scores[player] = ScoreEntry.from_dict(dict[player])
|
||||
return scores
|
||||
return {}
|
Loading…
Add table
Add a link
Reference in a new issue