Added Lobbies

This commit is contained in:
2026-02-19 16:14:30 +01:00
parent eeaf092780
commit 94a40e3d14
6 changed files with 2097 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using TMPro;
/*
GameManager - hlavní tøida pro správu hry
GameManager_Network - subsystém pro správu komunikace se serverem
@@ -16,10 +17,19 @@ using System.Collections.Generic;
*/
public class GameManager : MonoBehaviour
{
protected GameClient gameClient;
[Header("Subsystems")]
protected GameManager_Network networkSubsystem;
protected GameManager_UI uiSubsystem;
protected GameClient gameClient;
[Header("Player Info")]
public string displayName;
[Header("UI Elements")]
public Canvas JoinCreateLobby;
public Canvas InLobby;
void Start()
{
DontDestroyOnLoad(this);
@@ -27,11 +37,20 @@ public class GameManager : MonoBehaviour
{
displayName = "Player_" + Random.Range(1000, 9999).ToString();
}
gameClient = new GameClient(GenerateUUID(), displayName);
gameClient = new GameClient(GenerateUUID(), /*displayName*/ GenerateUsername());
uiSubsystem = new GameManager_UI(gameClient, JoinCreateLobby, InLobby);
networkSubsystem = new GameManager_Network(gameClient);
networkSubsystem.checkState();
networkSubsystem.OpenConection();
}
private void Update()
{
if (gameClient.CurrentLobbyState != null)
{
uiSubsystem.UpdateLobbyUI();
}
}
protected string GenerateUUID()
{
@@ -39,4 +58,34 @@ public class GameManager : MonoBehaviour
Debug.Log(UUID);
return UUID;
}
protected string GenerateUsername()
{
string Username = Random.Range(0,10).ToString() + Random.Range(0, 10).ToString() + Random.Range(0, 10).ToString() + Random.Range(0, 10).ToString();
Debug.Log(Username);
return Username;
}
public void CreateLobbyButton()
{
networkSubsystem.CrateLobby(50.0755, 14.4378);
}
public void JoinLobbyButton()
{
TMP_InputField joinCode = JoinCreateLobby.transform.Find("InputCode").GetComponent<TMP_InputField>();
if (joinCode.text != null && joinCode.text != "")
{
networkSubsystem.JoinLobby(joinCode.text);
}
else
{
Debug.Log("Join code is empty!");
}
}
public void LeaveLobbyButton()
{
networkSubsystem.LeaveLobby();
}
void OnApplicationQuit()
{
gameClient.Disconnect();
}
}

View File

@@ -1,4 +1,5 @@
using GeoSus.Client;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
@@ -9,7 +10,7 @@ namespace Subsystems
private const string _serverAddress = "geosus.honzuvkod.dev";
private const int _serverPort = 7777;
private GameClient _gameClient;
public async void checkState()
public async void OpenConection()
{
while (true)
{
@@ -18,7 +19,7 @@ namespace Subsystems
if (state.Result)
{
Debug.Log("Connected to server.");
_gameClient.Disconnect();
break;
}
else
{
@@ -29,10 +30,123 @@ namespace Subsystems
}
public GameManager_Network(GameClient gameClient)
{
_gameClient = gameClient;
_gameClient = gameClient;
RegisterEventHandlers();
}
public void RegisterEventHandlers()
{
_gameClient.OnConnected += OnConnected;
_gameClient.OnDisconnected += OnDisconnected;
_gameClient.OnError += OnError;
_gameClient.OnMessage += OnMessage;
_gameClient.OnGameEvent += OnGameEvent;
}
private void OnConnected()
{
Debug.Log("Successfully connected to the server.");
}
private void OnDisconnected(string reason)
{
Debug.Log($"Host disconnected due to {reason}");
}
private void OnError(string error)
{
Debug.LogError($"Network error: {error}");
}
private void OnMessage(Message message)
{
switch (message.Type)
{
case "GameEvent":
OnGameEvent(message as GameEvent);
break;
case "CreateLobbyResponse":
Debug.Log("Received CreateLobbyResponse message");
HandleCreateLobbyResponse(message as CreateLobbyResponse);
break;
case "JoinLobbyResponse":
Debug.Log("Received JoinLobbyResponse message");
HandleJoinLobbyResponse(message as JoinLobbyResponse);
break;
case "Ack":
Debug.Log("Received Ack message");
break;
default:
Debug.Log("Received message of type: " + message.Type);
break;
}
}
private void OnGameEvent(GameEvent gameEvent)
{
switch (gameEvent.Type)
{
case "PlayerJoined":
Debug.Log($"Player {gameEvent.GetPayload<PlayerJoinedPayload>().DisplayName} joined");
HandlePlayerJoined(gameEvent);
break;
default:
Debug.Log("Received GameEvent of type: " + gameEvent.Type);
break;
}
}
private void HandleCreateLobbyResponse(CreateLobbyResponse message)
{
if (message.Success)
{
Debug.Log("Lobby created successfully. Join Code: " + message.JoinCode + ", Lobby ID: " + message.LobbyId);
}
else
{
Debug.LogError("Failed to create lobby: " + message.Error);
}
}
private void HandleJoinLobbyResponse(JoinLobbyResponse message)
{
if (message.Success)
{
Debug.Log("Lobby created successfully." + ", Lobby ID: " + message.LobbyId);
}
else
{
Debug.LogError("Failed to create lobby: " + message.Error);
}
}
private void HandlePlayerJoined(GameEvent gameEvent)
{
var payload = gameEvent.GetPayload<PlayerJoinedPayload>();
_gameClient.CurrentLobbyState.Players.Add(new PlayerInfo
{
ClientUuid = payload.ClientUuid,
DisplayName = payload.DisplayName,
IsOwner = false,
IsReady = false,
State = PlayerState.Alive
});
}
}
public void CrateLobby(double lat, double lon)
{
_gameClient.CreateLobby(new Position(lat, lon));
}
public void JoinLobby(string joinCode)
{
try
{
_gameClient.JoinLobby(joinCode);
}
catch (System.Exception ex)
{
Debug.LogError("Error joining lobby: " + ex.Message);
}
}
public void LeaveLobby()
{
_gameClient.Disconnect();
Application.Quit();
}
}
}

View File

@@ -0,0 +1,35 @@
using UnityEngine;
using Subsystems;
using GeoSus.Client;
using System.ComponentModel;
namespace Subsystems
{
public class GameManager_UI
{
private GameClient _gameClient;
private Canvas _CreateJoinLobby;
private Canvas _InLobby;
public GameManager_UI(GameClient gameClient, Canvas CreateJoinLobby, Canvas InLobby)
{
_gameClient = gameClient;
_CreateJoinLobby = CreateJoinLobby;
_InLobby = InLobby;
_CreateJoinLobby.enabled = true;
_InLobby.enabled = false;
}
public void UpdateLobbyUI()
{
_InLobby.enabled = true;
_CreateJoinLobby.enabled = false;
var playerList = _InLobby.transform.Find("PlayerList").GetComponent<TMPro.TMP_Text>();
playerList.text = "";
foreach (var player in _gameClient.CurrentLobbyState.Players)
{
playerList.text += player.DisplayName + "\n";
}
_InLobby.transform.Find("JoinCode").GetComponent<TMPro.TMP_Text>().text = _gameClient.CurrentLobbyState.JoinCode;
}
}
}

File diff suppressed because it is too large Load Diff