using System; using System.Collections.Generic; using UnityEngine; using Mirror; public class ServerMainController : MonoBehaviour { [Header("Status")] public string password = null; public List players; public List viewers; public GamePhase phase = GamePhase.UNINTIALIZED; [Header("Constants")] public const int MAX_CONNECTIONS = 32; protected void Start() { StartServer(); } protected void OnDestroy() { if(NetworkServer.active) NetworkServer.Shutdown(); } public void StartServer() { LogFilter.Debug = true; phase = GamePhase.LOBBY; Transport.activeTransport = GetComponent(); NetworkServer.RegisterHandler(OnPlayerJoin); NetworkServer.RegisterHandler(OnInvalidPassword); NetworkServer.RegisterHandler(OnViewerLink); NetworkServer.RegisterHandler(OnGameSettings); NetworkServer.RegisterHandler(OnActResults); NetworkServer.Listen(MAX_CONNECTIONS); } public void OnPlayerJoin(NetworkConnection connection, NetMessage.Connect.PlayerJoin message) { Debug.LogFormat("Received NetMessage.Connect.PlayerJoin from {0}", message.playerName); if(message.gamePassword != password) { connection.Send(new NetMessage.Error.InvalidPassword()); connection.Disconnect(); return; } Player newPlayer = new Player { name = message.playerName, id = players.Count }; players.Add(newPlayer); NetMessage.Connect.PlayerJoinSuccessful reply = new NetMessage.Connect.PlayerJoinSuccessful { player = newPlayer }; connection.Send(reply); } public void OnInvalidPassword(NetworkConnection connection, NetMessage.Error.InvalidPassword message) { Debug.LogError("Invalid gamePassword"); } public void OnViewerLink(NetworkConnection connection, NetMessage.Connect.ViewerLink message) { if(message.gamePassword != password) { connection.Send(new NetMessage.Error.InvalidPassword()); return; } Viewer newViewer = new Viewer { id = viewers.Count }; viewers.Add(newViewer); NetMessage.Connect.ViewerLinkSuccessful reply = new NetMessage.Connect.ViewerLinkSuccessful { viewer = newViewer }; connection.Send(reply); } public void OnGameSettings(NetworkConnection connection, NetMessage.Game.Settings message) {} public void OnActResults(NetworkConnection connection, NetMessage.Act.Results message) {} }