Beispiel #1
0
        public void SerializeChest()
        {
            var row   = _tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.Chest);
            var chest = ItemFactory.CreateChest(row, null);

            Assert.IsNotNull(chest);
            Assert.AreEqual(chest.Rewards, new List <RedeemRewardSheet.RewardInfo>());
            var serialized = (Dictionary)chest.Serialize();

            Assert.IsTrue(serialized.ContainsKey((IKey)(Text)"rewards"));
            Assert.AreEqual(chest, ItemFactory.Deserialize(serialized));

            var rewards = _tableSheets.RedeemRewardSheet.Values.First().Rewards;
            var chest2  = ItemFactory.CreateChest(row, rewards);

            Assert.IsNotNull(chest2);
            Assert.AreEqual(chest2.Rewards, rewards);
            var serialized2 = (Dictionary)chest2.Serialize();

            Assert.IsTrue(serialized2.ContainsKey((IKey)(Text)"rewards"));
            Assert.AreEqual(chest2, ItemFactory.Deserialize(serialized2));
            Assert.AreNotEqual(serialized, serialized2);
            Assert.AreNotEqual(chest, chest2);
            Assert.AreNotEqual(chest.ItemId, chest2.ItemId);
        }
Beispiel #2
0
        public override IAccountStateDelta Execute(IActionContext context)
        {
            var states = context.PreviousStates;

            if (context.Rehearsal)
            {
                states = states.SetState(RedeemCodeState.Address, MarkChanged);
                states = states.SetState(avatarAddress, MarkChanged);
                states = states.SetState(context.Signer, MarkChanged);
                return(states);
            }

            if (!states.TryGetAgentAvatarStates(context.Signer, avatarAddress, out AgentState agentState,
                                                out AvatarState avatarState))
            {
                return(states);
            }

            var redeemState = states.GetRedeemCodeState();

            if (redeemState is null)
            {
                return(states);
            }

            int redeemId;

            try
            {
                redeemId = redeemState.Redeem(code, avatarAddress);
            }
            catch (InvalidRedeemCodeException)
            {
                Log.Error("Invalid Code");
                throw;
            }
            catch (DuplicateRedeemException e)
            {
                Log.Warning(e.Message);
                throw;
            }

            var tableSheets = TableSheets.FromActionContext(context);
            var row         = tableSheets.RedeemRewardSheet.Values.First(r => r.Id == redeemId);
            var rewards     = row.Rewards;
            var materialRow = tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.Chest);
            var chest       = ItemFactory.CreateChest(materialRow, rewards);

            avatarState.inventory.AddItem(chest, 1);
            states = states.SetState(avatarAddress, avatarState.Serialize());
            states = states.SetState(RedeemCodeState.Address, redeemState.Serialize());
            states = states.SetState(context.Signer, agentState.Serialize());
            return(states);
        }
Beispiel #3
0
        public void SerializeChestDoesNotThrowArgumentNullException()
        {
            var row           = _tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.Chest);
            var chestMaterial = ItemFactory.CreateMaterial(row);
            var chest         = ItemFactory.CreateChest(row, null);

            Assert.IsNotNull(chestMaterial);
            Assert.DoesNotThrow(() => chestMaterial.Serialize());
            var serialized = (Dictionary)chestMaterial.Serialize();

            Assert.IsTrue(serialized.ContainsKey((IKey)(Text)"rewards"));
            Assert.AreEqual(chest, ItemFactory.Deserialize(serialized));
        }
Beispiel #4
0
        public void CreateChest()
        {
            var row   = _tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.Chest);
            var chest = ItemFactory.CreateChest(row, null);

            Assert.IsNotNull(chest);
            Assert.AreEqual(chest.Rewards, new List <RedeemRewardSheet.RewardInfo>());

            var rewards = _tableSheets.RedeemRewardSheet.Values.First().Rewards;
            var chest2  = ItemFactory.CreateChest(row, rewards);

            Assert.IsNotNull(chest2);
            Assert.AreEqual(chest2.Rewards, rewards);
            Assert.AreNotEqual(chest, chest2);
        }