|
| 1 | +extends Node |
| 2 | + |
| 3 | +const API_KEY := "AIzaSyDJV6lBsiOon84U5T49jboFSBxrPOdhT2k" |
| 4 | +const PROJECT_ID := "samurai-game-8a82f" |
| 5 | + |
| 6 | +const REGISTER_URL := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=%s" % API_KEY |
| 7 | +const LOGIN_URL := "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=%s" % API_KEY |
| 8 | +const FIRESTORE_URL := "https://firestore.googleapis.com/v1/projects/%s/databases/(default)/documents/" % PROJECT_ID |
| 9 | + |
| 10 | +var leaderboard := [] |
| 11 | + |
| 12 | +var current_record_score := 0 |
| 13 | + |
| 14 | +var user_info := {} |
| 15 | +var username := "" |
| 16 | + |
| 17 | +var is_logged = false |
| 18 | + |
| 19 | + |
| 20 | +func _get_user_info(result: Array) -> Dictionary: |
| 21 | + var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary |
| 22 | + return { |
| 23 | + "token": result_body.idToken, |
| 24 | + "id": result_body.localId |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +func _get_request_headers() -> PoolStringArray: |
| 29 | + return PoolStringArray([ |
| 30 | + "Content-Type: application/json", |
| 31 | + "Authorization: Bearer %s" % user_info.token |
| 32 | + ]) |
| 33 | + |
| 34 | + |
| 35 | +func register(email: String, password: String, http: HTTPRequest) -> void: |
| 36 | + var body := { |
| 37 | + "email": email, |
| 38 | + "password": password, |
| 39 | + } |
| 40 | + http.request(REGISTER_URL, [], false, HTTPClient.METHOD_POST, to_json(body)) |
| 41 | + var result := yield(http, "request_completed") as Array |
| 42 | + if result[1] == 200: |
| 43 | + user_info = _get_user_info(result) |
| 44 | + |
| 45 | +func logout(): |
| 46 | + user_info = {} |
| 47 | + is_logged=false |
| 48 | + |
| 49 | +func login(email: String, password: String, http: HTTPRequest) -> void: |
| 50 | + var body := { |
| 51 | + "email": email, |
| 52 | + "password": password, |
| 53 | + "returnSecureToken": true |
| 54 | + } |
| 55 | + http.request(LOGIN_URL, [], false, HTTPClient.METHOD_POST, to_json(body)) |
| 56 | + var result := yield(http, "request_completed") as Array |
| 57 | + if result[1] == 200: |
| 58 | + username = email.split("@")[0] |
| 59 | + user_info = _get_user_info(result) |
| 60 | + is_logged = true |
| 61 | + |
| 62 | + |
| 63 | +func save_document(path: String, fields: Dictionary, http: HTTPRequest) -> void: |
| 64 | + var document := { "fields": fields } |
| 65 | + var body := to_json(document) |
| 66 | + var url := FIRESTORE_URL + path |
| 67 | + http.request(url, _get_request_headers(), false, HTTPClient.METHOD_POST, body) |
| 68 | + var result := yield(http, "request_completed") as Array |
| 69 | + var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +func get_leaderboard(path: String, http: HTTPRequest) -> void: |
| 74 | + var url := FIRESTORE_URL + path |
| 75 | + http.request(url, _get_request_headers(), false, HTTPClient.METHOD_GET) |
| 76 | + var result := yield(http, "request_completed") as Array |
| 77 | + if result[1] == 200: |
| 78 | + _get_leaderboard(result) |
| 79 | + |
| 80 | + |
| 81 | +func get_current_record_score(path: String, http: HTTPRequest) -> void: |
| 82 | + var url := FIRESTORE_URL + path |
| 83 | + http.request(url, _get_request_headers(), false, HTTPClient.METHOD_GET) |
| 84 | + var result := yield(http, "request_completed") as Array |
| 85 | + if result[1] == 200: |
| 86 | + var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary |
| 87 | + current_record_score = int(result_body.fields.Score.stringValue) |
| 88 | + |
| 89 | + |
| 90 | +func customComparison(a, b): |
| 91 | + return int(a['score']) > int(b['score']) |
| 92 | + |
| 93 | +func _get_leaderboard(result: Array): |
| 94 | + |
| 95 | + leaderboard = [] |
| 96 | + |
| 97 | + var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary |
| 98 | + |
| 99 | + for element in result_body.documents: |
| 100 | + var user = element.fields.User.stringValue |
| 101 | + var score = element.fields.Score.stringValue |
| 102 | + leaderboard.append({'user': user, 'score': score}) |
| 103 | + |
| 104 | + leaderboard.sort_custom(self, 'customComparison') |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +func update_document(path: String, fields: Dictionary, http: HTTPRequest) -> void: |
| 109 | + var document := { "fields": fields } |
| 110 | + var body := to_json(document) |
| 111 | + var url := FIRESTORE_URL + path |
| 112 | + http.request(url, _get_request_headers(), false, HTTPClient.METHOD_PATCH, body) |
| 113 | + var result := yield(http, "request_completed") as Array |
| 114 | + var result_body := JSON.parse(result[3].get_string_from_ascii()).result as Dictionary |
| 115 | + |
| 116 | + |
| 117 | +func delete_document(path: String, http: HTTPRequest) -> void: |
| 118 | + var url := FIRESTORE_URL + path |
| 119 | + http.request(url, _get_request_headers(), false, HTTPClient.METHOD_DELETE) |
0 commit comments