diff --git a/Assets/ClientSDK.meta b/Assets/ClientSDK.meta new file mode 100644 index 0000000..a54062d --- /dev/null +++ b/Assets/ClientSDK.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 799f52449ae21404c9a7593f6dc28c60 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ClientSDK/Encryption.cs b/Assets/ClientSDK/Encryption.cs new file mode 100644 index 0000000..3def88a --- /dev/null +++ b/Assets/ClientSDK/Encryption.cs @@ -0,0 +1,285 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace GeoSus.Client +{ + // Klientská strana šifrování - generuje session key, šifruje RSA, AES-CBC session + // Používá AES-CBC místo AES-GCM pro kompatibilitu s Unity + public class ClientEncryption : IDisposable + { + private byte[] _sessionKey; + private byte[] _sessionIv; + private long _nonceCounter; + private readonly object _lock = new object(); + + // Kontrola, zda je session key nastaven + public bool HasSessionKey => _sessionKey != null && _sessionIv != null; + + // Generuje nový session key a IV + public void GenerateSessionKey() + { + _sessionKey = new byte[32]; // AES-256 + _sessionIv = new byte[16]; // CBC IV (16 bytes) + + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(_sessionKey); + rng.GetBytes(_sessionIv); + } + } + + public byte[] SessionKey => _sessionKey ?? throw new InvalidOperationException("Session key not generated"); + public byte[] SessionIV => _sessionIv ?? throw new InvalidOperationException("Session IV not generated"); + + // Zašifruje session key pomocí RSA public key serveru + public (string EncryptedKey, string EncryptedIV) EncryptSessionKeyForServer(string rsaPublicKeyPem) + { + if (_sessionKey == null || _sessionIv == null) + throw new InvalidOperationException("Session key not generated"); + + using (var rsa = RSA.Create()) + { + // Parse PEM - extrahuj Base64 obsah + var pemLines = rsaPublicKeyPem.Split('\n'); + var base64 = new StringBuilder(); + foreach (var line in pemLines) + { + var trimmed = line.Trim(); + if (!trimmed.StartsWith("-----") && !string.IsNullOrEmpty(trimmed)) + { + base64.Append(trimmed); + } + } + + var keyBytes = Convert.FromBase64String(base64.ToString()); + + // Unity kompatibilní import - parsujeme SubjectPublicKeyInfo ručně + ImportSubjectPublicKeyInfoManual(rsa, keyBytes); + + // Používáme OaepSHA1 pro Unity kompatibilitu (OaepSHA256 není podporován) + var encryptedKey = rsa.Encrypt(_sessionKey, RSAEncryptionPadding.OaepSHA1); + var encryptedIv = rsa.Encrypt(_sessionIv, RSAEncryptionPadding.OaepSHA1); + + return (Convert.ToBase64String(encryptedKey), Convert.ToBase64String(encryptedIv)); + } + } + + // Ručně parsuje SubjectPublicKeyInfo (DER) a importuje RSA klíč - Unity kompatibilní + private static void ImportSubjectPublicKeyInfoManual(RSA rsa, byte[] subjectPublicKeyInfo) + { + // SubjectPublicKeyInfo ::= SEQUENCE { + // algorithm AlgorithmIdentifier, + // subjectPublicKey BIT STRING } + // RSAPublicKey ::= SEQUENCE { modulus INTEGER, publicExponent INTEGER } + + int index = 0; + + // Outer SEQUENCE + if (subjectPublicKeyInfo[index++] != 0x30) + throw new InvalidOperationException("Invalid SubjectPublicKeyInfo"); + ReadLength(subjectPublicKeyInfo, ref index); + + // AlgorithmIdentifier SEQUENCE - skip it + if (subjectPublicKeyInfo[index++] != 0x30) + throw new InvalidOperationException("Invalid AlgorithmIdentifier"); + int algLen = ReadLength(subjectPublicKeyInfo, ref index); + index += algLen; + + // BIT STRING containing RSAPublicKey + if (subjectPublicKeyInfo[index++] != 0x03) + throw new InvalidOperationException("Invalid BIT STRING"); + ReadLength(subjectPublicKeyInfo, ref index); + index++; // Skip unused bits byte (should be 0) + + // RSAPublicKey SEQUENCE + if (subjectPublicKeyInfo[index++] != 0x30) + throw new InvalidOperationException("Invalid RSAPublicKey"); + ReadLength(subjectPublicKeyInfo, ref index); + + // Modulus INTEGER + byte[] modulus = ReadInteger(subjectPublicKeyInfo, ref index); + + // Exponent INTEGER + byte[] exponent = ReadInteger(subjectPublicKeyInfo, ref index); + + var parameters = new RSAParameters + { + Modulus = modulus, + Exponent = exponent + }; + rsa.ImportParameters(parameters); + } + + private static int ReadLength(byte[] data, ref int index) + { + int length = data[index++]; + if ((length & 0x80) != 0) + { + int numBytes = length & 0x7F; + length = 0; + for (int i = 0; i < numBytes; i++) + { + length = (length << 8) | data[index++]; + } + } + return length; + } + + private static byte[] ReadInteger(byte[] data, ref int index) + { + if (data[index++] != 0x02) + throw new InvalidOperationException("Expected INTEGER"); + int length = ReadLength(data, ref index); + + // Skip leading zero if present (used for positive sign in DER) + int originalLength = length; + int start = index; + if (length > 1 && data[start] == 0x00) + { + start++; + length--; + } + + byte[] result = new byte[length]; + Buffer.BlockCopy(data, start, result, 0, length); + index += originalLength; + + return result; + } + + // Šifruje zprávu pomocí AES-256-CBC s HMAC + public byte[] Encrypt(byte[] plaintext) + { + if (_sessionKey == null || _sessionIv == null) + throw new InvalidOperationException("Session key not set"); + + lock (_lock) + { + // Generuj unikátní IV pro tuto zprávu + var iv = GetNextIV(); + + using (var aes = Aes.Create()) + { + aes.Key = _sessionKey; + aes.IV = iv; + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.PKCS7; + + byte[] ciphertext; + using (var encryptor = aes.CreateEncryptor()) + using (var ms = new MemoryStream()) + { + using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) + { + cs.Write(plaintext, 0, plaintext.Length); + } + ciphertext = ms.ToArray(); + } + + // Compute HMAC pro integritu + byte[] hmac; + using (var hmacSha = new HMACSHA256(_sessionKey)) + { + var toSign = new byte[iv.Length + ciphertext.Length]; + Buffer.BlockCopy(iv, 0, toSign, 0, iv.Length); + Buffer.BlockCopy(ciphertext, 0, toSign, iv.Length, ciphertext.Length); + hmac = hmacSha.ComputeHash(toSign); + } + + // Výstup: [16 bytes IV][32 bytes HMAC][ciphertext] + var result = new byte[16 + 32 + ciphertext.Length]; + Buffer.BlockCopy(iv, 0, result, 0, 16); + Buffer.BlockCopy(hmac, 0, result, 16, 32); + Buffer.BlockCopy(ciphertext, 0, result, 48, ciphertext.Length); + + return result; + } + } + } + + // Dešifruje zprávu pomocí AES-256-CBC s HMAC ověřením + public byte[] Decrypt(byte[] encrypted) + { + if (_sessionKey == null) + throw new InvalidOperationException("Session key not set"); + + if (encrypted.Length < 48) return null; + + try + { + var iv = new byte[16]; + var hmac = new byte[32]; + var ciphertext = new byte[encrypted.Length - 48]; + + Buffer.BlockCopy(encrypted, 0, iv, 0, 16); + Buffer.BlockCopy(encrypted, 16, hmac, 0, 32); + Buffer.BlockCopy(encrypted, 48, ciphertext, 0, ciphertext.Length); + + // Ověř HMAC + byte[] expectedHmac; + using (var hmacSha = new HMACSHA256(_sessionKey)) + { + var toVerify = new byte[iv.Length + ciphertext.Length]; + Buffer.BlockCopy(iv, 0, toVerify, 0, iv.Length); + Buffer.BlockCopy(ciphertext, 0, toVerify, iv.Length, ciphertext.Length); + expectedHmac = hmacSha.ComputeHash(toVerify); + } + + // Constant-time compare + var diff = 0; + for (int i = 0; i < 32; i++) + { + diff |= hmac[i] ^ expectedHmac[i]; + } + if (diff != 0) return null; // HMAC mismatch + + using (var aes = Aes.Create()) + { + aes.Key = _sessionKey; + aes.IV = iv; + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.PKCS7; + + using (var decryptor = aes.CreateDecryptor()) + using (var ms = new MemoryStream(ciphertext)) + using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) + using (var output = new MemoryStream()) + { + cs.CopyTo(output); + return output.ToArray(); + } + } + } + catch (CryptographicException) + { + return null; + } + } + + private byte[] GetNextIV() + { + if (_sessionIv == null) + throw new InvalidOperationException("Session IV not set"); + + var iv = new byte[16]; + Buffer.BlockCopy(_sessionIv, 0, iv, 0, 8); + + var counter = System.Threading.Interlocked.Increment(ref _nonceCounter); + var counterBytes = BitConverter.GetBytes(counter); + Buffer.BlockCopy(counterBytes, 0, iv, 8, 8); + + return iv; + } + + public void Dispose() + { + if (_sessionKey != null) + { + Array.Clear(_sessionKey, 0, _sessionKey.Length); + _sessionKey = null; + } + } + } +} diff --git a/Assets/ClientSDK/Encryption.cs.meta b/Assets/ClientSDK/Encryption.cs.meta new file mode 100644 index 0000000..f850920 --- /dev/null +++ b/Assets/ClientSDK/Encryption.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: bc06bb57786c7e142b06ec231e5cf709 \ No newline at end of file diff --git a/Assets/ClientSDK/EventDispatcher.cs b/Assets/ClientSDK/EventDispatcher.cs new file mode 100644 index 0000000..839def1 --- /dev/null +++ b/Assets/ClientSDK/EventDispatcher.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace GeoSus.Client +{ + // Event dispatcher pro Unity main thread +// Unity může přidat SynchronizationContext, nebo polling z Update() +public class EventDispatcher +{ + private readonly Queue _pendingActions = new Queue(); + private readonly object _lock = new object(); + private SynchronizationContext? _syncContext; + + public EventDispatcher() + { + // Pokusíme se zachytit aktuální synchronization context (Unity main thread) + _syncContext = SynchronizationContext.Current; + } + + // Volat z networking vlákna - naplánuje callback na main thread + public void Post(Action action) + { + if (_syncContext != null) + { + _syncContext.Post(_ => action(), null); + } + else + { + // Fallback - přidáme do fronty pro polling + lock (_lock) + { + _pendingActions.Enqueue(action); + } + } + } + + // Volat z Unity Update() pokud není SynchronizationContext + public void ProcessPendingActions() + { + Action[] actions; + lock (_lock) + { + if (_pendingActions.Count == 0) return; + actions = _pendingActions.ToArray(); + _pendingActions.Clear(); + } + + foreach (var action in actions) + { + try + { + action(); + } + catch (Exception ex) + { + Console.WriteLine($"EventDispatcher error: {ex}"); + } + } + } + + public int PendingCount + { + get + { + lock (_lock) + { + return _pendingActions.Count; + } + } + } +} +} diff --git a/Assets/ClientSDK/EventDispatcher.cs.meta b/Assets/ClientSDK/EventDispatcher.cs.meta new file mode 100644 index 0000000..2eaf448 --- /dev/null +++ b/Assets/ClientSDK/EventDispatcher.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 1d2251b279edb0147bd274a884ac878b \ No newline at end of file diff --git a/Assets/ClientSDK/GameClient.cs b/Assets/ClientSDK/GameClient.cs new file mode 100644 index 0000000..09fa7f1 --- /dev/null +++ b/Assets/ClientSDK/GameClient.cs @@ -0,0 +1,607 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace GeoSus.Client +{ + // Hlavní klientská třída pro připojení k serveru +public class GameClient : IDisposable +{ + private TcpClient? _tcpClient; + private NetworkStream? _stream; + private ClientEncryption? _encryption; + private CancellationTokenSource? _cts; + private Task? _receiveTask; + private int _clientSeq; + private readonly object _sendLock = new object(); + private bool _handshakeComplete; + + public string ClientUuid { get; } + public string DisplayName { get; set; } + public bool IsConnected => _tcpClient?.Connected ?? false; + public bool IsReady => IsConnected && _handshakeComplete && (_encryption?.HasSessionKey ?? false); + public EventDispatcher Dispatcher { get; } + + // Events - voláno na main thread přes dispatcher + public event Action? OnConnected; + public event Action? OnDisconnected; + public event Action? OnError; + public event Action? OnMessage; + public event Action? OnGameEvent; + + // Lobby state + public string? LobbyId { get; private set; } + public string? JoinCode { get; private set; } + public LobbyState? CurrentLobbyState { get; private set; } + public PlayerRole? MyRole { get; private set; } + public List MyTasks { get; } = new List(); + public Position MyPosition { get; set; } + public Dictionary PlayerPositions { get; } = new Dictionary(); + public List Bodies { get; } = new List(); + public int Ping { get; private set; } + public long LastEventId { get; private set; } + + /// Returns true if this client is the current lobby owner + public bool IsOwner => CurrentLobbyState?.OwnerId == ClientUuid; + + public GameClient(string clientUuid, string displayName) + { + ClientUuid = clientUuid; + DisplayName = displayName; + Dispatcher = new EventDispatcher(); + } + + #region Connection + + public async Task ConnectAsync(string host, int port) + { + try + { + _tcpClient = new TcpClient(); + await _tcpClient.ConnectAsync(host, port); + _stream = _tcpClient.GetStream(); + _encryption = new ClientEncryption(); + _cts = new CancellationTokenSource(); + + // Handshake + if (!await PerformHandshakeAsync()) + { + Disconnect("Handshake failed"); + return false; + } + + // Spustíme příjem zpráv + _receiveTask = Task.Run(() => ReceiveLoopAsync(_cts.Token)); + + Dispatcher.Post(() => OnConnected?.Invoke()); + return true; + } + catch (Exception ex) + { + Dispatcher.Post(() => OnError?.Invoke(ex.Message)); + return false; + } + } + + private async Task PerformHandshakeAsync() + { + if (_stream == null || _encryption == null) return false; + + // 1. ClientHello + var hello = new ClientHello + { + ClientUuid = ClientUuid, + DisplayName = DisplayName + }; + await SendPlainAsync(hello); + + // 2. ServerHello + var serverHelloData = await ReadMessageAsync(); + if (serverHelloData == null) return false; + + var serverHello = MessageSerializer.Deserialize(serverHelloData) as ServerHello; + if (serverHello == null) return false; + + // 3. Generujeme session key a šifrujeme RSA + _encryption.GenerateSessionKey(); + var (encKey, encIv) = _encryption.EncryptSessionKeyForServer(serverHello.RsaPublicKeyPem); + + var keyExchange = new KeyExchange + { + EncryptedSessionKey = encKey, + EncryptedIV = encIv + }; + await SendPlainAsync(keyExchange); + + // 4. KeyExchangeAck (šifrovaně) + var ackData = await ReadMessageAsync(); + if (ackData == null) return false; + + var decrypted = _encryption.Decrypt(ackData); + if (decrypted == null) return false; + + var ack = MessageSerializer.Deserialize(decrypted) as KeyExchangeAck; + if (ack?.Status == "success") + { + _handshakeComplete = true; + return true; + } + return false; + } + + public void Disconnect(string reason = "User disconnected") + { + _cts?.Cancel(); + _tcpClient?.Close(); + _tcpClient = null; + _stream = null; + _encryption?.Dispose(); + _encryption = null; + + LobbyId = null; + JoinCode = null; + CurrentLobbyState = null; + MyRole = null; + MyTasks.Clear(); + PlayerPositions.Clear(); + Bodies.Clear(); + + Dispatcher.Post(() => OnDisconnected?.Invoke(reason)); + } + + #endregion + + #region Sending + + public void Send(Message message) + { + if (_stream == null || _encryption == null || !IsConnected) return; + + message.ClientSeq = Interlocked.Increment(ref _clientSeq); + if (string.IsNullOrEmpty(message.ActionId)) + { + message.ActionId = Guid.NewGuid().ToString("N").Substring(0, 8); + } + + var plain = MessageSerializer.Serialize(message); + var encrypted = _encryption.Encrypt(plain); + + lock (_sendLock) + { + try + { + SendData(encrypted); + } + catch (Exception ex) + { + Dispatcher.Post(() => OnError?.Invoke($"Send error: {ex.Message}")); + } + } + } + + private async Task SendPlainAsync(Message message) + { + if (_stream == null) return; + var data = MessageSerializer.Serialize(message); + await SendDataAsync(data); + } + + private void SendData(byte[] data) + { + if (_stream == null) return; + + var lengthBuffer = BitConverter.GetBytes(data.Length); + if (BitConverter.IsLittleEndian) + Array.Reverse(lengthBuffer); + + _stream.Write(lengthBuffer, 0, 4); + _stream.Write(data, 0, data.Length); + _stream.Flush(); + } + + private async Task SendDataAsync(byte[] data) + { + if (_stream == null) return; + + var lengthBuffer = BitConverter.GetBytes(data.Length); + if (BitConverter.IsLittleEndian) + Array.Reverse(lengthBuffer); + + await _stream.WriteAsync(lengthBuffer, 0, 4); + await _stream.WriteAsync(data, 0, data.Length); + await _stream.FlushAsync(); + } + + #endregion + + #region Receiving + + private async Task ReceiveLoopAsync(CancellationToken ct) + { + int decryptFailures = 0; + + try + { + while (!ct.IsCancellationRequested && IsConnected) + { + var data = await ReadMessageAsync(); + if (data == null) break; + + var decrypted = _encryption?.Decrypt(data); + if (decrypted == null) + { + decryptFailures++; + if (decryptFailures >= 3) + { + Disconnect("Too many decryption failures"); + return; + } + continue; + } + + decryptFailures = 0; + + var message = MessageSerializer.Deserialize(decrypted); + if (message != null) + { + ProcessMessage(message); + } + } + } + catch (Exception ex) when (!ct.IsCancellationRequested) + { + Disconnect($"Connection error: {ex.Message}"); + } + } + + private async Task ReadMessageAsync() + { + if (_stream == null) return null; + + var lengthBuffer = new byte[4]; + var read = await _stream.ReadAsync(lengthBuffer, 0, 4); + if (read < 4) return null; + + if (BitConverter.IsLittleEndian) + Array.Reverse(lengthBuffer); + var length = BitConverter.ToInt32(lengthBuffer, 0); + + if (length <= 0 || length > 1048576) return null; + + var buffer = new byte[length]; + var totalRead = 0; + while (totalRead < length) + { + read = await _stream.ReadAsync(buffer, totalRead, length - totalRead); + if (read == 0) return null; + totalRead += read; + } + + return buffer; + } + + private void ProcessMessage(Message message) + { + // Zpracujeme speciální typy + switch (message) + { + case CreateLobbyResponse r: + if (r.Success) + { + LobbyId = r.LobbyId; + JoinCode = r.JoinCode; + CurrentLobbyState = r.LobbyState; + } + break; + + case JoinLobbyResponse r: + if (r.Success) + { + LobbyId = r.LobbyId; + CurrentLobbyState = r.LobbyState; + JoinCode = r.LobbyState?.JoinCode; + } + break; + + case PositionBroadcast b: + ProcessPositionBroadcast(b); + break; + + case Pong p: + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + Ping = (int)(now - p.ClientTime); + break; + + case GameEvent evt: + ProcessGameEvent(evt); + Dispatcher.Post(() => OnGameEvent?.Invoke(evt)); + break; + } + + Dispatcher.Post(() => OnMessage?.Invoke(message)); + } + + private void ProcessPositionBroadcast(PositionBroadcast broadcast) + { + PlayerPositions.Clear(); + foreach (var player in broadcast.Players) + { + PlayerPositions[player.ClientUuid] = player; + } + } + + private void ProcessGameEvent(GameEvent evt) + { + LastEventId = evt.EventId; + + switch (evt.EventType) + { + case "PlayerJoined": + // Add player to lobby state + var joinedPayload = evt.GetPayload(); + if (joinedPayload != null && CurrentLobbyState?.Players != null) + { + // Check if player already exists + bool exists = CurrentLobbyState.Players.Any(p => p.ClientUuid == joinedPayload.ClientUuid); + if (!exists) + { + CurrentLobbyState.Players.Add(new PlayerInfo + { + ClientUuid = joinedPayload.ClientUuid, + DisplayName = joinedPayload.DisplayName, + IsOwner = false, + IsReady = false, + State = PlayerState.Alive + }); + } + } + break; + + case "PlayerLeft": + // Remove player from lobby state + var leftPayload = evt.GetPayload(); + if (leftPayload != null && CurrentLobbyState?.Players != null) + { + CurrentLobbyState.Players.RemoveAll(p => p.ClientUuid == leftPayload.ClientUuid); + } + break; + + case "HostChanged": + // Update lobby owner + var hostPayload = evt.GetPayload(); + if (hostPayload != null && CurrentLobbyState != null) + { + CurrentLobbyState.OwnerId = hostPayload.NewHostId; + // Update IsOwner flag on all players + foreach (var player in CurrentLobbyState.Players) + { + player.IsOwner = player.ClientUuid == hostPayload.NewHostId; + } + } + break; + + case "GameStarting": + // Game is entering loading phase - update lobby state if available + if (CurrentLobbyState != null) + { + CurrentLobbyState.Phase = GamePhase.Loading; + } + break; + + case "MapDataReady": + // Map data received - store it and send confirmation + var mapDataPayload = evt.GetPayload(); + if (mapDataPayload != null && CurrentLobbyState != null) + { + CurrentLobbyState.MapData = mapDataPayload.MapData; + CurrentLobbyState.MapDataReady = true; + } + // Send confirmation to server + Send(new MapDataReceived()); + break; + + case "GameStarted": + // Game officially started - update phase + if (CurrentLobbyState != null) + { + CurrentLobbyState.Phase = GamePhase.Playing; + } + break; + + case "RoleAssigned": + var rolePayload = evt.GetPayload(); + if (rolePayload != null && rolePayload.ClientUuid == ClientUuid) + { + MyRole = rolePayload.Role; + MyTasks.Clear(); + if (rolePayload.Tasks != null) + { + MyTasks.AddRange(rolePayload.Tasks); + } + } + break; + + case "PlayerKilled": + var killPayload = evt.GetPayload(); + if (killPayload != null) + { + Bodies.Add(new Body + { + BodyId = killPayload.BodyId, + VictimId = killPayload.VictimId, + Location = killPayload.Location + }); + } + break; + + case "MeetingStarted": + if (CurrentLobbyState != null) + { + CurrentLobbyState.Phase = GamePhase.Meeting; + } + break; + + case "VotingClosed": + Bodies.Clear(); // Bodies zmizí po meetingu + if (CurrentLobbyState != null) + { + CurrentLobbyState.Phase = GamePhase.Playing; + } + break; + + case "GameEnded": + if (CurrentLobbyState != null) + { + CurrentLobbyState.Phase = GamePhase.Ended; + } + break; + } + } + + #endregion + + #region Game Actions + + public void CreateLobby(Position? center = null, int impostorCount = 1, int taskCount = 5, string? password = null, double playAreaRadius = 500) + { + Send(new CreateLobby + { + PlayAreaCenter = center, + PlayAreaRadius = playAreaRadius, + ImpostorCount = impostorCount, + TaskCount = taskCount, + Password = password + }); + } + + public void JoinLobby(string joinCode, string? password = null) + { + Send(new JoinLobby + { + JoinCode = joinCode.ToUpperInvariant(), + Password = password + }); + } + + public void LeaveLobby() + { + Send(new LeaveLobby()); + LobbyId = null; + JoinCode = null; + } + + public void StartGame() + { + Send(new StartGame()); + } + + public void ReturnToLobby() + { + Send(new ReturnToLobby()); + } + + public void UpdatePosition(Position position) + { + MyPosition = position; + Send(new UpdatePosition { Position = position }); + } + + public void Kill(string targetUuid) + { + Send(new KillAttempt { TargetClientUuid = targetUuid }); + } + + public void ReportBody(string bodyId) + { + Send(new ReportBody { BodyId = bodyId }); + } + + public void CallEmergencyMeeting() + { + Send(new CallEmergencyMeeting()); + } + + public void Vote(string? targetUuid) + { + Send(new CastVote { TargetClientUuid = targetUuid }); + } + + /// + /// Pokus o dokončení tasku. Server ověří že hráč je na správné pozici. + /// + public void CompleteTask(string taskId) + { + Send(new TaskComplete { TaskId = taskId }); + } + + public void SendPing() + { + Send(new Ping { ClientTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }); + } + + public void Reconnect(string lobbyId) + { + Send(new Reconnect { LobbyId = lobbyId, LastEventId = LastEventId }); + } + + #endregion + + #region Helpers + + public Body? FindNearbyBody(double maxDistance) + { + foreach (var body in Bodies) + { + if (MyPosition.DistanceTo(body.Location) <= maxDistance) + { + return body; + } + } + return null; + } + + public string? FindNearbyPlayer(double maxDistance, bool aliveOnly = true) + { + foreach (var (uuid, info) in PlayerPositions) + { + if (uuid == ClientUuid) continue; + if (aliveOnly && info.State != PlayerState.Alive) continue; + + if (MyPosition.DistanceTo(info.Position) <= maxDistance) + { + return uuid; + } + } + return null; + } + + public GameTask? FindNearbyTask(double maxDistance) + { + foreach (var task in MyTasks) + { + if (MyPosition.DistanceTo(task.Location) <= maxDistance) + { + return task; + } + } + return null; + } + + // Volat z Unity Update() pro zpracování callbacků + public void Update() + { + Dispatcher.ProcessPendingActions(); + } + + #endregion + + public void Dispose() + { + Disconnect("Disposed"); + _encryption?.Dispose(); + } +} +} diff --git a/Assets/ClientSDK/GameClient.cs.meta b/Assets/ClientSDK/GameClient.cs.meta new file mode 100644 index 0000000..b454c2d --- /dev/null +++ b/Assets/ClientSDK/GameClient.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 91e0f647c37b0b94b83f53bb854db28c \ No newline at end of file diff --git a/Assets/ClientSDK/Protocol.cs b/Assets/ClientSDK/Protocol.cs new file mode 100644 index 0000000..e867ad5 --- /dev/null +++ b/Assets/ClientSDK/Protocol.cs @@ -0,0 +1,1055 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Converters; +using System.Collections.Generic; +using System.Text; + +namespace GeoSus.Client +{ + #region Základní typy + + public struct Position + { + [JsonProperty("lat")] + public double Lat { get; set; } + + [JsonProperty("lon")] + public double Lon { get; set; } + + public Position(double lat, double lon) + { + Lat = lat; + Lon = lon; + } + + // Haversine vzdálenost v metrech + public double DistanceTo(Position other) + { + const double R = 6371000; + var lat1 = Lat * Math.PI / 180; + var lat2 = other.Lat * Math.PI / 180; + var dLat = (other.Lat - Lat) * Math.PI / 180; + var dLon = (other.Lon - Lon) * Math.PI / 180; + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(lat1) * Math.Cos(lat2) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return R * c; + } + public static bool operator ==(Position left, Position right) { if (left.Lat == right.Lat && left.Lon == right.Lon) { return true; } else { return false; } } + public static bool operator !=(Position left, Position right) { return !(left == right); } + } +[JsonConverter(typeof(StringEnumConverter))] +public enum PlayerRole { Crew, Impostor } + +[JsonConverter(typeof(StringEnumConverter))] +public enum PlayerState { Alive, Dead } + +[JsonConverter(typeof(StringEnumConverter))] +public enum GamePhase { Lobby, Loading, Playing, Meeting, Voting, Ended } + +[JsonConverter(typeof(StringEnumConverter))] +public enum TaskType { Instant } + +[JsonConverter(typeof(StringEnumConverter))] +public enum MeetingType { BodyReport, Emergency } + +[JsonConverter(typeof(StringEnumConverter))] +public enum SabotageType { CommsBlackout, CriticalMeltdown } + +[JsonConverter(typeof(StringEnumConverter))] +public enum SabotageState { Inactive, Active, Repaired } + +// Map data types for Overpass integration +[JsonConverter(typeof(StringEnumConverter))] +public enum PathType +{ + Footway, + Path, + Steps, + Cycleway, + Pedestrian, + Road, + Service, + Residential, + Track, + Other +} + +[JsonConverter(typeof(StringEnumConverter))] +public enum MapAreaType +{ + Park, + Garden, + Playground, + Forest, + Grass, + Water, + Other +} + +[JsonConverter(typeof(StringEnumConverter))] +public enum MapPOIType +{ + FoodDrink, // Restaurants, cafes, bars + Shop, // Shops + Health, // Pharmacies, hospitals + Transport, // Bus stops, parking + Culture, // Museums, theaters + Landmark, // Churches, monuments + Recreation, // Parks, playgrounds + Other +} + +#endregion + +#region Zprávy + +public abstract class Message +{ + [JsonProperty("type")] + public abstract string Type { get; } + + [JsonProperty("clientSeq")] + public int ClientSeq { get; set; } + + [JsonProperty("actionId")] + public string? ActionId { get; set; } +} + +// Handshake +public class ClientHello : Message +{ + public override string Type => "ClientHello"; + + [JsonProperty("protocolVersion")] + public string ProtocolVersion { get; set; } = "1.0"; + + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("displayName")] + public string? DisplayName { get; set; } +} + +public class ServerHello : Message +{ + public override string Type => "ServerHello"; + + [JsonProperty("rsaPublicKeyPem")] + public string RsaPublicKeyPem { get; set; } = ""; + + [JsonProperty("serverId")] + public string ServerId { get; set; } = ""; +} + +public class KeyExchange : Message +{ + public override string Type => "KeyExchange"; + + [JsonProperty("encryptedSessionKey")] + public string EncryptedSessionKey { get; set; } = ""; + + [JsonProperty("encryptedIV")] + public string EncryptedIV { get; set; } = ""; +} + +public class KeyExchangeAck : Message +{ + public override string Type => "KeyExchangeAck"; + + [JsonProperty("status")] + public string Status { get; set; } = ""; +} + +// Lobby +public class CreateLobby : Message +{ + public override string Type => "CreateLobby"; + + [JsonProperty("password")] + public string? Password { get; set; } + + [JsonProperty("playAreaCenter")] + public Position? PlayAreaCenter { get; set; } + + [JsonProperty("playAreaRadius")] + public double PlayAreaRadius { get; set; } = 500; + + [JsonProperty("impostorCount")] + public int ImpostorCount { get; set; } = 1; + + [JsonProperty("taskCount")] + public int TaskCount { get; set; } = 5; +} + +public class CreateLobbyResponse : Message +{ + public override string Type => "CreateLobbyResponse"; + + [JsonProperty("success")] + public bool Success { get; set; } + + [JsonProperty("joinCode")] + public string? JoinCode { get; set; } + + [JsonProperty("lobbyId")] + public string? LobbyId { get; set; } + + [JsonProperty("error")] + public string? Error { get; set; } + + [JsonProperty("lobbyState")] + public LobbyState? LobbyState { get; set; } +} + +public class JoinLobby : Message +{ + public override string Type => "JoinLobby"; + + [JsonProperty("joinCode")] + public string JoinCode { get; set; } = ""; + + [JsonProperty("password")] + public string? Password { get; set; } +} + +public class JoinLobbyResponse : Message +{ + public override string Type => "JoinLobbyResponse"; + + [JsonProperty("success")] + public bool Success { get; set; } + + [JsonProperty("lobbyId")] + public string? LobbyId { get; set; } + + [JsonProperty("error")] + public string? Error { get; set; } + + [JsonProperty("lobbyState")] + public LobbyState? LobbyState { get; set; } +} + +public class LeaveLobby : Message +{ + public override string Type => "LeaveLobby"; +} + +public class ReturnToLobby : Message +{ + public override string Type => "ReturnToLobby"; +} + +public class StartGame : Message +{ + public override string Type => "StartGame"; +} + +// Client confirmation that map data was received +public class MapDataReceived : Message +{ + public override string Type => "MapDataReceived"; +} + +// Hra +public class UpdatePosition : Message +{ + public override string Type => "UpdatePosition"; + + [JsonProperty("position")] + public Position Position { get; set; } +} + +public class PositionBroadcast : Message +{ + public override string Type => "PositionBroadcast"; + + [JsonProperty("players")] + public List Players { get; set; } = new List(); +} + +public class PlayerPositionInfo +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("position")] + public Position Position { get; set; } + + [JsonProperty("state")] + public PlayerState State { get; set; } +} + +public class KillAttempt : Message +{ + public override string Type => "KillAttempt"; + + [JsonProperty("targetClientUuid")] + public string TargetClientUuid { get; set; } = ""; +} + +public class ReportBody : Message +{ + public override string Type => "ReportBody"; + + [JsonProperty("bodyId")] + public string BodyId { get; set; } = ""; +} + +public class CallEmergencyMeeting : Message +{ + public override string Type => "CallEmergencyMeeting"; +} + +public class CastVote : Message +{ + public override string Type => "CastVote"; + + [JsonProperty("targetClientUuid")] + public string? TargetClientUuid { get; set; } +} + +public class TaskStart : Message +{ + public override string Type => "TaskStart"; + + [JsonProperty("taskId")] + public string TaskId { get; set; } = ""; +} + +public class TaskProgress : Message +{ + public override string Type => "TaskProgress"; + + [JsonProperty("taskId")] + public string TaskId { get; set; } = ""; + + [JsonProperty("step")] + public int Step { get; set; } = 1; +} + +public class TaskComplete : Message +{ + public override string Type => "TaskComplete"; + + [JsonProperty("taskId")] + public string TaskId { get; set; } = ""; +} + +public class Ping : Message +{ + public override string Type => "Ping"; + + [JsonProperty("clientTime")] + public long ClientTime { get; set; } +} + +public class Pong : Message +{ + public override string Type => "Pong"; + + [JsonProperty("clientTime")] + public long ClientTime { get; set; } + + [JsonProperty("serverTime")] + public long ServerTime { get; set; } +} + +public class Reconnect : Message +{ + public override string Type => "Reconnect"; + + [JsonProperty("lobbyId")] + public string LobbyId { get; set; } = ""; + + [JsonProperty("lastEventId")] + public long LastEventId { get; set; } +} + +public class Ack : Message +{ + public override string Type => "Ack"; + + [JsonProperty("ackedSeq")] + public int AckedSeq { get; set; } + + [JsonProperty("success")] + public bool Success { get; set; } + + [JsonProperty("error")] + public string? Error { get; set; } +} + +public class ErrorMessage : Message +{ + public override string Type => "Error"; + + [JsonProperty("errorCode")] + public string ErrorCode { get; set; } = ""; + + [JsonProperty("errorText")] + public string ErrorText { get; set; } = ""; +} + +// Sabotage messages +public class StartSabotage : Message +{ + public override string Type => "StartSabotage"; + + [JsonProperty("sabotageType")] + public SabotageType SabotageType { get; set; } +} + +public class ActivateRepairStation : Message +{ + public override string Type => "ActivateRepairStation"; + + [JsonProperty("stationId")] + public string StationId { get; set; } = ""; +} + +public class DeactivateRepairStation : Message +{ + public override string Type => "DeactivateRepairStation"; + + [JsonProperty("stationId")] + public string StationId { get; set; } = ""; +} + +#endregion + +#region Eventy + +public class GameEvent : Message +{ + public override string Type => "GameEvent"; + + [JsonProperty("eventId")] + public long EventId { get; set; } + + [JsonProperty("serverSeq")] + public long ServerSeq { get; set; } + + [JsonProperty("timestamp")] + public DateTime Timestamp { get; set; } + + [JsonProperty("actor")] + public string? Actor { get; set; } + + [JsonProperty("eventType")] + public string EventType { get; set; } = ""; + + [JsonProperty("payload")] + public JObject? Payload { get; set; } + + public T? GetPayload() where T : class + { + if (Payload == null) return null; + return Payload.ToObject(JsonSerializer.Create(JsonOptions.Default)); + } +} + +// Payload typy +public class HostChangedPayload +{ + [JsonProperty("newHostId")] + public string NewHostId { get; set; } = ""; + + [JsonProperty("previousHostId")] + public string PreviousHostId { get; set; } = ""; +} + +public class PlayerJoinedPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("displayName")] + public string DisplayName { get; set; } = ""; +} + +public class PlayerLeftPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("reason")] + public string? Reason { get; set; } +} + +// Loading phase payloads +public class GameStartingPayload +{ + [JsonProperty("message")] + public string Message { get; set; } = ""; +} + +public class MapDataReadyPayload +{ + [JsonProperty("mapData")] + public MapDataPayload? MapData { get; set; } +} + +public class PlayerMapDataReceivedPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("displayName")] + public string DisplayName { get; set; } = ""; + + [JsonProperty("playersReady")] + public int PlayersReady { get; set; } + + [JsonProperty("totalPlayers")] + public int TotalPlayers { get; set; } +} + +public class GameStartedPayload +{ + [JsonProperty("impostorCount")] + public int ImpostorCount { get; set; } + + [JsonProperty("taskCount")] + public int TaskCount { get; set; } +} + +public class RoleAssignedPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("role")] + public PlayerRole Role { get; set; } + + [JsonProperty("tasks")] + public List? Tasks { get; set; } +} + +public class PlayerKilledPayload +{ + [JsonProperty("victimId")] + public string VictimId { get; set; } = ""; + + [JsonProperty("killerId")] + public string KillerId { get; set; } = ""; + + [JsonProperty("bodyId")] + public string BodyId { get; set; } = ""; + + [JsonProperty("location")] + public Position Location { get; set; } +} + +public class BodyReportedPayload +{ + [JsonProperty("reporterId")] + public string ReporterId { get; set; } = ""; + + [JsonProperty("bodyId")] + public string BodyId { get; set; } = ""; + + [JsonProperty("victimId")] + public string VictimId { get; set; } = ""; +} + +public class EmergencyMeetingCalledPayload +{ + [JsonProperty("callerId")] + public string CallerId { get; set; } = ""; +} + +public class MeetingStartedPayload +{ + [JsonProperty("meetingId")] + public string MeetingId { get; set; } = ""; + + [JsonProperty("type")] + public MeetingType Type { get; set; } + + [JsonProperty("meetingLocation")] + public Position MeetingLocation { get; set; } + + [JsonProperty("arrivalDeadline")] + public DateTime ArrivalDeadline { get; set; } + + [JsonProperty("discussionEndTime")] + public DateTime? DiscussionEndTime { get; set; } + + [JsonProperty("votingEndTime")] + public DateTime VotingEndTime { get; set; } +} + +public class PlayerArrivedAtMeetingPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("meetingId")] + public string MeetingId { get; set; } = ""; +} + +public class PlayerVotedPayload +{ + [JsonProperty("voterId")] + public string VoterId { get; set; } = ""; + + [JsonProperty("targetId")] + public string? TargetId { get; set; } +} + +public class VotingClosedPayload +{ + [JsonProperty("voteCounts")] + public Dictionary VoteCounts { get; set; } = new Dictionary(); + + [JsonProperty("ejectedPlayerId")] + public string? EjectedPlayerId { get; set; } + + [JsonProperty("wasTie")] + public bool WasTie { get; set; } +} + +public class PlayerEjectedPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("role")] + public PlayerRole Role { get; set; } +} + +public class TaskCompletedPayload +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("taskId")] + public string TaskId { get; set; } = ""; + + [JsonProperty("totalCompleted")] + public int TotalCompleted { get; set; } + + [JsonProperty("totalTasks")] + public int TotalTasks { get; set; } +} + +public class GameEndedPayload +{ + [JsonProperty("winningFaction")] + public string WinningFaction { get; set; } = ""; + + [JsonProperty("reason")] + public string Reason { get; set; } = ""; + + [JsonProperty("winners")] + public List Winners { get; set; } = new List(); +} + +public class ReturnedToLobbyPayload +{ + [JsonProperty("message")] + public string Message { get; set; } = ""; +} + +// System message payload (admin broadcast) +public class SystemMessagePayload +{ + [JsonProperty("message")] + public string Message { get; set; } = ""; + + [JsonProperty("timestamp")] + public DateTime Timestamp { get; set; } +} + +// Sabotage event payloads +public class SabotageStartedPayload +{ + [JsonProperty("sabotageId")] + public string SabotageId { get; set; } = ""; + + [JsonProperty("type")] + public SabotageType Type { get; set; } + + [JsonProperty("initiatorId")] + public string InitiatorId { get; set; } = ""; + + [JsonProperty("deadline")] + public DateTime? Deadline { get; set; } + + [JsonProperty("repairStations")] + public List RepairStations { get; set; } = new List(); + + [JsonProperty("requiredSimultaneousRepairs")] + public int RequiredSimultaneousRepairs { get; set; } +} + +public class RepairStationInfo +{ + [JsonProperty("stationId")] + public string StationId { get; set; } = ""; + + [JsonProperty("name")] + public string Name { get; set; } = ""; + + [JsonProperty("location")] + public Position Location { get; set; } + + [JsonProperty("repairDurationMs")] + public int RepairDurationMs { get; set; } + + /// + /// Track locally if this station has been repaired + /// + [JsonIgnore] + public bool IsRepaired { get; set; } +} + +public class RepairStartedPayload +{ + [JsonProperty("sabotageId")] + public string SabotageId { get; set; } = ""; + + [JsonProperty("stationId")] + public string StationId { get; set; } = ""; + + [JsonProperty("playerId")] + public string PlayerId { get; set; } = ""; +} + +public class RepairStoppedPayload +{ + [JsonProperty("sabotageId")] + public string SabotageId { get; set; } = ""; + + [JsonProperty("stationId")] + public string StationId { get; set; } = ""; + + [JsonProperty("playerId")] + public string PlayerId { get; set; } = ""; +} + +public class SabotageRepairedPayload +{ + [JsonProperty("sabotageId")] + public string SabotageId { get; set; } = ""; + + [JsonProperty("type")] + public SabotageType Type { get; set; } + + [JsonProperty("repairerIds")] + public List RepairerIds { get; set; } = new List(); +} + +public class SabotageMeltdownPayload +{ + [JsonProperty("sabotageId")] + public string SabotageId { get; set; } = ""; +} + +#endregion + +#region State + +public class LobbyState +{ + [JsonProperty("lobbyId")] + public string LobbyId { get; set; } = ""; + + [JsonProperty("joinCode")] + public string JoinCode { get; set; } = ""; + + [JsonProperty("ownerId")] + public string? OwnerId { get; set; } + + [JsonProperty("phase")] + public GamePhase Phase { get; set; } + + [JsonProperty("players")] + public List Players { get; set; } = new List(); + + [JsonProperty("playAreaCenter")] + public Position PlayAreaCenter { get; set; } + + [JsonProperty("playAreaRadius")] + public double PlayAreaRadius { get; set; } + + [JsonProperty("impostorCount")] + public int ImpostorCount { get; set; } + + [JsonProperty("hasPassword")] + public bool HasPassword { get; set; } + + [JsonProperty("mapData")] + public MapDataPayload? MapData { get; set; } + + /// True if map data has been loaded (or Overpass is disabled) + [JsonProperty("mapDataReady")] + public bool MapDataReady { get; set; } = true; +} + +// Map data classes for rendering - compact format from server +public class MapDataPayload +{ + [JsonProperty("center")] + public Position Center { get; set; } + + [JsonProperty("radiusMeters")] + public double RadiusMeters { get; set; } + + /// Buildings: [[lat,lon,lat,lon,...], ...] + [JsonProperty("buildings")] + public List Buildings { get; set; } = new List(); + + /// Building types: ["residential", "commercial", ...] + [JsonProperty("buildingTypes")] + public List BuildingTypes { get; set; } = new List(); + + /// Pathways: [[lat,lon,lat,lon,...], ...] + [JsonProperty("pathways")] + public List Pathways { get; set; } = new List(); + + /// Pathway types: [0=footway, 1=steps, ...] + [JsonProperty("pathwayTypes")] + public List PathwayTypes { get; set; } = new List(); + + /// Areas (parks): [[lat,lon,lat,lon,...], ...] + [JsonProperty("areas")] + public List Areas { get; set; } = new List(); + + /// Area types + [JsonProperty("areaTypes")] + public List AreaTypes { get; set; } = new List(); + + /// POIs: [lat, lon, type, lat, lon, type, ...] + [JsonProperty("pOIs")] + public List POIs { get; set; } = new List(); + + // Helper methods for extracting structured data + public List GetBuildings() + { + var result = new List(); + for (int i = 0; i < Buildings.Count; i++) + { + var coords = Buildings[i]; + var outline = new List(); + for (int j = 0; j < coords.Length - 1; j += 2) + { + outline.Add(new Position(coords[j], coords[j + 1])); + } + result.Add(new MapBuilding + { + Id = i, + Outline = outline, + BuildingType = i < BuildingTypes.Count ? BuildingTypes[i] : "yes" + }); + } + return result; + } + + public List GetPathways() + { + var result = new List(); + for (int i = 0; i < Pathways.Count; i++) + { + var coords = Pathways[i]; + var points = new List(); + for (int j = 0; j < coords.Length - 1; j += 2) + { + points.Add(new Position(coords[j], coords[j + 1])); + } + result.Add(new MapPathway + { + Id = i, + Points = points, + PathType = i < PathwayTypes.Count ? (PathType)PathwayTypes[i] : PathType.Other + }); + } + return result; + } + + public List GetAreas() + { + var result = new List(); + for (int i = 0; i < Areas.Count; i++) + { + var coords = Areas[i]; + var outline = new List(); + for (int j = 0; j < coords.Length - 1; j += 2) + { + outline.Add(new Position(coords[j], coords[j + 1])); + } + result.Add(new MapArea + { + Id = i, + Outline = outline, + AreaType = i < AreaTypes.Count ? (MapAreaType)AreaTypes[i] : MapAreaType.Other + }); + } + return result; + } + + public List GetPOIs() + { + var result = new List(); + for (int i = 0; i < POIs.Count - 2; i += 3) + { + result.Add(new MapPOI + { + Id = i / 3, + Location = new Position(POIs[i], POIs[i + 1]), + POIType = (MapPOIType)(int)POIs[i + 2] + }); + } + return result; + } +} + +public class MapBuilding +{ + public long Id { get; set; } + public List Outline { get; set; } = new List(); + public string? Name { get; set; } + public string? BuildingType { get; set; } +} + +public class MapPathway +{ + public long Id { get; set; } + public List Points { get; set; } = new List(); + public PathType PathType { get; set; } + public string? Name { get; set; } +} + +public class MapArea +{ + public long Id { get; set; } + public List Outline { get; set; } = new List(); + public MapAreaType AreaType { get; set; } + public string? Name { get; set; } +} + +public class MapPOI +{ + public long Id { get; set; } + public Position Location { get; set; } + public string? Name { get; set; } + public MapPOIType POIType { get; set; } +} + +public class PlayerInfo +{ + [JsonProperty("clientUuid")] + public string ClientUuid { get; set; } = ""; + + [JsonProperty("displayName")] + public string DisplayName { get; set; } = ""; + + [JsonProperty("isOwner")] + public bool IsOwner { get; set; } + + [JsonProperty("isReady")] + public bool IsReady { get; set; } + + [JsonProperty("state")] + public PlayerState State { get; set; } +} + +public class GameTask +{ + [JsonProperty("taskId")] + public string TaskId { get; set; } = ""; + + [JsonProperty("name")] + public string Name { get; set; } = ""; + + [JsonProperty("location")] + public Position Location { get; set; } + + [JsonProperty("type")] + public TaskType Type { get; set; } = TaskType.Instant; +} + +public class Body +{ + [JsonProperty("bodyId")] + public string BodyId { get; set; } = ""; + + [JsonProperty("victimId")] + public string VictimId { get; set; } = ""; + + [JsonProperty("location")] + public Position Location { get; set; } +} + +#endregion + +#region Serializace + +public static class JsonOptions +{ + public static readonly JsonSerializerSettings Default = new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore, + Converters = { new StringEnumConverter() } + }; +} + +public static class MessageSerializer +{ + private static readonly Dictionary MessageTypes = new Dictionary + { + ["ClientHello"] = typeof(ClientHello), + ["ServerHello"] = typeof(ServerHello), + ["KeyExchange"] = typeof(KeyExchange), + ["KeyExchangeAck"] = typeof(KeyExchangeAck), + ["CreateLobby"] = typeof(CreateLobby), + ["CreateLobbyResponse"] = typeof(CreateLobbyResponse), + ["JoinLobby"] = typeof(JoinLobby), + ["JoinLobbyResponse"] = typeof(JoinLobbyResponse), + ["LeaveLobby"] = typeof(LeaveLobby), + ["ReturnToLobby"] = typeof(ReturnToLobby), + ["StartGame"] = typeof(StartGame), + ["MapDataReceived"] = typeof(MapDataReceived), + ["UpdatePosition"] = typeof(UpdatePosition), + ["PositionBroadcast"] = typeof(PositionBroadcast), + ["KillAttempt"] = typeof(KillAttempt), + ["ReportBody"] = typeof(ReportBody), + ["CallEmergencyMeeting"] = typeof(CallEmergencyMeeting), + ["CastVote"] = typeof(CastVote), + ["TaskStart"] = typeof(TaskStart), + ["TaskProgress"] = typeof(TaskProgress), + ["TaskComplete"] = typeof(TaskComplete), + ["Ping"] = typeof(Ping), + ["Pong"] = typeof(Pong), + ["Reconnect"] = typeof(Reconnect), + ["Ack"] = typeof(Ack), + ["Error"] = typeof(ErrorMessage), + ["GameEvent"] = typeof(GameEvent) + }; + + public static byte[] Serialize(Message msg) + { + var json = JsonConvert.SerializeObject(msg, JsonOptions.Default); + return Encoding.UTF8.GetBytes(json); + } + + public static Message? Deserialize(byte[] data) + { + var json = Encoding.UTF8.GetString(data); + var jObj = JObject.Parse(json); + + var typeName = jObj["type"]?.Value(); + if (typeName == null || !MessageTypes.TryGetValue(typeName, out var type)) + return null; + + return (Message?)JsonConvert.DeserializeObject(json, type, JsonOptions.Default); + } +} + + #endregion +} diff --git a/Assets/ClientSDK/Protocol.cs.meta b/Assets/ClientSDK/Protocol.cs.meta new file mode 100644 index 0000000..719b429 --- /dev/null +++ b/Assets/ClientSDK/Protocol.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 14463228dfea2264ebfc36c3a7dc4b99 \ No newline at end of file diff --git a/Assets/ClientSDK/SimulatorClient.cs b/Assets/ClientSDK/SimulatorClient.cs new file mode 100644 index 0000000..733418a --- /dev/null +++ b/Assets/ClientSDK/SimulatorClient.cs @@ -0,0 +1,1992 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace GeoSus.Client +{ + /// +/// Comprehensive headless simulator client for testing all game aspects. +/// Supports both autonomous simulation and step-by-step controlled testing. +/// +public class SimulatorClient : IDisposable +{ + private readonly GameClient _client; + private readonly Random _random = new Random(); + private CancellationTokenSource? _cts; + private Task? _simulationTask; + private PlayerState _myState = PlayerState.Alive; + + #region Public Properties + + public string ClientUuid => _client.ClientUuid; + public string DisplayName => _client.DisplayName; + public bool IsConnected => _client.IsConnected; + public LobbyState? LobbyState => _client.CurrentLobbyState; + public string? LobbyId => _client.LobbyId; + public string? JoinCode => _client.JoinCode; + public PlayerRole? Role => _client.MyRole; + public PlayerState State => _myState; + public Position Position => _client.MyPosition; + public bool IsAlive => _myState == PlayerState.Alive; + public bool IsDead => _myState == PlayerState.Dead; + public bool IsImpostor => _client.MyRole == PlayerRole.Impostor; + public bool IsCrew => _client.MyRole == PlayerRole.Crew; + + // Tasks + public List MyTasks => _client.MyTasks; + public HashSet CompletedTaskIds { get; } = new HashSet(); + public string? CurrentTaskId { get; private set; } + + // Game state tracking + public int TotalKills { get; private set; } + public int TasksCompleted { get; private set; } + public int MeetingsAttended { get; private set; } + public int VotesCast { get; private set; } + public int BodiesReported { get; private set; } + public bool WasEjected { get; private set; } + public bool WasKilled { get; private set; } + public string? LastError { get; private set; } + public string? GameResult { get; private set; } + public string? WinningFaction { get; private set; } + public bool GameEnded => GameResult != null; + + // Nearby entities + public Dictionary NearbyPlayers => _client.PlayerPositions; + public List NearbyBodies => _client.Bodies; + + // Meeting state + public bool InMeeting { get; private set; } + public bool HasVotedThisMeeting { get; private set; } + public string? CurrentMeetingId { get; private set; } + public Position? MeetingLocation { get; private set; } + public DateTime? MeetingVotingEndTime { get; private set; } + public DateTime? MeetingDiscussionEndTime { get; private set; } + + /// + /// Returns true if the discussion phase has ended and voting can begin. + /// + public bool CanVote => InMeeting && + (!MeetingDiscussionEndTime.HasValue || DateTime.UtcNow >= MeetingDiscussionEndTime.Value); + + // Current game phase from lobby state + public string GamePhase => _client.CurrentLobbyState?.Phase.ToString() ?? "Unknown"; + + // Sabotage state + public bool SabotageActive { get; private set; } + public string? CurrentSabotageId { get; private set; } + public SabotageType? CurrentSabotageType { get; private set; } + public DateTime? SabotageDeadline { get; private set; } + public List RepairStations { get; private set; } = new List(); + public int SabotagesStarted { get; private set; } + public int SabotagesRepaired { get; private set; } + public bool IsRepairing { get; private set; } + public string? RepairingStationId { get; private set; } + + /// + /// Returns true if comms are blocked (can't report/meeting) + /// + public bool IsCommsBlocked => SabotageActive && CurrentSabotageType == SabotageType.CommsBlackout; + + /// + /// Returns true if there's a critical meltdown countdown + /// + public bool IsMeltdownActive => SabotageActive && CurrentSabotageType == SabotageType.CriticalMeltdown; + + #endregion + + #region Events + + public event Action? OnLog; + public event Action? OnError; + public event Action? OnGameEvent; + public event Action? OnKilled; + public event Action? OnEjected; + public event Action? OnGameEnded; + public event Action? OnMeetingStarted; + public event Action? OnMeetingEnded; + public event Action? OnSabotageStarted; + public event Action? OnSabotageRepaired; + public event Action? OnMeltdown; + + #endregion + + #region Constructor + + public SimulatorClient(string clientUuid, string displayName) + { + _client = new GameClient(clientUuid, displayName); + + _client.OnConnected += () => Log("Připojen k serveru"); + _client.OnDisconnected += (reason) => Log($"Odpojen: {reason}"); + _client.OnError += (error) => + { + LastError = error; + Log($"Chyba: {error}"); + OnError?.Invoke(error); + }; + + _client.OnMessage += HandleMessage; + _client.OnGameEvent += HandleGameEvent; + } + + #endregion + + #region Message Handlers + + private void HandleMessage(Message msg) + { + switch (msg) + { + case CreateLobbyResponse r: + if (r.Success) + Log($"Lobby vytvořeno: {r.JoinCode}"); + else + LogError($"Lobby creation failed"); + break; + + case JoinLobbyResponse r: + if (r.Success) + Log($"Připojen do lobby: {r.LobbyId}"); + else + LogError("Join lobby failed"); + break; + + case Ack a when !a.Success: + LastError = a.Error; + Log($"Akce zamítnuta: {a.Error}"); + break; + } + } + + private void HandleGameEvent(GameEvent evt) + { + OnGameEvent?.Invoke(evt.EventType, evt.Payload); + + switch (evt.EventType) + { + case "GameStarting": + Log("Game loading - fetching map data..."); + break; + + case "MapDataReady": + var mapPayload = evt.GetPayload(); + var buildingCount = mapPayload?.MapData?.Buildings?.Count ?? 0; + var pathwayCount = mapPayload?.MapData?.Pathways?.Count ?? 0; + Log($"Map data received: {buildingCount} buildings, {pathwayCount} pathways - sending confirmation"); + break; + + case "PlayerMapDataReceived": + var progressPayload = evt.GetPayload(); + if (progressPayload != null) + { + Log($"Player {progressPayload.DisplayName} ready ({progressPayload.PlayersReady}/{progressPayload.TotalPlayers})"); + } + break; + + case "GameStarted": + Log("Hra začala!"); + break; + + case "RoleAssigned": + var rolePayload = evt.GetPayload(); + if (rolePayload?.ClientUuid == ClientUuid) + { + Log($"Moje role: {rolePayload.Role}"); + } + break; + + case "PlayerKilled": + var killPayload = evt.GetPayload(); + Log($"Hráč {killPayload?.VictimId} byl zabit"); + if (killPayload?.VictimId == ClientUuid) + { + WasKilled = true; + _myState = PlayerState.Dead; + Log("BYL JSEM ZABIT!"); + OnKilled?.Invoke(); + } + break; + + case "MeetingStarted": + var meetingPayload = evt.GetPayload(); + InMeeting = true; + HasVotedThisMeeting = false; + CurrentMeetingId = meetingPayload?.MeetingId; + MeetingLocation = meetingPayload?.MeetingLocation; + MeetingVotingEndTime = meetingPayload?.VotingEndTime; + MeetingDiscussionEndTime = meetingPayload?.DiscussionEndTime; + Log($"MEETING ZAČAL! Typ: {meetingPayload?.Type}, Lokace: {MeetingLocation?.Lat:F4},{MeetingLocation?.Lon:F4}"); + if (MeetingDiscussionEndTime.HasValue) + { + var discussionMs = (MeetingDiscussionEndTime.Value - DateTime.UtcNow).TotalMilliseconds; + Log($" Diskuze do: {MeetingDiscussionEndTime.Value:HH:mm:ss} ({discussionMs:F0}ms)"); + } + OnMeetingStarted?.Invoke(); + break; + + case "VotingStarted": + Log("Hlasování začalo!"); + break; + + case "PlayerVoted": + var voteInfoPayload = evt.GetPayload(); + Log($"Hráč {voteInfoPayload?.VoterId} hlasoval"); + break; + + case "VotingClosed": + var votePayload = evt.GetPayload(); + InMeeting = false; + CurrentMeetingId = null; + MeetingLocation = null; + MeetingVotingEndTime = null; + MeetingDiscussionEndTime = null; + if (votePayload?.EjectedPlayerId != null) + { + Log($"Hráč {votePayload.EjectedPlayerId} byl VYHOZEN! (remíza: {votePayload.WasTie})"); + if (votePayload.EjectedPlayerId == ClientUuid) + { + WasEjected = true; + _myState = PlayerState.Dead; + Log("BYL JSEM VYHOZEN!"); + OnEjected?.Invoke(); + } + } + else + { + Log("Nikdo nebyl vyhozen (skip nebo remíza)"); + } + OnMeetingEnded?.Invoke(); + break; + + case "TaskCompleted": + var taskPayload = evt.GetPayload(); + if (taskPayload?.ClientUuid == ClientUuid) + { + TasksCompleted++; + CompletedTaskIds.Add(taskPayload.TaskId); + CurrentTaskId = null; + Log($"TASK DOKONČEN: {taskPayload.TaskId} (celkem: {TasksCompleted})"); + } + break; + + case "GameEnded": + var endPayload = evt.GetPayload(); + GameResult = endPayload?.Reason; + WinningFaction = endPayload?.WinningFaction; + Log($"=== HRA SKONČILA! Vítěz: {endPayload?.WinningFaction} - {endPayload?.Reason} ==="); + OnGameEnded?.Invoke(endPayload?.WinningFaction ?? "Unknown"); + break; + + // Sabotage events + case "SabotageStarted": + var sabStartPayload = evt.GetPayload(); + if (sabStartPayload != null) + { + SabotageActive = true; + CurrentSabotageId = sabStartPayload.SabotageId; + CurrentSabotageType = sabStartPayload.Type; + SabotageDeadline = sabStartPayload.Deadline; + RepairStations = sabStartPayload.RepairStations; + + Log($"⚠ SABOTÁŽ SPUŠTĚNA: {sabStartPayload.Type}!"); + if (sabStartPayload.Deadline.HasValue) + { + var remaining = (sabStartPayload.Deadline.Value - DateTime.UtcNow).TotalSeconds; + Log($" ⏱ DEADLINE: {remaining:F0}s - musíte opravit nebo prohrajete!"); + } + foreach (var station in sabStartPayload.RepairStations) + { + Log($" 📍 Stanice {station.Name}: {station.Location.Lat:F4},{station.Location.Lon:F4}"); + } + OnSabotageStarted?.Invoke(sabStartPayload.Type); + } + break; + + case "RepairStarted": + var repStartPayload = evt.GetPayload(); + if (repStartPayload?.PlayerId == ClientUuid) + { + IsRepairing = true; + RepairingStationId = repStartPayload.StationId; + Log($"🔧 Zahájil jsem opravu stanice {repStartPayload.StationId}"); + } + else + { + Log($"🔧 Hráč {repStartPayload?.PlayerId} opravuje {repStartPayload?.StationId}"); + } + break; + + case "RepairStopped": + var repStopPayload = evt.GetPayload(); + if (repStopPayload?.PlayerId == ClientUuid) + { + IsRepairing = false; + RepairingStationId = null; + Log($"❌ Oprava přerušena: {repStopPayload.StationId}"); + } + break; + + case "SabotageRepaired": + case "SabotageExpired": + var sabRepPayload = evt.GetPayload(); + if (sabRepPayload != null) + { + SabotageActive = false; + CurrentSabotageId = null; + CurrentSabotageType = null; + SabotageDeadline = null; + RepairStations.Clear(); + IsRepairing = false; + RepairingStationId = null; + SabotagesRepaired++; + + var repairers = sabRepPayload.RepairerIds.Count > 0 + ? string.Join(", ", sabRepPayload.RepairerIds) + : "auto-expire"; + Log($"✅ SABOTÁŽ OPRAVENA: {sabRepPayload.Type} (opravili: {repairers})"); + OnSabotageRepaired?.Invoke(sabRepPayload.Type); + } + break; + + case "SabotageMeltdown": + var meltdownPayload = evt.GetPayload(); + Log($"💥 MELTDOWN! Sabotáž nebyla opravena včas - Impostoři vyhráli!"); + OnMeltdown?.Invoke(); + break; + } + } + + #endregion + + #region Connection & Lobby + + public async Task ConnectAsync(string host, int port) + { + return await _client.ConnectAsync(host, port); + } + + public void Disconnect() + { + StopSimulation(); + _client.Disconnect(); + } + + public void Update() + { + _client.Update(); + } + + public void CreateLobby(Position center, int impostorCount = 1, int taskCount = 5, string? password = null) + { + _client.CreateLobby(center, impostorCount, taskCount, password); + } + + /// + /// Async wrapper for CreateLobby - waits for lobby to be created + /// + public async Task CreateLobbyAsync(string? password, Position center, double radius = 500, int impostorCount = 1, int taskCount = 5) + { + _client.CreateLobby(center, impostorCount, taskCount, password, radius); + + // Wait for lobby creation response + for (int i = 0; i < 50; i++) // 5 seconds timeout + { + Update(); + if (!string.IsNullOrEmpty(JoinCode)) + return true; + if (LastError != null && LastError.Contains("lobby")) + return false; + await Task.Delay(100); + } + return false; + } + + public void JoinLobby(string joinCode, string? password = null) + { + _client.JoinLobby(joinCode, password); + } + + /// + /// Async wrapper for JoinLobby - waits for join confirmation + /// + public async Task JoinLobbyAsync(string joinCode, string? password = null) + { + _client.JoinLobby(joinCode, password); + + // Wait for join response + for (int i = 0; i < 50; i++) + { + Update(); + if (!string.IsNullOrEmpty(LobbyId)) + return true; + if (LastError != null && LastError.Contains("join")) + return false; + await Task.Delay(100); + } + return false; + } + + public void LeaveLobby() + { + _client.LeaveLobby(); + } + + public void StartGame() + { + _client.StartGame(); + } + + /// + /// Async wrapper for StartGame - waits for game to start + /// + public async Task StartGameAsync() + { + _client.StartGame(); + + // Wait for game start + for (int i = 0; i < 50; i++) + { + Update(); + if (Role.HasValue) + return true; + await Task.Delay(100); + } + return false; + } + + #endregion + + #region Movement + + public void MoveTo(Position position) + { + _client.UpdatePosition(position); + } + + public void MoveTowards(Position target, double maxDistanceMeters) + { + var current = Position; + var distance = current.DistanceTo(target); + + if (distance <= maxDistanceMeters) + { + MoveTo(target); + } + else + { + var ratio = maxDistanceMeters / distance; + var newPos = new Position( + current.Lat + (target.Lat - current.Lat) * ratio, + current.Lon + (target.Lon - current.Lon) * ratio + ); + MoveTo(newPos); + } + } + + public Position GetRandomPositionNear(Position center, double radiusMeters) + { + var angle = _random.NextDouble() * 2 * Math.PI; + var distance = _random.NextDouble() * radiusMeters; + var lat = center.Lat + (distance / 111000) * Math.Cos(angle); + var lon = center.Lon + (distance / (111000 * Math.Cos(center.Lat * Math.PI / 180))) * Math.Sin(angle); + return new Position(lat, lon); + } + + #endregion + + #region Kill Actions (Impostor) + + public bool TryKill(string targetUuid) + { + if (!IsImpostor || !IsAlive) + { + Log("Nemohu zabíjet - nejsem živý impostor"); + return false; + } + + Log($">>> POKUS O ZABITÍ: {targetUuid}"); + _client.Kill(targetUuid); + TotalKills++; + return true; + } + + public string? FindKillTarget(double maxDistance = 5.0) + { + return _client.FindNearbyPlayer(maxDistance, aliveOnly: true); + } + + public bool TryKillNearby(double maxDistance = 5.0) + { + var target = FindKillTarget(maxDistance); + if (target != null) + { + return TryKill(target); + } + return false; + } + + #endregion + + #region Report & Meeting Actions + + public bool TryReportBody(string bodyId) + { + if (!IsAlive) + { + Log("Nemohu reportovat - jsem mrtvý"); + return false; + } + + Log($">>> REPORTUJI TĚLO: {bodyId}"); + _client.ReportBody(bodyId); + BodiesReported++; + return true; + } + + public Body? FindNearbyBody(double maxDistance = 5.0) + { + return _client.FindNearbyBody(maxDistance); + } + + public bool TryReportNearbyBody(double maxDistance = 5.0) + { + var body = FindNearbyBody(maxDistance); + if (body != null) + { + return TryReportBody(body.BodyId); + } + return false; + } + + public bool TryCallEmergencyMeeting() + { + if (!IsAlive) + { + Log("Nemohu svolat meeting - jsem mrtvý"); + return false; + } + + Log(">>> SVOLÁVÁM EMERGENCY MEETING!"); + _client.CallEmergencyMeeting(); + return true; + } + + #endregion + + #region Voting Actions + + public bool TryVote(string? targetUuid) + { + if (!IsAlive) + { + Log("Nemohu hlasovat - jsem mrtvý"); + return false; + } + + if (!InMeeting) + { + Log("Nemohu hlasovat - není meeting"); + return false; + } + + var voteTarget = targetUuid ?? "SKIP"; + Log($">>> HLASUJI PRO: {voteTarget}"); + _client.Vote(targetUuid); + VotesCast++; + HasVotedThisMeeting = true; + MeetingsAttended++; + return true; + } + + public bool TryVoteSkip() + { + return TryVote(null); + } + + public bool TryVoteRandom() + { + if (!InMeeting || !IsAlive) return false; + + // Pick a random alive player (or skip) + var alivePlayers = NearbyPlayers.Values + .Where(p => p.State == PlayerState.Alive && p.ClientUuid != ClientUuid) + .ToList(); + + if (alivePlayers.Count == 0 || _random.NextDouble() < 0.3) + { + return TryVoteSkip(); + } + + var target = alivePlayers[_random.Next(alivePlayers.Count)]; + return TryVote(target.ClientUuid); + } + + /// + /// Vote for the player with the most suspicion (for crew) or a random crew (for impostor) + /// + public bool TryVoteSmart() + { + if (!InMeeting || !IsAlive) return false; + + var alivePlayers = NearbyPlayers.Values + .Where(p => p.State == PlayerState.Alive && p.ClientUuid != ClientUuid) + .ToList(); + + if (alivePlayers.Count == 0) + { + return TryVoteSkip(); + } + + // Impostors vote randomly among non-impostors or skip + if (IsImpostor) + { + if (_random.NextDouble() < 0.5) + { + return TryVoteSkip(); + } + var target = alivePlayers[_random.Next(alivePlayers.Count)]; + return TryVote(target.ClientUuid); + } + + // Crew votes randomly for now (could be smarter with suspicion tracking) + if (_random.NextDouble() < 0.2) + { + return TryVoteSkip(); + } + var crewTarget = alivePlayers[_random.Next(alivePlayers.Count)]; + return TryVote(crewTarget.ClientUuid); + } + + #endregion + + #region Task Actions + + public bool TryCompleteTask(string taskId) + { + if (IsImpostor) + { + Log($"Nemohu dělat tasky - jsem impostor"); + return false; + } + + if (CompletedTaskIds.Contains(taskId)) + { + Log($"Task {taskId} již dokončen"); + return false; + } + + Log($">>> DOKONČUJI TASK: {taskId}"); + _client.CompleteTask(taskId); + return true; + } + + public GameTask? FindNearbyTask(double maxDistance = 5.0) + { + foreach (var task in MyTasks) + { + if (CompletedTaskIds.Contains(task.TaskId)) continue; + + if (Position.DistanceTo(task.Location) <= maxDistance) + { + return task; + } + } + return null; + } + + public GameTask? GetNextIncompleteTask() + { + return MyTasks.FirstOrDefault(t => !CompletedTaskIds.Contains(t.TaskId)); + } + + public int GetRemainingTaskCount() + { + return MyTasks.Count - CompletedTaskIds.Count; + } + + #endregion + + #region Sabotage Actions (Impostor) + + /// + /// Start a sabotage (impostor only) + /// + public bool TrySabotage(SabotageType sabotageType) + { + if (!IsImpostor) + { + Log("Nemohu sabotovat - nejsem impostor"); + return false; + } + + if (!IsAlive) + { + Log("Nemohu sabotovat - jsem mrtvý"); + return false; + } + + if (SabotageActive) + { + Log($"Nemohu sabotovat - již probíhá sabotáž: {CurrentSabotageType}"); + return false; + } + + if (InMeeting) + { + Log("Nemohu sabotovat - probíhá meeting"); + return false; + } + + Log($">>> SPOUŠTÍM SABOTÁŽ: {sabotageType}"); + _client.Send(new StartSabotage { SabotageType = sabotageType }); + SabotagesStarted++; + return true; + } + + /// + /// Start repairing at a repair station (crew or impostor can repair) + /// + public bool TryStartRepair(string stationId) + { + if (!IsAlive) + { + Log("Nemohu opravovat - jsem mrtvý"); + return false; + } + + if (!SabotageActive) + { + Log("Nemohu opravovat - není aktivní sabotáž"); + return false; + } + + if (IsRepairing) + { + Log($"Již opravuji stanici: {RepairingStationId}"); + return false; + } + + var station = RepairStations.FirstOrDefault(s => s.StationId == stationId); + if (station == null) + { + Log($"Opravná stanice {stationId} neexistuje"); + return false; + } + + var distance = Position.DistanceTo(station.Location); + if (distance > 5.0) + { + Log($"Opravná stanice {stationId} je příliš daleko: {distance:F1}m"); + return false; + } + + Log($">>> ZAČÍNÁM OPRAVU stanice: {stationId}"); + _client.Send(new ActivateRepairStation { StationId = stationId }); + return true; + } + + /// + /// Stop repairing current station + /// + public bool TryStopRepair() + { + if (!IsRepairing || RepairingStationId == null) + { + Log("Nejsem u opravné stanice"); + return false; + } + + Log($">>> UKONČUJI OPRAVU stanice: {RepairingStationId}"); + _client.Send(new DeactivateRepairStation { StationId = RepairingStationId }); + return true; + } + + /// + /// Find nearest repair station for current sabotage + /// + public RepairStationInfo? FindNearestRepairStation(double maxDistance = double.MaxValue) + { + if (!SabotageActive) return null; + + RepairStationInfo? nearest = null; + double nearestDist = maxDistance; + + foreach (var station in RepairStations) + { + if (station.IsRepaired) continue; + + var dist = Position.DistanceTo(station.Location); + if (dist < nearestDist) + { + nearestDist = dist; + nearest = station; + } + } + + return nearest; + } + + /// + /// Move to nearest repair station + /// + public bool MoveTowardsNearestRepairStation(double speed = 1.0) + { + var station = FindNearestRepairStation(); + if (station == null) return false; + + MoveTowards(station.Location, speed); + return true; + } + + /// + /// Check if at repair station and can start repair + /// + public bool IsAtRepairStation(string stationId, double maxDistance = 5.0) + { + var station = RepairStations.FirstOrDefault(s => s.StationId == stationId); + if (station == null) return false; + + return Position.DistanceTo(station.Location) <= maxDistance; + } + + /// + /// Automatic repair: find nearest station, move to it, and start repair + /// + public bool TryAutoRepair() + { + if (!SabotageActive) return false; + if (IsRepairing) return false; + + var station = FindNearestRepairStation(5.0); + if (station != null && !station.IsRepaired) + { + return TryStartRepair(station.StationId); + } + + return false; + } + + #endregion + + #region Autonomous Simulation + + public void StartSimulation() + { + if (_simulationTask != null) return; + + _cts = new CancellationTokenSource(); + _simulationTask = Task.Run(() => SimulationLoopAsync(_cts.Token)); + Log("Simulace spuštěna"); + } + + public void StopSimulation() + { + if (_cts == null) return; + + _cts.Cancel(); + try { _simulationTask?.Wait(1000); } catch { } + _simulationTask = null; + _cts = null; + Log("Simulace zastavena"); + } + + private async Task SimulationLoopAsync(CancellationToken ct) + { + var center = LobbyState?.PlayAreaCenter ?? new Position(50.0, 14.0); + var radius = LobbyState?.PlayAreaRadius ?? 500; + + MoveTo(center); + + while (!ct.IsCancellationRequested && !GameEnded) + { + try + { + Update(); + + // In meeting - handle voting + if (InMeeting) + { + // First move to meeting location + if (MeetingLocation.HasValue && IsAlive) + { + var meetLoc = MeetingLocation.Value; + var distToMeeting = Position.DistanceTo(meetLoc); + if (distToMeeting > 5) + { + MoveTowards(meetLoc, 20); + await Task.Delay(200, ct); + continue; + } + } + + if (IsAlive && !HasVotedThisMeeting) + { + // Wait a bit before voting (simulate discussion) + await Task.Delay(500 + _random.Next(1500), ct); + Update(); + + if (InMeeting && !HasVotedThisMeeting) + { + TryVoteSmart(); + } + } + + // Wait for meeting to end + await Task.Delay(300, ct); + continue; + } + + // Impostor logic + if (IsImpostor && IsAlive) + { + await ImpostorActionAsync(center, radius, ct); + } + // Alive Crew logic + else if (IsCrew && IsAlive) + { + await CrewActionAsync(center, radius, ct); + } + // Dead player (ghost) - can still do tasks + else if (IsCrew && IsDead) + { + await GhostTaskActionAsync(center, radius, ct); + } + // Dead impostor - just watch + else if (IsImpostor && IsDead) + { + await Task.Delay(1000, ct); + } + + await Task.Delay(300, ct); + } + catch (OperationCanceledException) + { + break; + } + catch (Exception ex) + { + Log($"Simulation error: {ex.Message}"); + await Task.Delay(1000, ct); + } + } + + Log("Simulace dokončena"); + } + + private async Task ImpostorActionAsync(Position center, double radius, CancellationToken ct) + { + // Try to kill nearby player + var target = FindKillTarget(8.0); + if (target != null) + { + // Higher chance to kill if alone with victim + var nearbyCount = NearbyPlayers.Values.Count(p => + p.State == PlayerState.Alive && + p.ClientUuid != ClientUuid && + Position.DistanceTo(p.Position) < 20); + + var killChance = nearbyCount <= 1 ? 0.9 : 0.3; + + if (_random.NextDouble() < killChance) + { + TryKill(target); + await Task.Delay(300, ct); + + // Move away from body + var escapePos = GetRandomPositionNear(Position, 30); + MoveTowards(escapePos, 15); + await Task.Delay(500, ct); + return; + } + } + + // Wander around looking for isolated targets + var wanderPos = GetRandomPositionNear(center, radius * 0.6); + MoveTowards(wanderPos, 5); + } + + private async Task CrewActionAsync(Position center, double radius, CancellationToken ct) + { + // Priority 1: Report nearby bodies + var body = FindNearbyBody(8.0); + if (body != null) + { + Log($"NAŠEL JSEM TĚLO: {body.BodyId}"); + // Move closer if needed + if (Position.DistanceTo(body.Location) > 3) + { + MoveTowards(body.Location, 3); + await Task.Delay(200, ct); + } + TryReportBody(body.BodyId); + await Task.Delay(500, ct); + return; + } + + // Priority 2: Do tasks (instant completion) + var nearbyTask = FindNearbyTask(5.0); + if (nearbyTask != null) + { + TryCompleteTask(nearbyTask.TaskId); + await Task.Delay(500, ct); + return; + } + + // Priority 3: Move to next task + var nextTask = GetNextIncompleteTask(); + if (nextTask != null) + { + MoveTowards(nextTask.Location, 5); + } + else + { + // All tasks done, wander + var wanderPos = GetRandomPositionNear(center, radius * 0.5); + MoveTowards(wanderPos, 3); + } + } + + private async Task GhostTaskActionAsync(Position center, double radius, CancellationToken ct) + { + // Ghosts can complete tasks faster (no danger) + var nearbyTask = FindNearbyTask(10.0); + if (nearbyTask != null) + { + TryCompleteTask(nearbyTask.TaskId); + await Task.Delay(100, ct); + return; + } + + // Move to next task + var nextTask = GetNextIncompleteTask(); + if (nextTask != null) + { + // Ghosts move much faster + MoveTowards(nextTask.Location, 20); + } + } + + #endregion + + #region Logging + + private void Log(string message) + { + OnLog?.Invoke(message); + } + + private void LogError(string message) + { + LastError = message; + OnError?.Invoke(message); + Log($"ERROR: {message}"); + } + + #endregion + + #region Stats + + public string GetStats() + { + return $"[{DisplayName}] Role={Role}, State={State}, Kills={TotalKills}, Tasks={TasksCompleted}/{MyTasks.Count}, " + + $"Reports={BodiesReported}, Votes={VotesCast}, Meetings={MeetingsAttended}, " + + $"Killed={WasKilled}, Ejected={WasEjected}"; + } + + public void PrintDetailedStats() + { + Log("========== DETAILED STATS =========="); + Log($" Display Name: {DisplayName}"); + Log($" Client UUID: {ClientUuid}"); + Log($" Role: {Role}"); + Log($" Final State: {State}"); + Log($" --- Actions ---"); + Log($" Kills Attempted: {TotalKills}"); + Log($" Tasks Completed: {TasksCompleted}/{MyTasks.Count}"); + Log($" Bodies Reported: {BodiesReported}"); + Log($" Votes Cast: {VotesCast}"); + Log($" Meetings Attended: {MeetingsAttended}"); + Log($" --- Fate ---"); + Log($" Was Killed: {WasKilled}"); + Log($" Was Ejected: {WasEjected}"); + Log($" --- Game Result ---"); + Log($" Winning Faction: {WinningFaction}"); + Log($" Result: {GameResult}"); + Log("====================================="); + } + + #endregion + + public void Dispose() + { + StopSimulation(); + _client.Dispose(); + } + + #region Map Data Access + + /// + /// Get the map data payload from the current lobby state (if available) + /// + public MapDataPayload? MapData => _client.CurrentLobbyState?.MapData; + + /// + /// Check if map data is available for the current lobby + /// + public bool HasMapData => MapData != null; + + /// + /// Check if map data loading is complete (true if loaded or Overpass disabled) + /// + public bool IsMapDataReady => _client.CurrentLobbyState?.MapDataReady ?? false; + + /// + /// Get the play area center from lobby state + /// + public Position? PlayAreaCenter => _client.CurrentLobbyState?.PlayAreaCenter; + + /// + /// Get the play area radius from lobby state + /// + public double PlayAreaRadius => _client.CurrentLobbyState?.PlayAreaRadius ?? 500; + + #endregion +} + +/// +/// Comprehensive test suite for Overpass API and reachability testing +/// +public class OverpassApiTests +{ + private readonly string _serverHost; + private readonly int _serverPort; + private readonly Action? _logger; + + public OverpassApiTests(string serverHost = "localhost", int serverPort = 7777, Action? logger = null) + { + _serverHost = serverHost; + _serverPort = serverPort; + _logger = logger; + } + + private void Log(string message) + { + _logger?.Invoke(message); + Console.WriteLine(message); + } + + /// + /// Run all Overpass API tests + /// + public async Task RunAllTestsAsync(Position testCenter, double testRadius = 500) + { + var results = new OverpassTestResults(); + + Log("═══════════════════════════════════════════════════════════════"); + Log(" OVERPASS API & REACHABILITY TEST SUITE"); + Log("═══════════════════════════════════════════════════════════════"); + Log($"Test Center: {testCenter.Lat:F6}, {testCenter.Lon:F6}"); + Log($"Test Radius: {testRadius}m"); + Log(""); + + // Test 1: Create lobby and verify map data is fetched + Log("TEST 1: Map Data Fetch on Lobby Creation"); + Log("─────────────────────────────────────────"); + results.MapDataFetchTest = await TestMapDataFetchAsync(testCenter, testRadius); + LogTestResult("Map Data Fetch", results.MapDataFetchTest); + await Task.Delay(1000); // Delay between tests + + // Test 2: Verify map data structure + Log(""); + Log("TEST 2: Map Data Structure Validation"); + Log("─────────────────────────────────────────"); + results.MapDataStructureTest = await TestMapDataStructureAsync(testCenter, testRadius); + LogTestResult("Map Data Structure", results.MapDataStructureTest); + await Task.Delay(1000); // Delay between tests + + // Test 3: Verify task positions are on reachable paths + Log(""); + Log("TEST 3: Task Position Reachability"); + Log("─────────────────────────────────────────"); + results.TaskReachabilityTest = await TestTaskReachabilityAsync(testCenter, testRadius); + LogTestResult("Task Reachability", results.TaskReachabilityTest); + await Task.Delay(1000); // Delay between tests + + // Test 4: Verify repair station positions are reachable + Log(""); + Log("TEST 4: Repair Station Reachability (Sabotage)"); + Log("─────────────────────────────────────────"); + results.RepairStationReachabilityTest = await TestRepairStationReachabilityAsync(testCenter, testRadius); + LogTestResult("Repair Station Reachability", results.RepairStationReachabilityTest); + await Task.Delay(1000); // Delay between tests + + // Test 5: Verify positions are within play area + Log(""); + Log("TEST 5: Play Area Boundary Validation"); + Log("─────────────────────────────────────────"); + results.PlayAreaBoundaryTest = await TestPlayAreaBoundaryAsync(testCenter, testRadius); + LogTestResult("Play Area Boundary", results.PlayAreaBoundaryTest); + await Task.Delay(1000); // Delay between tests + + // Test 6: Test multiple lobby creations for consistency + Log(""); + Log("TEST 6: Map Data Consistency Across Lobbies"); + Log("─────────────────────────────────────────"); + results.ConsistencyTest = await TestMapDataConsistencyAsync(testCenter, testRadius); + LogTestResult("Map Data Consistency", results.ConsistencyTest); + + // Summary + Log(""); + Log("═══════════════════════════════════════════════════════════════"); + Log(" TEST SUMMARY"); + Log("═══════════════════════════════════════════════════════════════"); + results.PrintSummary(Log); + + return results; + } + + private void LogTestResult(string testName, TestResult result) + { + var status = result.Passed ? "✓ PASS" : "✗ FAIL"; + Log($" [{status}] {testName}"); + if (!string.IsNullOrEmpty(result.Details)) + { + foreach (var line in result.Details.Split('\n')) + { + Log($" {line}"); + } + } + if (!result.Passed && !string.IsNullOrEmpty(result.Error)) + { + Log($" ERROR: {result.Error}"); + } + } + + /// + /// Wait for map data to be loaded with polling and timeout + /// Note: Map data is now fetched when game starts, not on lobby creation + /// + private async Task WaitForMapDataAsync(SimulatorClient client, SimulatorClient? otherClient, int timeoutMs = 10000, int pollIntervalMs = 500) + { + var sw = System.Diagnostics.Stopwatch.StartNew(); + + while (sw.ElapsedMilliseconds < timeoutMs) + { + // Update client to receive any pending messages + client.Update(); + otherClient?.Update(); + + // Check if client has map data loaded + if (client.HasMapData) + { + Log($" Map data loaded after {sw.ElapsedMilliseconds}ms"); + return true; + } + + // Also check if MapDataReady indicates no data expected (Overpass disabled) + if (client.IsMapDataReady && !client.HasMapData) + { + Log($" Map data not available (Overpass may be disabled)"); + return false; + } + + await Task.Delay(pollIntervalMs); + } + + Log($" Timeout waiting for map data ({timeoutMs}ms)"); + return false; + } + + /// + /// Wait for game to start after map data is received (Loading phase complete) + /// + private async Task WaitForGameStartAfterMapDataAsync(SimulatorClient owner, SimulatorClient player, int timeoutMs = 20000, int pollIntervalMs = 500) + { + var sw = System.Diagnostics.Stopwatch.StartNew(); + + while (sw.ElapsedMilliseconds < timeoutMs) + { + owner.Update(); + player.Update(); + + // Game is fully started when phase is Playing and roles are assigned + if (owner.GamePhase == "Playing" && owner.Role.HasValue) + { + Log($" Game started after {sw.ElapsedMilliseconds}ms"); + return true; + } + + await Task.Delay(pollIntervalMs); + } + + Log($" Timeout waiting for game start ({timeoutMs}ms) - Phase: {owner.GamePhase}"); + return false; + } + + /// + /// Start a game and wait for map data to be loaded + /// The new flow is: StartGame -> Loading phase -> MapData fetched -> All confirm -> Playing + /// + private async Task StartGameAndWaitForMapDataAsync(SimulatorClient owner, SimulatorClient player, int timeoutMs = 25000) + { + Log(" Starting game (this will trigger map data fetch)..."); + owner.StartGame(); + + // Wait for map data first (update both clients so both send confirmations) + if (!await WaitForMapDataAsync(owner, player, timeoutMs)) + { + return false; + } + + // Ensure both clients update to send MapDataReceived confirmation + for (int i = 0; i < 10; i++) + { + owner.Update(); + player.Update(); + await Task.Delay(100); + } + + // Wait for game to actually start (after all confirmations) + return await WaitForGameStartAfterMapDataAsync(owner, player, 10000); + } + + private async Task TestMapDataFetchAsync(Position center, double radius) + { + var result = new TestResult { TestName = "MapDataFetch" }; + SimulatorClient? owner = null; + SimulatorClient? player = null; + + try + { + // Create owner and player (need 2 players to start game) + owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "MapOwner"); + player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "MapPlayer"); + + // Connect owner + Log(" Connecting owner to server..."); + if (!await owner.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = $"Failed to connect owner to server at {_serverHost}:{_serverPort}"; + return result; + } + + // Create lobby with specific center + Log($" Creating lobby at {center.Lat:F6}, {center.Lon:F6} with radius {radius}m..."); + if (!await owner.CreateLobbyAsync(null, center, radius)) + { + result.Error = $"Failed to create lobby: {owner.LastError ?? "Unknown error"}"; + return result; + } + Log($" Lobby created: {owner.JoinCode}"); + + // Connect player + Log(" Connecting player to server..."); + if (!await player.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = "Failed to connect player"; + return result; + } + + // Join player to lobby + if (!await player.JoinLobbyAsync(owner.JoinCode!)) + { + result.Error = "Failed to join player to lobby"; + return result; + } + + // Start game - this triggers map data fetch + Log(" Starting game (triggers Overpass API fetch)..."); + if (!await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + result.Error = "Timeout waiting for map data (Overpass API may be slow or unavailable)"; + return result; + } + + if (owner.HasMapData) + { + var mapData = owner.MapData!; + var buildings = mapData.GetBuildings(); + var pathways = mapData.GetPathways(); + var areas = mapData.GetAreas(); + var pois = mapData.GetPOIs(); + result.Passed = true; + result.Details = $"Buildings: {buildings.Count}\n" + + $"Pathways: {pathways.Count}\n" + + $"Areas: {areas.Count}\n" + + $"POIs: {pois.Count}"; + } + else + { + result.Error = "Map data was not received after game start"; + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + owner?.Dispose(); + player?.Dispose(); + } + + return result; + } + + private async Task TestMapDataStructureAsync(Position center, double radius) + { + var result = new TestResult { TestName = "MapDataStructure" }; + SimulatorClient? owner = null; + SimulatorClient? player = null; + + try + { + owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "StructOwner"); + player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "StructPlayer"); + + if (!await owner.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = "Failed to connect owner"; + return result; + } + + if (!await owner.CreateLobbyAsync(null, center, radius)) + { + result.Error = "Failed to create lobby"; + return result; + } + + if (!await player.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = "Failed to connect player"; + return result; + } + + if (!await player.JoinLobbyAsync(owner.JoinCode!)) + { + result.Error = "Failed to join player to lobby"; + return result; + } + + // Start game to trigger map data fetch + Log(" Starting game (triggers Overpass API fetch)..."); + if (!await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + result.Error = "Timeout waiting for map data"; + return result; + } + + if (!owner.HasMapData) + { + result.Error = "No map data available"; + return result; + } + + var mapData = owner.MapData!; + var buildings = mapData.GetBuildings(); + var pathways = mapData.GetPathways(); + var areas = mapData.GetAreas(); + var pois = mapData.GetPOIs(); + var issues = new List(); + var stats = new List(); + + // Check buildings have valid outlines + int validBuildings = 0; + foreach (var building in buildings) + { + if (building.Outline == null || building.Outline.Count < 3) + { + issues.Add($"Building {building.Id}: Invalid outline (< 3 points)"); + } + else + { + validBuildings++; + } + } + stats.Add($"Valid buildings: {validBuildings}/{buildings.Count}"); + + // Check pathways have valid points + int validPathways = 0; + foreach (var pathway in pathways) + { + if (pathway.Points == null || pathway.Points.Count < 2) + { + issues.Add($"Pathway {pathway.Id}: Invalid points (< 2)"); + } + else + { + validPathways++; + } + } + stats.Add($"Valid pathways: {validPathways}/{pathways.Count}"); + + // Check areas + int validAreas = 0; + foreach (var area in areas) + { + if (area.Outline == null || area.Outline.Count < 3) + { + issues.Add($"Area {area.Id}: Invalid outline"); + } + else + { + validAreas++; + } + } + stats.Add($"Valid areas: {validAreas}/{areas.Count}"); + + // Check POIs have valid positions + int validPOIs = 0; + foreach (var poi in pois) + { + if (poi.Location.Lat != 0 || poi.Location.Lon != 0) + { + validPOIs++; + } + } + stats.Add($"Valid POIs: {validPOIs}/{pois.Count}"); + + result.Passed = issues.Count == 0; + result.Details = string.Join("\n", stats); + if (issues.Count > 0) + { + result.Error = string.Join("; ", issues.Take(5)); + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + owner?.Dispose(); + player?.Dispose(); + } + + return result; + } + + private async Task TestTaskReachabilityAsync(Position center, double radius) + { + var result = new TestResult { TestName = "TaskReachability" }; + SimulatorClient? owner = null; + SimulatorClient? player = null; + + try + { + // Create lobby owner + owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "TaskOwner"); + player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "TaskPlayer"); + + if (!await owner.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = "Owner failed to connect"; + return result; + } + + if (!await owner.CreateLobbyAsync(null, center, radius, 1, 5)) + { + result.Error = "Failed to create lobby"; + return result; + } + + var joinCode = owner.JoinCode; + + if (!await player.ConnectAsync(_serverHost, _serverPort)) + { + result.Error = "Player failed to connect"; + return result; + } + + if (!await player.JoinLobbyAsync(joinCode!)) + { + result.Error = "Player failed to join"; + return result; + } + + // Start game - this triggers map data fetch + Log(" Starting game (triggers Overpass API fetch)..."); + if (!await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + result.Error = "Timeout waiting for game to start"; + return result; + } + + // Check task positions + var mapData = owner.MapData; + var tasks = owner.MyTasks.Count > 0 ? owner.MyTasks : player.MyTasks; + + if (tasks.Count == 0) + { + result.Error = "No tasks were assigned"; + return result; + } + + int tasksInPlayArea = 0; + int tasksOnPathways = 0; + var details = new List(); + + foreach (var task in tasks) + { + var distFromCenter = task.Location.DistanceTo(center); + bool inPlayArea = distFromCenter <= radius; + if (inPlayArea) tasksInPlayArea++; + + // Check if task is near any pathway + bool nearPathway = false; + if (mapData != null) + { + var pathways = mapData.GetPathways(); + foreach (var pathway in pathways) + { + foreach (var point in pathway.Points) + { + if (task.Location.DistanceTo(point) < 15) // Within 15m of pathway + { + nearPathway = true; + break; + } + } + if (nearPathway) break; + } + } + if (nearPathway) tasksOnPathways++; + + details.Add($"{task.Name}: {distFromCenter:F0}m from center, {(inPlayArea ? "in" : "OUT OF")} play area, {(nearPathway ? "near" : "NOT NEAR")} pathway"); + } + + result.Passed = tasksInPlayArea == tasks.Count; + result.Details = $"Tasks in play area: {tasksInPlayArea}/{tasks.Count}\n" + + $"Tasks near pathways: {tasksOnPathways}/{tasks.Count}\n" + + string.Join("\n", details.Take(3)); + + if (tasksInPlayArea < tasks.Count) + { + result.Error = $"{tasks.Count - tasksInPlayArea} tasks are outside play area!"; + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + owner?.Dispose(); + player?.Dispose(); + } + + return result; + } + + private async Task TestRepairStationReachabilityAsync(Position center, double radius) + { + var result = new TestResult { TestName = "RepairStationReachability" }; + SimulatorClient? owner = null; + SimulatorClient? player = null; + + try + { + // Create lobby with 2 players + owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "SabOwner"); + player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "SabPlayer"); + + if (!await owner.ConnectAsync(_serverHost, _serverPort)) { result.Error = "Owner connect failed"; return result; } + if (!await owner.CreateLobbyAsync(null, center, radius, 1, 3)) { result.Error = "Create lobby failed"; return result; } + + var joinCode = owner.JoinCode; + + if (!await player.ConnectAsync(_serverHost, _serverPort)) { result.Error = "Player connect failed"; return result; } + if (!await player.JoinLobbyAsync(joinCode!)) { result.Error = "Join lobby failed"; return result; } + + // Start game - this triggers map data fetch + Log(" Starting game (triggers Overpass API fetch)..."); + if (!await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + result.Error = "Timeout waiting for game to start"; + return result; + } + + // Find impostor and trigger sabotage + var impostor = owner.IsImpostor ? owner : (player.IsImpostor ? player : null); + if (impostor == null) + { + result.Error = "No impostor found"; + return result; + } + + // Wait for sabotage cooldown + await Task.Delay(1000); + + // Try to start a sabotage + impostor.TrySabotage(SabotageType.CriticalMeltdown); + await Task.Delay(1500); + + // Check repair station positions + var crew = owner.IsCrew ? owner : player; + var stations = crew.RepairStations; + + if (stations.Count == 0) + { + // Sabotage might have been blocked, try CommsBlackout + impostor.TrySabotage(SabotageType.CommsBlackout); + await Task.Delay(1000); + stations = crew.RepairStations; + } + + if (stations.Count == 0) + { + result.Passed = true; // No sabotage was possible, but that's OK + result.Details = "Sabotage cooldown active, skipping repair station test"; + return result; + } + + int stationsInPlayArea = 0; + var details = new List(); + + foreach (var station in stations) + { + var distFromCenter = station.Location.DistanceTo(center); + bool inPlayArea = distFromCenter <= radius; + if (inPlayArea) stationsInPlayArea++; + + details.Add($"{station.Name}: {distFromCenter:F0}m from center ({(inPlayArea ? "OK" : "OUT!")})"); + } + + result.Passed = stationsInPlayArea == stations.Count; + result.Details = $"Stations in play area: {stationsInPlayArea}/{stations.Count}\n" + + string.Join("\n", details); + + if (stationsInPlayArea < stations.Count) + { + result.Error = $"{stations.Count - stationsInPlayArea} repair stations are outside play area!"; + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + owner?.Dispose(); + player?.Dispose(); + } + + return result; + } + + private async Task TestPlayAreaBoundaryAsync(Position center, double radius) + { + var result = new TestResult { TestName = "PlayAreaBoundary" }; + SimulatorClient? owner = null; + SimulatorClient? player = null; + + try + { + owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "BoundOwner"); + player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), "BoundPlayer"); + + if (!await owner.ConnectAsync(_serverHost, _serverPort)) { result.Error = "Connect failed"; return result; } + if (!await owner.CreateLobbyAsync(null, center, radius)) { result.Error = "Create lobby failed"; return result; } + + if (!await player.ConnectAsync(_serverHost, _serverPort)) { result.Error = "Player connect failed"; return result; } + if (!await player.JoinLobbyAsync(owner.JoinCode!)) { result.Error = "Join lobby failed"; return result; } + + // Start game - this triggers map data fetch + Log(" Starting game (triggers Overpass API fetch)..."); + if (!await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + result.Passed = true; + result.Details = "No map data to validate (Overpass might be disabled or timed out)"; + return result; + } + + if (!owner.HasMapData) + { + result.Passed = true; + result.Details = "No map data to validate (Overpass might be disabled)"; + return result; + } + + var mapData = owner.MapData!; + var pathways = mapData.GetPathways(); + var pois = mapData.GetPOIs(); + int outsidePathwayPoints = 0; + int totalPathwayPoints = 0; + int outsidePOIs = 0; + int totalPOIs = pois.Count; + + // Check all pathway points + foreach (var pathway in pathways) + { + foreach (var point in pathway.Points) + { + totalPathwayPoints++; + if (point.DistanceTo(center) > radius * 1.1) // Allow 10% margin + { + outsidePathwayPoints++; + } + } + } + + // Check all POIs + foreach (var poi in pois) + { + if (poi.Location.DistanceTo(center) > radius * 1.1) + { + outsidePOIs++; + } + } + + // Allow some points outside (Overpass query might include slightly outside data) + double pathwayOutsidePercent = totalPathwayPoints > 0 ? (outsidePathwayPoints * 100.0 / totalPathwayPoints) : 0; + double poiOutsidePercent = totalPOIs > 0 ? (outsidePOIs * 100.0 / totalPOIs) : 0; + + result.Passed = pathwayOutsidePercent < 20 && poiOutsidePercent < 20; + result.Details = $"Pathway points outside boundary: {outsidePathwayPoints}/{totalPathwayPoints} ({pathwayOutsidePercent:F1}%)\n" + + $"POIs outside boundary: {outsidePOIs}/{totalPOIs} ({poiOutsidePercent:F1}%)"; + + if (!result.Passed) + { + result.Error = "Too many map elements outside play area boundary"; + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + owner?.Dispose(); + player?.Dispose(); + } + + return result; + } + + private async Task TestMapDataConsistencyAsync(Position center, double radius) + { + var result = new TestResult { TestName = "MapDataConsistency" }; + var owners = new List(); + var players = new List(); + + try + { + // Create 3 lobbies at the same location and compare map data + var mapDataResults = new List<(int buildings, int pathways, int pois)>(); + + for (int i = 0; i < 3; i++) + { + Log($" Creating lobby {i + 1}/3..."); + var owner = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), $"ConsOwner{i}"); + var player = new SimulatorClient(Guid.NewGuid().ToString("N").Substring(0, 8), $"ConsPlayer{i}"); + owners.Add(owner); + players.Add(player); + + if (!await owner.ConnectAsync(_serverHost, _serverPort)) continue; + if (!await owner.CreateLobbyAsync(null, center, radius)) continue; + + if (!await player.ConnectAsync(_serverHost, _serverPort)) continue; + if (!await player.JoinLobbyAsync(owner.JoinCode!)) continue; + + // Start game to trigger map data fetch + if (await StartGameAndWaitForMapDataAsync(owner, player, timeoutMs: 25000)) + { + if (owner.HasMapData) + { + var md = owner.MapData!; + mapDataResults.Add((md.GetBuildings().Count, md.GetPathways().Count, md.GetPOIs().Count)); + } + } + + await Task.Delay(300); + } + + if (mapDataResults.Count < 2) + { + result.Passed = true; + result.Details = "Not enough successful map data fetches to compare consistency"; + return result; + } + + // Check if all results are the same (should be cached) + var first = mapDataResults[0]; + bool allSame = mapDataResults.All(r => r == first); + + result.Passed = allSame; + result.Details = string.Join("\n", mapDataResults.Select((r, i) => + $"Lobby {i + 1}: {r.buildings} buildings, {r.pathways} pathways, {r.pois} POIs")); + + if (!allSame) + { + result.Error = "Map data varies between lobbies at same location (caching issue?)"; + } + } + catch (Exception ex) + { + result.Error = ex.Message; + } + finally + { + foreach (var o in owners) o?.Dispose(); + foreach (var p in players) p?.Dispose(); + } + + return result; + } +} + +/// +/// Results from a single test +/// +public class TestResult +{ + public string TestName { get; set; } = ""; + public bool Passed { get; set; } + public string? Details { get; set; } + public string? Error { get; set; } +} + +/// +/// Aggregate results from all Overpass tests +/// +public class OverpassTestResults +{ + public TestResult MapDataFetchTest { get; set; } = new(); + public TestResult MapDataStructureTest { get; set; } = new(); + public TestResult TaskReachabilityTest { get; set; } = new(); + public TestResult RepairStationReachabilityTest { get; set; } = new(); + public TestResult PlayAreaBoundaryTest { get; set; } = new(); + public TestResult ConsistencyTest { get; set; } = new(); + + public int TotalTests => 6; + public int PassedTests => new[] { MapDataFetchTest, MapDataStructureTest, TaskReachabilityTest, + RepairStationReachabilityTest, PlayAreaBoundaryTest, ConsistencyTest }.Count(t => t.Passed); + public int FailedTests => TotalTests - PassedTests; + public bool AllPassed => PassedTests == TotalTests; + + public void PrintSummary(Action log) + { + log($" Total Tests: {TotalTests}"); + log($" Passed: {PassedTests}"); + log($" Failed: {FailedTests}"); + log(""); + + if (AllPassed) + { + log(" ✓ ALL OVERPASS TESTS PASSED!"); + } + else + { + log(" ✗ SOME TESTS FAILED:"); + if (!MapDataFetchTest.Passed) log($" - Map Data Fetch: {MapDataFetchTest.Error}"); + if (!MapDataStructureTest.Passed) log($" - Map Data Structure: {MapDataStructureTest.Error}"); + if (!TaskReachabilityTest.Passed) log($" - Task Reachability: {TaskReachabilityTest.Error}"); + if (!RepairStationReachabilityTest.Passed) log($" - Repair Station Reachability: {RepairStationReachabilityTest.Error}"); + if (!PlayAreaBoundaryTest.Passed) log($" - Play Area Boundary: {PlayAreaBoundaryTest.Error}"); + if (!ConsistencyTest.Passed) log($" - Consistency: {ConsistencyTest.Error}"); + } + } +} +} diff --git a/Assets/ClientSDK/SimulatorClient.cs.meta b/Assets/ClientSDK/SimulatorClient.cs.meta new file mode 100644 index 0000000..33e1383 --- /dev/null +++ b/Assets/ClientSDK/SimulatorClient.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 80ef0979df5d1fe489225f3e5edadc5c \ No newline at end of file diff --git a/Assets/ClientSDK/bin.meta b/Assets/ClientSDK/bin.meta new file mode 100644 index 0000000..595f334 --- /dev/null +++ b/Assets/ClientSDK/bin.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3a4035bdb812fee4f96cb1aa1b24c999 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ClientSDK/obj.meta b/Assets/ClientSDK/obj.meta new file mode 100644 index 0000000..c1a843e --- /dev/null +++ b/Assets/ClientSDK/obj.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 131d9de257c8edc49991d792c6e702f6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/GameManager/ITask.cs b/Assets/GameManager/ITask.cs index 2df5907..b1afee0 100644 --- a/Assets/GameManager/ITask.cs +++ b/Assets/GameManager/ITask.cs @@ -1,7 +1,8 @@ -//using GeoSus.Client; +using GeoSus.Client; using System; using UnityEngine; + public enum TaskType { Task //TODO: Typy úkolù @@ -14,7 +15,7 @@ public interface ITask public string TaskID { get; } // Unikátní ID úkolu pro server public TaskType TaskType { get; } // Typ úkolu public string TaskName { get; } // Viditelný název úkolu - public (double, double) TaskLocation { get; } // Polohy na mapì + public Position TaskLocation { get; } // Polohy na mapì public bool IsCompleted { get; } // Stav dokonèení úkolu void Initialize(Action onCompleted); // Vytvoøení tasku + naètení postupu diff --git a/Assets/Scenes/minihra cistici dira.unity b/Assets/Scenes/minihra cistici dira.unity index b4085f5..b232237 100644 --- a/Assets/Scenes/minihra cistici dira.unity +++ b/Assets/Scenes/minihra cistici dira.unity @@ -133,7 +133,7 @@ GameObject: - component: {fileID: 6503151} - component: {fileID: 6503150} m_Layer: 0 - m_Name: Cylinder + m_Name: SpecialHole m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -151,11 +151,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ca7423fcca5f83249a2574cd84b7f806, type: 3} m_Name: m_EditorClassIdentifier: Assembly-CSharp::Hole - catchRadius: 1 + catchRadius: 10 pullForce: 4 - hasMovement: 0 - moveSpeed: 2 - moveRange: {x: 1.5, y: 0} glowRenderer: {fileID: 0} --- !u!136 &6503151 CapsuleCollider: @@ -172,14 +169,14 @@ CapsuleCollider: serializedVersion: 2 m_Bits: 0 m_LayerOverridePriority: 0 - m_IsTrigger: 0 + m_IsTrigger: 1 m_ProvidesContacts: 0 - m_Enabled: 0 + m_Enabled: 1 serializedVersion: 2 m_Radius: 0.5000001 - m_Height: 2 + m_Height: 50 m_Direction: 1 - m_Center: {x: 0.000000059604645, y: 0, z: -0.00000008940697} + m_Center: {x: 0.00000006274173, y: 7.2227635, z: -0.0000000941126} --- !u!23 &6503152 MeshRenderer: m_ObjectHideFlags: 0 @@ -1600,179 +1597,38 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 50, y: 180, z: 0} ---- !u!1 &727261126 +--- !u!1 &727261126 stripped GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} + m_CorrespondingSourceObject: {fileID: 4463416604269783915, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + m_PrefabInstance: {fileID: 9074197596846296259} m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 727261130} - - component: {fileID: 727261129} - - component: {fileID: 727261128} - - component: {fileID: 727261133} - - component: {fileID: 727261132} - - component: {fileID: 727261131} - m_Layer: 0 - m_Name: Sphere - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!23 &727261128 -MeshRenderer: +--- !u!54 &727261133 +Rigidbody: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 727261126} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RayTracingAccelStructBuildFlagsOverride: 0 - m_RayTracingAccelStructBuildFlags: 1 - m_SmallMeshCulling: 1 - m_ForceMeshLod: -1 - m_MeshLodSelectionBias: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: 1a424fd36745ea14682570c13681319e, type: 2} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_GlobalIlluminationMeshLod: 0 - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} ---- !u!33 &727261129 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 727261126} - m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &727261130 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 727261126} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -13.138, y: -2.91, z: -0.63} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!50 &727261131 -Rigidbody2D: serializedVersion: 5 - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 727261126} - m_BodyType: 0 - m_Simulated: 1 - m_UseFullKinematicContacts: 0 - m_UseAutoMass: 0 m_Mass: 1 m_LinearDamping: 0 m_AngularDamping: 0.05 - m_GravityScale: 1 - m_Material: {fileID: 0} + m_CenterOfMass: {x: 0, y: 0, z: 0} + m_InertiaTensor: {x: 1, y: 1, z: 1} + m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} m_IncludeLayers: serializedVersion: 2 m_Bits: 0 m_ExcludeLayers: serializedVersion: 2 m_Bits: 0 + m_ImplicitCom: 1 + m_ImplicitTensor: 1 + m_UseGravity: 1 + m_IsKinematic: 0 m_Interpolate: 0 - m_SleepingMode: 1 - m_CollisionDetection: 0 m_Constraints: 0 ---- !u!114 &727261132 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 727261126} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: fb5157d7cd78450439c40cd6f5afe6ac, type: 3} - m_Name: - m_EditorClassIdentifier: Assembly-CSharp::DraggableObject - dragSmoothness: 15 - spriteRenderer: {fileID: 0} - normalColor: {r: 1, g: 1, b: 1, a: 1} - dragColor: {r: 1, g: 1, b: 0.5, a: 1} - scaleOnDrag: 1.15 ---- !u!70 &727261133 -CapsuleCollider2D: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 727261126} - m_Enabled: 1 - serializedVersion: 3 - m_Density: 1 - m_Material: {fileID: 0} - m_IncludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_ExcludeLayers: - serializedVersion: 2 - m_Bits: 0 - m_LayerOverridePriority: 0 - m_ForceSendLayers: - serializedVersion: 2 - m_Bits: 4294967295 - m_ForceReceiveLayers: - serializedVersion: 2 - m_Bits: 4294967295 - m_ContactCaptureLayers: - serializedVersion: 2 - m_Bits: 4294967295 - m_CallbackLayers: - serializedVersion: 2 - m_Bits: 4294967295 - m_IsTrigger: 1 - m_UsedByEffector: 0 - m_CompositeOperation: 0 - m_CompositeOrder: 0 - m_Offset: {x: 0, y: 0} - m_Size: {x: 1, y: 1} - m_Direction: 0 + m_CollisionDetection: 0 --- !u!1 &776138489 GameObject: m_ObjectHideFlags: 0 @@ -3068,7 +2924,7 @@ GameObject: - component: {fileID: 1190939575} - component: {fileID: 1190939574} m_Layer: 0 - m_Name: GameObject + m_Name: Spawner m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -3086,9 +2942,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 071f79f81861c2741a92d8b044457d94, type: 3} m_Name: m_EditorClassIdentifier: Assembly-CSharp::ObjectSpawner + spawnedHoles: + - {fileID: 6503149} + - {fileID: 1386843746} objectPrefabs: - {fileID: 727261126} - holePrefab: {fileID: 6503149} objectCount: 3 holeCount: 0 holesMove: 0 @@ -3097,8 +2955,6 @@ MonoBehaviour: maxX: 3.5 minY: -5 maxY: 4 - objectParent: {fileID: 727261130} - holeParent: {fileID: 6503154} --- !u!4 &1190939575 Transform: m_ObjectHideFlags: 0 @@ -3683,7 +3539,7 @@ GameObject: - component: {fileID: 1386843748} - component: {fileID: 1386843747} m_Layer: 0 - m_Name: Cylinder (1) + m_Name: ClassicHole m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -3701,11 +3557,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ca7423fcca5f83249a2574cd84b7f806, type: 3} m_Name: m_EditorClassIdentifier: Assembly-CSharp::Hole - catchRadius: 1 + catchRadius: 10 pullForce: 4 - hasMovement: 0 - moveSpeed: 2 - moveRange: {x: 1.5, y: 0} glowRenderer: {fileID: 0} --- !u!136 &1386843748 CapsuleCollider: @@ -3722,14 +3575,14 @@ CapsuleCollider: serializedVersion: 2 m_Bits: 0 m_LayerOverridePriority: 0 - m_IsTrigger: 0 + m_IsTrigger: 1 m_ProvidesContacts: 0 - m_Enabled: 0 + m_Enabled: 1 serializedVersion: 2 m_Radius: 0.5000001 - m_Height: 2 + m_Height: 50 m_Direction: 1 - m_Center: {x: 0.000000059604645, y: 0, z: -0.00000008940697} + m_Center: {x: 0.00000006274173, y: 7.0164957, z: -0.0000000941126} --- !u!23 &1386843749 MeshRenderer: m_ObjectHideFlags: 0 @@ -5156,6 +5009,68 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1001 &9074197596846296259 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + serializedVersion: 3 + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 4463416604269783915, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_Name + value: Sphere + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalPosition.x + value: -13.138 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalPosition.y + value: -2.91 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalPosition.z + value: -0.63 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5999486385027004331, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: + - {fileID: 8982888368890146633, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + - {fileID: 1594777189990218189, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + m_RemovedGameObjects: [] + m_AddedGameObjects: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 4463416604269783915, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} + insertIndex: -1 + addedObject: {fileID: 727261133} + m_SourcePrefab: {fileID: 100100000, guid: 4c53ddc4a767a214e853d6ef8e5841a1, type: 3} --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 @@ -5199,7 +5114,7 @@ SceneRoots: - {fileID: 1280898516} - {fileID: 1092836727} - {fileID: 1169290264} - - {fileID: 727261130} + - {fileID: 9074197596846296259} - {fileID: 6503154} - {fileID: 1386843751} - {fileID: 1704821284} diff --git a/Assets/Scripts/flappy_bird - přejmenovat/flappy bird.cs b/Assets/Scripts/flappy_bird - přejmenovat/flappy bird.cs index 6706877..a9ef1a3 100644 --- a/Assets/Scripts/flappy_bird - přejmenovat/flappy bird.cs +++ b/Assets/Scripts/flappy_bird - přejmenovat/flappy bird.cs @@ -2,6 +2,7 @@ using System; using UnityEngine; using UnityEngine.SceneManagement; using TMPro; +using GeoSus.Client; public class FlappyBirdAllInOne : MonoBehaviour, ITask { @@ -35,7 +36,7 @@ public class FlappyBirdAllInOne : MonoBehaviour, ITask public string TaskID { get; set; } public TaskType TaskType { get; set; } public string TaskName { get; set; } - public (double, double) TaskLocation { get; set; } + public Position TaskLocation { get; set; } public bool IsCompleted { get; private set; } diff --git a/Assets/Scripts/hod_veci_do_diry/DraggableObject.cs b/Assets/Scripts/hod_veci_do_diry/DraggableObject.cs index e8238b9..678ff4f 100644 --- a/Assets/Scripts/hod_veci_do_diry/DraggableObject.cs +++ b/Assets/Scripts/hod_veci_do_diry/DraggableObject.cs @@ -1,46 +1,55 @@ using UnityEngine; -[RequireComponent(typeof(Rigidbody2D))] -[RequireComponent(typeof(Collider2D))] +[RequireComponent(typeof(Rigidbody))] +[RequireComponent(typeof(Collider))] public class DraggableObject : MonoBehaviour { [Header("Přetahování")] public float dragSmoothness = 15f; [Header("Vizuální zpětná vazba")] - public SpriteRenderer spriteRenderer; + public Renderer Renderer; public Color normalColor = Color.white; public Color dragColor = new Color(1f, 1f, 0.5f); public float scaleOnDrag = 1.15f; - private Rigidbody2D rb; + private Rigidbody rb; private Camera mainCamera; private bool isDragging = false; private Vector3 targetPosition; private Vector3 originalScale; private bool hasBeenScored = false; + private Plane _dragPlane; + private Collider col; void Awake() { - rb = GetComponent(); + rb = GetComponent(); + col = GetComponent(); mainCamera = Camera.main; originalScale = transform.localScale; - if (spriteRenderer == null) - spriteRenderer = GetComponent(); + if (Renderer == null) + Renderer = GetComponent(); } void Start() { - rb.gravityScale = 0f; - rb.constraints = RigidbodyConstraints2D.FreezeRotation; + rb.useGravity = false; + rb.constraints = RigidbodyConstraints.FreezeRotation; targetPosition = transform.position; } void Update() { HandleInput(); + } + void FixedUpdate() + { if (isDragging) - transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * dragSmoothness); + { + Vector3 newPos = Vector3.Lerp(rb.position, targetPosition, Time.fixedDeltaTime * dragSmoothness); + rb.MovePosition(newPos); + } } void HandleInput() @@ -48,13 +57,13 @@ public class DraggableObject : MonoBehaviour if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); - Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f)); + //Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f)); if (touch.phase == TouchPhase.Began) - TryStartDrag(worldPos); + TryStartDrag(touch.position); else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { - if (isDragging) targetPosition = worldPos; + if (isDragging) UpdateDrag(touch.position); } else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { @@ -63,49 +72,70 @@ public class DraggableObject : MonoBehaviour } else { - Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f)); + //Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f)); if (Input.GetMouseButtonDown(0)) - TryStartDrag(worldPos); + TryStartDrag(Input.mousePosition); else if (Input.GetMouseButton(0) && isDragging) - targetPosition = worldPos; + UpdateDrag(Input.mousePosition); else if (Input.GetMouseButtonUp(0) && isDragging) EndDrag(); } } - void TryStartDrag(Vector3 worldPos) + void TryStartDrag(Vector3 screenPos) { - if (GetComponent().OverlapPoint(worldPos)) - StartDrag(worldPos); - } + Debug.Log("Trying to start drag at: " + screenPos); + Ray ray = mainCamera.ScreenPointToRay(screenPos); + RaycastHit hit; - void StartDrag(Vector3 worldPos) - { - isDragging = true; - rb.linearVelocity = Vector2.zero; - targetPosition = worldPos; - transform.localScale = originalScale * scaleOnDrag; - if (spriteRenderer != null) + if (Physics.Raycast(ray, out hit)) { - spriteRenderer.color = dragColor; - spriteRenderer.sortingOrder = 10; + if (hit.collider == col) + { + StartDrag(hit.point); + } } } + void StartDrag(Vector3 hitPoint) + { + Debug.Log("Started dragging at: " + hitPoint); + isDragging = true; + rb.isKinematic = false; + rb.linearVelocity = Vector3.zero; + _dragPlane = new Plane(-mainCamera.transform.forward, hitPoint); + targetPosition = hitPoint; + transform.localScale = originalScale * scaleOnDrag; + if (Renderer != null) + { + Renderer.material.color = dragColor; + } + } + void UpdateDrag(Vector3 screenPos) + { + Ray ray = mainCamera.ScreenPointToRay(screenPos); + float distance; + if (_dragPlane.Raycast(ray, out distance)) + { + targetPosition = ray.GetPoint(distance); + } + } void EndDrag() { + Debug.Log("Ended dragging"); isDragging = false; + rb.isKinematic = true; transform.localScale = originalScale; - if (spriteRenderer != null) + if (Renderer != null) { - spriteRenderer.color = normalColor; - spriteRenderer.sortingOrder = 0; + Renderer.material.color = normalColor; } } public void OnScored() { + Debug.Log("Object scored"); if (hasBeenScored) return; hasBeenScored = true; isDragging = false; @@ -123,8 +153,8 @@ public class DraggableObject : MonoBehaviour elapsed += Time.deltaTime; float t = elapsed / duration; transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t); - if (spriteRenderer != null) - spriteRenderer.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t); + if (Renderer != null) + Renderer.material.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t); yield return null; } diff --git a/Assets/Scripts/hod_veci_do_diry/Hole.cs b/Assets/Scripts/hod_veci_do_diry/Hole.cs index 48cb1c6..44c3460 100644 --- a/Assets/Scripts/hod_veci_do_diry/Hole.cs +++ b/Assets/Scripts/hod_veci_do_diry/Hole.cs @@ -1,6 +1,7 @@ -using UnityEngine; using System.Collections; +using UnityEngine; +[RequireComponent(typeof(CapsuleCollider))] public class Hole : MonoBehaviour { [Header("Nastavení")] @@ -10,13 +11,9 @@ public class Hole : MonoBehaviour [Tooltip("Síla vtahování itemu k díře")] public float pullForce = 4f; - [Header("Pohyb díry (volitelné)")] - public bool hasMovement = false; - public float moveSpeed = 2f; - public Vector2 moveRange = new Vector2(1.5f, 0f); [Header("Vizuál")] - public SpriteRenderer glowRenderer; + public Renderer glowRenderer; private Vector3 startPosition; private bool isGlowing = false; @@ -35,27 +32,24 @@ public class Hole : MonoBehaviour void Update() { - if (hasMovement) - { - float x = startPosition.x + Mathf.Sin(Time.time * moveSpeed) * moveRange.x; - float y = startPosition.y + Mathf.Cos(Time.time * moveSpeed * 0.7f) * moveRange.y; - transform.position = new Vector3(x, y, transform.position.z); - } + } - void OnTriggerStay2D(Collider2D other) + void OnTriggerStay(Collider other) { + Debug.Log($"Trigger stay with: {other.gameObject.name}"); DraggableObject draggable = other.GetComponent(); if (draggable == null) return; - - float dist = Vector2.Distance(transform.position, other.transform.position); + Debug.Log($"Draggable object detected: {other.gameObject.name}"); + float dist = Vector3.Distance(transform.position, other.transform.position); - Rigidbody2D rb = other.GetComponent(); + Rigidbody rb = other.GetComponent(); if (rb != null) { - Vector2 dir = ((Vector2)transform.position - rb.position).normalized; - rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode2D.Impulse); + Debug.Log($"Rigidbody detected: {other.gameObject.name}, distance: {dist}"); + Vector3 dir = (transform.position - rb.position).normalized; + rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode.Impulse); } @@ -66,18 +60,21 @@ public class Hole : MonoBehaviour } } - void OnTriggerEnter2D(Collider2D other) + void OnTriggerEnter(Collider other) { + Debug.Log($"Collider entered: {other.gameObject.name}"); if (other.GetComponent() != null) SetGlow(true); } - void OnTriggerExit2D(Collider2D other) + void OnTriggerExit(Collider other) { + Debug.Log($"Collider exited: {other.gameObject.name}"); if (other.GetComponent() != null) SetGlow(false); } void SetGlow(bool active) { + Debug.Log($"SetGlow: {active}"); isGlowing = active; if (glowRenderer == null) return; glowRenderer.enabled = active; @@ -89,8 +86,8 @@ public class Hole : MonoBehaviour while (isGlowing && glowRenderer != null) { float t = Mathf.PingPong(Time.time * 3f, 1f); - Color c = glowRenderer.color; - glowRenderer.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t)); + Color c = glowRenderer.sharedMaterial.color; + glowRenderer.sharedMaterial.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t)); yield return null; } } diff --git a/Assets/Scripts/hod_veci_do_diry/LevelManager.cs b/Assets/Scripts/hod_veci_do_diry/LevelManager.cs index f39d4c8..28ed5f6 100644 --- a/Assets/Scripts/hod_veci_do_diry/LevelManager.cs +++ b/Assets/Scripts/hod_veci_do_diry/LevelManager.cs @@ -1,7 +1,9 @@ +using System; using UnityEngine; using UnityEngine.Events; +using GeoSus.Client; -public class LevelManager : MonoBehaviour +public class LevelManager : MonoBehaviour, ITask { public static LevelManager Instance; @@ -14,11 +16,15 @@ public class LevelManager : MonoBehaviour private int scoredCount = 0; - void Awake() - { - if (Instance == null) Instance = this; - else Destroy(gameObject); - } + public string TaskID { get; set; } + + public TaskType TaskType { get; set; } + public string TaskName { get; set; } + + public Position TaskLocation { get; set; } + + public bool IsCompleted { get; private set; } = false; + protected Action OnCompleted; public void RegisterItem() { @@ -38,4 +44,25 @@ public class LevelManager : MonoBehaviour public int GetScoredCount() => scoredCount; public int GetTotalCount() => itemsToScore; + + public void Initialize(Action onCompleted) + { + OnCompleted = onCompleted; + + IsCompleted = false; + ResetCounter(); + } + + public void ExitTask(Action onExit) + { + onExit?.Invoke(this); + } + + public void Complete() + { + if (IsCompleted) return; + + IsCompleted = true; + OnCompleted?.Invoke(this); + } } diff --git a/Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs b/Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs index 71a46da..e22ce5c 100644 --- a/Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs +++ b/Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs @@ -7,10 +7,11 @@ using System.Collections.Generic; public class ObjectSpawner : MonoBehaviour { public static ObjectSpawner Instance; + [SerializeField] + private List spawnedHoles = new List(); [Header("Prefaby")] public GameObject[] objectPrefabs; - public GameObject holePrefab; [Header("Počty")] [Tooltip("Kolik předmětů spawnovat")] @@ -28,12 +29,9 @@ public class ObjectSpawner : MonoBehaviour public float minY = -5f; public float maxY = 4f; - [Header("Rodiče pro přehlednost (volitelné)")] - public Transform objectParent; - public Transform holeParent; private List spawnedObjects = new List(); - private List spawnedHoles = new List(); + void Awake() { @@ -57,41 +55,42 @@ public class ObjectSpawner : MonoBehaviour LevelManager.Instance.ResetCounter(); } - SpawnHoles(); + //SpawnHoles(); SpawnObjects(); } - void SpawnHoles() - { - for (int i = 0; i < holeCount; i++) - { - Vector2 pos = RandomPos(1f); - GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent); - - Hole h = hole.GetComponent(); - if (h != null && holesMove) - { - h.hasMovement = true; - h.moveSpeed = holeMoveSpeed; - h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f); - } - - spawnedHoles.Add(hole); - } - } - - void SpawnObjects() + //void SpawnHoles() + //{ + // for (int i = 0; i < holeCount; i++) + // { + // Vector2 pos = RandomPos(1f); + // GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent); + + // Hole h = hole.GetComponent(); + // if (h != null && holesMove) + // { + // h.hasMovement = true; + // h.moveSpeed = holeMoveSpeed; + // h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f); + // } + + // spawnedHoles.Add(hole); + // } + //} + + public void SpawnObjects() { + Debug.Log($"Spawning {objectCount} objects..."); for (int i = 0; i < objectCount; i++) { GameObject prefab = objectPrefabs[Random.Range(0, objectPrefabs.Length)]; - Vector2 pos = RandomPos(0.5f); - GameObject obj = Instantiate(prefab, pos, Quaternion.identity, objectParent); + Vector3 pos = RandomPos(0.5f); + GameObject obj = Instantiate(prefab, pos, Quaternion.identity); // Náhodná barva - SpriteRenderer sr = obj.GetComponent(); + Renderer sr = obj.GetComponent(); if (sr != null) - sr.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f); + sr.sharedMaterial.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f); spawnedObjects.Add(obj); } @@ -100,14 +99,17 @@ public class ObjectSpawner : MonoBehaviour public void Clear() { foreach (var o in spawnedObjects) if (o != null) Destroy(o); - foreach (var h in spawnedHoles) if (h != null) Destroy(h); - spawnedObjects.Clear(); + //foreach (var h in spawnedHoles) if (h != null) Destroy(h); + //spawnedObjects.Clear(); spawnedHoles.Clear(); } - Vector2 RandomPos(float margin) => - new Vector2( + Vector3 RandomPos(float margin) + { + return new Vector3( Random.Range(minX + margin, maxX - margin), - Random.Range(minY + margin, maxY - margin) + Random.Range(minY + margin, maxY - margin), + 0.5f ); + } } diff --git a/Assets/Scripts/insert key/insertkeys.cs b/Assets/Scripts/insert key/insertkeys.cs index 9d68ced..a4d052f 100644 --- a/Assets/Scripts/insert key/insertkeys.cs +++ b/Assets/Scripts/insert key/insertkeys.cs @@ -3,6 +3,7 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; +using GeoSus.Client; public class DraggableKey : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, ITask @@ -27,7 +28,7 @@ public class DraggableKey : MonoBehaviour, public string TaskID { get; set; } public TaskType TaskType { get; set; } public string TaskName { get; set; } - public (double, double) TaskLocation { get; set; } + public Position TaskLocation { get; set; } public bool IsCompleted { get; private set; } private Action _onCompleted; diff --git a/Assets/Scripts/kabely/kabely.cs b/Assets/Scripts/kabely/kabely.cs index 511be97..99ade4c 100644 --- a/Assets/Scripts/kabely/kabely.cs +++ b/Assets/Scripts/kabely/kabely.cs @@ -3,6 +3,7 @@ using System.Collections; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; +using GeoSus.Client; public class CableMiniGame : MonoBehaviour, ITask { @@ -21,7 +22,7 @@ public class CableMiniGame : MonoBehaviour, ITask public string TaskID { get; set; } public TaskType TaskType { get; set; } public string TaskName { get; set; } - public (double, double) TaskLocation { get; set; } + public Position TaskLocation { get; set; } public bool IsCompleted { get; private set; } private Action _onCompleted; diff --git a/Assets/Scripts/satelity/WindController.cs b/Assets/Scripts/satelity/WindController.cs index abb9640..7999840 100644 --- a/Assets/Scripts/satelity/WindController.cs +++ b/Assets/Scripts/satelity/WindController.cs @@ -2,7 +2,7 @@ using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; -using UnityEngine.UI; +using GeoSus.Client; public class WindController : MonoBehaviour, ITask { @@ -24,7 +24,7 @@ public class WindController : MonoBehaviour, ITask public string TaskID { get; set; } public TaskType TaskType { get; set; } public string TaskName { get; set; } - public (double, double) TaskLocation { get; set; } + public Position TaskLocation { get; set; } public bool IsCompleted { get; private set; } void Start() diff --git a/Assets/minigame/Materials/brick_wall_08_1k.blend/Materials/brick_wall_08_diff_1k.mat b/Assets/minigame/Materials/brick_wall_08_1k.blend/Materials/brick_wall_08_diff_1k.mat index 6bb62d6..bdf71f5 100644 --- a/Assets/minigame/Materials/brick_wall_08_1k.blend/Materials/brick_wall_08_diff_1k.mat +++ b/Assets/minigame/Materials/brick_wall_08_1k.blend/Materials/brick_wall_08_diff_1k.mat @@ -78,7 +78,7 @@ Material: - _UVSec: 0 - _ZWrite: 1 m_Colors: - - _Color: {r: 1, g: 1, b: 1, a: 1} + - _Color: {r: 0.12311344, g: 0.96881944, b: 0.7101699, a: 1} - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} m_BuildTextureStacks: [] m_AllowLocking: 1 diff --git a/Assets/minigame/Sphere.prefab b/Assets/minigame/Sphere.prefab new file mode 100644 index 0000000..e4383f8 --- /dev/null +++ b/Assets/minigame/Sphere.prefab @@ -0,0 +1,161 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4463416604269783915 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 5999486385027004331} + - component: {fileID: 3096372749762576219} + - component: {fileID: 1253701241776310731} + - component: {fileID: 7907572649200639552} + - component: {fileID: 4926246710868963563} + - component: {fileID: 4731565560732594675} + m_Layer: 0 + m_Name: Sphere + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &5999486385027004331 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -13.138, y: -2.91, z: -0.63} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!33 &3096372749762576219 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} +--- !u!23 &1253701241776310731 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RayTracingAccelStructBuildFlagsOverride: 0 + m_RayTracingAccelStructBuildFlags: 1 + m_SmallMeshCulling: 1 + m_ForceMeshLod: -1 + m_MeshLodSelectionBias: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 1a424fd36745ea14682570c13681319e, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_GlobalIlluminationMeshLod: 0 + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_AdditionalVertexStreams: {fileID: 0} +--- !u!114 &7907572649200639552 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fb5157d7cd78450439c40cd6f5afe6ac, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::DraggableObject + dragSmoothness: 15 + Renderer: {fileID: 0} + normalColor: {r: 1, g: 1, b: 1, a: 1} + dragColor: {r: 1, g: 1, b: 0.5, a: 1} + scaleOnDrag: 1.15 +--- !u!54 &4926246710868963563 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + serializedVersion: 5 + m_Mass: 1 + m_LinearDamping: 0 + m_AngularDamping: 0.05 + m_CenterOfMass: {x: 0, y: 0, z: 0} + m_InertiaTensor: {x: 1, y: 1, z: 1} + m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ImplicitCom: 1 + m_ImplicitTensor: 1 + m_UseGravity: 1 + m_IsKinematic: 0 + m_Interpolate: 0 + m_Constraints: 0 + m_CollisionDetection: 0 +--- !u!136 &4731565560732594675 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4463416604269783915} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 0 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Radius: 0.5 + m_Height: 1 + m_Direction: 1 + m_Center: {x: 0, y: 0, z: 0} diff --git a/Assets/minigame/Sphere.prefab.meta b/Assets/minigame/Sphere.prefab.meta new file mode 100644 index 0000000..d74d099 --- /dev/null +++ b/Assets/minigame/Sphere.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4c53ddc4a767a214e853d6ef8e5841a1 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: