Example #1
0
        protected async Task FleetUp()
        {
            try
            {
                //Check Fleetup Operations

                if (_lastChecked == null)
                {
                    var dateStr = await SQLHelper.GetCacheDataFleetUpLastChecked();

                    _lastChecked = DateTime.TryParseExact(dateStr,
                                                          new[]
                    {
                        "dd.MM.yyyy HH:mm:ss", $"{CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern} {CultureInfo.InvariantCulture.DateTimeFormat.LongTimePattern}"
                    },
                                                          CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out var time) ? time : DateTime.MinValue;
                }

                if (DateTime.Now > _lastChecked.Value.AddMinutes(1))
                {
                    await LogHelper.LogModule("Running FleetUp module check...", Category);

                    var userId       = SettingsManager.Settings.FleetupModule.UserId;
                    var apiCode      = SettingsManager.Settings.FleetupModule.APICode;
                    var appKey       = SettingsManager.Settings.FleetupModule.AppKey;
                    var groupID      = SettingsManager.Settings.FleetupModule.GroupID;
                    var channelid    = SettingsManager.Settings.FleetupModule.Channel;
                    var lastopid     = Convert.ToInt64(await SQLHelper.GetCacheDataFleetUpLastPosted());
                    var announcePost = SettingsManager.Settings.FleetupModule.Announce_Post;
                    var channel      = channelid == 0 ? null : APIHelper.DiscordAPI.GetChannel(channelid);

                    _lastChecked = DateTime.Now;
                    await SQLHelper.SetCacheDataFleetUpLastChecked(_lastChecked);

                    if (string.IsNullOrWhiteSpace(userId) || string.IsNullOrWhiteSpace(apiCode) || string.IsNullOrWhiteSpace(groupID) || string.IsNullOrWhiteSpace(appKey) ||
                        channel == null)
                    {
                        await LogHelper.LogInfo(LM.Get("fuNeedSetup"), Category);

                        return;
                    }

                    var result = await APIHelper.FleetUpAPI.GetOperations(Reason, userId, apiCode, appKey, groupID);

                    if (result == null)
                    {
                        return;
                    }

                    foreach (var operation in result.Data)
                    {
                        var lastAnnounce = await SQLHelper.GetFleetupAnnounce(operation.Id);

                        if (operation.OperationId > lastopid && announcePost)
                        {
                            await SendMessage(operation, channel, $"{Settings.FleetupModule.DefaultMention} FleetUp Op <https://www.taifl.org/operations#{operation.OperationId}>", true);

                            await SQLHelper.SetCacheDataFleetUpLastPosted(operation.OperationId);
                        }

                        var timeDiff = TimeSpan.FromTicks(operation.Start.Ticks - DateTime.UtcNow.Ticks);
                        //no need to notify, it is already started
                        if (lastAnnounce == 0 && timeDiff.TotalMinutes < 1)
                        {
                            continue;
                        }
                        var array = SettingsManager.Settings.FleetupModule.Announce.Where(a => a < lastAnnounce || lastAnnounce == 0).ToArray();

                        foreach (var i in array)
                        {
                            var epic1 = TimeSpan.FromMinutes(i);
                            var epic2 = TimeSpan.FromMinutes(i + 1);

                            if (timeDiff >= epic1 && timeDiff <= epic2)
                            {
                                await SendMessage(operation, channel, $"{Settings.FleetupModule.DefaultMention} {LM.Get("fuFormIn", i, $"https://www.taifl.org/operations#{operation.OperationId}")}",
                                                  false);

                                await SQLHelper.AddFleetupOp(operation.Id, i);
                            }
                        }

                        //NOW
                        if (timeDiff.TotalMinutes < 1)
                        {
                            await SendMessage(operation, channel, $"{Settings.FleetupModule.FinalTimeMention} {LM.Get("fuFormNow", $"https://www.taifl.org/operations#{operation.OperationId}")}",
                                              false);

                            await SQLHelper.DeleteFleetupOp(operation.Id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await LogHelper.LogEx($"ERROR {ex.Message}", ex, Category);
            }
        }