feat: added insert method

pull/7/head
Евгений Сугоняко 2024-05-12 17:32:48 +03:00
parent a1c5dad58c
commit c2d2d2655d
3 changed files with 39 additions and 15 deletions

View File

@ -1 +0,0 @@
{"email":"test_user@gmail.com","name":"test_user","password":"9a3d1f5ce0bbbdf38ca26740bdc5aa55","token":"c623864771319a70581900cb7a4cd60d","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:
@ -151,7 +151,7 @@ func registration(_login, _password, _name):
var client_id : int = multiplayer.get_remote_sender_id()
var player : Node2D = _get_player(client_id)
var token : String = str(client_id).md5_text()
var user = db.findOne("User", _login)
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, _name, token)
@ -160,14 +160,24 @@ func registration(_login, _password, _name):
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, "", _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)])
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"])
#--------------------------------------------------------------------------------------------------#