//Constructor: accepts arguments for the above variables public Achievement(int id, string name, string description, AchievementStat stat, int state, int requirement, GameReward reward, AchievementGroup group) { //Assign values ID = id; Name = name; Description = description; Stat = stat; State = state; Requirement = requirement; _reward = reward; Group = group; //Initialize Stat.AddDependency(this); Group.Add(this); }
//Create all achievement stats private void CreateStats() { _statCrafted = new AchievementStat(0, _networkManager.GetAchievementStatStatus(0), GameAction.CRAFTED); _statQuestCompleted = new AchievementStat(1, _networkManager.GetAchievementStatStatus(1), GameAction.QUEST_COMPLETED); _statSecretArea = new AchievementStat(2, _networkManager.GetAchievementStatStatus(2), GameAction.SECRET_AREA); _statMonsterKilled = new AchievementStat(3, _networkManager.GetAchievementStatStatus(3), GameAction.MONSTER_KILLED); _statAttackMagic = new AchievementStat(4, _networkManager.GetAchievementStatStatus(4), GameAction.ATTACK_MAGIC); _statAttackMelee = new AchievementStat(5, _networkManager.GetAchievementStatStatus(5), GameAction.ATTACK_MELEE); _statAttackHeal = new AchievementStat(6, _networkManager.GetAchievementStatStatus(6), GameAction.ATTACK_HEAL); //Add all of these to a list _achievementStats = new List <AchievementStat>() { _statCrafted, _statQuestCompleted, _statSecretArea, _statMonsterKilled, _statAttackMagic, _statAttackMelee, _statAttackHeal }; }
public void UpdateAchievements(List <object> worldEvent) { //Get the achievement stat that matches this event AchievementStat stat = null; GameAction? action = worldEvent[0] as GameAction?; if (action == null) { return; //Just in case } foreach (AchievementStat s in _achievementStats) { if (action == s.Stat) { stat = s; break; } } //If nothing matches the event, return if (stat == null) { return; } //Update this stat's value, depending on the message type switch (action) { //Format: <ACTION> <ID> <QUANTITY>. We want quantity case GameAction.CRAFTED: case GameAction.CONSUMED: case GameAction.DISCARDED: case GameAction.DROPPED: //Not implemented case GameAction.LOOTED: //Not implemented case GameAction.NPC_GIVE: case GameAction.NPC_RECEIVE: case GameAction.PICKED_UP: case GameAction.QUEST_CONSUMED: //Not implemented case GameAction.TRADE_GIVE: //Not implemented case GameAction.TRADE_RECEIVED: //Not implemented if (worldEvent[2] is int) //Just in case { stat.AddToValue((int)worldEvent[2]); } break; //Format: <ACTION>. Just increment stat by one case GameAction.ATTACK_MAGIC: case GameAction.ATTACK_MELEE: case GameAction.ATTACK_RANGED: case GameAction.ATTACK_HEAL: case GameAction.MONSTER_KILLED: case GameAction.MOUSE_CLICK: case GameAction.SECRET_AREA: default: stat.AddToValue(1); break; //Many action types are not included - these only send strings and thus can't be used here } //Save this update on the server _networkManager.UpdateAchievementStat(stat.ID, stat.Value); //Update all achievements that subscribe to this stat foreach (Achievement a in stat.Subscribers) { //Ignore completed achievements if (a.IsCompleted()) { continue; } //If this latest stat update completed the achievement, notify the user and save this to the server if (a.JustCompleted()) { //Notify user if (_messageOverlayController == null) { _messageOverlayController = GameObject.FindGameObjectWithTag("MessageOverlay").GetComponent <MessageOverlayController>(); } _messageOverlayController.EnqueueMessage("Achievement \"" + a.Name + "\" completed!"); //Save this to the server _networkManager.UpdateAchievement(a.ID, a.State); //Check for milestone completion int completedInGroup = a.Group.NumberCompleted(); foreach (AchievementMilestone m in a.Group.Milestones) { //Skip completed milestones if (m.IsCompleted()) { continue; } //If milestone has been completed, notify user and save to server if (m.Required <= completedInGroup) { m.Complete(); //Notify user _messageOverlayController.EnqueueMessage("\"" + a.Group.Name + "\" milestone completed!"); //Save to server _networkManager.UpdateMilestone(m.ID, m.State); } } } //Lastly, set that the achievement group has been modified a.Group.AnyChanges = true; } }