/// <summary> /// Retrieves feat user stats. Data is retrieved differently based on whether user is a player or NPC. /// </summary> /// <returns>The stats of the user</returns> private static UserStats GetUserStats(NWGameObject user, NWGameObject target, Feat feat) { var ability = AbilityRegistry.Get(feat); var targetLocation = GetIsObjectValid(target) ? GetLocation(target) : NWNXEvents.OnFeatUsed_GetTargetLocation(); var stats = new UserStats { User = user, AbilityDefinition = ability, Target = target, TargetLocation = targetLocation, CastingPosition = GetPosition(user), MPCost = ability.MP(user), Feat = feat }; // Players - retrieve from DB if (GetIsPlayer(user)) { var playerID = GetGlobalID(user); var player = PlayerRepo.Get(playerID); var cooldown = CooldownRepo.Get(playerID, feat); stats.MP = player.MP; stats.CooldownUnlock = cooldown.DateUnlocked; } // NPCs - retrieve from local variables else if (GetIsNPC(user)) { stats.MP = GetLocalInt(user, "MP_CURRENT"); var cooldown = GetLocalString(user, $"ABILITY_COOLDOWN_{feat}"); if (string.IsNullOrWhiteSpace(cooldown)) { stats.CooldownUnlock = DateTime.UtcNow; } else { stats.CooldownUnlock = DateTime.ParseExact(cooldown, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture); } } stats.Now = DateTime.UtcNow; return(stats); }
/// <summary> /// Applies all changes in stats to the database or creature local variables. /// </summary> /// <param name="stats">The current user stats.</param> private static void ApplyUserStatChanges(UserStats stats) { var delay = stats.AbilityDefinition.CooldownTime(stats.User); var dateUnlocks = DateTime.UtcNow.AddSeconds(delay); if (GetIsPlayer(stats.User)) { var playerID = GetGlobalID(stats.User); var player = PlayerRepo.Get(playerID); var cooldown = CooldownRepo.Get(playerID, stats.Feat); player.MP = stats.MP; cooldown.DateUnlocked = dateUnlocks; PlayerRepo.Set(player); CooldownRepo.Set(playerID, cooldown); } else if (GetIsNPC(stats.User)) { SetLocalInt(stats.User, "MP_CURRENT", stats.MP); SetLocalString(stats.User, $"ABILITY_COOLDOWN_{stats.Feat}", dateUnlocks.ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture)); } }