using ODMO.Commons.DTOs.Routine; using ODMO.Commons.Interfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace ODMO.Infrastructure.Repositories.Routine { public class RoutineRepository : IRoutineRepository { private readonly DatabaseContext _context; private readonly ILogger _logger; public RoutineRepository(DatabaseContext context, ILogger logger) { _context = context; _logger = logger; } // -------------------------------------------------------------------- // Bitwise helpers // -------------------------------------------------------------------- public class BitwiseOperations { public static int GetBitValue(int[] array, int x) { int arrIDX = x / 32; int bitPosition = x % 32; if (array == null || arrIDX >= array.Length) throw new ArgumentOutOfRangeException(nameof(x), "Invalid array index"); int value = array[arrIDX]; return (value >> bitPosition) & 1; } public static void SetBitValue(ref int[] array, int x, int bitValue) { int arrIDX = x / 32; int bitPosition = x % 32; if (array == null || arrIDX >= array.Length) throw new ArgumentOutOfRangeException(nameof(x), "Invalid array index"); if (bitValue != 0 && bitValue != 1) throw new ArgumentException("Invalid bit value. Only 0 or 1 are allowed."); int mask = 1 << bitPosition; if (bitValue == 1) array[arrIDX] |= mask; else array[arrIDX] &= ~mask; } } // -------------------------------------------------------------------- // DAILY QUEST RESET (CRASH-SAFE) // -------------------------------------------------------------------- public async Task ExecuteDailyQuestsAsync(List questIdList) { if (questIdList == null || questIdList.Count == 0) return; var progressList = await _context.CharacterProgress.ToListAsync(); foreach (var progress in progressList) { if (progress == null) continue; // 🔴 CRITICAL FIX: initialize NULL data if (progress.CompletedDataValue == null) { _logger.LogWarning( "CharacterProgress {CharacterProgressId} has NULL CompletedDataValue. Initializing.", progress.Id ); // 8 ints = 256 quest flags (adjust if needed) progress.CompletedDataValue = new int[8]; } foreach (var questId in questIdList) { progress.CompletedDataValue = MarkQuestIncomplete( questId, progress.CompletedDataValue ); } } await _context.SaveChangesAsync(); } // -------------------------------------------------------------------- // SAFE BIT RESET // -------------------------------------------------------------------- public int[] MarkQuestIncomplete(int qIDX, int[] completeDataInt) { if (completeDataInt == null) return completeDataInt; int bitIndex = qIDX - 1; if (bitIndex < 0) return completeDataInt; int requiredLength = (bitIndex / 32) + 1; if (completeDataInt.Length < requiredLength) return completeDataInt; int bitValue = BitwiseOperations.GetBitValue(completeDataInt, bitIndex); if (bitValue == 1) { BitwiseOperations.SetBitValue(ref completeDataInt, bitIndex, 0); } return completeDataInt; } // -------------------------------------------------------------------- // READ-ONLY ROUTINES (AsNoTracking OK) // -------------------------------------------------------------------- public async Task> GetActiveRoutinesAsync() { return await _context.Routine.AsNoTracking().Where(x => x.Active).ToListAsync(); } // -------------------------------------------------------------------- // UPDATE ROUTINE EXECUTION TIME (TRACKED) // -------------------------------------------------------------------- public async Task UpdateRoutineExecutionTimeAsync(long routineId) { var dto = await _context.Routine.FirstOrDefaultAsync(x => x.Id == routineId); if (dto != null) { dto.NextRunTime = dto.NextRunTime.AddDays(dto.Interval); await _context.SaveChangesAsync(); } } } }