Skip to content

Commit

Permalink
All changes done for map voting/server browser/compression. multiplay…
Browse files Browse the repository at this point in the history
…er v0.5.0 is ready to go
  • Loading branch information
silentbaws committed Oct 12, 2019
1 parent b6a1e22 commit 3a29ad5
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 37 deletions.
2 changes: 2 additions & 0 deletions XLMultiplayer/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ static bool OnToggle(UnityModManager.ModEntry modEntry, bool value) {
statusMenu = new GameObject().AddComponent<MultiplayerStatusMenu>();
UnityEngine.Object.DontDestroyOnLoad(menu.gameObject);
UnityEngine.Object.DontDestroyOnLoad(statusMenu.gameObject);
MultiplayerUtils.StartMapLoading();
} else {
menu.EndMultiplayer();
UnityEngine.Object.Destroy(menu.gameObject);
UnityEngine.Object.Destroy(statusMenu.gameObject);
MultiplayerUtils.StopMapLoading();
}

return true;
Expand Down
39 changes: 29 additions & 10 deletions XLMultiplayer/MultiplayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ private void Start() {
}

private void Update() {
if (Input.GetKeyDown(KeyCode.F5)) {

if(GameManagement.GameStateMachine.Instance.CurrentState.GetType() == typeof(GameManagement.LevelSelectionState) && MultiplayerUtils.serverMapDictionary.Count > 0) {
GameManagement.GameStateMachine.Instance.RequestPauseState();
}

if(previousFrameTimes.Count == 0)
Expand All @@ -93,7 +93,7 @@ private void Update() {

if (client == null) return;

if (client.elapsedTime.ElapsedMilliseconds - client.lastAlive > 5000 + previousFrameTimes.Max() && ((IsInvoking("SendUpdate") && !Application.isLoadingLevel && textureQueue.Count == 0) || !client.tcpConnection.Connected)) {
if (client.elapsedTime.ElapsedMilliseconds - client.lastAlive > 5000 + previousFrameTimes.Max() && ((IsInvoking("SendUpdate") && textureQueue.Count == 0) || !client.tcpConnection.Connected)) {
bool loadedAll = true;
foreach (MultiplayerPlayerController controller in this.otherControllers) {
if (!controller.shirtMP.loaded) {
Expand Down Expand Up @@ -153,6 +153,8 @@ public void ConnectToServer(string serverIP, int port, string user) {
this.ourController.username = user;
this.runningClient = true;

MultiplayerUtils.serverMapDictionary.Clear();

client = new NetworkClient(serverIP, port, this, this.debugWriter);

FullBodyBipedIK biped = Traverse.Create(PlayerController.Instance.ikController).Field("_finalIk").GetValue<FullBodyBipedIK>();
Expand All @@ -162,13 +164,30 @@ public void ConnectToServer(string serverIP, int port, string user) {
debugWriter.WriteLine(parent.name);
parent = parent.parent;
}
}
}

public void StartLoadMap(string path) {
this.StartCoroutine(LoadMap(path));
}

//Load map with path
//LevelSelectionController levelSelectionController = GameManagement.GameStateMachine.Instance.LevelSelectionObject.GetComponentInChildren<LevelSelectionController>();
//GameManagement.GameStateMachine.Instance.LevelSelectionObject.SetActive(true);
//levelSelectionController.StartCoroutine(levelSelectionController.LoadLevel(levelSelectionController.CustomLevels.Find(l => l.path.Equals("C:\\Users\\davisellwood\\Documents\\SkaterXL\\Maps\\Industrial Zone by Jean-Olive v2"))));
//StartCoroutine(CloseAfterLoad());
public IEnumerator LoadMap(string path) {
while (!IsInvoking("SendUpdate")) {
yield return new WaitForEndOfFrame();
}
//Load map with path
LevelSelectionController levelSelectionController = GameManagement.GameStateMachine.Instance.LevelSelectionObject.GetComponentInChildren<LevelSelectionController>();
GameManagement.GameStateMachine.Instance.LevelSelectionObject.SetActive(true);
LevelInfo target = levelSelectionController.Levels.Find(level => level.path.Equals(path));
if(target == null) {
target = levelSelectionController.CustomLevels.Find(level => level.path.Equals(path));
}
if(target == null) {
Main.statusMenu.DisplayNoMap(path);
}
levelSelectionController.StartCoroutine(levelSelectionController.LoadLevel(target));
StartCoroutine(CloseAfterLoad());
yield break;
}

private IEnumerator CloseAfterLoad() {
Expand Down Expand Up @@ -415,7 +434,7 @@ public void ProcessMessage(byte[] buffer, int bufferSize) {
debugWriter.WriteLine("Processing settings from {0}", playerID);
foreach (MultiplayerPlayerController controller in otherControllers) {
if (controller.playerID == playerID) {
controller.username = Encoding.ASCII.GetString(newBuffer, 1, newBuffer.Length - 1);
controller.username = ASCIIEncoding.ASCII.GetString(newBuffer, 1, newBuffer.Length - 1);
debugWriter.WriteLine(controller.username);
MultiplayerController.chatMessages.Add("Player <color=\"yellow\">" + controller.username + "{" + controller.playerID + "}</color> <b><color=\"green\">CONNECTED</color></b>");
}
Expand Down Expand Up @@ -505,7 +524,7 @@ public void SendChatMessage(string message) {
}
}

private void SendBytes(OpCode opCode, byte[] msg, bool reliable) {
public void SendBytes(OpCode opCode, byte[] msg, bool reliable) {
if (!reliable) {
client.SendUnreliable(msg, opCode);
}else if(reliable && client.tcpConnection.Connected) {
Expand Down
6 changes: 5 additions & 1 deletion XLMultiplayer/MultiplayerServerBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public IEnumerator StartUpdatingServerList() {
case "currentPlayers":
newServer.playerCurrent = (int)p2.Value;
break;
case "mapName":
newServer.mapName = (string)p2.Value;
break;
}
}
}
Expand Down Expand Up @@ -114,7 +117,7 @@ void RenderWindow(int windowID) {

foreach (var s in servers) {
Label($"<b>{s.name} v{s.version}</b>\n", title);
Label($"<b>IP:</b> {s.ip} <b>port:</b> {s.port} <b>players:</b> {s.playerCurrent}/{s.playerMax}", center);
Label($"<b>IP:</b> {s.ip} <b>port:</b> {s.port}\n<b>map: {s.mapName} </b><b>players:</b> {s.playerCurrent}/{s.playerMax}", center);
if (Button($"Connect with username \"{Main.menu.username}\"")) {
this.Close();
Main.menu.CreateMultiplayerManager();
Expand Down Expand Up @@ -142,6 +145,7 @@ private class Server {
public string ip;
public string port;
public string version;
public string mapName;
public int playerMax;
public int playerCurrent;
}
Expand Down
99 changes: 99 additions & 0 deletions XLMultiplayer/MultiplayerStatusMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Text;

namespace XLMultiplayer {
class MultiplayerStatusMenu : MonoBehaviour {
Expand Down Expand Up @@ -43,6 +44,17 @@ class MultiplayerStatusMenu : MonoBehaviour {
bool displayInvalidVersion = false;
string serverVersion = "";

float invalidMapTimer = 0.0f;
bool displayInvalidMap = false;
string serverMap = "";

bool showVoteMenu = false;
Vector2 scrollPosition = Vector2.zero;
Rect mapVoteRect = new Rect(20, 10, 250, 420);

bool showMapVote = false;
Stopwatch voteWatch = new Stopwatch();

public void Start() {
InitializeMenu();

Expand All @@ -56,6 +68,12 @@ public void Start() {
colorTexture.Apply();
}

public void OnDestroy() {
UnityEngine.Object.Destroy(statusCanvas);
UnityEngine.Object.Destroy(connectedTexture);
UnityEngine.Object.Destroy(disconnectedTexture);
}

public void InitializeMenu() {
this.statusCanvas = new GameObject().AddComponent<Canvas>();
this.statusCanvas.gameObject.AddComponent<GraphicRaycaster>();
Expand Down Expand Up @@ -107,6 +125,19 @@ public void Update() {
showStatus = !showStatus;
}

if (Input.GetKeyDown(KeyCode.M)) {
showVoteMenu = !showVoteMenu;
if(showVoteMenu)
XLShredLib.ModMenu.Instance.ShowCursor(Main.modId);
else
XLShredLib.ModMenu.Instance.HideCursor(Main.modId);
}

if(voteWatch.ElapsedMilliseconds > 5000) {
showMapVote = false;
voteWatch.Stop();
}

if (displayInvalidVersion)
invalidVersionTimer += Time.unscaledDeltaTime;

Expand All @@ -115,6 +146,14 @@ public void Update() {
invalidVersionTimer = 0.0f;
}

if (displayInvalidMap)
invalidMapTimer += Time.unscaledDeltaTime;

if (invalidMapTimer > 25.0f) {
displayInvalidMap = false;
invalidMapTimer = 0.0f;
}

if (this.connectedImage != null && this.disconnectedImage != null) {
this.connectedImage.enabled = (Main.menu.multiplayerManager != null && Main.menu.multiplayerManager.runningClient) && showStatus;
this.disconnectedImage.enabled = (Main.menu.multiplayerManager == null || !Main.menu.multiplayerManager.runningClient) && showStatus;
Expand All @@ -132,13 +171,62 @@ public void Update() {
}
}

public void StartVotePopup() {
showMapVote = true;
voteWatch = Stopwatch.StartNew();
}

public void DisplayInvalidVersion(string serverVersion) {
displayInvalidVersion = true;
invalidVersionTimer = 0.0f;
this.serverVersion = serverVersion;
}

public void DisplayNoMap(string mapName) {
displayInvalidMap = true;
invalidMapTimer = 0.0f;
this.serverMap = mapName;
}

public void DrawVoteMenu(int windowID) {
GUI.DragWindow(new Rect(0, 0, 10000, 20));

//Rect mapVoteRect = new Rect(20, 10, 250, 420);

GUIStyle style = new GUIStyle(GUI.skin.verticalScrollbar);
scrollPosition = GUI.BeginScrollView(new Rect(0, 20, 250, 320), scrollPosition, new Rect(0, 0, 250, 30 * MultiplayerUtils.serverMapDictionary.Count), false, true, GUIStyle.none, style);

int count = 0;
foreach(var item in MultiplayerUtils.serverMapDictionary) {
GUI.backgroundColor = Color.grey;
if (MultiplayerUtils.currentVote == item.Key)
GUI.backgroundColor = Color.green;

if(GUI.Button(new Rect(10, 30*count, 220, 25), item.Value)) {
byte[] mapVote = ASCIIEncoding.ASCII.GetBytes(item.Key);
Main.menu.multiplayerManager.SendBytes(OpCode.MapVote, mapVote, true);
MultiplayerUtils.currentVote = item.Key;
}
count++;
}

GUI.EndScrollView();
GUI.Label(new Rect(10, 345, 230, 65), "Your vote is on the current map by default.\nDon't see a map you like on the list? Ask the host to add it.");
}

public void OnGUI() {
if (showMapVote) {
GUIStyle text = new GUIStyle();
text.normal.textColor = Color.yellow;

GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "Map voting has begun, more than half the server has requested a map change. You have 30 seconds to vote.", text);
}

if (showVoteMenu) {
GUI.backgroundColor = Color.black;
mapVoteRect = GUI.Window(3, mapVoteRect, DrawVoteMenu, "Map Vote");
}

if (displayInvalidVersion) {
Rect rect = new Rect(Screen.width*0.35f, 0, Screen.width*0.55f, 400);

Expand All @@ -150,6 +238,17 @@ public void OnGUI() {
GUI.Label(rect, "Tried to connect to server with different version than client\n Client version: " + Main.modEntry.Version.ToString() + "\nServer Version: " + serverVersion, style);
}

if (displayInvalidMap) {
Rect rect = new Rect(Screen.width * 0.35f, 0, Screen.width * 0.55f, 400);

GUIStyle style = new GUIStyle();
style.fontSize = 24;
style.wordWrap = true;
style.alignment = TextAnchor.UpperCenter;

GUI.Label(rect, "Tried to connect to server using a map you don't have. If you have the map either the client hasn't hashed all your maps, it's a different version from the servers, or it has a '.' in the name\nServer Map: " + serverMap, style);
}

if (showStatus && Main.menu.multiplayerManager != null && Main.menu.multiplayerManager.runningClient) {
Rect rect = new Rect(Screen.width - Screen.width * 0.2f, Screen.width * 0.1f, Screen.width * 0.2f - 5, (numPlayers + 1) * 25f);

Expand Down
Loading

0 comments on commit 3a29ad5

Please sign in to comment.