Implemented local backend (untested)
This commit is contained in:
parent
d0b4734572
commit
0270c50090
8
Achievments/Backend.meta
Normal file
8
Achievments/Backend.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3541d03056defb3468d957da0b69a7b9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Achievments/Backend/LoacalBaackend.meta
Normal file
8
Achievments/Backend/LoacalBaackend.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0226747b74e86c4b8e89d01e9eeeb3f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
129
Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs
Normal file
129
Achievments/Backend/LoacalBaackend/LoaclBackendConfig.cs
Normal file
@ -0,0 +1,129 @@
|
||||
using NEG.Utils.Achievments.AchievmentTypes;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NEG.Utils.Achievments
|
||||
{
|
||||
public class LoaclBackendConfig : ScriptableObject, IAchievmentBackendConfig
|
||||
{
|
||||
[SerializeField]
|
||||
private string saveLocation;
|
||||
|
||||
public IAchievmentBackend ConstructBackend()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalBackend : IAchievmentBackend
|
||||
{
|
||||
private string saveLocation;
|
||||
|
||||
public LocalBackend(string saveLocation)
|
||||
{
|
||||
this.saveLocation = saveLocation;
|
||||
}
|
||||
|
||||
public void AchievmentCompleted(AchievmentData achievment)
|
||||
{
|
||||
string id = achievment.Achievment.Id;
|
||||
JObject jobj = LoadJson();
|
||||
|
||||
JToken token = jobj[id];
|
||||
|
||||
if (token is not JObject achievmentObj)
|
||||
{
|
||||
achievmentObj = new JObject();
|
||||
}
|
||||
|
||||
achievmentObj["completed"] = true;
|
||||
|
||||
token.Replace(achievmentObj);
|
||||
|
||||
SaveJson(jobj);
|
||||
}
|
||||
|
||||
public void AchievmentStateChanged(AchievmentData achievment)
|
||||
{
|
||||
string id = achievment.Achievment.Id;
|
||||
JObject jobj = LoadJson();
|
||||
|
||||
JToken token = jobj[id];
|
||||
|
||||
if (token is not JObject achievmentObj)
|
||||
{
|
||||
achievmentObj = new JObject();
|
||||
}
|
||||
|
||||
switch (achievment)
|
||||
{
|
||||
case IntAchievmentData intAchievment:
|
||||
achievmentObj["data"] = intAchievment.CurrentProgress;
|
||||
break;
|
||||
case FloatAchievmentData floatAchievment:
|
||||
achievmentObj["data"] = floatAchievment.CurrentProgress;
|
||||
break;
|
||||
case ToggleAchievmentData toggleAchievment:
|
||||
achievmentObj["data"] = toggleAchievment.IsCompleted;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
token.Replace(achievmentObj);
|
||||
|
||||
SaveJson(jobj);
|
||||
}
|
||||
|
||||
public AchievmentData GetStoredAchivment(AchievmentDefinition definition)
|
||||
{
|
||||
JObject jobj = LoadJson();
|
||||
|
||||
JToken token = jobj[definition.Id];
|
||||
|
||||
if (token is not JObject)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
AchievmentData achievment = definition.Construct();
|
||||
|
||||
switch (achievment)
|
||||
{
|
||||
case IntAchievmentData intAchievment:
|
||||
intAchievment.CurrentProgress = (int)token["data"];
|
||||
break;
|
||||
case FloatAchievmentData floatAchievment:
|
||||
floatAchievment.CurrentProgress = (float)token["data"];
|
||||
break;
|
||||
case ToggleAchievmentData toggleAchievment:
|
||||
toggleAchievment.CompletionState = (bool)token["data"];
|
||||
break;
|
||||
}
|
||||
|
||||
return achievment;
|
||||
}
|
||||
|
||||
private JObject LoadJson()
|
||||
{
|
||||
if (Directory.Exists(Path.GetDirectoryName(saveLocation)) && File.Exists(saveLocation))
|
||||
{
|
||||
return JObject.Parse(File.ReadAllText(saveLocation));
|
||||
}
|
||||
return new JObject();
|
||||
}
|
||||
|
||||
private void SaveJson(JObject obj)
|
||||
{
|
||||
if (!Directory.Exists(Path.GetDirectoryName(saveLocation)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(saveLocation));
|
||||
}
|
||||
|
||||
File.WriteAllText(saveLocation, obj.ToString(Formatting.Indented));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a1257a87feec064697193df412554d4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user