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-15 22:28:36 +00:00
|
|
|
public void ServerStart() {
|
2019-09-16 15:34:22 +00:00
|
|
|
phase = GamePhase.LOBBY;
|
|
|
|
NetworkServer.RegisterHandler<NetMessage.Connect.PlayerJoin>(OnPlayerJoin);
|
|
|
|
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) {
|
|
|
|
if(message.gamePassword != password) {
|
|
|
|
connection.Send<NetMessage.Error.InvalidPassword>(new NetMessage.Error.InvalidPassword());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Player newPlayer = new Player(message.playerName, new Guid());
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
Viewer newViewer = new Viewer(new Guid());
|
|
|
|
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
|
|
|
}
|