From 5436f7184030ac9a66d80e1e70b11604412f071c Mon Sep 17 00:00:00 2001 From: in0finite Date: Sat, 20 Feb 2021 02:23:16 +0100 Subject: [PATCH] display player stats dynamically based on data from server --- Assets/Scripts/Stats/PlayerStats.cs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Stats/PlayerStats.cs b/Assets/Scripts/Stats/PlayerStats.cs index 869979b9..bb0d39be 100644 --- a/Assets/Scripts/Stats/PlayerStats.cs +++ b/Assets/Scripts/Stats/PlayerStats.cs @@ -1,7 +1,5 @@ -using System.Collections.Generic; using System.Linq; using UnityEngine; -using Mirror; using SanAndreasUnity.Net; namespace SanAndreasUnity.Stats @@ -12,6 +10,13 @@ namespace SanAndreasUnity.Stats [SerializeField] string[] m_columnNames = new string[]{"Address", "Net id", "Ped net id", "Ped model", "Ped state", "Health", "Weapon"}; int m_currentIndex = 0; + float[] m_currentWidths = new float[0]; + string[] m_currentColumnNames = new string[0]; + + public const string ColumnDataKeysKey = "player_stats_column_data_keys"; + public const string ColumnNamesKey = "player_stats_column_names"; + public const string ColumnWidthsKey = "player_stats_column_widths"; + void Start() { @@ -23,11 +28,19 @@ namespace SanAndreasUnity.Stats bool isServer = NetStatus.IsServer; + string[] dataKeys = SyncedServerData.Data.GetStringArray(ColumnDataKeysKey) ?? new string[0]; + + m_currentColumnNames = m_columnNames.Concat(SyncedServerData.Data.GetStringArray(ColumnNamesKey) ?? new string[0]).ToArray(); + m_currentWidths = m_widths.Concat(SyncedServerData.Data.GetFloatArray(ColumnWidthsKey) ?? new float[0]).ToArray(); + + if (m_currentColumnNames.Length != m_currentWidths.Length) + return; + // columns GUILayout.BeginHorizontal(); m_currentIndex = 0; - for (int i=0; i < m_columnNames.Length; i++) - GUILayout.Button(m_columnNames[i], GUILayout.Width(GetWidth())); + for (int i=0; i < m_currentColumnNames.Length; i++) + GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth())); GUILayout.EndHorizontal(); foreach (var p in Player.AllPlayersEnumerable) @@ -43,12 +56,18 @@ namespace SanAndreasUnity.Stats GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.Health.ToString() : "", GUILayout.Width(GetWidth())); GUILayout.Label(p.OwnedPed != null && p.OwnedPed.CurrentWeapon != null && p.OwnedPed.CurrentWeapon.Definition != null ? p.OwnedPed.CurrentWeapon.Definition.ModelName : "", GUILayout.Width(GetWidth())); + foreach (string dataKey in dataKeys) + { + string data = p.ExtraData.GetString(dataKey); + GUILayout.Label(data, GUILayout.Width(GetWidth())); + } + GUILayout.EndHorizontal(); } } - float GetWidth() => m_widths[m_currentIndex++]; + float GetWidth() => m_currentWidths[m_currentIndex++]; } }