Skip to content

Commit

Permalink
- Create pvp subscription table.
Browse files Browse the repository at this point in the history
- Add DropDefaultTables method.
  • Loading branch information
versx committed Dec 15, 2019
1 parent ebc20ba commit 5fe9f3e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/Commands/CommunityDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,11 @@ public async Task RemoveAsync(CommandContext ctx,
}
await ctx.RespondEmbed(message);
}

public async Task StatsAsync(CommandContext ctx,
[Description("")] string pokemon)
{
await Task.CompletedTask;
}
}
}
23 changes: 13 additions & 10 deletions src/Commands/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,9 @@ public async Task PokeMeAsync(CommandContext ctx,
for (int i = 1; i < Strings.MaxPokemonIds; i++)
{
var subPkmn = subscription.Pokemon.FirstOrDefault(x => x.PokemonId == i);
//TODO: Check if uxie, mesprite, azelf
//Always ignore the user's input for Unown and set it to 0 by default.
var minIV = i == 201 ? 0 : realIV;
var minLvl = i == 201 ? 0 : lvl;
//Always ignore the user's input for Unown, Azelf, Mesprit, or Uxie and set it to 0 by default.
var minIV = IsRarePokemon(i) ? 0 : realIV;
var minLvl = IsRarePokemon(i) ? 0 : lvl;
if (subPkmn == null)
{
//Does not exist, create.
Expand Down Expand Up @@ -1304,18 +1303,22 @@ public async Task ImportAsync(CommandContext ctx)
return;
}

var oldSubscription = _dep.SubscriptionProcessor.Manager.GetUserSubscriptions(ctx.Guild.Id, ctx.User.Id);
if (oldSubscription != null)
{
var result = Data.Subscriptions.SubscriptionManager.RemoveAllUserSubscriptions(ctx.Guild.Id, ctx.User.Id);
if (!result)
{
_logger.Error($"Failed to clear old user subscriptions for {ctx.User.Username} ({ctx.User.Id}) in guild {ctx.Guild?.Name} ({ctx.Guild?.Id}) before importing.");
}
}

var subscription = JsonConvert.DeserializeObject<SubscriptionObject>(data);
if (subscription == null)
{
await ctx.RespondEmbed(_dep.Language.Translate("NOTIFY_IMPORT_MALFORMED_DATA").FormatText(ctx.User.Username), DiscordColor.Red);
return;
}
//TODO: Drop existing before import?
//subscription?.Pokemon?.ForEach(x => x.SubscriptionId = 0);
//subscription?.Raids?.ForEach(x => x.SubscriptionId = 0);
//subscription?.Quests?.ForEach(x => x.SubscriptionId = 0);
//subscription?.Invasions?.ForEach(x => x.SubscriptionId = 0);
//subscription?.Gyms?.ForEach(x => x.SubscriptionId = 0);
subscription.Save();
await ctx.RespondEmbed(_dep.Language.Translate("NOTIFY_IMPORT_SUCCESS").FormatText(ctx.User.Username));
}
Expand Down
38 changes: 36 additions & 2 deletions src/Data/Subscriptions/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public SubscriptionManager(WhConfig whConfig)
_connFactory = new OrmLiteConnectionFactory(_whConfig.Database.Main.ToString(), MySqlDialect.Provider);
//_scanConnFactory = new OrmLiteConnectionFactory(_whConfig.Database.Scanner.ToString(), MySqlDialect.Provider);

CreateDefaultTables();
if (!CreateDefaultTables())
{
_logger.Error("FAiled to create default tables");
}
ReloadSubscriptions();
}

Expand Down Expand Up @@ -171,6 +174,7 @@ public static bool RemoveAllUserSubscriptions(ulong guildId, ulong userId)
using (var conn = DataAccessLayer.CreateFactory().Open())
{
conn.Delete<PokemonSubscription>(x => x.GuildId == guildId && x.UserId == userId);
conn.Delete<PvPSubscription>(x => x.GuildId == guildId && x.UserId == userId);
conn.Delete<RaidSubscription>(x => x.GuildId == guildId && x.UserId == userId);
conn.Delete<QuestSubscription>(x => x.GuildId == guildId && x.UserId == userId);
conn.Delete<GymSubscription>(x => x.GuildId == guildId && x.UserId == userId);
Expand All @@ -192,7 +196,7 @@ public static bool RemoveAllUserSubscriptions(ulong guildId, ulong userId)

#region Private Methods

private void CreateDefaultTables()
private bool CreateDefaultTables()
{
_logger.Trace($"SubscriptionManager::CreateDefaultTables");

Expand Down Expand Up @@ -234,11 +238,41 @@ private void CreateDefaultTables()
}

_logger.Info($"Database tables created.");
return true;
}
catch (Exception ex)
{
_logger.Error(ex);
}
return false;
}

private bool DropDefaultTables()
{
_logger.Trace($"SubscriptionManager::CreateDefaultTables");

if (!IsDbConnectionOpen())
{
throw new Exception("Not connected to database.");
}

try
{
var conn = GetConnection();
conn.DropTable<InvasionSubscription>();
conn.DropTable<QuestSubscription>();
conn.DropTable<GymSubscription>();
conn.DropTable<RaidSubscription>();
conn.DropTable<PvPSubscription>();
conn.DropTable<PokemonSubscription>();
conn.DropTable<SubscriptionObject>();
return true;
}
catch (Exception ex)
{
_logger.Error(ex);
}
return false;
}

private System.Data.IDbConnection GetConnection()
Expand Down
3 changes: 1 addition & 2 deletions src/Extensions/DiscordExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static async Task<DiscordMessage> SendDirectMessage(this DiscordClient cl
{
//_logger.Error(ex);
_logger.Error($"Failed to send DM to user {user.Username}.");
//TODO: Delete user from subscriptions
}

return null;
Expand Down Expand Up @@ -158,7 +157,7 @@ internal static async Task<bool> IsDirectMessageSupported(this DiscordMessage me
if (message?.Channel?.Guild == null)
{
//TODO: Localize
await message.RespondEmbed($"{message.Author.Mention} DM is not supported for this command.", DiscordColor.Yellow);
await message.RespondEmbed($"{message.Author.Mention} Direct message is not supported for this command.", DiscordColor.Yellow);
return false;
}

Expand Down

0 comments on commit 5fe9f3e

Please sign in to comment.