117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Tiger.Communication.Messages.Outgoing.Inventory.Achievements;
|
|
using Tiger.Communication.Messages.Outgoing.Notifications;
|
|
using Tiger.Game.Habbos;
|
|
using Tiger.Networking.Game.Sessions;
|
|
using Tiger.Storage;
|
|
|
|
namespace Tiger.Game.Achievements;
|
|
|
|
public class AchievementManager : IAchievementManager
|
|
{
|
|
private readonly IRepository<Achievement> _achievementRepository;
|
|
private readonly ILogger<IAchievementManager> _logger;
|
|
private readonly IRepository<Badge> _badgesRepository;
|
|
private readonly IRepository<Habbo> _habboRepository;
|
|
|
|
public AchievementManager(IRepository<Achievement> achievementRepository, ILogger<IAchievementManager> logger, IRepository<Badge> badgesRepository, IRepository<Habbo> habboRepository)
|
|
{
|
|
_achievementRepository = achievementRepository;
|
|
_logger = logger;
|
|
_badgesRepository = badgesRepository;
|
|
_habboRepository = habboRepository;
|
|
}
|
|
|
|
public IDictionary<string, Achievement> Achievements { get; private set; } = new Dictionary<string, Achievement>();
|
|
|
|
public async Task LoadAchievementsAsync()
|
|
{
|
|
Achievements = (await _achievementRepository.FindByAsync()).ToDictionary(a => a.Badge, a => a);
|
|
|
|
_logger.LogInformation("Loaded {Count} Achievements", Achievements.Count);
|
|
}
|
|
|
|
public async Task UpdateAchievementAsync(string achievementName, int progress, GameSession session)
|
|
{
|
|
if (session.Habbo is null) return;
|
|
|
|
if (!Achievements.TryGetValue(achievementName, out var achievement)) return;
|
|
|
|
if (progress == 0) return;
|
|
|
|
if (!session.Habbo.Achievements.TryGetValue(achievement.Id, out var habboAchievement))
|
|
{
|
|
habboAchievement = new HabboAchievement()
|
|
{
|
|
Level = 0,
|
|
Progress = 0,
|
|
Achievement = achievement,
|
|
Habbo = session.Habbo
|
|
};
|
|
|
|
session.Habbo.Achievements.Add(achievement.Id, habboAchievement);
|
|
}
|
|
|
|
if (habboAchievement.Level == achievement.Levels.Count) return;
|
|
|
|
var targetLevel = habboAchievement.Level + 1;
|
|
var achievementLevel = achievement.Levels[targetLevel];
|
|
|
|
habboAchievement.Progress += progress;
|
|
|
|
if (habboAchievement.Progress >= achievementLevel.ProgressNeeded)
|
|
{
|
|
habboAchievement.Level++;
|
|
|
|
Badge badge;
|
|
|
|
if (habboAchievement.Level == 1)
|
|
{
|
|
badge = new Badge
|
|
{
|
|
Habbo = session.Habbo,
|
|
Code = $"{achievement.Badge}1"
|
|
};
|
|
session.Habbo.Badges.Add(badge);
|
|
}
|
|
else
|
|
{
|
|
var currentBadge =
|
|
session.Habbo.Badges.SingleOrDefault(b =>
|
|
b.Code == $"{achievement.Badge}{habboAchievement.Level - 1}");
|
|
|
|
if (currentBadge != null)
|
|
{
|
|
currentBadge.Code = $"{achievement.Badge}{habboAchievement.Level - 1}";
|
|
badge = currentBadge;
|
|
}
|
|
else
|
|
{
|
|
badge = new Badge
|
|
{
|
|
Habbo = session.Habbo,
|
|
Code = $"{achievement.Badge}1"
|
|
};
|
|
session.Habbo.Badges.Add(badge);
|
|
}
|
|
}
|
|
|
|
session.Habbo.AchievementScore += achievementLevel.Points;
|
|
|
|
if (achievementLevel.RewardAmount > 0)
|
|
{
|
|
session.Habbo.UpdateCurrency(achievementLevel.RewardType, achievementLevel.RewardAmount);
|
|
session.SendComposerAsync(new ActivityPointNotificationMessageComposer(session.Habbo.Activitypoints[achievementLevel.RewardType].Amount, achievementLevel.RewardAmount, achievementLevel.RewardType));
|
|
}
|
|
|
|
// session.CurrentRoom?.SendMessage(new UserChangeMessageComposer(session.RoomUser.VirtualId, session.User));
|
|
|
|
session.SendComposerAsync(new AchievementsScoreComposer(session.Habbo.AchievementScore));
|
|
session.SendComposerAsync(new AchievementNotificationMessageComposer(habboAchievement, badge.Id));
|
|
}
|
|
|
|
_habboRepository.SaveAsync(session.Habbo);
|
|
|
|
await session.SendComposerAsync(new AchievementComposer(achievement, habboAchievement));
|
|
}
|
|
} |