Пример #1
0
 private void TriggerEvent(StatRecord record)
 {
     if (record != null && _events.TryGetValue(record, out var @event))
     {
         @event.Invoke(record);
     }
 }
Пример #2
0
        public void GetValueUsesCustomOperatorOrderOnSettings()
        {
            var ooo = ScriptableObject.CreateInstance <OrderOfOperations>();
            var o   = ooo.Operators[0];

            ooo.Operators.RemoveAt(0);
            ooo.Operators.Add(o);
            settings.OrderOfOperations = ooo;

            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 4f);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 4);
            r.modifierSubtract.Set("a", 2);
            r.modifierMultiply.Set("a", 4);
            r.modifierDivide.Set("a", 2);

            var result = 4 - 2;

            result += result * 4;
            result /= 2;
            result += 4;

            Assert.AreEqual(result, r.GetValue());
        }
Пример #3
0
        public void GetBaseValue()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);
            var r = new StatRecord(d);

            Assert.AreEqual(2, r.GetValue());
        }
Пример #4
0
 /// <summary>
 /// Retrieve an int value by record with an optional index
 /// </summary>
 /// <param name="record"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public int GetStatInt(StatRecord record, float index = 0)
 {
     if (record == null)
     {
         return(0);
     }
     return(Mathf.RoundToInt(record.GetValue(index)));
 }
Пример #5
0
 private void loadItems()
 {
     foreach (int addr in RecordAddresses())
     {
         var r = new StatRecord(M, addr);
         records.Add(r.Key, r);
     }
 }
Пример #6
0
 private void loadItems()
 {
     foreach (var addr in RecordAdresses())
     {
         StatRecord r = new StatRecord(m, addr);
         records.Add(r.Key, r);
     }
 }
Пример #7
0
 private void loadItems()
 {
     foreach (int addr in RecordAddresses())
     {
         var r = new StatRecord(M, addr);
         records.Add(r.Key, r);
     }
 }
Пример #8
0
        public void GetValueWithoutModifier()
        {
            var d      = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);
            var r      = new StatRecord(d);
            var result = r.GetValue();

            Assert.AreEqual(2, result);
        }
Пример #9
0
 /// <summary>
 /// Retrieve a float value by record with an optional index
 /// </summary>
 /// <param name="record"></param>
 /// <param name="index"></param>
 /// <returns></returns>
 public float GetStat(StatRecord record, float index = 0)
 {
     if (record == null)
     {
         return(0);
     }
     return(record.GetValue(index));
 }
Пример #10
0
        public async Task CreateStatRecord(Player player)
        {
            List <Skill>    skills    = new List <Skill>();
            List <Minigame> minigames = new List <Minigame>();
            var             apiData   = await OfficialApiCall(_address + player.Username);

            if (apiData == null)
            {
                return;
            }
            string[] lines = apiData.Split('\n');

            // adding a StatRecord object
            StatRecord newStatRecord = new StatRecord()
            {
                DateCreated = DateTime.Now,
                UserId      = player.Id,
                Player      = player
            };

            // looping through the skills and adding them
            for (var i = 0; i < _totalSkills; i++)
            {
                String[] stat  = lines[i].Split(',');
                Skill    skill = new Skill()
                {
                    SkillId      = i,
                    Xp           = Int64.Parse(stat[2]),
                    Level        = Int32.Parse(stat[1]),
                    Rank         = Int32.Parse(stat[0]),
                    StatRecordId = newStatRecord.Id
                };
                skill.Xp   = skill.Xp < 0 ? 0 : skill.Xp;
                skill.Rank = skill.Rank < 0 ? 0 : skill.Rank;
                skills.Add(skill);
            }

            // looping through the minigames and adding them
            for (var i = _totalSkills; i < lines.Length - 1; i++)
            {
                String[] stat     = lines[i].Split(',');
                Minigame minigame = new Minigame()
                {
                    MinigameId   = i,
                    Score        = Int32.Parse(stat[1]),
                    Rank         = Int32.Parse(stat[0]),
                    StatRecordId = newStatRecord.Id
                };
                minigame.Score = minigame.Score < 0 ? 0 : minigame.Score;
                minigame.Rank  = minigame.Rank < 0 ? 0 : minigame.Rank;
                minigames.Add(minigame);
            }

            newStatRecord.Skills    = skills;
            newStatRecord.Minigames = minigames;
            player.StatRecords.Add(newStatRecord);
        }
Пример #11
0
        public void GetValueRoundsForceRoundedDefinitions()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2.4f);

            SetPrivateField(d, "_roundResult", true);
            var r = new StatRecord(d);

            Assert.AreEqual(2, r.GetValue());
        }
Пример #12
0
        /// <summary>
        /// Wipe all modifiers (add, sub, mult, div) on a particular record
        /// </summary>
        /// <param name="record"></param>
        /// <param name="modifierId"></param>
        public void ClearAllModifiers(StatRecord record, string modifierId)
        {
            RemoveModifier(OperatorType.Add, record, modifierId);
            RemoveModifier(OperatorType.Subtract, record, modifierId);
            RemoveModifier(OperatorType.Multiply, record, modifierId);
            RemoveModifier(OperatorType.Divide, record, modifierId);

            TriggerEvent(record);
        }
Пример #13
0
 /// <summary>
 /// Set a modifier by ID and value
 /// </summary>
 /// <param name="operation"></param>
 /// <param name="record"></param>
 /// <param name="modifierId"></param>
 /// <param name="value"></param>
 public void SetModifier(OperatorType operation, StatRecord record, string modifierId, float value)
 {
     if (record == null)
     {
         return;
     }
     record.GetModifier(operation).Set(modifierId, value);
     TriggerEvent(record);
 }
Пример #14
0
        public void GetValueStoresLastRetrievedValue()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 2f);
            r.GetValue();

            Assert.AreEqual(4, r.LastRetrievedValue);
        }
Пример #15
0
        public void OnStatChangeSubscribe(StatRecord record, UnityAction <StatRecord> callback)
        {
            if (!_events.TryGetValue(record, out var @event))
            {
                @event          = new StatRecordEvent();
                _events[record] = @event;
            }

            @event.AddListener(callback);
        }
Пример #16
0
        public void ModifierNotForceRoundedWithoutOperatorForceRound()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2.4f);

            SetPrivateField(d, "_roundResult", true);
            var r = new StatRecord(d);

            r.modifierMultiply.Set("a", 0.05f);

            Assert.AreEqual(3, r.GetValue(3f));
        }
Пример #17
0
        public void ChangingModifierChangesCachedValueLookups()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 2f);
            r.GetValue();
            r.modifierAdd.Set("a", 4f);

            Assert.AreEqual(6, r.GetValue());
        }
Пример #18
0
 private void loadItems()
 {
     foreach (int addr in RecordAddresses())
     {
         var r = new StatRecord(M, addr);
         if (!records.ContainsKey(r.Key))
         {
             records.Add(r.Key, r);
         }
     }
 }
Пример #19
0
        public void ModifierIsNotForceRoundedWhenDefinitionForceRoundFalse()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2.4f);

            SetPrivateField(d, "_roundModifiers", false);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 2.4f);

            Assert.AreEqual(4.8f, r.GetValue());
        }
Пример #20
0
        private void loadItems()
        {
            var iCounter = 1;

            foreach (var addr in RecordAddresses())
            {
                var r = new StatRecord(M, addr, iCounter++);
                records[r.Key]    = r;
                recordsById[r.ID] = r;
            }
        }
Пример #21
0
            public void It_should_subscribe_with_a_definition()
            {
                _health.Value.GetValue().SetInt(0);

                StatRecord result = null;
                var        copy   = _container.CreateRuntimeCopy();

                copy.OnStatChangeSubscribe(_health, (record) => result = record);
                copy.SetModifier(OperatorType.Add, "health", "plus", 5f);

                Assert.AreEqual(5f, result.GetValue());
            }
Пример #22
0
        public void ModifierIsForceRoundedWithDefinitionForceRound()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2.4f);

            SetPrivateField(d, "_roundResult", true);
            SetPrivateField(d, "_roundModifiers", true);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 2.4f);

            Assert.AreEqual(4, r.GetValue());
        }
Пример #23
0
            public void It_should_return_the_new_value_on_SetModifier()
            {
                _health.Value.GetValue().SetInt(0);

                StatRecord result = null;
                var        copy   = _container.CreateRuntimeCopy();

                copy.OnStatChangeSubscribe("health", (record) => result = record);
                copy.SetModifier(OperatorType.Add, "health", "plus", 5f);

                Assert.AreEqual(5f, result.GetValue());
            }
Пример #24
0
        private void loadItems()
        {
            int iCounter = 1;

            foreach (long addr in RecordAddresses())
            {
                var r = new StatRecord(M, addr, iCounter++);
                if (!records.ContainsKey(r.Key))
                {
                    records.Add(r.Key, r);
                }
            }
        }
Пример #25
0
        /// <summary>
        /// Remove a modifier by ID
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="record"></param>
        /// <param name="modifierId"></param>
        /// <returns></returns>
        public bool RemoveModifier(OperatorType operation, StatRecord record, string modifierId)
        {
            if (record == null)
            {
                return(false);
            }

            var result = record.GetModifier(operation).Remove(modifierId);

            TriggerEvent(record);

            return(result);
        }
Пример #26
0
            public void It_should_trigger_an_event_on_RemoveModifier()
            {
                _health.Value.GetValue().SetInt(0);

                StatRecord result = null;
                var        copy   = _container.CreateRuntimeCopy();

                copy.SetModifier(OperatorType.Add, "health", "plus", 5f);
                copy.OnStatChangeSubscribe("health", (record) => result = record);
                copy.RemoveModifier(OperatorType.Add, "health", "plus");

                Assert.AreEqual(0f, result.GetValue());
            }
Пример #27
0
        public void GetValueWithEveryModifer()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);
            var r = new StatRecord(d);

            r.modifierAdd.Set("a", 4);
            r.modifierSubtract.Set("a", 2);
            r.modifierMultiply.Set("a", 3);
            r.modifierDivide.Set("a", 2);
            var result = r.GetValue();

            Assert.AreEqual(8, result);
        }
Пример #28
0
        private async Task <StatRecord> CreateStatRecord(User user)
        {
            List <Skill>    skills    = new List <Skill>();
            List <Minigame> minigames = new List <Minigame>();
            var             apiData   = await OfficialApiCall(user.Username);

            string[] lines = apiData.Split('\n');

            // adding a StatRecord object
            StatRecord newStatRecord = new StatRecord()
            {
                DateCreated = DateTime.Now,
                UserId      = user.Id,
            };

            // looping through the skills and adding them
            for (int i = 0; i < _totalSkills; i++)
            {
                String[] stat  = lines[i].Split(',');
                Skill    skill = new Skill()
                {
                    SkillId      = i,
                    Xp           = Int64.Parse(stat[2]),
                    Level        = Int32.Parse(stat[1]),
                    Rank         = Int32.Parse(stat[0]),
                    StatRecordId = newStatRecord.Id
                };
                skills.Add(skill);
            }

            // looping through the minigames and adding them
            for (int i = _totalSkills; i < lines.Length - 1; i++)
            {
                String[] stat     = lines[i].Split(',');
                Minigame minigame = new Minigame()
                {
                    MinigameId   = i,
                    Score        = Int32.Parse(stat[1]),
                    Rank         = Int32.Parse(stat[0]),
                    StatRecordId = newStatRecord.Id
                };
                minigames.Add(minigame);
            }

            newStatRecord.Skills    = skills;
            newStatRecord.Minigames = minigames;

            return(newStatRecord);
        }
Пример #29
0
        public void GetBaseValueOverride()
        {
            var d = TestStatValueSelector.CreateDefinition(StatValueType.Float, 2);

            var statOverride = new StatValueSelector {
                type = StatValueType.Int
            };
            var sInt = (StatValueInt)statOverride.GetValue();

            sInt.value = 8;

            var r = new StatRecord(d, statOverride);

            Assert.AreEqual(8, r.GetBaseValue());
        }
Пример #30
0
            public void It_should_remove_the_passed_subscription_via_string()
            {
                _health.Value.GetValue().SetInt(0);

                StatRecord result = null;
                var        copy   = _container.CreateRuntimeCopy();
                var        record = copy.GetRecord("health");
                var        action = new UnityAction <StatRecord>(r => result = r);

                copy.OnStatChangeSubscribe(record, action);
                copy.OnStatChangeUnsubscribe("health", action);
                copy.SetModifier(OperatorType.Add, "health", "plus", 5f);

                Assert.IsNull(result);
            }
Пример #31
0
        /// <summary>
        /// Retrieve the value of a modifier
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="record"></param>
        /// <param name="modifierId"></param>
        /// <returns></returns>
        public float GetModifier(OperatorType operation, StatRecord record, string modifierId)
        {
            if (record == null)
            {
                return(0);
            }

            var modifier = record.GetModifier(operation).Get(modifierId);

            if (modifier == null)
            {
                return(0);
            }

            return(modifier.value);
        }