Skip to content

Commit

Permalink
Add support for grouped multiple invasion reward subscription. (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
versx authored Jun 27, 2021
1 parent 3a69817 commit e5222d0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 110 deletions.
2 changes: 2 additions & 0 deletions migrations/11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `invasions`
MODIFY COLUMN `reward_pokemon_id` text DEFAULT NULL;
202 changes: 96 additions & 106 deletions src/Commands/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,42 +1072,40 @@ public async Task InvMeAsync(CommandContext ctx,
return;
}

foreach (var (pokemonId, form) in validation.Valid)
var valid = string.Join(",", validation.Valid.Keys.ToList());
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonIdString == valid);
if (subInvasion != null)
{
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonId == pokemonId);
if (subInvasion != null)
// Existing invasion subscription
// Loop all areas, check if the area is already in subs, if not add it
foreach (var area in areas)
{
// Existing invasion subscription
// Loop all areas, check if the area is already in subs, if not add it
foreach (var area in areas)
if (!subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
if (!subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
subInvasion.Areas.Add(area);
}
subInvasion.Areas.Add(area);
}
// Save quest subscription and continue;
// REVIEW: Might not be needed
subInvasion.Save();
}
else
// Save quest subscription and continue;
// REVIEW: Might not be needed
subInvasion.Save();
}
else
{
// New invasion subscription
subscription.Invasions.Add(new InvasionSubscription
{
// New invasion subscription
subscription.Invasions.Add(new InvasionSubscription
{
GuildId = guildId,
UserId = ctx.User.Id,
RewardPokemonId = pokemonId,
Areas = areas
});
}
GuildId = guildId,
UserId = ctx.User.Id,
RewardPokemonIdString = valid,
Areas = areas
});
}
subscription.Save();

var valid = validation.Valid.Keys.Select(x => MasterFile.GetPokemon(x, 0).Name);
var validPokemonNames = validation.Valid.Keys.Select(x => MasterFile.GetPokemon(x, 0).Name);
await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_INVASION_SUBSCRIPTIONS_SUBSCRIBE").FormatText(
ctx.User.Username,
string.Compare(poke, Strings.All, true) == 0 ? Strings.All : string.Join(", ", valid),
string.Compare(poke, Strings.All, true) == 0 ? Strings.All : string.Join(", ", validPokemonNames),
string.IsNullOrEmpty(city)
? Translator.Instance.Translate("SUBSCRIPTIONS_FROM_ALL_CITIES")
: Translator.Instance.Translate("SUBSCRIPTIONS_FROM_CITY").FormatText(city))
Expand Down Expand Up @@ -1161,46 +1159,43 @@ await ctx.RespondEmbed(Translator.Instance.Translate("ERROR_NO_INVASION_SUBSCRIP
}

var areas = SubscriptionAreas.GetAreas(_dep.WhConfig.Servers[guildId], city);
foreach (var item in validation.Valid)
{
var pokemonId = item.Key;
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonId == pokemonId);
// Check if subscribed
if (subInvasion == null)
return;
var valid = string.Join(",", validation.Valid.Keys.ToList());
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonIdString == valid);
// Check if subscribed
if (subInvasion == null)
return;

foreach (var area in areas)
foreach (var area in areas)
{
if (subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
if (subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
var index = subInvasion.Areas.FindIndex(x => string.Compare(x, area, true) == 0);
subInvasion.Areas.RemoveAt(index);
}
var index = subInvasion.Areas.FindIndex(x => string.Compare(x, area, true) == 0);
subInvasion.Areas.RemoveAt(index);
}
}

// Check if there are no more areas set for the invasion subscription
//if (subInvasion.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the invasion subscription, delete it
if (!subInvasion.Id.Remove<InvasionSubscription>())
{
_logger.Error($"Unable to remove invasion subscription for user id {subInvasion.UserId} from guild id {subInvasion.GuildId}");
}
}
else
// Check if there are no more areas set for the invasion subscription
//if (subInvasion.Areas.Count == 0)
// If no city specified then remove the whole subscription
if (string.IsNullOrEmpty(city))
{
// If no more areas set for the invasion subscription, delete it
if (!subInvasion.Id.Remove<InvasionSubscription>())
{
// Save/update invasion subscription if cities still assigned
subInvasion.Save();
_logger.Error($"Unable to remove invasion subscription for user id {subInvasion.UserId} from guild id {subInvasion.GuildId}");
}
}
else
{
// Save/update invasion subscription if cities still assigned
subInvasion.Save();
}
subscription.Save();

var valid = validation.Valid.Keys.Select(x => MasterFile.GetPokemon(x, 0).Name);
var validPokemonNames = validation.Valid.Keys.Select(x => MasterFile.GetPokemon(x, 0).Name);
await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_INVASION_SUBSCRIPTIONS_UNSUBSCRIBE").FormatText(
ctx.User.Username,
string.Compare(poke, Strings.All, true) == 0 ? Strings.All : string.Join(", ", valid),
string.Compare(poke, Strings.All, true) == 0 ? Strings.All : string.Join(", ", validPokemonNames),
string.IsNullOrEmpty(city)
? Translator.Instance.Translate("SUBSCRIPTIONS_FROM_ALL_CITIES")
: Translator.Instance.Translate("SUBSCRIPTIONS_FROM_CITY").FormatText(city))
Expand Down Expand Up @@ -1835,44 +1830,41 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_QUEST_SUBSCRIPTION
var invasionPokemon = await invasionInput.GetPokemonResult(_dep.WhConfig.MaxPokemonId);
var invasionAreas = await invasionInput.GetAreasResult(guildId);

var valid = string.Join(",", invasionPokemon.Valid.ToList());
var validPokemonNames = string.Join(", ", invasionPokemon.Valid.Select(x => MasterFile.Instance.Pokedex[x.Key].Name));
foreach (var (pokemonId, form) in invasionPokemon.Valid)
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonIdString == valid);
if (subInvasion != null)
{
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonId == pokemonId);
if (subInvasion != null)
// Existing invasion subscription
// Loop all areas, check if the area is already in subs, if not add it
foreach (var area in invasionAreas)
{
// Existing invasion subscription
// Loop all areas, check if the area is already in subs, if not add it
foreach (var area in invasionAreas)
if (!subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
if (!subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
subInvasion.Areas.Add(area);
}
subInvasion.Areas.Add(area);
}
// Save quest subscription and continue;
// REVIEW: Might not be needed
subInvasion.Save();
}
else
// Save quest subscription and continue;
// REVIEW: Might not be needed
subInvasion.Save();
}
else
{
// New invasion subscription
subscription.Invasions.Add(new InvasionSubscription
{
// New invasion subscription
subscription.Invasions.Add(new InvasionSubscription
{
GuildId = guildId,
UserId = ctx.User.Id,
RewardPokemonId = pokemonId,
Areas = invasionAreas
});
}
GuildId = guildId,
UserId = ctx.User.Id,
RewardPokemonIdString = valid,
Areas = invasionAreas
});
}
var result = subscription.Save();
if (!result)
{
}

var isAll = string.Compare(Strings.All, validPokemonNames, true) == 0;
var valid = invasionPokemon.Valid.Keys.Select(x => MasterFile.GetPokemon(x, 0).Name);
await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_INVASION_SUBSCRIPTIONS_SUBSCRIBE").FormatText(
ctx.User.Username,
isAll ? Strings.All : validPokemonNames,
Expand Down Expand Up @@ -2289,41 +2281,39 @@ await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_QUEST_SUBSCRIPTION
var invasionPokemonResult = await invasionInput.GetPokemonResult(_dep.WhConfig.MaxPokemonId);
var invasionAreasResult = await invasionInput.GetAreasResult(guildId);

foreach (var item in invasionPokemonResult.Valid)
{
var pokemonId = item.Key;
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonId == pokemonId);
// Check if subscribed
if (subInvasion == null)
return;
var valid = string.Join(",", invasionPokemonResult.Valid.Keys.ToList());
var subInvasion = subscription.Invasions.FirstOrDefault(x => x.RewardPokemonIdString == valid);
// Check if subscribed
if (subInvasion == null)
return;

foreach (var area in invasionAreasResult)
foreach (var area in invasionAreasResult)
{
if (subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
if (subInvasion.Areas.Select(x => x.ToLower()).Contains(area.ToLower()))
{
var index = subInvasion.Areas.FindIndex(x => string.Compare(x, area, true) == 0);
subInvasion.Areas.RemoveAt(index);
}
var index = subInvasion.Areas.FindIndex(x => string.Compare(x, area, true) == 0);
subInvasion.Areas.RemoveAt(index);
}
}

// Check if there are no more areas set for the invasion subscription
if (subInvasion.Areas.Count == 0)
{
// If no more areas set for the invasion subscription, delete it
if (!subInvasion.Id.Remove<InvasionSubscription>())
{
_logger.Error($"Unable to remove invasion subscription for user id {subInvasion.UserId} from guild id {subInvasion.GuildId}");
}
}
else
// Check if there are no more areas set for the invasion subscription
if (subInvasion.Areas.Count == 0)
{
// If no more areas set for the invasion subscription, delete it
if (!subInvasion.Id.Remove<InvasionSubscription>())
{
// Save/update invasion subscription if cities still assigned
subInvasion.Save();
_logger.Error($"Unable to remove invasion subscription for user id {subInvasion.UserId} from guild id {subInvasion.GuildId}");
}
}
else
{
// Save/update invasion subscription if cities still assigned
subInvasion.Save();
}
subscription.Save();

var validPokemonNames = string.Join("**, **", invasionPokemonResult.Valid.Select(x => MasterFile.Instance.Pokedex[x.Key].Name));
var validIds = valid.Split(',').Select(x => uint.Parse(x));
var validPokemonNames = string.Join("**, **", validIds.Select(x => MasterFile.Instance.Pokedex[(int)x].Name));
var isAll = string.Compare(Strings.All, validPokemonNames, true) == 0;
await ctx.RespondEmbed(Translator.Instance.Translate("SUCCESS_INVASION_SUBSCRIPTIONS_UNSUBSCRIBE").FormatText(
ctx.User.Username,
Expand Down Expand Up @@ -3113,13 +3103,13 @@ private List<string> GetInvasionSubscriptionNames(ulong guildId, ulong userId)
var list = new List<string>();
var subscription = _dep.SubscriptionProcessor.Manager.GetUserSubscriptions(guildId, userId);
var subscribedInvasions = subscription.Invasions;
subscribedInvasions.Sort((x, y) => string.Compare(MasterFile.GetPokemon(x.RewardPokemonId, 0).Name, MasterFile.GetPokemon(y.RewardPokemonId, 0).Name, true));
subscribedInvasions.Sort((x, y) => string.Compare(string.Join(", ", x.RewardPokemonId.Select(a => MasterFile.GetPokemon((int)a, 0).Name)), string.Join(", ", y.RewardPokemonId.Select(b => MasterFile.GetPokemon((int)b, 0).Name)), true));
var cityRoles = _dep.WhConfig.Servers[guildId].Geofences.Select(x => x.Name.ToLower());

foreach (var invasion in subscribedInvasions)
{
var isAllCities = cityRoles.ScrambledEquals(invasion.Areas, StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true));
list.Add(Translator.Instance.Translate("NOTIFY_FROM").FormatText(MasterFile.GetPokemon(invasion.RewardPokemonId, 0).Name, isAllCities ? Translator.Instance.Translate("ALL_AREAS") : string.Join(", ", invasion.Areas)));
list.Add(Translator.Instance.Translate("NOTIFY_FROM").FormatText(string.Join(", ", invasion.RewardPokemonId.Select(x => MasterFile.GetPokemon((int)x, 0).Name)), isAllCities ? Translator.Instance.Translate("ALL_AREAS") : string.Join(", ", invasion.Areas)));
}

return list;
Expand Down
11 changes: 10 additions & 1 deletion src/Data/Subscriptions/Models/InvasionSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Linq;

using InvasionCharacter = POGOProtos.Rpc.EnumWrapper.Types.InvasionCharacter;
using Newtonsoft.Json;
Expand Down Expand Up @@ -31,11 +32,19 @@ public class InvasionSubscription : SubscriptionItem
]
public InvasionCharacter InvasionType { get; set; }

[
JsonIgnore,
Ignore,
]
public List<uint> RewardPokemonId => RewardPokemonIdString?.Split(',')?
.Select(x => uint.Parse(x))
.ToList();

[
JsonProperty("reward_pokemon_id"),
Alias("reward_pokemon_id"),
]
public uint RewardPokemonId { get; set; }
public string RewardPokemonIdString { get; set; }

[
JsonProperty("city"),
Expand Down
7 changes: 5 additions & 2 deletions src/Data/Subscriptions/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using WhMgr.Configuration;
using WhMgr.Data.Subscriptions.Models;
using WhMgr.Diagnostics;
using WhMgr.Extensions;
using WhMgr.Net.Models;

/// <summary>
Expand Down Expand Up @@ -192,15 +193,17 @@ public List<SubscriptionObject> GetUserSubscriptionsByInvasion(string pokestopNa
return _subscriptions?
.Where(x => x.IsEnabled(NotificationStatusType.Invasions) &&
x.Invasions != null &&
x.Invasions.Exists(y =>
encounterRewards.Contains(y.RewardPokemonId) ||
x.Invasions.Exists(y =>
y.RewardPokemonId.Intersects(encounterRewards) ||
gruntType == y.InvasionType ||
(y.PokestopName != null && (pokestopName.Contains(y.PokestopName) || string.Equals(pokestopName, y.PokestopName, StringComparison.OrdinalIgnoreCase)))
)
)
.ToList();
}



/// <summary>
/// Gets user subscriptions from subscribed Pokestop lures
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Subscriptions/SubscriptionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ GeofenceItem GetGeofence(ulong guildId)
continue;
}

var subInvasion = user.Invasions.FirstOrDefault(x => encounters.Contains(x.RewardPokemonId));
var subInvasion = user.Invasions.FirstOrDefault(x => x.RewardPokemonId.Intersects(encounters));
// Not subscribed to invasion
if (subInvasion == null)
{
Expand Down
Loading

0 comments on commit e5222d0

Please sign in to comment.