Merge pull request 'feat: added user authorization on user registrated' (#7) from develop into production

Reviewed-on: #7
pull/10/head
Евгений Сугоняко 2024-05-12 14:35:08 +00:00
commit 355e454521
5 changed files with 44 additions and 16 deletions

View File

@ -1 +0,0 @@
{"email":"test_user@gmail.com","name":"test_user","password":"9a3d1f5ce0bbbdf38ca26740bdc5aa55","token":"cf49b818d38904019638d6b73be8ec40","userType":"User"}

View File

@ -20,8 +20,8 @@ func _init(_db_path = "res://data/DataBase"):
#--------------------------------------------------------------------------------------------------#
# Methods
# Get
func findOne(_model, _instance):
# Select
func findOne(_model: String, _instance: String):
if !MODEL.has(_model):
return null
if !MODEL[_model].instancies.has(_instance):
@ -34,8 +34,8 @@ func findOne(_model, _instance):
#--------------------------------------------------------------------------------------------------#
# Methods
# Set
func update(_model, _instance, _data):
# Update
func update(_model: String, _instance: String, _data: Dictionary):
if !MODEL.has(_model):
return null
if !MODEL[_model].instancies.has(_instance):
@ -44,3 +44,18 @@ func update(_model, _instance, _data):
file.store_string(JSON.stringify(_data))
file.close()
return true
#--------------------------------------------------------------------------------------------------#
# Methods
# Insert
func insert(_model: String, _instance: String, _data: Dictionary):
if !MODEL.has(_model):
return null
MODEL[_model].instancies[_instance] = db_path.path_join(_model.path_join(_instance + ".json"))
var file = FileAccess.open(MODEL[_model].instancies[_instance], FileAccess.WRITE)
if !file:
return null
file.store_string(JSON.stringify(_data))
file.close()
return true

View File

@ -126,14 +126,14 @@ func authorization(_login, _password):
var player : Node2D = _get_player(client_id)
var token : String = str(client_id).md5_text()
var isAuthorized : bool = false
var user = db.findOne("User", _login)
var user = db.findOne("User", _login.to_lower())
if user:
if _password == user.password:
token = (token + str(user.password)).md5_text()
user.token = token
player.token = token
player.playerName = user.name
if db.update("User", _login, user):
if db.update("User", _login.to_lower(), user):
isAuthorized = true
var rpc_response = player.rpc_id(client_id, 'authorizated', player.playerName, token, isAuthorized)
if rpc_response != Error.OK:
@ -146,24 +146,38 @@ func authorization(_login, _password):
@rpc("any_peer", "reliable", "call_local", 1)
func registration(_login, _password):
func registration(_login, _password, _name):
var db = ORM.new()
var client_id : int = multiplayer.get_remote_sender_id()
var user = db.findOne("User", _login)
var player : Node2D = _get_player(client_id)
var token : String = str(client_id).md5_text()
var user = db.findOne("User", _login.to_lower())
if user:
var reason = "User already exist"
var rpc_response = rpc_id(client_id, 'is_registrated', false, reason)
var rpc_response = rpc_id(client_id, 'is_registrated', false, reason, _name, token)
if rpc_response != Error.OK:
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
else:
FileManager.add_to_log("WARNING | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.WARNING, client_id, reason])
else:
# Add user to DB
var rpc_response = rpc_id(client_id, 'is_registrated', true, "")
if rpc_response != Error.OK:
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
var user_data = {
"login" = _login,
"name" = _name,
"password" = _password,
"token" = token,
"userType" = "User"
}
if db.insert("User", _login.to_lower(), user_data):
var rpc_response = rpc_id(client_id, 'is_registrated', true, "", _name, token)
player.token = token
player.playerName = _name
if rpc_response != Error.OK:
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
else:
FileManager.add_to_log("OK | Status: 200 | Message: CLIENT REGISTRATED id: %s" % [client_id])
else:
FileManager.add_to_log("OK | Status: 200 | Message: CLIENT REGISTRATED id: %s" % [client_id])
rpc_id(client_id, 'is_registrated', false, "DataBase inset error", _name, token)
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.ERROR, client_id, "DataBase inset error"])
#--------------------------------------------------------------------------------------------------#

View File

@ -15,7 +15,7 @@ config/name_localized={
"en_US": "Cashflow Game Server"
}
config/version="0.0.0.001"
run/main_scene="res://data/scenes/server.tscn"
run/main_scene="res://data/scenes/Lobbies.tscn"
config/features=PackedStringArray("4.2", "GL Compatibility")
boot_splash/bg_color=Color(0, 0, 0, 1)
boot_splash/image="res://game-server.png"