Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace EveKillboard.Services.EveKillServices
- {
- using System;
- using System.Globalization;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using Core.Services;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using System.Diagnostics;
- public sealed class KillmailFeedProvider : IKillmailFeedServices
- {
- /// <summary>
- ///
- /// </summary>
- private enum OutputOptionsEnum
- {
- NotDefined = 0,
- /// <summary>
- /// Show the URL to EVE-KILL.
- /// </summary>
- Url = 1,
- /// <summary>
- /// Show the Timestamp.
- /// </summary>
- TimeStamp = 2,
- /// <summary>
- /// Show the internal EVE-KILL kill ID.
- /// </summary>
- InternalId = 4,
- /// <summary>
- /// Show the CCP API kill ID.
- /// </summary>
- CcpId = 8,
- /// <summary>
- /// Show the Victim Name.
- /// </summary>
- VictimName = 16,
- /// <summary>
- /// Show the Victim External ID.
- /// </summary>
- VictimId = 32,
- /// <summary>
- /// Show the Victim Corp Name.
- /// </summary>
- VictimCorporationName = 64,
- /// <summary>
- /// Show the Victim Alliance Name.
- /// </summary>
- VictimAllianceName = 128,
- /// <summary>
- /// Show the Victim Ship Name.
- /// </summary>
- VictimShipName = 256,
- /// <summary>
- /// Show the Victim Ships Class Name.
- /// </summary>
- VictimnShipClassName = 512,
- /// <summary>
- /// Show the Victim Ships External ID.
- /// </summary>
- VictimShipId = 1024,
- /// <summary>
- /// Show the FB Pilots name.
- /// </summary>
- FinalBlowPilotName = 2048,
- /// <summary>
- /// Show the FB Pilots corp name.
- /// </summary>
- FinalBlowPilotCorporationName = 4096,
- /// <summary>
- /// Show the FB Pilots alliance name.
- /// </summary>
- FinalBlowPilotAllianceName = 8192,
- /// <summary>
- /// Show the count of involved pilots.
- /// </summary>
- InvolvedCount = 16384,
- /// <summary>
- /// Show the Solar System the kill happened in.
- /// </summary>
- System = 32768,
- /// <summary>
- /// Show the Solar System true security.
- /// </summary>
- SystemSecurity = 65536,
- /// <summary>
- /// Show the Regions name.
- /// </summary>
- RegionName = 131072,
- /// <summary>
- /// Show the ISK value of the kill (total loss (ship + modules)).
- /// </summary>
- TotalValue = 262144,
- /// <summary>
- /// Show the pilots involved.
- /// </summary>
- InvolvedPilots = 524288,
- /// <summary>
- /// Show the items that was destroyed and dropped.
- /// </summary>
- Items = 1048576,
- /// <summary>
- /// Default output option
- /// </summary>
- Default = TimeStamp | InternalId | CcpId | VictimName | VictimId | VictimCorporationName | VictimAllianceName | VictimShipId | System | InvolvedPilots | Items
- }
- private JsonSerializer _serializer = null;
- public KillmailFeedProvider()
- {
- _serializer = JsonSerializer.Create(new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore });
- _serializer.Converters.Add(new FeedKillmailConverter());
- _serializer.Converters.Add(new FeedKillmailInvolvedConverter());
- _serializer.Converters.Add(new FeedKillmailInvolvedListConverter());
- _serializer.Converters.Add(new DroppedKillmailItemListConverter());
- _serializer.Converters.Add(new DestroyedKillmailItemListConverter());
- }
- ~KillmailFeedProvider()
- {
- _serializer = null;
- }
- public void Dispose()
- {
- GC.SuppressFinalize(this);
- _serializer = null;
- }
- public IEnumerable<IFeedKillmail> GetKillmails(int[] allianceIds)
- {
- var result = new List<IFeedKillmail>();
- try
- {
- var week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.UtcNow.Date, CultureInfo.InvariantCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.InvariantCulture.DateTimeFormat.FirstDayOfWeek);
- foreach (var allianceId in allianceIds)
- {
- var uri = Uri.EscapeUriString(string.Format("http://eve-kill.net/epic/combinedAllianceExtID:{1}/mask:{0}", (Int32)OutputOptionsEnum.Default, allianceId,week));
- var request = (HttpWebRequest)WebRequest.Create(uri);
- request.KeepAlive = false;
- request.Timeout = 10000;
- var response = request.GetResponse();
- var st = response.GetResponseStream();
- if(st ==null) continue;
- string content = null;
- using (st)
- using (var reader = new StreamReader(st))
- content = reader.ReadToEnd();
- using (var jreader = new JsonTextReader(new StringReader(content)))
- {
- var kills = _serializer.Deserialize(jreader, typeof (List<IFeedKillmail>)) as List<IFeedKillmail>;
- if (kills != null && kills.Any())
- result.AddRange(kills);
- }
- }
- }
- catch (Exception)
- {
- }
- ClearNames(result);
- return result;
- }
- private static void ClearNames(IEnumerable<IFeedKillmail> kills)
- {
- foreach (var kill in kills)
- {
- kill.VictimAllianceName = GetClearName(kill.VictimAllianceName);
- kill.VictimFactionName = GetClearName(kill.VictimFactionName);
- foreach (var involved in kill.Involved)
- {
- involved.AllianceName = GetClearName(involved.AllianceName);
- involved.FactionName = GetClearName(involved.FactionName);
- }
- }
- }
- private static string GetClearName(string name)
- {
- var temp = string.IsNullOrEmpty(name)? null : name.Replace("None", null).Replace("none", null).Replace("Unknown", null).Replace("unknown",null).Trim();
- return string.IsNullOrEmpty(temp) ? null : temp;
- }
- private class FeedKillmailConverter : CustomCreationConverter<IFeedKillmail>
- {
- public override IFeedKillmail Create(Type objectType)
- {
- return new FeedKillmail();
- }
- }
- private class FeedKillmailInvolvedConverter : CustomCreationConverter<IFeedKillmailInvolved>
- {
- public override IFeedKillmailInvolved Create(Type objectType)
- {
- return new FeedKillmailInvolved();
- }
- }
- private class FeedKillmailInvolvedListConverter : CustomCreationConverter<IEnumerable<IFeedKillmailInvolved>>
- {
- public override IEnumerable<IFeedKillmailInvolved> Create(Type objectType)
- {
- return new List<FeedKillmailInvolved>();
- }
- }
- private class DroppedKillmailItemListConverter : CustomCreationConverter<IEnumerable<DroppedKillmailItem>>
- {
- public override IEnumerable<DroppedKillmailItem> Create(Type objectType)
- {
- return new List<DroppedKillmailItem>();
- }
- }
- private class DestroyedKillmailItemListConverter : CustomCreationConverter<IEnumerable<DestroyedKillmailItem>>
- {
- public override IEnumerable<DestroyedKillmailItem> Create(Type objectType)
- {
- return new List<DestroyedKillmailItem>();
- }
- }
- [JsonObjectAttribute(MemberSerialization.OptIn)]
- internal class FeedKillmail : IFeedKillmail
- {
- [JsonProperty(PropertyName = "timestamp")]
- public DateTime Date { get; set; }
- [JsonProperty(PropertyName = "internalID")]
- public int InternalId { get; set; }
- [JsonProperty(PropertyName = "externalID")]
- public int CcpId { get; set; }
- [JsonProperty(PropertyName = "victimExternalID")]
- public int VictimId { get; set; }
- [JsonProperty(PropertyName = "victimName")]
- public string VictimName { get; set; }
- [JsonProperty(PropertyName = "victimCorpName")]
- public string VictimCorporationName { get; set; }
- [JsonIgnore]
- public int VictimCorporationId { get; set; }
- [JsonProperty(PropertyName = "victimAllianceName")]
- public string VictimAllianceName { get; set; }
- [JsonIgnore]
- public int VictimAllianceId { get; set; }
- [JsonProperty(PropertyName = "victimFactionName")]
- public string VictimFactionName { get; set; }
- [JsonIgnore]
- public int VictimFactionId { get; set; }
- [JsonProperty(PropertyName = "victimShipID")]
- public int VictimShipId { get; set; }
- [JsonIgnore]
- public string VictimShipName { get; set; }
- [JsonProperty(PropertyName = "solarSystemName")]
- public string SystemName { get; set; }
- [JsonIgnore]
- public int SystemId { get; set; }
- [JsonProperty(PropertyName = "items")]
- public KillmailItems KillItems { get; set; }
- [JsonProperty(PropertyName = "involved")]
- public IEnumerable<IFeedKillmailInvolved> Involved { get; set; }
- [JsonIgnore]
- public IEnumerable<IFeedKillmailItem> Items
- {
- get
- {
- var items = new List<FeedKillmailItem>();
- if (KillItems == null) return items;
- items.AddRange(
- KillItems.Destroyed.Select(
- i =>
- new FeedKillmailItem()
- {
- ItemId = i.ItemId,
- Quantity = i.Quantity,
- IsDropped = false,
- IsDestroyed = true,
- IsCargo = i.Flag == 5,
- IsDroneBay = i.Flag == 87,
- IsImplant = i.Flag == 89,
- IsCopy = false
- }));
- items.AddRange(
- KillItems.Dropped.Select(
- i =>
- new FeedKillmailItem()
- {
- ItemId = i.ItemId,
- Quantity = i.Quantity,
- IsDropped = true,
- IsDestroyed = false,
- IsCargo = i.Flag == 5,
- IsDroneBay = i.Flag == 87,
- IsImplant = i.Flag == 89,
- IsCopy = false
- }));
- return items;
- }
- set { }
- }
- [JsonIgnore]
- public int DamageReceived
- {
- get { return Involved.Sum(i => i.DamageDone); }
- set { }
- }
- }
- [JsonObjectAttribute(MemberSerialization.OptIn)]
- internal class KillmailItems
- {
- [JsonProperty("destroyed")]
- public IEnumerable<DestroyedKillmailItem> Destroyed { get; set; }
- [JsonProperty("dropped")]
- public IEnumerable<DroppedKillmailItem> Dropped { get; set; }
- }
- [JsonObjectAttribute(MemberSerialization.OptIn)]
- internal class DestroyedKillmailItem
- {
- [JsonProperty("typeID")]
- public int ItemId { get; set; }
- [JsonProperty("flag")]
- public int Flag { get; set; }
- [JsonProperty("qtyDestroyed")]
- public int Quantity { get; set; }
- }
- [JsonObjectAttribute(MemberSerialization.OptIn)]
- internal class DroppedKillmailItem
- {
- [JsonProperty("typeID")]
- public int ItemId { get; set; }
- [JsonProperty("flag")]
- public int Flag { get; set; }
- [JsonProperty("qtyDropped")]
- public int Quantity { get; set; }
- }
- [JsonObjectAttribute(MemberSerialization.OptIn)]
- internal class FeedKillmailInvolved : IFeedKillmailInvolved
- {
- [JsonProperty(PropertyName = "characterID")]
- public int CharacterId { get; set; }
- [JsonProperty(PropertyName = "characterName")]
- public string CharacterName { get; set; }
- [JsonProperty(PropertyName = "corporationID")]
- public int CorporationId { get; set; }
- [JsonProperty(PropertyName = "corporationName")]
- public string CorporationName { get; set; }
- [JsonProperty(PropertyName = "allianceID")]
- public int AllianceId { get; set; }
- [JsonProperty(PropertyName = "allianceName")]
- public string AllianceName { get; set; }
- [JsonProperty(PropertyName = "factionID")]
- public int FactionId { get; set; }
- [JsonProperty(PropertyName = "factionName")]
- public string FactionName { get; set; }
- [JsonProperty(PropertyName = "securityStatus")]
- public float SecurityStatus { get; set; }
- [JsonProperty(PropertyName = "damageDone")]
- public int DamageDone { get; set; }
- [JsonProperty(PropertyName = "finalBlow")]
- public bool IsFinalBlow { get; set; }
- [JsonProperty(PropertyName = "shipTypeID")]
- public int ShipId { get; set; }
- [JsonIgnore]
- public string ShipName { get; set; }
- [JsonProperty(PropertyName = "weaponTypeID")]
- public int WeaponId { get; set; }
- [JsonIgnore]
- public string WeaponName { get; set; }
- }
- internal class FeedKillmailItem : IFeedKillmailItem
- {
- public int ItemId { get; set; }
- public string ItemName { get; set; }
- public int Quantity { get; set; }
- public bool IsDestroyed { get; set; }
- public bool IsDropped { get; set; }
- public bool IsCargo { get; set; }
- public bool IsDroneBay { get; set; }
- public bool IsImplant { get; set; }
- public bool IsCopy { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment