display player stats dynamically based on data from server

This commit is contained in:
in0finite 2021-02-20 02:23:16 +01:00
parent b5e308c135
commit 5436f71840

View file

@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using Mirror;
using SanAndreasUnity.Net; using SanAndreasUnity.Net;
namespace SanAndreasUnity.Stats 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"}; [SerializeField] string[] m_columnNames = new string[]{"Address", "Net id", "Ped net id", "Ped model", "Ped state", "Health", "Weapon"};
int m_currentIndex = 0; 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() void Start()
{ {
@ -23,11 +28,19 @@ namespace SanAndreasUnity.Stats
bool isServer = NetStatus.IsServer; 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 // columns
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
m_currentIndex = 0; m_currentIndex = 0;
for (int i=0; i < m_columnNames.Length; i++) for (int i=0; i < m_currentColumnNames.Length; i++)
GUILayout.Button(m_columnNames[i], GUILayout.Width(GetWidth())); GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth()));
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
foreach (var p in Player.AllPlayersEnumerable) 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.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())); 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(); GUILayout.EndHorizontal();
} }
} }
float GetWidth() => m_widths[m_currentIndex++]; float GetWidth() => m_currentWidths[m_currentIndex++];
} }
} }