Added Lobbies
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user