Skip to content

Instantly share code, notes, and snippets.

@Godatplay
Last active March 18, 2020 20:57
Show Gist options
  • Save Godatplay/5fe4335d518afc40f95be7a4ed47ae07 to your computer and use it in GitHub Desktop.
Save Godatplay/5fe4335d518afc40f95be7a4ed47ae07 to your computer and use it in GitHub Desktop.
leaderboard implementation as a string
// in LeaderboardSyncModel
using UnityEngine;
using Normal.Realtime.Serialization;
[RealtimeModel]
public partial class LeaderboardSyncModel
{
[RealtimeProperty(2, true, true)] // 1 and 2 have been used as IDs
private string _leaderboard;
}
// SNIP
// in LeaderboardSync
using Normal.Realtime;
public class LeaderboardSync : RealtimeComponent
{
private LeaderboardSyncModel _model;
private LeaderboardScores _leaderboardScores;
private void Start()
{
_leaderboardScores = GetComponent<LeaderboardScores>();
}
private LeaderboardSyncModel model
{
set
{
if (_model != null)
{
_model.leaderboardDidChange -= LeaderboardDidChange;
}
_model = value;
if (_model != null)
{
UpdateLeaderboardScores();
_model.leaderboardDidChange += LeaderboardDidChange;
}
}
}
private void LeaderboardDidChange(LeaderboardSyncModel model, string value)
{
UpdateLeaderboardScores();
}
private void UpdateLeaderboardScores()
{
_leaderboardScores.scoresString = _model.leaderboard;
}
public void SetLeaderboard(string leaderboard)
{
_model.leaderboard = leaderboard;
}
}
// SNIP
// in LeaderboardScores
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class LeaderboardScores : MonoBehaviour
{
public string scoresString; // If you delete this in the editor it will erase the Leaderboard scores
private string _previousScoresString;
private GameObject _leaderboardText;
private List<string> _leaderNames;
private List<float> _leaderScores;
public int maxLeaderboardSize;
private TextMeshPro _txt;
private string _stringForUpdate;
private LeaderboardSync _leaderboardSync;
void Start()
{
scoresString = "";
_leaderboardText = GameObject.Find("LeaderboardText");
_txt = _leaderboardText.GetComponent<TextMeshPro>();
_leaderboardSync = GetComponent<LeaderboardSync>();
_leaderNames = new List<string>();
_leaderScores = new List<float>();
maxLeaderboardSize = 5;
_stringForUpdate = BuildStringForUpdate();
//Debug.Log("scoresString: " + scoresString);
}
void Update()
{
_stringForUpdate = BuildStringForUpdate();
if (scoresString != _previousScoresString)
{
_leaderboardSync.SetLeaderboard(scoresString);
_previousScoresString = scoresString;
SeparateStringToLists();
BuildStringFromLists();
}
_txt.SetText(_stringForUpdate);
}
private void SeparateStringToLists()
{
if( scoresString == null )
{
Debug.Log("scoreString == null");
Debug.Log("Does this ever happen, or can this if block be deleted? Brand new level?");
return;
}
_leaderNames.Clear();
_leaderScores.Clear();
//Debug.Log("scoresString: " + scoresString);
if (scoresString.Length == 0 ) return;
string[] separated = scoresString.Split(',');
//Debug.Log("separated: " + separated.ToString());
for(int i = 0; i < separated.Length; i++)
{
if (i % 2 == 0)
{
_leaderNames.Add(separated[i]);
}
else
{
_leaderScores.Add(float.Parse(separated[i]));
}
}
}
private void BuildStringFromLists()
{
string temp = "";
if (_leaderNames.Count == 0) return;
if(_leaderNames.Count != _leaderScores.Count)
{
Debug.Log("The number of scores doesn't match the number of names. If there is no high score yet, hopefully this will resolve itself.");
foreach (float score in _leaderScores)
{
Debug.Log("score: " + score);
}
foreach (string name in _leaderNames)
{
Debug.Log("name: " + name);
}
return;
}
for (int i = 0; i < _leaderNames.Count ; i++)
{
temp += _leaderNames[i] + ',';
temp += _leaderScores[i].ToString() + ',';
}
temp = temp.Remove(temp.Length - 1);
if (scoresString != temp)
{
scoresString = temp;
}
}
public void AddLeaderboardEntry(float score, string playerName)
{
SeparateStringToLists();
if (_leaderScores.Count == 0)
{
_leaderScores.Add(score);
_leaderNames.Clear();
_leaderNames.Add(playerName);
BuildStringFromLists();
return;
}
if (_leaderScores.Count != _leaderNames.Count) Debug.LogError("WTF is this???");
bool didExit = false;
for (int i = 0; i < _leaderScores.Count; i++)
{
if (score > _leaderScores[i])
{
_leaderScores.Insert(i, score);
_leaderNames.Insert(i, playerName);
didExit = true;
break;
}
}
if (! didExit && _leaderScores.Count < 5)
{
_leaderScores.Add(score);
_leaderNames.Add(name);
}
if (_leaderScores.Count > maxLeaderboardSize)
{
_leaderScores.RemoveAt(maxLeaderboardSize);
_leaderNames.RemoveAt(maxLeaderboardSize);
}
BuildStringFromLists();
}
private string BuildStringForUpdate()
{
string tmp = "Leaderboard";
for (int i = 0; i < _leaderScores.Count; i++)
{
switch (i)
{
case 0:
tmp += "\n1st " + _leaderNames[i] + "\n" + _leaderScores[i].ToString();
break;
case 1:
tmp += "\n2nd " + _leaderNames[i] + "\n" + _leaderScores[i].ToString();
break;
case 2:
tmp += "\n3rd " + _leaderNames[i] + "\n" + _leaderScores[i].ToString();
break;
default:
tmp += "\n" + (i + 1) + "th " + _leaderNames[i] + "\n" + _leaderScores[i].ToString();
break;
}
}
return tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment