Skip to content

Instantly share code, notes, and snippets.

@dwcullop
Last active July 27, 2018 20:38
Show Gist options
  • Save dwcullop/9c6b7933fe23163e59b94da1997adee7 to your computer and use it in GitHub Desktop.
Save dwcullop/9c6b7933fe23163e59b94da1997adee7 to your computer and use it in GitHub Desktop.
Helper Classes for using the Star Wars Galaxy of Heroes API provided by www.swgoh.help and a simple example of how to use it. The API requires a login but you can get one by joining their discord server: https://discord.gg/6SbH8kE The helper classes and AllyCode classes are files copied verbatim from another project (that's why the odd namespace…
using DareWare.SWGoH.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DareWare.SWGoH.Data.WebServices.SwgohHelp
{
#region WebService Data Classes
/////////////////
public class PlayerInfo
{
public string name { get; set; }
public int level { get; set; }
public int allyCode { get; set; }
public string guildName { get; set; }
public int gpFull { get; set; }
public int gpChar { get; set; }
public int gpShip { get; set; }
public Roster[] roster { get; set; }
public Arena arena { get; set; }
public long updated { get; set; }
public DateTimeOffset Updated => DateTimeOffset.FromUnixTimeMilliseconds( this.updated );
}
public class Arena
{
[JsonProperty( "char" )]
public Ranking _char { get; set; }
public Ranking ship { get; set; }
}
public class Ranking
{
public int rank { get; set; }
public Squad[] squad { get; set; }
}
public class Squad
{
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Roster
{
public string id { get; set; }
public string defId { get; set; }
public string name { get; set; }
public string type { get; set; }
public int rarity { get; set; }
public int level { get; set; }
public float gp { get; set; }
public int xp { get; set; }
public int gear { get; set; }
public Equipped[] equipped { get; set; }
public Skill[] skills { get; set; }
public Crew[] crew { get; set; }
public Mod[] mods { get; set; }
}
public class Equipped
{
public string equipmentId { get; set; }
public int slot { get; set; }
}
public class Skill
{
public int tier { get; set; }
public string name { get; set; }
public bool isZeta { get; set; }
public string type { get; set; }
}
public class Crew
{
public string unitId { get; set; }
public int slot { get; set; }
public SkillReference[] skillReferenceList { get; set; }
public int cp { get; set; }
public float gp { get; set; }
}
public class SkillReference
{
public string skillId { get; set; }
public int requiredTier { get; set; }
public int requiredRarity { get; set; }
}
public class Mod
{
public string id { get; set; }
public string slot { get; set; }
public int setId { get; set; }
public string set { get; set; }
public int level { get; set; }
public int pips { get; set; }
public string primaryBonusType { get; set; }
public string primaryBonusValue { get; set; }
public string secondaryType_1 { get; set; }
public string secondaryValue_1 { get; set; }
public string secondaryType_2 { get; set; }
public string secondaryValue_2 { get; set; }
public string secondaryType_3 { get; set; }
public string secondaryValue_3 { get; set; }
public string secondaryType_4 { get; set; }
public string secondaryValue_4 { get; set; }
}
/////////////////
public class Battle
{
public string id { get; set; }
public string name { get; set; }
public string desc { get; set; }
public string type { get; set; }
public Map[] mapList { get; set; }
}
public class Map
{
public BattleInfo[] easy { get; set; }
public BattleInfo[] hard { get; set; }
}
public class BattleInfo
{
public string id { get; set; }
public string desc { get; set; }
public object[] rewards { get; set; }
}
public class Reward
{
public string id { get; set; }
public string name { get; set; }
public object desc { get; set; }
}
/////////////////
public class Event
{
public string id { get; set; }
public int priority { get; set; }
public string name { get; set; }
public string summary { get; set; }
public string desc { get; set; }
public int type { get; set; }
public int status { get; set; }
public Schedule[] schedule { get; set; }
}
public class Schedule
{
public long start { get; set; }
public long end { get; set; }
public long show { get; set; }
public long hide { get; set; }
public DateTimeOffset Start => DateTimeOffset.FromUnixTimeMilliseconds( this.start );
public DateTimeOffset End => DateTimeOffset.FromUnixTimeMilliseconds( this.end );
public DateTimeOffset Show => DateTimeOffset.FromUnixTimeMilliseconds( this.show );
public DateTimeOffset Hide => DateTimeOffset.FromUnixTimeMilliseconds( this.hide );
}
/////////////////
public class Gear
{
public string id { get; set; }
public string name { get; set; }
public EquipmentStat equipmentStat { get; set; }
public MissionList[] lookupMissionList { get; set; }
public string mark { get; set; }
public MissionList[] raidLookupList { get; set; }
public object[] actionLinkLookupList { get; set; }
}
public class EquipmentStat
{
public StatList[] statList { get; set; }
}
public class StatList
{
public int unitStatId { get; set; }
public int statValueDecimal { get; set; }
public decimal unscaledDecimalValue { get; set; }
}
public class MissionList
{
public MissionIdentifier missionIdentifier { get; set; }
[JsonProperty( "event" )] public bool _event { get; set; }
}
public class MissionIdentifier
{
public string campaignId { get; set; }
public string campaignMapId { get; set; }
public string campaignNodeId { get; set; }
public int campaignNodeDifficulty { get; set; }
public string campaignMissionId { get; set; }
}
/////////////////
public class ModSetInfo
{
public string name { get; set; }
public int count { get; set; }
public UnitStat bonus { get; set; }
}
public class UnitStat
{
public int unitStatId { get; set; }
public int statValueDecimal { get; set; }
public decimal unscaledDecimalValue { get; set; }
}
/////////////////
public class SkillInfo
{
public string name { get; set; }
public bool isZeta { get; set; }
}
/////////////////
public class TerritoryBattleInfo
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public int roundCount { get; set; }
public ConflictZone[] conflictZoneDefinitionList { get; set; }
public ZoneDefinitionList[] strikeZoneDefinitionList { get; set; }
public ReconZoneDefinition[] reconZoneDefinitionList { get; set; }
public Description[] dynamicDescriptionList { get; set; }
public ZoneDefinitionList[] covertZoneDefinitionList { get; set; }
public StatCategory[] statCategoryList { get; set; }
}
public class ConflictZone
{
public int combatType { get; set; }
public VictoryPointsRewards[] victoryPointRewardsList { get; set; }
public ZoneDefinition zoneDefinition { get; set; }
}
public class ZoneDefinition
{
public string zoneId { get; set; }
public string name { get; set; }
public string description { get; set; }
public string linkedConflictId { get; set; }
}
public class VictoryPointsRewards
{
public int galacticScoreRequirement { get; set; }
}
public class ZoneDefinitionList
{
public ZoneDefinition zoneDefinition { get; set; }
}
public class ReconZoneDefinition
{
public ZoneDefinition zoneDefinition { get; set; }
public string goalDesc { get; set; }
public int unitRarity { get; set; }
public string rewardDesc { get; set; }
public string subTitle { get; set; }
}
public class Description
{
public string text { get; set; }
}
public class StatCategory
{
public string id { get; set; }
public string name { get; set; }
}
/////////////////
public class UnitInfo
{
public string name { get; set; }
public UnitStat[] baseStats { get; set; }
public int alignment { get; set; }
public string type { get; set; }
public Crew[] crew { get; set; }
public string[] tags { get; set; }
}
/////////////////
public class PlayerModList
{
public string name { get; set; }
public int allyCode { get; set; }
public long updated { get; set; }
public PlayerMod[] mods { get; set; }
public DateTimeOffset Updated => DateTimeOffset.FromUnixTimeMilliseconds( this.updated );
}
public class PlayerMod
{
public string slot { get; set; }
public int setId { get; set; }
public string set { get; set; }
public int level { get; set; }
public int pips { get; set; }
public string primaryBonusType { get; set; }
public string primaryBonusValue { get; set; }
public string secondaryType_1 { get; set; }
public string secondaryValue_1 { get; set; }
public string secondaryType_2 { get; set; }
public string secondaryValue_2 { get; set; }
public string secondaryType_3 { get; set; }
public string secondaryValue_3 { get; set; }
public string secondaryType_4 { get; set; }
public string secondaryValue_4 { get; set; }
public string characterName { get; set; }
public string mod_uid { get; set; }
}
/////////////////
public class GuildInfo
{
public string name { get; set; }
public string desc { get; set; }
public int members { get; set; }
public int required { get; set; }
public string bannerColor { get; set; }
public string bannerLogo { get; set; }
public string message { get; set; }
public int gp { get; set; }
public Raid raid { get; set; }
public long updated { get; set; }
public DateTimeOffset Updated => DateTimeOffset.FromUnixTimeMilliseconds( this.updated );
}
public class Raid
{
public string rancor { get; set; }
public string aat { get; set; }
}
#endregion
#region API Helper Class
public class ApiHelper
{
private class LoginResponse
{
public string token_type { get; set; }
public string access_token { get; set; }
public int expires_in { get; set; }
}
public string Username { get; set; }
public string Password { set; private get; }
private string AccessToken { get; set; }
public ApiHelper()
{
}
public void Login( string username, string password )
{
this.Username = username;
this.Password = password;
Login();
}
public void Login()
{
LoginResponse token = null;
var web = new WebClient();
var response = web.UploadValues(URL_BASE + URL_SIGNIN, HTTP_METHOD, GetLoginVals());
var json = web.Encoding.GetString(response);
token = JsonConvert.DeserializeObject<LoginResponse>( json );
if ( token != null )
{
this.AccessToken = token.access_token;
}
else
{
throw new ArgumentException( "Failed to get AccessToken" );
}
}
public PlayerInfo GetPlayer( AllyCode allyCode )
{
ThrowInvalidAllyCode( allyCode );
return Invoke<PlayerInfo>( URL_PLAYER, allyCode );
}
public PlayerModList GetPlayerMods( AllyCode allyCode )
{
ThrowInvalidAllyCode( allyCode );
return Invoke<PlayerModList>( URL_PLAYER, allyCode, PLAYER_MODS );
}
public Battle[] GetBattles() => Invoke<Battle[]>( URL_DATA, DATA_BATTLES );
public Event[] GetEvents() => Invoke<Event[]>( URL_DATA, DATA_EVENTS );
public IDictionary<string, Gear> GetGear() => Invoke<IDictionary<string, Gear>>( URL_DATA, DATA_GEAR );
public ModSetInfo[] GetModSets() => Invoke<ModSetInfo[]>( URL_DATA, DATA_MODSETS );
public IDictionary<string, SkillInfo> GetSkills() => Invoke<IDictionary<string, SkillInfo>>( URL_DATA, DATA_SKILLS );
public TerritoryBattleInfo[] GetTBs() => Invoke<TerritoryBattleInfo[]>( URL_DATA, DATA_TB );
public IDictionary<string, UnitInfo> GetUnits() => Invoke<IDictionary<string, UnitInfo>>( URL_DATA, DATA_UNITS );
public GuildInfo GetGuild( AllyCode allyCode )
{
ThrowInvalidAllyCode( allyCode );
return Invoke<GuildInfo>( URL_GUILD, allyCode, GUILD_DETAILS );
}
private T Invoke<T>( params string[] parts )
{
return JsonConvert.DeserializeObject<T>( Invoke( parts ) );
}
private string Invoke( params string[] parts )
{
string url = String.Join("/", parts.Where(s => !String.IsNullOrEmpty(s)));
if ( String.IsNullOrEmpty( url ) )
{
throw new ArgumentNullException( nameof( parts ) );
}
if ( String.IsNullOrEmpty( this.AccessToken ) )
{
Login();
}
var web = new WebClient();
web.Headers.Add( "Authorization", "Bearer " + this.AccessToken );
var result = web.UploadString(URL_BASE + url, HTTP_METHOD, "");
return result;
}
private NameValueCollection GetLoginVals() => GetLoginVals( this.Username, this.Password );
static private NameValueCollection GetLoginVals( string username, string password )
{
if ( String.IsNullOrEmpty( username ) )
{
throw new ArgumentNullException( nameof( username ) );
}
if ( String.IsNullOrEmpty( password ) )
{
throw new ArgumentNullException( nameof( password ) );
}
return new NameValueCollection
{
{"username", username},
{"password", password},
{"grant_type", "password"},
{"client_id", "123"},
{"client_secret", "ABC"}
};
}
static private void ThrowInvalidAllyCode( AllyCode ac ) { if ( !ac ) throw new ArgumentException( "AllyCode" ); }
static private readonly string URL_BASE = "https://api.swgoh.help";
static private readonly string URL_SIGNIN = "/auth/signin";
static private readonly string URL_DATA = "/swgoh/data";
static private readonly string URL_PLAYER = "/swgoh/player";
static private readonly string URL_GUILD = "/swgoh/guild";
static private readonly string HTTP_METHOD = "POST";
static private readonly string DATA_BATTLES = "battles";
static private readonly string DATA_EVENTS = "events";
static private readonly string DATA_UNITS = "units";
static private readonly string DATA_ARENA = "arena";
static private readonly string DATA_GEAR = "gear";
static private readonly string DATA_MODSETS = "mod-sets";
static private readonly string DATA_MODSTATS = "mod-stats";
static private readonly string DATA_SKILLS = "skills";
static private readonly string DATA_SKILLTYPES = "skill-types";
static private readonly string DATA_TB = "tb";
//static private readonly string DATA_ZETAS = "zetas";
static private readonly string DATA_ZETA_ABILITIIES = "zeta-abilities";
static private readonly string DATA_ZETA_RECOMMENDATIONS = "zeta-recommendations";
static private readonly string PLAYER_MODS = "mods";
static private readonly string PLAYER_ZETAS = "zetas";
static private readonly string GUILD_DETAILS = "details";
}
#endregion
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SwgohHelpApi
{
using ApiHelper = DareWare.SWGoH.Data.WebServices.SwgohHelp.ApiHelper;
class Program
{
private static string ExeName => Assembly.GetEntryAssembly().Location.Split( Path.DirectorySeparatorChar ).Last();
static void Main( string[] args )
{
if ( args.Length < 2 )
{
Console.WriteLine( "Usage: " + ExeName + " <ApiUserName> <ApiPassword> [<AllyCode>]" );
return;
}
var api = new ApiHelper()
{
Username = args[0],
Password = args[1]
};
var events = api.GetEvents();
var heists = events.Where( e => e.id.Contains("CREDIT_HEIST") );
if ( heists.Any() )
{
Console.WriteLine( "The next credit heist starts at " + heists.First().schedule.First().Start.ToLocalTime() );
}
else
{
Console.WriteLine( "No credit heists coming up" );
}
if ( args.Length >= 3 )
{
var me = api.GetPlayer( args[2] );
Console.WriteLine( $"As of {me.Updated.ToLocalTime()}, {me.name} is #{me.arena._char.rank} in Squad Arena and #{me.arena.ship.rank} in Fleet Arena" );
}
Console.WriteLine( "Please press the any key to continue..." );
Console.ReadKey();
}
}
}
using System;
namespace DareWare.SWGoH.Core
{
public struct AllyCode : IEquatable<AllyCode>, IComparable<AllyCode>
{
#region Constructors
public AllyCode( uint ac ) : this( ac, true ) { }
public AllyCode( string ac ) : this( uint.Parse( ac ), true ) { }
public AllyCode( uint ac, bool validate )
{
if ( validate ) ThrowInvalid( ac );
_value = ac;
}
#endregion
#region The Property / Field
private readonly uint _value;
public uint Value => _value;
#endregion
#region Methods
public bool IsValid() => IsValid( this.Value );
#endregion
#region Object Overrides
public override string ToString() => this.Value.ToString().Insert( 6, "-" ).Insert( 3, "-" );
public override bool Equals( object obj ) => ( obj is AllyCode ac ) ? Equals( ac ) : base.Equals( obj );
public override int GetHashCode() => this.Value.GetHashCode();
#endregion
#region IEquatable
public bool Equals( AllyCode ac ) => this.Value.Equals( ac.Value );
#endregion
#region IComparable
public int CompareTo( AllyCode ac ) => this.Value.CompareTo( ac.Value );
#endregion
#region Conversation Operators
public static implicit operator bool( AllyCode ac ) => ac.IsValid();
public static implicit operator uint( AllyCode ac ) => ac.Value;
public static implicit operator string( AllyCode ac ) => ac.Value.ToString();
public static implicit operator AllyCode( string s ) => new AllyCode( s );
public static implicit operator AllyCode( uint n ) => new AllyCode( n );
#endregion
#region Comparison Operators
public static bool operator ==( AllyCode left, AllyCode right ) => left.Equals( right );
public static bool operator !=( AllyCode left, AllyCode right ) => !left.Equals( right );
public static bool operator >( AllyCode left, AllyCode right ) => left.CompareTo( right ) >= 1;
public static bool operator <( AllyCode left, AllyCode right ) => left.CompareTo( right ) <= -1;
public static bool operator >=( AllyCode left, AllyCode right ) => left.CompareTo( right ) >= 0;
public static bool operator <=( AllyCode left, AllyCode right ) => left.CompareTo( right ) <= 0;
#endregion
#region Public Static Values
public static AllyCode None { get; } = new AllyCode( 0, false );
public static AllyCode MaxValue { get; } = new AllyCode( _max, false );
public static AllyCode MinValue { get; } = new AllyCode( _min, false );
#endregion
#region Private Static Helpers
private static void ThrowInvalid( uint ac ) { if ( !IsValid( ac ) ) throw new ArgumentOutOfRangeException(); }
private static bool IsValid( uint ac ) => ( ( ac >= _min ) && ( ac <= _max ) );
private static uint _max = 999_999_999U;
private static uint _min = 100_000_000U;
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment