SDK/MrAG/Achievements.cs

128 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
namespace MrAG
{
public class Achievements
{
public class GamerServicesAchievementsException : Exception{
public GamerServicesAchievementsException(string msg): base(msg){
}
}
public class Achievement{
public short ID;
public string Name;
public string Desc;
public string Hash;
public int CurrentValue;
public int TargetValue;
public bool Completed;
public byte Points;
}
public static Dictionary<string, Achievement> AchievementList = new Dictionary<string,Achievement>();
public static void Load(){
AchievementList = new Dictionary<string,Achievement>();
Gamerservices.GetAPIConnection().Send(2, 0, (byte)0);
/*
string[] lines = Networking.PostData("achievement", "sub=list");
for (short i = 0; i < lines.Length; i++){
AchievementList[lines[i]] = new Achievement();
AchievementList[lines[i]].Name = lines[i];
AchievementList[lines[i]].Desc = lines[i + 1];
AchievementList[lines[i]].CurrentValue = int.Parse(lines[i + 2]);
AchievementList[lines[i]].TargetValue = int.Parse(lines[i + 3]);
AchievementList[lines[i]].Completed = AchievementList[lines[i]].CurrentValue >= AchievementList[lines[i]].TargetValue;
AchievementList[lines[i]].ID = i;
i += 3;
}*/
}
public static Achievement Get(string ach){
if (!Gamerservices.IsLoggedIn()) return null;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
return AchievementList[ach];
}
public static Dictionary<string, Achievement> GetAll(){
return AchievementList;
}
public static void Add(string ach, int value){
if (!Gamerservices.IsLoggedIn()) return;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
if (AchievementList[ach].Completed){
return;
}else{
if (AchievementList[ach].CurrentValue + value > AchievementList[ach].TargetValue) {
value = AchievementList[ach].TargetValue - AchievementList[ach].CurrentValue;
}
AchievementList[ach].CurrentValue += value;
AchievementList[ach].Completed = AchievementList[ach].CurrentValue >= AchievementList[ach].TargetValue;
if (AchievementList[ach].Completed){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
}
Gamerservices.GetAPIConnection().Send(2, 9 + AchievementList[ach].Hash.Length, (byte)1, AchievementList[ach].Hash, value);
/*
new Thread(new ThreadStart(delegate{
string[] res = Networking.PostData("achievement", "sub=add&achievement[0]=" + ach + "&val[0]=" + value);
if (res.Length == 1){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
})).Start();
* */
}
public static void Set(string ach, int value){
if (!Gamerservices.IsLoggedIn()) return;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
if (AchievementList[ach].Completed){
return;
}else{
AchievementList[ach].CurrentValue = value;
AchievementList[ach].Completed = AchievementList[ach].CurrentValue >= AchievementList[ach].TargetValue;
if (AchievementList[ach].Completed){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
}
Gamerservices.GetAPIConnection().Send(2, 9 + AchievementList[ach].Hash.Length, (byte)2, AchievementList[ach].Hash, value);
/*
string[] res = Networking.PostData("achievement", "sub=set&achievement[0]=" + ach + "&val[0]=" + value);
if (res.Length == 1){
Overlay.AddNotice(0, res[0]);
}
* */
}
}
}