feat: added Games Manager
parent
d800f46004
commit
271c0b1139
|
@ -8,14 +8,12 @@ func escape_bbcode(bbcode_text):
|
|||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Server
|
||||
@rpc("any_peer", "call_local", "unreliable", 9)
|
||||
func rpc_send_message(_client_id, _message):
|
||||
var player = GATEWAY._get_player(_client_id)
|
||||
if player:
|
||||
func rpc_send_message(_client_id, _message, _token):
|
||||
var player : Node2D = GATEWAY._get_player(_client_id)
|
||||
if GATEWAY.autentificate(_client_id, _token, player):
|
||||
var message = escape_bbcode(_message)
|
||||
rpc("rpc_add_message", player.playerName, message)
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s Send message: %s" % [_client_id, _message])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s NOT FOUND" % [_client_id])
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
extends Node
|
||||
|
||||
|
||||
func escape_bbcode(bbcode_text):
|
||||
return bbcode_text.replace("[", "[lb]")
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Client
|
||||
@rpc("authority", "call_local", "unreliable", 10)
|
||||
func rpc_add_message(_name, _message): pass
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Server
|
||||
@rpc("any_peer", "call_local", "unreliable", 10)
|
||||
func rpc_send_message(_client_id, _message, _players, _token):
|
||||
var player = GATEWAY._get_player(_client_id)
|
||||
if GATEWAY.autentificate(_client_id, _token, player):
|
||||
var message = escape_bbcode(_message)
|
||||
for player_to_send in _players:
|
||||
rpc_id(int(player_to_send), "rpc_add_message", player.playerName, message)
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s Send game chat message: %s" % [_client_id, _message])
|
|
@ -10,6 +10,7 @@ var players : Array
|
|||
var players_info : Dictionary
|
||||
var maxPlayers : int
|
||||
var gamePassword : String
|
||||
var instanceName: String
|
||||
|
||||
var isStarted : bool
|
||||
|
||||
|
@ -17,6 +18,7 @@ var isStarted : bool
|
|||
#--------------------------------------------------------------------------------------------------#
|
||||
# Signals
|
||||
signal game_info_changed
|
||||
signal game_empty
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------#
|
||||
|
@ -25,6 +27,11 @@ func _ready():
|
|||
multiplayer.peer_disconnected.connect(delete_player)
|
||||
|
||||
|
||||
func _enter_tree():
|
||||
players.clear()
|
||||
players_info.clear()
|
||||
__save_game_info()
|
||||
|
||||
#--------------------------------------------------------------------------------------------------#
|
||||
# Methods
|
||||
func get_game_info():
|
||||
|
@ -36,7 +43,8 @@ func get_game_info():
|
|||
players_info = self.players_info,
|
||||
maxPlayers = self.maxPlayers,
|
||||
gamePassword = self.gamePassword,
|
||||
isStarted = self.isStarted
|
||||
isStarted = self.isStarted,
|
||||
instanceName = self.instanceName
|
||||
}
|
||||
|
||||
|
||||
|
@ -47,41 +55,51 @@ func set_game_info(_gameSettings:Dictionary):
|
|||
self.maxPlayers = _gameSettings.maxPlayers
|
||||
self.gamePassword = _gameSettings.gamePassword
|
||||
self.name = str(_gameSettings.gameId)
|
||||
self.instanceName = _gameSettings.instanceName
|
||||
self.isStarted = false
|
||||
__save_game_info()
|
||||
for player in players:
|
||||
rpc_id(player, "rpc_change_game_info", get_game_info())
|
||||
rpc_id(int(player), "rpc_change_game_info", get_game_info())
|
||||
_emmit_game_info_changed()
|
||||
|
||||
|
||||
func added_player(_player:Dictionary):
|
||||
players.push_back(_player.id)
|
||||
players_info[_player.id] = {
|
||||
players.push_back(int(_player.id))
|
||||
players_info[int(_player.id)] = {
|
||||
'login' = _player.login,
|
||||
'name' = _player.name
|
||||
'name' = _player.name,
|
||||
'role' = _player.role
|
||||
}
|
||||
__save_game_info()
|
||||
for player in players:
|
||||
rpc_id(player, "rpc_set_player_info", _player)
|
||||
rpc_id(int(player), "rpc_set_player_info", _player)
|
||||
_emmit_game_info_changed()
|
||||
|
||||
|
||||
func delete_player(_clientId:int):
|
||||
if players.has(_clientId):
|
||||
players.erase(_clientId)
|
||||
players_info.erase(_clientId)
|
||||
__save_game_info()
|
||||
for player in players:
|
||||
rpc_id(player, "rpc_unset_player_info", _clientId)
|
||||
if players.is_empty():
|
||||
self.queue_free()
|
||||
else :
|
||||
_emmit_game_info_changed()
|
||||
if GATEWAY._get_player(_clientId).login == instanceName:
|
||||
close_game(gameId)
|
||||
else:
|
||||
for player in players:
|
||||
rpc_id(int(player), "rpc_unset_player_info", _clientId)
|
||||
players.erase(_clientId)
|
||||
players_info.erase(_clientId)
|
||||
__save_game_info()
|
||||
|
||||
|
||||
func __save_game_info():
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
db.update("Game", ownerName, get_game_info())
|
||||
db.update("Game", instanceName, get_game_info())
|
||||
|
||||
|
||||
func close_game(_gameId):
|
||||
for player in players:
|
||||
rpc_id(int(player), "rpc_close_game")
|
||||
players.clear()
|
||||
players_info.clear()
|
||||
__save_game_info()
|
||||
game_empty.emit(_gameId)
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------#
|
||||
|
@ -104,27 +122,51 @@ func rpc_set_player_info(_player:Dictionary): pass
|
|||
func rpc_unset_player_info(_clientId:int): pass
|
||||
|
||||
|
||||
@rpc("authority", "call_local", "reliable", 3)
|
||||
func rpc_close_game(): pass
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Server
|
||||
@rpc("any_peer", "call_local", "reliable", 3)
|
||||
func rpc_game_info_changed(_gameSettings:Dictionary):
|
||||
set_game_info(_gameSettings)
|
||||
func rpc_game_info_changed(_gameSettings:Dictionary, _token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
set_game_info(_gameSettings)
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable", 3)
|
||||
func rpc_set_player(_clientId:int):
|
||||
func rpc_set_player(_clientId:int, _token:String):
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var player : Node2D = GATEWAY._get_player(_clientId)
|
||||
var user = db.findOne("User", player.login)
|
||||
if user:
|
||||
var player_data : Dictionary = {
|
||||
'id' = _clientId,
|
||||
'login' = player.login,
|
||||
'name' = player.playerName
|
||||
}
|
||||
added_player(player_data)
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
var user = db.findOne("User", player.login)
|
||||
if user:
|
||||
var player_data : Dictionary = {
|
||||
'id' = _clientId,
|
||||
'login' = player.login,
|
||||
'name' = player.playerName
|
||||
}
|
||||
if player.login == instanceName:
|
||||
player_data['role'] = 'Leader'
|
||||
else:
|
||||
player_data['role'] = 'Player'
|
||||
added_player(player_data)
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable", 3)
|
||||
func rpc_unset_player(_clientId:int):
|
||||
delete_player(_clientId)
|
||||
func rpc_unset_player(_clientId:int, _token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
delete_player(_clientId)
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable", 3)
|
||||
func rpc_set_close_game(_gameId:int, _token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
close_game(_gameId)
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://u3ht16dhk3iq"]
|
||||
[gd_scene load_steps=3 format=3 uid="uid://u3ht16dhk3iq"]
|
||||
|
||||
[ext_resource type="Script" path="res://data/scenes/Game/game.gd" id="1_2bjjo"]
|
||||
[ext_resource type="Script" path="res://data/scenes/Game/Scripts/Sinhronizers/ChatSinhronizer.gd" id="2_ovjnl"]
|
||||
|
||||
[node name="Game" type="Node"]
|
||||
script = ExtResource("1_2bjjo")
|
||||
|
||||
[node name="ChatSinhronizer" type="Node" parent="."]
|
||||
script = ExtResource("2_ovjnl")
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
extends Node
|
||||
|
||||
|
||||
func remove_game_node(_id):
|
||||
var game_node = get_node("/root/%s" % [_id])
|
||||
game_node.set_process_mode(PROCESS_MODE_DISABLED)
|
||||
game_node.queue_free()
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Client
|
||||
@rpc("authority", "call_local", "unreliable", 2)
|
||||
|
@ -14,55 +21,96 @@ func rpc_game_created(_game:Dictionary): pass
|
|||
func rpc_cant_create_game(_message : String): pass
|
||||
|
||||
|
||||
@rpc("authority", "call_local", "unreliable", 2)
|
||||
func rpc_set_game_info(_gameData:Dictionary): pass
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
# RPC Server
|
||||
@rpc("any_peer", "call_local", "unreliable", 2)
|
||||
func rpc_create_game(_gameData:Dictionary):
|
||||
func rpc_create_game(_gameData:Dictionary, _token:String):
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var client_id : int = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(client_id)
|
||||
if db.findOne("Game", player.login):
|
||||
rpc_id(client_id, "rpc_cant_create_game", "$only_one_game_for_one_player")
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT CREATE Game. Reason: Player can create only one game" % [client_id])
|
||||
else:
|
||||
if db.insert("Game", player.login, _gameData):
|
||||
var game_instance = preload("res://data/scenes/Game/game.tscn").instantiate()
|
||||
game_instance.set_game_info(_gameData)
|
||||
get_tree().root.add_child(game_instance)
|
||||
rpc_id(client_id, "rpc_game_created", _gameData)
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s CREATE Game." % [client_id])
|
||||
if GATEWAY.autentificate(client_id, _token, player):
|
||||
if db.findOne("Game", player.login):
|
||||
rpc_id(client_id, "rpc_cant_create_game", "$only_one_game_for_one_player")
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT CREATE Game. Reason: Player can create only one game" % [client_id])
|
||||
else:
|
||||
rpc_id(client_id, "rpc_cant_create_game", "$data_base_error")
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: CLIENT %s DO NOT CREATE Game. Reason: Data base error" % [client_id])
|
||||
_gameData["instanceName"] = player.login
|
||||
if db.insert("Game", player.login, _gameData):
|
||||
var game_instance = preload("res://data/scenes/Game/game.tscn").instantiate()
|
||||
game_instance.set_game_info(_gameData)
|
||||
game_instance.game_empty.connect(remove_game_node)
|
||||
get_tree().root.add_child(game_instance)
|
||||
rpc_id(client_id, "rpc_game_created", _gameData)
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s CREATE Game." % [client_id])
|
||||
else:
|
||||
rpc_id(client_id, "rpc_cant_create_game", "$data_base_error")
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: CLIENT %s DO NOT CREATE Game. Reason: Data base error" % [client_id])
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "unreliable", 2)
|
||||
func rpc_get_games_list():
|
||||
func rpc_get_games_list(_token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var gamesData = db.findAll("Game")
|
||||
if gamesData != null:
|
||||
if rpc_id(clientId, "rpc_set_games_list", gamesData) != Error.OK:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT GET Games List" % [clientId])
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var gamesData = db.findAll("Game")
|
||||
if gamesData != null:
|
||||
var index = 0
|
||||
for game in gamesData:
|
||||
if game.instanceName == player.login:
|
||||
game.isMy = true
|
||||
else:
|
||||
if len(game.players) == 0:
|
||||
gamesData.remove_at(index)
|
||||
index += 1
|
||||
if rpc_id(clientId, "rpc_set_games_list", gamesData) != Error.OK:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT GET Games List" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s GET GAMES LIST" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s GET GAMES LIST" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: Game MODEL NOT FOUND" % [clientId])
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: Game MODEL NOT FOUND")
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "unreliable", 2)
|
||||
func rpc_get_game(_name:String):
|
||||
func rpc_get_game(_name:String, _token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var gamesData = db.findAll("Game")
|
||||
if gamesData != null:
|
||||
var searchedGame : Array = []
|
||||
for game in gamesData:
|
||||
if game.gameName == _name:
|
||||
searchedGame.append(game)
|
||||
if rpc_id(clientId, "rpc_set_games_list", searchedGame) != Error.OK:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT GET GAME BY SEARCH" % [clientId])
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var gamesData = db.findAll("Game")
|
||||
if gamesData != null:
|
||||
var searchedGame : Array = []
|
||||
for game in gamesData:
|
||||
if game.gameName == _name:
|
||||
searchedGame.append(game)
|
||||
if rpc_id(clientId, "rpc_set_games_list", searchedGame) != Error.OK:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT GET GAME BY SEARCH" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s GET GAME BY SEARCH" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s GET GAME BY SEARCH" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: Game MODEL NOT FOUND" % [clientId])
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: Game MODEL NOT FOUND" % [clientId])
|
||||
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable", 2)
|
||||
func rpc_get_game_info(_instanceName:String, _token:String):
|
||||
var clientId = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = GATEWAY._get_player(clientId)
|
||||
if GATEWAY.autentificate(clientId, _token, player):
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var gameData = db.findOne("Game", _instanceName)
|
||||
if gameData != null:
|
||||
var isReady = get_node_or_null("/root/" + str(gameData.gameId))
|
||||
if isReady == null:
|
||||
var game_instance = preload("res://data/scenes/Game/game.tscn").instantiate()
|
||||
game_instance.set_game_info(gameData)
|
||||
game_instance.game_empty.connect(remove_game_node)
|
||||
get_tree().root.add_child(game_instance)
|
||||
if rpc_id(clientId, "rpc_set_game_info", gameData) != Error.OK:
|
||||
GATEWAY.FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT %s DO NOT GET GAME INFO" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("OK | Status: 200 | Message: CLIENT %s GET GAME INFO" % [clientId])
|
||||
else:
|
||||
GATEWAY.FileManager.add_to_log("ERROR | Status: 500 | Message: Game MODEL NOT FOUND")
|
||||
|
|
|
@ -8,8 +8,8 @@ process_mode = 3
|
|||
process_priority = 1
|
||||
process_physics_priority = 1
|
||||
|
||||
[node name="ChatSinhronizer" type="Node" parent="."]
|
||||
script = ExtResource("1_imuqr")
|
||||
|
||||
[node name="GamesSinhronizer" type="Node" parent="."]
|
||||
script = ExtResource("2_bpnu1")
|
||||
|
||||
[node name="ChatSinhronizer" type="Node" parent="."]
|
||||
script = ExtResource("1_imuqr")
|
||||
|
|
|
@ -20,3 +20,7 @@ func _init():
|
|||
# RPC Client
|
||||
@rpc("authority", "reliable", "call_local", 1)
|
||||
func authorizated(): pass
|
||||
|
||||
|
||||
@rpc("authority", "reliable", "call_local", 1)
|
||||
func autentificated(_isAuth:bool): pass
|
||||
|
|
|
@ -92,6 +92,24 @@ func start_server() -> void:
|
|||
FileManager.add_to_log(error_string(peer_status) + " | Status: %s | Message: %s" % [response.status, response.message])
|
||||
|
||||
|
||||
func autentificate(_client_id: int, _token: String, _player: Node2D, _login: String = "", _call_rpc: bool = true) -> bool:
|
||||
var db = ORM.new(OS.get_executable_path().get_base_dir())
|
||||
var login: String = _login.to_lower()
|
||||
if _login == "":
|
||||
login = _player.login.to_lower()
|
||||
var user = db.findOne("User", login)
|
||||
if user:
|
||||
if user.token == _token:
|
||||
_player.playerName = user['name']
|
||||
_player.login = login
|
||||
_player.token = _token
|
||||
return true
|
||||
FileManager.add_to_log("WARNING | Status: 300 | Message: CLIENT NOT AUTENTIFICATED id: %s - Warning: Invalid token" % [_client_id])
|
||||
if _call_rpc:
|
||||
_player.autentificated.rpc_id(_client_id, false)
|
||||
return false
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------#
|
||||
# Functions
|
||||
func _get_player(_id):
|
||||
|
@ -212,6 +230,22 @@ func registration(_login, _password, _name):
|
|||
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT REGISTRATED id: %s - Error: %s" % [STATUS.ERROR, client_id, "DataBase inset error"])
|
||||
|
||||
|
||||
@rpc("any_peer", "reliable", "call_local", 1)
|
||||
func autentification(_token: String, _login: String = ""):
|
||||
var client_id : int = multiplayer.get_remote_sender_id()
|
||||
var player : Node2D = _get_player(client_id)
|
||||
if autentificate(client_id, _token, player, _login, false):
|
||||
var rpc_response = player.rpc_id(client_id, 'autentificated', true)
|
||||
if rpc_response != Error.OK:
|
||||
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT AUTENTIFICATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
|
||||
else:
|
||||
FileManager.add_to_log("OK | Status: 200 | Message: CLIENT AUTENTIFICATED id: %s" % [client_id])
|
||||
else:
|
||||
var rpc_response = player.rpc_id(client_id, 'autentificated', false)
|
||||
if rpc_response != Error.OK:
|
||||
FileManager.add_to_log("ERROR | Status: %s | Message: CLIENT NOT AUTENTIFICATED id: %s - Error: %s" % [STATUS.ERROR, client_id, error_string(rpc_response)])
|
||||
|
||||
|
||||
#--------------------------------------------------------------------------------------------------#
|
||||
# RPC Client
|
||||
@rpc("any_peer", "reliable", "call_local", 1)
|
||||
|
|
Loading…
Reference in New Issue