Resolve WES-187 "Unit tests account and system"

This commit is contained in:
Dries Van Schuylenbergh
2023-04-30 15:51:41 +00:00
committed by Jerome Coudron
parent c4b6c60288
commit b9bbef8dcf
143 changed files with 2008 additions and 542 deletions

View File

@@ -26,19 +26,16 @@ public class User
/// <summary>
/// Get the username
/// </summary>
/// <returns></returns>
public string GetUsername() { return storedUserData.username; }
/// <summary>
/// Get the total playtime
/// </summary>
/// <returns></returns>
public double GetPlaytime() { return storedUserData.playtime; }
/// <summary>
/// Get the avatar
/// </summary>
/// <returns></returns>
public Sprite GetAvatar() { return UserList.AVATARS[storedUserData.avatarIndex]; }

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -70,26 +69,6 @@ public class UserProgressScreen : MonoBehaviour
/// </summary>
public RawImage progressGraph;
/// <summary>
/// Left and right padding of the graph
/// </summary>
private const int GRAPH_PADDING_X_PX = 50;
/// <summary>
/// Top and bottom padding of the graph
/// </summary>
private const int GRAPH_PADDING_Y_PX = 50;
/// <summary>
/// Radius of the point on the graph
/// </summary>
private const int GRAPH_POINT_RADIUS = 10;
/// <summary>
/// Size of the line on the graph
/// </summary>
private const int GRAPH_LINE_SIZE = 4;
/// <summary>
/// Current selected activity draw to the graph
/// </summary>
@@ -113,8 +92,7 @@ public class UserProgressScreen : MonoBehaviour
// Set correct displayed items
username.text = user.GetUsername();
avatar.sprite = user.GetAvatar();
// TODO: implement total playtime
//playtime.text = $"Totale speeltijd: {user.playtime.ToString("0.00")}";
playtime.text = $"Totale speeltijd: {user.GetPlaytime().ToString("0.00")}";
// Set graph inactive
progressGraph.gameObject.SetActive(false);
@@ -206,170 +184,6 @@ public class UserProgressScreen : MonoBehaviour
/// Plot the graph of a minigame
/// </summary>
/// <param name="minigameIndex">Index of the minigame</param>
private void DisplayMinigameGraph(MinigameIndex minigameIndex)
{
var progress = user.GetMinigameProgress(minigameIndex);
List<Score> latestScores = progress.latestScores;
List<Score> highestScores = progress.highestScores;
if (0 < highestScores.Count)
{
PlotGraph(latestScores.ConvertAll<double>((s) => (double)s.scoreValue), highestScores[0].scoreValue);
}
else
{
progressGraph.gameObject.SetActive(false);
}
}
/// <summary>
/// Plot points and a highscore on the graph
/// </summary>
/// <param name="scores">List of score values to plot</param>
/// <param name="highscore">Highscore value (this will be plotted in a fancy color)</param>
private void PlotGraph(List<double> scores, double highscore)
{
// Remove previous marker(s)
foreach (Transform child in progressGraph.gameObject.transform)
{
Destroy(child.gameObject);
}
// Get texture reference
Texture2D tex = progressGraph.texture as Texture2D;
if (tex == null)
{
RectTransform rt = progressGraph.gameObject.transform as RectTransform;
tex = new Texture2D(
width: (int)rt.sizeDelta.x,
height: (int)rt.sizeDelta.y,
textureFormat: TextureFormat.ARGB32,
mipCount: 3,
linear: true
);
}
tex.filterMode = FilterMode.Point;
// calculate positions and offsets
int x0 = GRAPH_PADDING_X_PX, x1 = tex.width - GRAPH_PADDING_X_PX;
int y0 = GRAPH_PADDING_Y_PX, y1 = tex.height - GRAPH_PADDING_Y_PX;
double min = scores.Min();
double max = scores.Max();
List<Tuple<int, int>> points = new List<Tuple<int, int>>();
for (int i = 0; i < scores.Count; i++)
{
int x = x0 + (scores.Count > 1 ? i * ((x1 - x0) / (scores.Count - 1)) : (x1 - x0) / 2);
int y = y0 + (int)((y1 - y0) * (min != max ? (scores[i] - min) / (max - min) : 0.5));
points.Add(Tuple.Create(x, y));
}
// Calculate scaling
int mag = (int)Math.Round(Math.Log10(max));
int MAG = (int)Math.Pow(10, mag);
double c = max / MAG;
// Draw axes
if (min != max)
{
for (double d = c / 5.0; d < c; d += 0.2 * c)
{
int y = y0 + (int)((y1 - y0) * (MAG * d - min) / (max - min));
DrawLine(tex, x0, y, x1, y, 2, Color.gray);
}
}
else
{
int y = y0 + (int)((y1 - y0) * 0.5);
DrawLine(tex, x0, y0, x1, y0, 2, Color.gray);
DrawLine(tex, x0, y, x1, y, 2, Color.gray);
DrawLine(tex, x0, y1, x1, y1, 2, Color.gray);
}
// Draw highscore
if (min <= highscore && highscore <= max)
{
int y = y0 + (int)((y1 - y0) * (min != max ? (highscore - min) / (max - min) : 0.5));
DrawLine(tex, x0, y, x1, y, 3, new Color(255, 192, 0));
GameObject marker = GameObject.Instantiate(highscoreMarker, progressGraph.gameObject.transform);
RectTransform rect = marker.GetComponent<RectTransform>();
rect.localPosition = new Vector3(0, y - 25, 0);
}
// Draw points
for (int i = 0; i < points.Count; i++)
{
Tuple<int, int> p = points[i];
if (0 < i)
{
Tuple<int, int> q = points[i - 1];
DrawLine(tex, p.Item1, p.Item2, q.Item1, q.Item2, GRAPH_LINE_SIZE, Color.blue);
}
DrawPoint(tex, p.Item1, p.Item2, GRAPH_POINT_RADIUS, Color.blue);
}
// Apply to graph GameObject
tex.Apply();
progressGraph.texture = tex;
}
/// <summary>
/// Draw a point to a texture
/// </summary>
/// <param name="tex">Texture2D to plot point on</param>
/// <param name="xc">Center x-pos</param>
/// <param name="yc">Center y-pos</param>
/// <param name="r">Radius (aka width and height)</param>
/// <param name="color">Color of the point</param>
private void DrawPoint(Texture2D tex, int xc, int yc, int r, Color color)
{
for (int y = yc - r; y < yc + r; y++)
{
for (int x = xc - r; x < xc + r; x++)
{
tex.SetPixel(x, y, color);
}
}
}
/// <summary>
/// Draw a line to a texture
/// </summary>
/// <param name="tex">Texture2D to plot line on</param>
/// <param name="x0">Starting x-pos</param>
/// <param name="y0">Strating y-pos</param>
/// <param name="x1">Ending x-pos</param>
/// <param name="y1">Ending y-pos</param>
/// <param name="size">Size of the line (width)</param>
/// <param name="color">Color of the line</param>
private void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, int size, Color color)
{
int w = x1 - x0;
int h = y1 - y0;
int length = Mathf.Abs(x1 - x0);
if (Mathf.Abs(y1 - y0) > length)
{
length = Mathf.Abs(h);
}
double dx = w / (double)length;
double dy = h / (double)length;
double x = x0;
double y = y0;
double r = size / 2;
for (int i = 0; i <= length; i++)
{
for (int j = (int)(y - r); j < y + r; j++)
{
for (int k = (int)(x - r); k < x + r; k++)
{
tex.SetPixel(k, j, color);
}
}
x += dx;
y += dy;
}
}
/// <remarks>TODO: reworking </remarks>
private void DisplayMinigameGraph(MinigameIndex minigameIndex) { }
}