Added AchievmentExceptions, fixed some names

This commit is contained in:
LubieKakao1212 2023-01-04 13:07:03 +01:00
parent 1bd77d9628
commit af47348919
4 changed files with 63 additions and 8 deletions

View File

@ -0,0 +1,44 @@
using JetBrains.Annotations;
using System;
namespace NEG.Utils.Achievments
{
public class AchievmentException : ApplicationException
{
/// <summary>
/// Id of related achievment
/// </summary>
/// <seealso cref="AchievmentTypes.AchievmentData"/>
/// <seealso cref="AchievmentTypes.AchievmentDefinition"/>
public string Id { get; private set; }
public AchievmentException(string message, string achievmentId) : base(message)
{
Id = achievmentId;
}
}
public class AchievmentTypeExcetion : AchievmentException
{
/// <summary>
/// Expected achievment type under <see cref="AchievmentException.Id"/>
/// </summary>
/// <seealso cref="AchievmentTypes.AchievmentData"/>
/// <seealso cref="AchievmentTypes.AchievmentDefinition"/>
public Type Expected { get; private set; }
/// <summary>
/// Actual achievment type under <see cref="AchievmentException.Id"/>
/// </summary>
/// <seealso cref="AchievmentTypes.AchievmentData"/>
/// <seealso cref="AchievmentTypes.AchievmentDefinition"/>
public Type Actual { get; private set; }
public AchievmentTypeExcetion(string message, string achievmentId, Type expectedType, Type actualType) : base(message, achievmentId)
{
Expected = expectedType;
Actual = actualType;
}
}
}

View File

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

View File

@ -264,7 +264,7 @@ namespace NEG.Utils.Achievments
/// <remarks>throws an <see cref="ApplicationException"/> if either there is no achievment under id or achievment under id is of a different type</remarks>
public T GetAchievmentForId<T>(string id) where T : AchievmentData
{
return CheckAchievmentType<T>(GetAchievmentForId(id));
return ValidateAchievmentType<T>(GetAchievmentForId(id));
}
/// <summary>
@ -281,15 +281,15 @@ namespace NEG.Utils.Achievments
}
else
{
throw new ApplicationException($"Invalid achivment id {id}");
throw new AchievmentException($"Invalid achivment id {id}", id);
}
}
private T CheckAchievmentType<T>(AchievmentData data) where T : AchievmentData
private T ValidateAchievmentType<T>(AchievmentData data) where T : AchievmentData
{
if (data is not T convetred)
{
throw new ApplicationException($"Attempting to perform an operation on an invalid achievment type. Expected {typeof(T)} got {data.GetType()}");
throw new AchievmentTypeExcetion($"Attempting to perform an operation on an invalid achievment type. Expected {typeof(T)} got {data.GetType()}", data.Achivment.Id, typeof(T), data.GetType());
}
return convetred;
}

View File

@ -11,13 +11,13 @@ namespace NEG.Utils.Achievments
/// <summary>
/// Called when an achivment is completed
/// </summary>
/// <param name="achivment"></param>
void AchievmentCompleted(AchievmentData achivment);
/// <param name="achievment"></param>
void AchievmentCompleted(AchievmentData achievment);
/// <summary>
/// Called when achivment progress changes
/// </summary>
/// <param name="achivment"></param>
void AchievmentStateChanged(AchievmentData achivment);
/// <param name="achievment"></param>
void AchievmentStateChanged(AchievmentData achievment);
}
}