Skip to content

Commit

Permalink
Wild creatures in web-app
Browse files Browse the repository at this point in the history
* Added wild creatures and wild creature statistics to the server view.

New access control configuration settings
```
{
"accessControl": {
"server": {
"wildcreatures": [
"guest"
],
"wildcreatures-basestats": [
"guest"
],
"wildcreatures-ids": [
"guest"
],
"wildcreatures-statistics": [
"guest"
]
}
}
}
```
  • Loading branch information
tsebring committed Nov 26, 2017
1 parent a9f1d93 commit e86b09a
Show file tree
Hide file tree
Showing 35 changed files with 1,325 additions and 134 deletions.
6 changes: 6 additions & 0 deletions ArkBot/ArkBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,13 @@
<Compile Include="Voting\VoteStateChangeResult.cs" />
<Compile Include="Voting\InitiateVoteResult.cs" />
<Compile Include="Voting\VotingManager.cs" />
<Compile Include="WebApi\Controllers\WildCreaturesController.cs" />
<Compile Include="WebApi\DemoMode.cs" />
<Compile Include="WebApi\Model\WildCreatureSpeciesStatistics.cs" />
<Compile Include="WebApi\Model\WildCreatureStatistics.cs" />
<Compile Include="WebApi\Model\WildCreatureSpeciesViewModel.cs" />
<Compile Include="WebApi\Model\WildCreatureViewModel.cs" />
<Compile Include="WebApi\Model\WildCreaturesViewModel.cs" />
<Compile Include="WebApi\Model\TribeLogEntryViewModel.cs" />
<Compile Include="WebApi\PlayerIdAttribute.cs" />
<Compile Include="WebApi\AccessControlAuthorizationFilter.cs" />
Expand Down
18 changes: 18 additions & 0 deletions ArkBot/Extensions/NumberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,23 @@ public static TValue Clamp<TValue>(this TValue v, TValue? min = null, TValue? ma
{
return (min.HasValue && v.CompareTo(min.Value) < 0 ? min.Value : max.HasValue && v.CompareTo(max.Value) > 0 ? max.Value : v);
}

/// <summary>
/// Rounds a floating point number
/// </summary>
/// <returns></returns>
public static double Round(this double v, int decimals)
{
return Math.Round(v, decimals);
}

/// <summary>
/// Rounds a floating point number
/// </summary>
/// <returns></returns>
public static float Round(this float v, int decimals)
{
return (float)Math.Round(v, decimals);
}
}
}
4 changes: 2 additions & 2 deletions ArkBot/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.74.*")]
[assembly: AssemblyFileVersion("1.74.0.0")]
[assembly: AssemblyVersion("1.75.*")]
[assembly: AssemblyFileVersion("1.75.0.0")]
221 changes: 221 additions & 0 deletions ArkBot/WebApi/Controllers/WildCreaturesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using ArkBot.Ark;
using ArkBot.Data;
using ArkBot.Database;
using ArkBot.Extensions;
using ArkBot.Helpers;
using ArkBot.ViewModel;
using ArkBot.WebApi.Model;
using ArkSavegameToolkitNet.Domain;
using Discord;
using QueryMaster.GameServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;

namespace ArkBot.WebApi.Controllers
{
[AccessControl("pages", "server")]
public class WildCreaturesController : BaseApiController
{
private ArkContextManager _contextManager;

public static readonly string[] UntameableClassNames = new[]
{
"MegaCarno_Character_BP_C",
"MegaDeathworm_Character_BP_C",
"MegaWyvern_Character_BP_Fire_C",
"Alpha_Leedsichthys_Character_BP_C",
"MegaMegalodon_Character_BP_C",
"Mosa_Character_BP_Mega_C",
"MegaRaptor_Character_BP_C",
"MegaRex_Character_BP_C",
"Mega_Tusoteuthis_Character_BP_C",
"Ammonite_Character_BP_C",
"Bone_MegaWyvern_Character_BP_Fire_C",
"SpiderL_Character_BP_C",
"SpiderL_Character_BP_Easy_C",
"SpiderL_Character_BP_Medium_C",
"SpiderL_Character_BP_Hard_C",
"SpiderL_Character_BP_TheCenter_C",
"SpiderL_Character_BP_TheCenterMedium_C",
"SpiderL_Character_BP_TheCenterHard_C",
"Dodo_Character_BP_Bunny_C",
"BunnyOviRaptor_Character_BP_C",
"Cnidaria_Character_BP_C",
"Coel_Character_BP_C",
"Coel_Character_BP_Ocean_C",
"Deathworm_Character_BP_C",
"DodoWyvern_Character_BP_C",
"Dragon_Character_BP_Boss_C",
"Dragon_Character_BP_Boss_Easy_C",
"Dragon_Character_BP_Boss_Medium_C",
"Dragon_Character_BP_Boss_Hard_C",
"Dragonfly_Character_BP_C",
"Euryp_Character_C",
"PlayerPawnTest_Male_C",
"PlayerPawnTest_Female_C",
"Jugbug_Character_BaseBP_C",
"Jugbug_Oil_Character_BP_C",
"Jugbug_Water_Character_BP_C",
"Leech_Character_C",
"Leech_character_Diseased_C",
"Leedsichthys_Character_BP_C",
"Manticore_Character_BP_C",
"Manticore_Character_BP_Easy_C",
"Manticore_Character_BP_Medium_C",
"Manticore_Character_BP_Hard_C",
"Gorilla_Character_BP_C",
"Gorilla_Character_BP_Easy_C",
"Gorilla_Character_BP_Medium_C",
"Gorilla_Character_BP_Hard_C",
"Gorilla_Character_BP_TheCenter_C",
"Gorilla_Character_BP_TheCenter_Medium_C",
"Gorilla_Character_BP_TheCenter_Hard_C",
"Piranha_Character_BP_C",
"Salmon_Character_BP_C",
"Bone_Sauropod_Character_BP_C",
"Bone_MegaCarno_Character_BP_C",
"Bone_Gigant_Character_BP_C",
"Bone_Jerboa_Character_BP_C",
"Bone_Quetz_Character_BP_C",
"Bone_MegaRaptor_Character_BP_C",
"Bone_MegaRex_Character_BP_C",
"Bone_Stego_Character_BP_C",
"Bone_Trike_Character_BP_C",
"BoaFrill_Character_BP_C",
"Ant_Character_BP_C",
"FlyingAnt_Character_BP_C",
"Trilobite_Character_C",
"Turkey_Character_BP_C",
"Wyvern_Character_BP_Base_C",
"Wyvern_Character_BP_Fire_C",
"Wyvern_Character_BP_Lightning_C",
"Wyvern_Character_BP_Poison_C",
"Ragnarok_Wyvern_Override_Ice_C",
"Yeti_Character_BP_C"
};

public WildCreaturesController(ArkContextManager contextManager, IConfig config) : base(config)
{
_contextManager = contextManager;
}

public WildCreaturesViewModel Get(string id)
{
var context = _contextManager.GetServer(id);
if (context == null) return null;

var demoMode = IsDemoMode() ? new DemoMode() : null;
var result = new WildCreaturesViewModel
{
};

// access control
var incWildCreatures = HasFeatureAccess("server", "wildcreatures", id);
var incWildCreaturesBaseStats = HasFeatureAccess("server", "wildcreatures-basestats", id);
var incStatistics = HasFeatureAccess("server", "wildcreatures-statistics", id);

var speciesGroups = context.WildCreatures.GroupBy(x => x.ClassName)
.ToDictionary(x => x.Key, x => new { items = x.ToArray(), aliases = ArkSpeciesAliases.Instance.GetAliases(x.Key) });

if (incWildCreatures)
{
var species = speciesGroups.Select(x =>
{
var vms = new WildCreatureSpeciesViewModel
{
ClassName = x.Key,
Name = x.Value.aliases?.FirstOrDefault(),
Aliases = x.Value.aliases?.Skip(2).ToArray() ?? new string[] { }, //skip primary name and class name
IsTameable = !UntameableClassNames.Contains(x.Key, StringComparer.OrdinalIgnoreCase)
};

vms.Creatures.AddRange(x.Value.items.Select(y =>
{
var vmc = new WildCreatureViewModel
{
Id1 = y.Id1,
Id2 = y.Id2,
Gender = y.Gender.ToString(),
BaseLevel = y.BaseLevel,
X = y.Location?.X.Round(0),
Y = y.Location?.Y.Round(0),
Z = y.Location?.Z.Round(0),
IsTameable = y.IsTameable,
Latitude = y.Location?.Latitude,
Longitude = y.Location?.Longitude,
TopoMapX = y.Location?.TopoMapX,
TopoMapY = y.Location?.TopoMapY
};
if (incWildCreaturesBaseStats)
{
//0: health
//1: stamina
//2: torpor
//3: oxygen
//4: food
//5: water
//6: temperature
//7: weight
//8: melee damage
//9: movement speed
//10: fortitude
//11: crafting speed

vmc.BaseStats = new CreatureBaseStatsViewModel
{
Health = y.BaseStats[0],
Stamina = y.BaseStats[1],
Oxygen = y.BaseStats[3],
Food = y.BaseStats[4],
Weight = y.BaseStats[7],
Melee = y.BaseStats[8],
MovementSpeed = y.BaseStats[9]
};
}

return vmc;
}).OrderByDescending(y => y.BaseLevel).ThenBy(y => y.Gender));

return vms;
}).ToArray();

foreach (var s in species)
{
result.Species.Add(s.ClassName, s);
}
}

var totalCount = context.WildCreatures.Length;
var stats = new WildCreatureStatistics
{
CreatureCount = totalCount
};

if (incStatistics)
{
stats.Species.AddRange(speciesGroups.Select(x =>
{
var vmcs = new WildCreatureSpeciesStatistics
{
ClassName = x.Key,
Name = x.Value.aliases?.FirstOrDefault(),
Aliases = x.Value.aliases?.Skip(2).ToArray() ?? new string[] { }, //skip primary name and class name
Count = x.Value.items.Count(),
Fraction = x.Value.items.Count() / (float)totalCount
};

return vmcs;
}).OrderBy(x => x.Name).ThenByDescending(x => x.Count));
}

result.Statistics = stats;

return result;
}
}
}
22 changes: 22 additions & 0 deletions ArkBot/WebApi/Model/WildCreatureSpeciesStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArkBot.WebApi.Model
{
public class WildCreatureSpeciesStatistics
{
public WildCreatureSpeciesStatistics()
{
Aliases = new string[] { };
}

public string ClassName { get; set; }
public string Name { get; set; }
public string[] Aliases { get; set; }
public int Count { get; set; }
public float Fraction { get; set; }
}
}
24 changes: 24 additions & 0 deletions ArkBot/WebApi/Model/WildCreatureSpeciesViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArkBot.WebApi.Model
{
public class WildCreatureSpeciesViewModel
{
public WildCreatureSpeciesViewModel()
{
Aliases = new string[] { };
Creatures = new List<WildCreatureViewModel>();
}

public string ClassName { get; set; }
public string Name { get; set; }
public string[] Aliases { get; set; }
public List<WildCreatureViewModel> Creatures { get; set; }
public bool IsTameable { get; set; }
}
}
19 changes: 19 additions & 0 deletions ArkBot/WebApi/Model/WildCreatureStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArkBot.WebApi.Model
{
public class WildCreatureStatistics
{
public WildCreatureStatistics()
{
Species = new List<WildCreatureSpeciesStatistics>();
}

public int CreatureCount { get; set; }
public List<WildCreatureSpeciesStatistics> Species { get; set; }
}
}
40 changes: 40 additions & 0 deletions ArkBot/WebApi/Model/WildCreatureViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArkBot.WebApi.Model
{
public class WildCreatureViewModel
{
public WildCreatureViewModel()
{
Aliases = new string[] { };
}

public long Id1 { get; set; }
public long Id2 { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string ClassName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Species { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string[] Aliases { get; set; }
public string Gender { get; set; }
public int BaseLevel { get; set; }
public float? X { get; set; }
public float? Y { get; set; }
public float? Z { get; set; }
public float? Latitude { get; set; }
public float? Longitude { get; set; }
public float? TopoMapX { get; set; }
public float? TopoMapY { get; set; }
public bool IsTameable { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public CreatureBaseStatsViewModel BaseStats { get; set; }

}
}
Loading

0 comments on commit e86b09a

Please sign in to comment.