achivments #1

Closed
Ghost wants to merge 13 commits from achivments into main
12 changed files with 191 additions and 19 deletions
Showing only changes of commit e71f0ec8da - Show all commits

View File

@ -17,7 +17,7 @@ namespace NEG.Utils.Achievments
{
if (instance == null)
{
instance = AchievmentManager.Builder.FromDefaultConfig()
instance = AchievmentManager.Builder.FromLabeledConfig(ConfigLabel)
.WithLabeledBackend(BackendLabel)
.Build();
}
@ -32,17 +32,37 @@ namespace NEG.Utils.Achievments
{
if(instance != null)
{
throw new ApplicationException("Achievments - Cannot set backend label, Managed already created");
//Log + Quit helps debug builds
Debug.LogError("Achievments - Cannot set backend label, Managed already created");
Application.Quit(1);
}
if (backendLabel != null)
{
throw new ApplicationException("Multiple AchievmentBackends enabled, this is not allowed");
//Log + Quit helps debug builds
Debug.LogError("Multiple AchievmentBackends enabled, this is not allowed");
Application.Quit(1);
}
backendLabel = value;
}
}
private static string backendLabel;
/// <summary>
/// You shouldn't have any reason to change this
/// Used for tests.
/// </summary>
public static string ConfigLabel
{
private get => configLabel;
set => configLabel = value;
}
/// <summary>
/// You shouldn't have any reason to change this
/// Used for tests.
/// </summary>
private static string configLabel = "Achivments";
private static AchievmentManager instance;
#region Achievment Manipulation (Sets, Gets)
@ -56,7 +76,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id</remarks>
public static bool IsCompleted(string id)
{
return instance.IsCompleted(id);
return Instance.IsCompleted(id);
}
#region Toggle
@ -68,7 +88,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool SetToggleAchivment(string id)
{
return instance.SetToggleAchivment(id);
return Instance.SetToggleAchivment(id);
}
/// <summary>
@ -79,7 +99,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool GetToggleState(string id)
{
return instance.GetToggleState(id);
return Instance.GetToggleState(id);
}
#endregion
@ -92,7 +112,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool SetIntProgress(string id, int progress)
{
return instance.SetIntProgress(id, progress);
return Instance.SetIntProgress(id, progress);
}
/// <summary>
@ -103,7 +123,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool ChangeIntProgress(string id, int delta)
{
return instance.ChangeIntProgress(id, delta);
return Instance.ChangeIntProgress(id, delta);
}
/// <summary>
@ -114,7 +134,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static int GetIntProgress(string id)
{
return instance.GetIntProgress(id);
return Instance.GetIntProgress(id);
}
#endregion
@ -127,7 +147,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool SetFloatProgress(string id, float progress)
{
return instance.SetFloatProgress(id, progress);
return Instance.SetFloatProgress(id, progress);
}
/// <summary>
@ -138,7 +158,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static bool ChangeFloatProgress(string id, float delta)
{
return instance.ChangeFloatProgress(id, delta);
return Instance.ChangeFloatProgress(id, delta);
}
/// <summary>
@ -149,10 +169,22 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
public static float GetFloatProgress(string id)
{
return instance.GetFloatProgress(id);
return Instance.GetFloatProgress(id);
}
#endregion
#endregion
#region Test Api
/// <summary>
/// You shouldn't have any reason to use this <br/>
/// Use at your own risk, may cause unexpected behaviour <br/>
/// Used for tests
/// </summary>
public static void NullifyInstance()
{
instance = null;
}
#endregion
}
}

View File

@ -9,7 +9,7 @@ using UnityEngine;
namespace NEG.Utils.Achievments
{
[CreateAssetMenu(menuName = "Achievments/Config/Backend/Local")]
public class LoaclBackendConfig : ScriptableObject, IAchievmentBackendConfig
public class LocalBackendConfig : ScriptableObject, IAchievmentBackendConfig
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void Init()
@ -51,7 +51,7 @@ namespace NEG.Utils.Achievments
achievmentObj["completed"] = true;
token.Replace(achievmentObj);
jobj[id] = achievmentObj;
SaveJson(jobj);
}
@ -81,8 +81,7 @@ namespace NEG.Utils.Achievments
break;
}
token.Replace(achievmentObj);
jobj[id] = achievmentObj;
SaveJson(jobj);
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d7c140577a904c8419a760a8ac6133c7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using UnityEngine;
namespace NEG.Utils.Achievments.Tests
{
public class BackendTests
{
//If stests start to fail first make sure these are correct in relation to test config asset
public const string configLabel = "TestAchievments";
public const string backendLabel = "AchievmentsLocalTests";
public const string saveLocation = "./LocalAchievments/Tests.json";
public const string AchievmentIdToggle = "TOGGLE";
public const string AchievmentIdInt = "INT";
public const string AchievmentIdFloat = "FLOAT";
[OneTimeSetUp]
public void OneTtimeSetup()
{
Achievment.BackendLabel = backendLabel;
Achievment.ConfigLabel = configLabel;
}
[TearDown]
public void TearDown()
{
Achievment.NullifyInstance();
if (File.Exists(saveLocation))
{
File.Delete(saveLocation);
}
}
[Test]
public void ReadWrite()
{
//We assume that the achievments are set correctly because otherwise other tests would fail
Achievment.SetToggleAchivment(AchievmentIdToggle);
Achievment.SetIntProgress(AchievmentIdInt, 20);
Achievment.SetFloatProgress(AchievmentIdFloat, 20);
//We need to assume NullifyInstance works correctly because we dont have access to an AchievmentManager which has not syncked yet
Achievment.NullifyInstance();
Assert.IsTrue(Achievment.IsCompleted(AchievmentIdToggle));
Assert.AreEqual(Achievment.GetIntProgress(AchievmentIdInt), 20);
Assert.AreEqual(Achievment.GetFloatProgress(AchievmentIdFloat), 20, 0f);
Achievment.SetIntProgress(AchievmentIdInt, 30);
Achievment.SetFloatProgress(AchievmentIdFloat, 30);
Achievment.NullifyInstance();
Assert.AreEqual(Achievment.GetIntProgress(AchievmentIdInt), 30);
Assert.AreEqual(Achievment.GetFloatProgress(AchievmentIdFloat), 30, 0f);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c09ce9536c2c5f541bb7d07d5eca1d69
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,22 @@
{
"name": "NEG.Utils.Achievments.Tests.Playmode",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"NEG.Utils.Achivments"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 38e8b1e483202e14182d34baaea3958e
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fad16eb700fc70c408c359dca9a76fc9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6a1257a87feec064697193df412554d4, type: 3}
m_Name: TestLocalBackend
m_EditorClassIdentifier:
saveLocation: ./LocalAchievments/Tests.json

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 47c9689c811dc9842a5a5e9ca19c6e3c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,5 @@
Static Achievments class
Static Achievments class (done)
Review

This file shouldn't exist

This file shouldn't exist
Review

will fix

will fix
Implement Storage again API (done)
Fix typos
Fix typos
Merge AchievmentCollection with AchievmentManagerConfig (done)
Static backend constructors
Static backend constructors (done)