using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zeta;
using Zeta.Common;
using Zeta.Common.Plugins;
using System.Windows;
using Zeta.CommonBot;
using Zeta.Internals.Actors;
using Zeta.CommonBot.Items;
using System.IO;
using System.Security.Cryptography;
namespace NaiceMatrix.AccountManagement
{
public class NaiceAccountNotificationHandler
{
#region Singleton
private static NaiceAccountNotificationHandler _instance = null;
public static NaiceAccountNotificationHandler Instance
{
get
{
if (_instance == null)
_instance = new NaiceAccountNotificationHandler();
return _instance;
}
}
private NaiceAccountNotificationHandler()
{
}
#endregion
private DateTime _dtLastUpdate = DateTime.MinValue;
public void OnPulse()
{
if (Settings.NotificationAPIKey == "")
return;
if ((DateTime.Now - _dtLastUpdate).TotalMinutes > Settings.WEBSERVICE_PostAccountIntervall)
{
if (Settings.DUMP_DEBUG)
{
NaiceMatrix.I(" ");
NaiceMatrix.I("BEGIN REPORT");
NaiceMatrix.I(" ");
}
if (_dtLastUpdate == DateTime.MinValue)
{
// skip first report!
_dtLastUpdate = DateTime.Now;
}
if (ZetaDia.IsInGame && !ZetaDia.IsLoadingWorld && !ZetaDia.IsPlayingCutscene &&
ZetaDia.Me != null && ZetaDia.Me.IsValid && ZetaDia.Service != null && ZetaDia.Service.CurrentHero != null)
{
string url = Settings.NotificationAPIUrl + Settings.WEBSERVICE_PostAccountModule;
string postData =
string.Format("{0}\n{1}",
Settings.NotificationAPIKey,
GenerateAccountDataString());
// perform http post
try
{
Tools.DoHttpPost(url, postData);
}
catch (Exception ex)
{
NaiceMatrix.L(ex.Message);
}
_dtLastUpdate = DateTime.Now;
}
}
}
public static string GenerateAccountDataString()
{
StringBuilder dStr = new StringBuilder();
if (Zeta.CommonBot.BotMain.CurrentBot != null)
dStr.AppendFormat("BotName={0}\n", Zeta.CommonBot.BotMain.CurrentBot.Name);
if (ZetaDia.Service != null && ZetaDia.Service.CurrentHero != null)
{
dStr.AppendFormat("HeroName={0}\n", ZetaDia.Service.CurrentHero.Name);
dStr.AppendFormat("HeroBattleTagName={0}\n", GetHash(ZetaDia.Service.CurrentHero.BattleTagName));
dStr.AppendFormat("HeroClass={0}\n", ZetaDia.Service.CurrentHero.Class.ToString());
dStr.AppendFormat("HeroCurrentDifficulty={0}\n", ZetaDia.Service.CurrentHero.CurrentDifficulty.ToString());
dStr.AppendFormat("HeroLevel={0}\n", ZetaDia.Service.CurrentHero.Level);
dStr.AppendFormat("HeroAct={0}\n", ZetaDia.Service.CurrentHero.Act.ToString());
}
if (ZetaDia.Me != null && ZetaDia.Me.IsValid)
{
if (ZetaDia.Me.Inventory != null)
dStr.AppendFormat("Coinage={0}\n", ZetaDia.Me.Inventory.Coinage);
dStr.AppendFormat("MeName={0}\n", ZetaDia.Me.Name);
dStr.AppendFormat("MeXPNextLevel={0}\n", ZetaDia.Me.ExperienceNextLevel);
dStr.AppendFormat("MeXP={0}\n", ZetaDia.Me.CurrentExperience);
dStr.AppendFormat("MeXPParagon={0}\n", ZetaDia.Me.ParagonCurrentExperience);
dStr.AppendFormat("MeXPParagonNextLevel={0}\n", ZetaDia.Me.ParagonExperienceNextLevel);
dStr.AppendFormat("MeLevel={0}\n", ZetaDia.Me.Level);
dStr.AppendFormat("MeLevelCap={0}\n", ZetaDia.Me.LevelCap);
dStr.AppendFormat("MeLevelParagon={0}\n", ZetaDia.Me.ParagonLevel);
dStr.AppendFormat("MeParagonGoldFind={0}\n", ZetaDia.Me.ParagonGoldFindTotal);
dStr.AppendFormat("MeParagonMagicFind={0}\n", ZetaDia.Me.ParagonMagicFindTotal);
}
dStr.AppendFormat("GameStatsDeathPerHour={0}\n", GameStats.Instance.DeathsPerHour);
dStr.AppendFormat("GameStatsXPPerHour={0}\n", GameStats.Instance.ExpPerHour);
dStr.AppendFormat("GameStatsGamesPerHour={0}\n", GameStats.Instance.GamesPerHour);
dStr.AppendFormat("GameStatsGoldPerHour={0}\n", GameStats.Instance.GoldPerHour);
byte[] toEncodeAsBytes = Encoding.UTF8.GetBytes(dStr.ToString());
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
private static string DumpProperties<T>(string prefix, T obj)
{
Type type = obj.GetType();
StringBuilder dStr = new StringBuilder();
foreach (var prop in type.GetProperties())
{
try
{
string name = prop.Name;
object value = prop.GetValue(obj, null);
dStr.AppendFormat("{0}{1}={2}\n", prefix, name, value.ToString());
}
catch (Exception e)
{
NaiceMatrix.I("StatError: {0}", e.Message);
}
}
return dStr.ToString();
}
public static string GetHash(string text)
{
var CSP = new SHA256CryptoServiceProvider();
byte[] arrayData;
byte[] arrayResult;
string result = null;
string temp = null;
arrayData = Encoding.ASCII.GetBytes(text);
arrayResult = CSP.ComputeHash(arrayData);
for (int i = 0; i < arrayResult.Length; i++)
{
temp = Convert.ToString(arrayResult[i], 16);
if (temp.Length == 1)
temp = "0" + temp;
result += temp;
}
return result;
}
}
}