mirror of
https://github.com/Steffo99/better-tee.git
synced 2024-11-21 14:54:18 +00:00
Change network things
This commit is contained in:
parent
6f6d436551
commit
40dbe98652
3 changed files with 44 additions and 30 deletions
|
@ -23,12 +23,7 @@ namespace NetMessages {
|
|||
|
||||
public class ConnectionSuccessfulResponse : MessageBase
|
||||
{
|
||||
public string[] playersConnected;
|
||||
}
|
||||
|
||||
public class NewPlayerConnectedNotification : MessageBase
|
||||
{
|
||||
public string playerName;
|
||||
}
|
||||
|
||||
public class GameStartMessage : MessageBase
|
||||
|
@ -45,4 +40,9 @@ namespace NetMessages {
|
|||
{
|
||||
public ActResults results;
|
||||
}
|
||||
|
||||
public class ActEndNotification : MessageBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -1,17 +1,10 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
|
||||
public class PlayerMainController : MonoBehaviour
|
||||
{
|
||||
[Header("TEST")]
|
||||
public string jsonData = "";
|
||||
|
||||
protected void Start() {
|
||||
Debug.Log("Testing ActInit with public jsonData...");
|
||||
LoadAct(jsonData);
|
||||
}
|
||||
|
||||
[Header("Objects")]
|
||||
public ActController currentAct;
|
||||
|
||||
|
@ -20,27 +13,39 @@ public class PlayerMainController : MonoBehaviour
|
|||
public GameObject typingControllerPrefab;
|
||||
|
||||
[Serializable]
|
||||
public class InvalidJsonDataException : Exception {
|
||||
public readonly string jsonData;
|
||||
public class InvalidActTypeException : Exception {
|
||||
public readonly string actType;
|
||||
|
||||
public InvalidJsonDataException(string jsonData) {
|
||||
this.jsonData = jsonData;
|
||||
public InvalidActTypeException(string actType) {
|
||||
this.actType = actType;
|
||||
}
|
||||
};
|
||||
|
||||
public void LoadAct(string jsonData) {
|
||||
ActSettings unknownSettings = JsonUtility.FromJson<ActSettings>(jsonData);
|
||||
|
||||
if(unknownSettings.type == "Drawing") {
|
||||
public void LoadAct(ActSettings settings) {
|
||||
if(settings.type == "Drawing") {
|
||||
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.settings = JsonUtility.FromJson<TypingSettings>(jsonData);
|
||||
}
|
||||
else {
|
||||
throw new InvalidJsonDataException(jsonData);
|
||||
}
|
||||
else throw new InvalidActTypeException(settings.type);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,12 +4,21 @@ using Mirror;
|
|||
|
||||
public class ServerMainController : MonoBehaviour
|
||||
{
|
||||
public const int MAX_CONNECTIONS = 32;
|
||||
|
||||
[Header("Status")]
|
||||
public bool isListening = false;
|
||||
|
||||
[Header("Constants")]
|
||||
public const int MAX_CONNECTIONS = 32;
|
||||
|
||||
public void ServerStart() {
|
||||
NetworkServer.Listen(MAX_CONNECTIONS);
|
||||
NetworkServer.RegisterHandler<NetMessages.PlayerConnectionMessage>(OnPlayerConnect);
|
||||
NetworkServer.RegisterHandler<NetMessages.ViewerConnectionMessage>(OnViewerConnect);
|
||||
NetworkServer.RegisterHandler<NetMessages.ActResultsMessage>(OnActResults);
|
||||
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) {}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue