-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,060 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
extends Node | ||
|
||
|
||
onready var default_stats = { | ||
"level1":{ | ||
"archers_killed":0, | ||
"collected_coins":0, | ||
"completed":false, | ||
"knights_killed":0, | ||
"red_archers_killed":0, | ||
"shield_knights_killed":0, | ||
"skeletons_killed":0, | ||
"time":null, | ||
"yellow_archers_killed":0 | ||
}, | ||
"level2":{ | ||
"archers_killed":0, | ||
"collected_coins":0, | ||
"completed":false, | ||
"knights_killed":0, | ||
"red_archers_killed":0, | ||
"shield_knights_killed":0, | ||
"skeletons_killed":0, | ||
"time":null, | ||
"yellow_archers_killed":0 | ||
} | ||
} | ||
|
||
onready var record_stats = {} | ||
onready var current_stats = {} | ||
|
||
func _ready(): | ||
var dir = Directory.new() | ||
dir.make_dir("data/org.godot.samuraigame") | ||
if not check_files_path("user://stats.json"): | ||
_save_stats("user://stats.json", default_stats) | ||
if not check_files_path("user://current_stats.json"): | ||
_save_stats("user://current_stats.json", default_stats) | ||
|
||
load_record_stats("user://stats.json") | ||
reset_current_stats() | ||
reset_record_stats() | ||
|
||
print(current_stats) | ||
|
||
|
||
func check_files_path(path): | ||
var file2Check = File.new() | ||
return file2Check.file_exists(path) | ||
|
||
func save_record_stats(): | ||
_save_stats("user://stats.json", record_stats) | ||
|
||
func save_current_stats(): | ||
_save_stats("user://stats.json", current_stats) | ||
|
||
func _save_stats(path, stats_to_save): | ||
""" | ||
Salva le statistiche su disco. | ||
""" | ||
var file = File.new() | ||
file.open(path, File.WRITE) | ||
file.store_line(to_json(stats_to_save)) | ||
file.close() | ||
|
||
func load_record_stats(path): | ||
""" | ||
Carica le statistiche da disco. | ||
""" | ||
|
||
var file = File.new() | ||
file.open(path, File.READ) | ||
var text = file.get_as_text() | ||
record_stats = parse_json(text) | ||
file.close() | ||
|
||
func reset_current_stats(): | ||
current_stats = default_stats.duplicate(true) | ||
|
||
func reset_record_stats(): | ||
record_stats = default_stats.duplicate(true) | ||
|
||
func update_current_stats(level, entity): | ||
""" | ||
Aggiorna le statitistiche relative all'entita specificata | ||
""" | ||
if entity == "completed" or entity == "time": | ||
pass | ||
current_stats[level][entity] = int(current_stats[level][entity]) + 1 | ||
|
||
func complete_level(level, elapsed): | ||
|
||
current_stats[level]["completed"] = true | ||
current_stats[level]["time"] = elapsed | ||
|
||
|
||
func register_current_stats(level): | ||
|
||
var record_score = get_score(level, record_stats) | ||
var current_score = get_score(level, current_stats) | ||
|
||
if current_score > record_score: | ||
record_stats[level] = current_stats[level] | ||
|
||
|
||
|
||
|
||
func get_score(level, stats): | ||
|
||
if stats[level]["completed"] == false: | ||
return 0 | ||
|
||
var total = 300 - int(stats[level]["time"]) | ||
if total <= 0: | ||
total = 0 | ||
|
||
total = total + int(stats[level]["collected_coins"]) * 100 | ||
total = total + int(stats[level]["skeletons_killed"]) * 20 | ||
total = total + int(stats[level]["knights_killed"]) * 50 | ||
total = total + int(stats[level]["shield_knights_killed"]) * 100 | ||
total = total + int(stats[level]["archers_killed"]) * 50 | ||
total = total + int(stats[level]["yellow_archers_killed"]) * 100 | ||
total = total + int(stats[level]["red_archers_killed"]) * 150 | ||
|
||
return total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
extends Node | ||
|
||
const API_KEY := "AIzaSyDJV6lBsiOon84U5T49jboFSBxrPOdhT2k" | ||
const PROJECT_ID := "samurai-game-8a82f" | ||
|
||
const REGISTER_URL := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=%s" % API_KEY | ||
const LOGIN_URL := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=%s" % API_KEY | ||
const FIRESTORE_URL := "https://firestore.googleapis.com/v1/projects/%s/databases/(default)/documents/" % PROJECT_ID | ||
|
||
var leaderboard := [] | ||
|
||
var current_record_score := 0 | ||
|
||
var user_info := {} | ||
var username := "" | ||
|
||
var is_logged = false | ||
|
||
|
||
func _get_user_info(result: Array) -> Dictionary: | ||
var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary | ||
return { | ||
"token": result_body.idToken, | ||
"id": result_body.localId | ||
} | ||
|
||
|
||
func _get_request_headers() -> PoolStringArray: | ||
return PoolStringArray([ | ||
"Content-Type: application/json", | ||
"Authorization: Bearer %s" % user_info.token | ||
]) | ||
|
||
|
||
func register(email: String, password: String, http: HTTPRequest) -> void: | ||
var body := { | ||
"email": email, | ||
"password": password, | ||
} | ||
http.request(REGISTER_URL, [], false, HTTPClient.METHOD_POST, to_json(body)) | ||
var result := yield(http, "request_completed") as Array | ||
if result[1] == 200: | ||
user_info = _get_user_info(result) | ||
|
||
func logout(): | ||
user_info = {} | ||
is_logged=false | ||
|
||
func login(email: String, password: String, http: HTTPRequest) -> void: | ||
var body := { | ||
"email": email, | ||
"password": password, | ||
"returnSecureToken": true | ||
} | ||
http.request(LOGIN_URL, [], false, HTTPClient.METHOD_POST, to_json(body)) | ||
var result := yield(http, "request_completed") as Array | ||
if result[1] == 200: | ||
username = email.split("@")[0] | ||
user_info = _get_user_info(result) | ||
is_logged = true | ||
|
||
|
||
func save_document(path: String, fields: Dictionary, http: HTTPRequest) -> void: | ||
var document := { "fields": fields } | ||
var body := to_json(document) | ||
var url := FIRESTORE_URL + path | ||
http.request(url, _get_request_headers(), false, HTTPClient.METHOD_POST, body) | ||
var result := yield(http, "request_completed") as Array | ||
var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary | ||
|
||
|
||
|
||
func get_leaderboard(path: String, http: HTTPRequest) -> void: | ||
var url := FIRESTORE_URL + path | ||
http.request(url, _get_request_headers(), false, HTTPClient.METHOD_GET) | ||
var result := yield(http, "request_completed") as Array | ||
if result[1] == 200: | ||
_get_leaderboard(result) | ||
|
||
|
||
func get_current_record_score(path: String, http: HTTPRequest) -> void: | ||
var url := FIRESTORE_URL + path | ||
http.request(url, _get_request_headers(), false, HTTPClient.METHOD_GET) | ||
var result := yield(http, "request_completed") as Array | ||
if result[1] == 200: | ||
var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary | ||
current_record_score = int(result_body.fields.Score.stringValue) | ||
|
||
|
||
func customComparison(a, b): | ||
return int(a['score']) > int(b['score']) | ||
|
||
func _get_leaderboard(result: Array): | ||
|
||
leaderboard = [] | ||
|
||
var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary | ||
|
||
for element in result_body.documents: | ||
var user = element.fields.User.stringValue | ||
var score = element.fields.Score.stringValue | ||
leaderboard.append({'user': user, 'score': score}) | ||
|
||
leaderboard.sort_custom(self, 'customComparison') | ||
|
||
|
||
|
||
func update_document(path: String, fields: Dictionary, http: HTTPRequest) -> void: | ||
var document := { "fields": fields } | ||
var body := to_json(document) | ||
var url := FIRESTORE_URL + path | ||
http.request(url, _get_request_headers(), false, HTTPClient.METHOD_PATCH, body) | ||
var result := yield(http, "request_completed") as Array | ||
var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary | ||
|
||
|
||
func delete_document(path: String, http: HTTPRequest) -> void: | ||
var url := FIRESTORE_URL + path | ||
http.request(url, _get_request_headers(), false, HTTPClient.METHOD_DELETE) |
Oops, something went wrong.