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.Threading.Tasks;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro;
/* /*
GameManager - hlavní tøida pro správu hry GameManager - hlavní tøida pro správu hry
GameManager_Network - subsystém pro správu komunikace se serverem GameManager_Network - subsystém pro správu komunikace se serverem
@@ -16,10 +17,19 @@ using System.Collections.Generic;
*/ */
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
protected GameClient gameClient; [Header("Subsystems")]
protected GameManager_Network networkSubsystem; protected GameManager_Network networkSubsystem;
protected GameManager_UI uiSubsystem;
protected GameClient gameClient;
[Header("Player Info")]
public string displayName; public string displayName;
[Header("UI Elements")]
public Canvas JoinCreateLobby;
public Canvas InLobby;
void Start() void Start()
{ {
DontDestroyOnLoad(this); DontDestroyOnLoad(this);
@@ -27,9 +37,18 @@ public class GameManager : MonoBehaviour
{ {
displayName = "Player_" + Random.Range(1000, 9999).ToString(); 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 = new GameManager_Network(gameClient);
networkSubsystem.checkState(); networkSubsystem.OpenConection();
}
private void Update()
{
if (gameClient.CurrentLobbyState != null)
{
uiSubsystem.UpdateLobbyUI();
}
} }
@@ -39,4 +58,34 @@ public class GameManager : MonoBehaviour
Debug.Log(UUID); Debug.Log(UUID);
return 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 GeoSus.Client;
using System.Collections;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
@@ -9,7 +10,7 @@ namespace Subsystems
private const string _serverAddress = "geosus.honzuvkod.dev"; private const string _serverAddress = "geosus.honzuvkod.dev";
private const int _serverPort = 7777; private const int _serverPort = 7777;
private GameClient _gameClient; private GameClient _gameClient;
public async void checkState() public async void OpenConection()
{ {
while (true) while (true)
{ {
@@ -18,7 +19,7 @@ namespace Subsystems
if (state.Result) if (state.Result)
{ {
Debug.Log("Connected to server."); Debug.Log("Connected to server.");
_gameClient.Disconnect(); break;
} }
else else
{ {
@@ -30,9 +31,122 @@ namespace Subsystems
public GameManager_Network(GameClient gameClient) 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

View File

@@ -5,9 +5,12 @@ EditorBuildSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 2 serializedVersion: 2
m_Scenes: m_Scenes:
- enabled: 1 - enabled: 0
path: Assets/Scenes/SampleScene.unity path: Assets/Scenes/SampleScene.unity
guid: 99c9720ab356a0642a771bea13969a05 guid: 3e95f16d8e50b3341925e51e50768027
- enabled: 1
path: Assets/Scenes/Client.unity
guid: 8f736798e2d13f14f903b26a2df0eed8
m_configObjects: m_configObjects:
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3} com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
m_UseUCBPForAssetBundles: 0 m_UseUCBPForAssetBundles: 0

View File

@@ -4,7 +4,7 @@
UnityConnectSettings: UnityConnectSettings:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 1 serializedVersion: 1
m_Enabled: 0 m_Enabled: 1
m_TestMode: 0 m_TestMode: 0
m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events
m_EventUrl: https://cdp.cloud.unity3d.com/v1/events m_EventUrl: https://cdp.cloud.unity3d.com/v1/events