1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-22 07:14:19 +00:00
better-tee/Assets/Code/ServerMainController.cs

80 lines
2.9 KiB
C#
Raw Normal View History

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