Example #1
0
        /// <summary>
        /// This method updates the player's online state based on the target status and the message coming in. It also sends out a discord webhook notification, if present.
        /// </summary>
        /// <param name="needle">Phrase to search for</param>
        /// <param name="haystack">Text to search</param>
        /// <param name="status">What to change the player's online state to - true for logged in, false for logged out</param>
        /// <returns>Returns true if the needle was found and the status was successfully updated</returns>
        private async Task <bool> ChangePlayerOnlineStatus(string needle, string haystack, bool status)
        {
            Regex regex   = new Regex($"{needle}(.+?),", RegexOptions.IgnoreCase);
            var   matches = regex.Matches(haystack);

            if (matches.Count() > 0)
            {
                try
                {
                    var gamerTag = matches[0].Groups[1].Value;

                    if (gamerTag != null)
                    {
                        using (var scope = _serviceProvider.CreateScope())
                        {
                            var userRepository        = scope.ServiceProvider.GetRequiredService <UserRepository>();
                            var minecraftStoreService = scope.ServiceProvider.GetRequiredService <MinecraftStoreService>();

                            _statusService.UpdateUserStatus(gamerTag, status);
                            var user = await userRepository.GetUserByGamerTagAsync(gamerTag);

                            if (status)
                            {
                                // Player has logged in
                                if (user != null && (user.LastLoginReward == null || user.LastLoginReward.Value.AddDays(1) <= DateTime.UtcNow))
                                {
                                    // daily login bonus
                                    await minecraftStoreService.AddCurrencyForUser(gamerTag, _applicationSettings.DailyLoginBonus, CurrencyTransactionReason.DailyLogin);

                                    user.LastLoginReward = DateTime.UtcNow;
                                }

                                user.LastMinecraftLogin = DateTime.UtcNow;
                                await userRepository.SaveUserAsync(user);

                                _discordService.SendWebhookMessage($"{gamerTag} has logged in!");
                            }
                            else
                            {
                                // Player has logged out
                                if (user.LastMinecraftLogin.HasValue)
                                {
                                    var seconds = (DateTime.UtcNow - user.LastMinecraftLogin.Value).TotalSeconds;
                                    await minecraftStoreService.AddCurrencyForUser(gamerTag, (decimal)seconds *_applicationSettings.PointsPerSecond, CurrencyTransactionReason.TimePlayed);
                                }
                            }
                        }
                    }

                    return(true);
                } catch (Exception ex)
                {
                    Log.Error(ex, $"An error occurred in ChangePlayerOnlineStatus({needle},{haystack},{status})");
                }
            }

            return(false);
        }
        private async Task ChangePlayerStatus(string gamertag, bool isOnline)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var userRepository        = scope.ServiceProvider.GetRequiredService <UserRepository>();
                var minecraftStoreService = scope.ServiceProvider.GetRequiredService <MinecraftStoreService>();

                _statusService.UpdateUserStatus(gamertag, isOnline);
                var user = await userRepository.GetUserByGamerTagAsync(gamertag);

                if (isOnline)
                {
                    // Player has logged in
                    if (user != null && (user.LastLoginReward == null || user.LastLoginReward.Value.AddDays(1) <= DateTime.UtcNow))
                    {
                        // daily login bonus
                        await minecraftStoreService.AddCurrencyForUser(gamertag, _applicationSettings.DailyLoginBonus, CurrencyTransactionReason.DailyLogin);

                        user.LastLoginReward = DateTime.UtcNow;
                    }

                    user.LastMinecraftLogin = DateTime.UtcNow;
                    await userRepository.SaveUserAsync(user);

                    await userRepository.AddPlaytimeEvent(user, PlaytimeEvent.LOGIN_EVENT_CODE);

                    _discordService.SendWebhookMessage($"{gamertag} has logged in!");
                }
                else
                {
                    // Player has logged out
                    if (user.LastMinecraftLogin.HasValue)
                    {
                        var seconds = (DateTime.UtcNow - user.LastMinecraftLogin.Value).TotalSeconds;

                        if (_applicationSettings.StoreEnabled)
                        {
                            await minecraftStoreService.AddCurrencyForUser(gamertag, (decimal)seconds *_applicationSettings.PointsPerSecond, CurrencyTransactionReason.TimePlayed);
                        }

                        await userRepository.AddPlaytimeEvent(user, PlaytimeEvent.LOGOUT_EVENT_CODE);

                        if (_applicationSettings.DiscordLogOutMessages)
                        {
                            _discordService.SendWebhookMessage($"{gamertag} has logged out!");
                        }
                    }
                }
            }
        }