Skip to content

Instantly share code, notes, and snippets.

@nooxouille
Created April 13, 2021 10:20
Show Gist options
  • Save nooxouille/04c780ff1b17ac4c1551ce02c01d0061 to your computer and use it in GitHub Desktop.
Save nooxouille/04c780ff1b17ac4c1551ce02c01d0061 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
public static class DBManager
{
public static string username;
public static string email;
public static int score;
public static int GetScore()
{
return score;
}
public static string GetUsername()
{
return username;
}
public static string GetEmail()
{
return email;
}
}
using Unisave.Facades;
using UnityEngine;
public class LeaderboardController : MonoBehaviour
{
// player name & player score
private string username, email;
private int score;
void Start()
{
LoadLeaderboard();
}
// Downloads the latest leaderboard from the server and displays it.
async void LoadLeaderboard()
{
LeaderboardEntity entity = await OnFacet<LeaderboardFacet>
.CallAsync<LeaderboardEntity>(
nameof(LeaderboardFacet.GetLeaderboard)
);
}
public async void AddRecord()
{
LeaderboardEntity entity = await OnFacet<LeaderboardFacet>
.CallAsync<LeaderboardEntity>(
nameof(LeaderboardFacet.AddRecord),
DBManager.GetUsername(), DBManager.GetEmail(),
DBManager.GetScore().ToString()
);
DisplayLeaderboardEntity(entity);
}
// Displays a given leaderboard to the console.
private void DisplayLeaderboardEntity(LeaderboardEntity entity)
{
Debug.Log("## Leaderboard ##");
foreach (var record in entity.records)
{
Debug.Log($" {record.username} | {record.score} points | {record.email} | {entity.EntityId} |");
}
if (entity.records.Count == 0)
{
Debug.Log("Empty leaderboard");
}
}
/*
#region New LeaderboardController
public async void DownloadLeaderboardFromServer(string username)
{
LeaderboardResponse response = await OnFacet<LeaderboardFacet>
.CallAsync<LeaderboardResponse>(
nameof(LeaderboardFacet.DownloadLeaderboard), username
);
}
#endregion
*/
}
using System;
using System.Collections;
using System.Collections.Generic;
using Unisave;
using Unisave.Entities;
using Unisave.Facades;
public class LeaderboardEntity : Entity
{
public class LeaderboardRecord
{
public string username, email;
public int score;
}
public List<LeaderboardRecord> records = new List<LeaderboardRecord>();
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unisave;
using Unisave.Facets;
using Unisave.Facades;
using Unisave.Authentication.Middleware;
using Unisave.Entities;
public class LeaderboardFacet : Facet
{
#region LeaderboardFacet v1
// Sends the only leaderboard in the database to the client.
// If no leaderboard entity exists, it gets automatically created.
public LeaderboardEntity GetLeaderboard()
{
return DB.FirstOrCreate<LeaderboardEntity>();
}
// Some player has scored, lets add them to the leaderboard
// (and then remove during truncation if the score isn't high enough)
public LeaderboardEntity AddRecord(string username, string email, int score)
{
// get the leaderboard entity
var leaderboard = DB.FirstOrCreate<LeaderboardEntity>();
// add the record
leaderboard.records.Add(new LeaderboardEntity.LeaderboardRecord() {
username = username,
email = email,
score = score
});
leaderboard.records = leaderboard.records
.OrderByDescending(r => r.score) // sort the records by score
.Take(100) // take only the top 100 players (truncate top 100)
.ToList(); // convert to list and assign it to the leaderboard entity
// save the leaderboard entity to database
leaderboard.Save();
// send the new leaderboard to the client
return leaderboard;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment