1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-21 23:04:19 +00:00

Change network things

This commit is contained in:
Steffo 2019-09-16 01:07:27 +02:00
parent 6f6d436551
commit 40dbe98652
3 changed files with 44 additions and 30 deletions

View file

@ -23,12 +23,7 @@ namespace NetMessages {
public class ConnectionSuccessfulResponse : MessageBase public class ConnectionSuccessfulResponse : MessageBase
{ {
public string[] playersConnected;
}
public class NewPlayerConnectedNotification : MessageBase
{
public string playerName;
} }
public class GameStartMessage : MessageBase public class GameStartMessage : MessageBase
@ -45,4 +40,9 @@ namespace NetMessages {
{ {
public ActResults results; public ActResults results;
} }
public class ActEndNotification : MessageBase
{
}
} }

View file

@ -1,17 +1,10 @@
using System; using System;
using UnityEngine; using UnityEngine;
using Mirror;
public class PlayerMainController : MonoBehaviour public class PlayerMainController : MonoBehaviour
{ {
[Header("TEST")]
public string jsonData = "";
protected void Start() {
Debug.Log("Testing ActInit with public jsonData...");
LoadAct(jsonData);
}
[Header("Objects")] [Header("Objects")]
public ActController currentAct; public ActController currentAct;
@ -20,27 +13,39 @@ public class PlayerMainController : MonoBehaviour
public GameObject typingControllerPrefab; public GameObject typingControllerPrefab;
[Serializable] [Serializable]
public class InvalidJsonDataException : Exception { public class InvalidActTypeException : Exception {
public readonly string jsonData; public readonly string actType;
public InvalidJsonDataException(string jsonData) { public InvalidActTypeException(string actType) {
this.jsonData = jsonData; this.actType = actType;
} }
}; };
public void LoadAct(string jsonData) { public void LoadAct(ActSettings settings) {
ActSettings unknownSettings = JsonUtility.FromJson<ActSettings>(jsonData); if(settings.type == "Drawing") {
if(unknownSettings.type == "Drawing") {
currentAct = Instantiate(drawingControllerPrefab, transform).GetComponent<DrawingController>(); currentAct = Instantiate(drawingControllerPrefab, transform).GetComponent<DrawingController>();
currentAct.settings = JsonUtility.FromJson<DrawingSettings>(jsonData);
} }
else if (unknownSettings.type == "Typing") { else if (settings.type == "Typing") {
currentAct = Instantiate(typingControllerPrefab, transform).GetComponent<TypingController>(); currentAct = Instantiate(typingControllerPrefab, transform).GetComponent<TypingController>();
currentAct.settings = JsonUtility.FromJson<TypingSettings>(jsonData);
} }
else { else throw new InvalidActTypeException(settings.type);
throw new InvalidJsonDataException(jsonData); currentAct.settings = settings;
} }
public void ConnectToServer(string address) {
NetworkClient.Connect(address);
NetworkClient.RegisterHandler<NetMessages.ConnectionSuccessfulResponse>(OnConnectionSuccessful);
NetworkClient.RegisterHandler<NetMessages.GameStartMessage>(OnGameStart);
NetworkClient.RegisterHandler<NetMessages.ActSettingsMessage>(OnActSettings);
NetworkClient.RegisterHandler<NetMessages.ActEndNotification>(OnActEnd);
} }
public void OnConnectionSuccessful(NetworkConnection connection, NetMessages.ConnectionSuccessfulResponse message) {}
public void OnGameStart(NetworkConnection connection, NetMessages.GameStartMessage message) {}
public void OnActEnd(NetworkConnection connection, NetMessages.ActEndNotification message) {}
public void OnActSettings(NetworkConnection connection, NetMessages.ActSettingsMessage message) {
LoadAct(message.settings);
}
} }

View file

@ -4,12 +4,21 @@ using Mirror;
public class ServerMainController : MonoBehaviour public class ServerMainController : MonoBehaviour
{ {
public const int MAX_CONNECTIONS = 32; [Header("Status")]
public bool isListening = false; public bool isListening = false;
[Header("Constants")]
public const int MAX_CONNECTIONS = 32;
public void ServerStart() { public void ServerStart() {
NetworkServer.Listen(MAX_CONNECTIONS); NetworkServer.Listen(MAX_CONNECTIONS);
NetworkServer.RegisterHandler<NetMessages.PlayerConnectionMessage>(OnPlayerConnect);
NetworkServer.RegisterHandler<NetMessages.ViewerConnectionMessage>(OnViewerConnect);
NetworkServer.RegisterHandler<NetMessages.ActResultsMessage>(OnActResults);
isListening = true; isListening = true;
} }
public void OnPlayerConnect(NetworkConnection connection, NetMessages.PlayerConnectionMessage message) {}
public void OnViewerConnect(NetworkConnection connection, NetMessages.ViewerConnectionMessage message) {}
public void OnActResults(NetworkConnection connection, NetMessages.ActResultsMessage message) {}
} }