Example #1
0
        public IObservable <ActionBase.ActionEvaluation <HackAndSlash> > HackAndSlash(
            List <Costume> costumes,
            List <Equipment> equipments,
            List <Consumable> foods,
            int worldId,
            int stageId)
        {
            if (!ArenaHelper.TryGetThisWeekAddress(out var weeklyArenaAddress))
            {
                throw new NullReferenceException(nameof(weeklyArenaAddress));
            }

            Mixpanel.Track("Unity/Create HackAndSlash");

            var avatarAddress = States.Instance.CurrentAvatarState.address;

            costumes   = costumes ?? new List <Costume>();
            equipments = equipments ?? new List <Equipment>();
            foods      = foods ?? new List <Consumable>();

            var action = new HackAndSlash
            {
                costumes           = costumes.Select(c => c.ItemId).ToList(),
                equipments         = equipments.Select(e => e.ItemId).ToList(),
                foods              = foods.Select(f => f.ItemId).ToList(),
                worldId            = worldId,
                stageId            = stageId,
                avatarAddress      = avatarAddress,
                WeeklyArenaAddress = weeklyArenaAddress,
                RankingMapAddress  = States.Instance.CurrentAvatarState.RankingMapAddress,
            };

            ProcessAction(action);

            var itemIDs = equipments
                          .Select(e => e.Id)
                          .Concat(foods.Select(f => f.Id))
                          .ToArray();

            AnalyticsManager.Instance.Battle(itemIDs);
            return(_renderer.EveryRender <HackAndSlash>()
                   .SkipWhile(eval => !eval.Action.Id.Equals(action.Id))
                   .Take(1)
                   .Last()
                   .ObserveOnMainThread()
                   .Timeout(ActionTimeout)
                   .DoOnError(e => HandleException(action.Id, e)));
        }
Example #2
0
        public void Execute(int avatarLevel, int worldId, int stageId, bool contains)
        {
            Assert.True(_tableSheets.WorldSheet.TryGetValue(worldId, out var worldRow));
            Assert.True(stageId >= worldRow.StageBegin);
            Assert.True(stageId <= worldRow.StageEnd);
            Assert.True(_tableSheets.StageSheet.TryGetValue(stageId, out _));

            var previousAvatarState = _initialState.GetAvatarState(_avatarAddress);

            previousAvatarState.level            = avatarLevel;
            previousAvatarState.worldInformation = new WorldInformation(
                0,
                _tableSheets.WorldSheet,
                Math.Max(_tableSheets.StageSheet.First?.Id ?? 1, stageId - 1));

            var costumeId = _tableSheets
                            .CostumeItemSheet
                            .Values
                            .First(r => r.ItemSubType == ItemSubType.FullCostume)
                            .Id;
            var costume =
                ItemFactory.CreateItem(_tableSheets.ItemSheet[costumeId], new TestRandom());

            previousAvatarState.inventory.AddItem(costume);

            var state = _initialState.SetState(_avatarAddress, previousAvatarState.Serialize());

            var action = new HackAndSlash()
            {
                costumes = new List <int> {
                    costumeId
                },
                equipments         = new List <Guid>(),
                foods              = new List <Guid>(),
                worldId            = worldId,
                stageId            = stageId,
                avatarAddress      = _avatarAddress,
                WeeklyArenaAddress = _weeklyArenaState.address,
                RankingMapAddress  = _rankingMapAddress,
            };

            Assert.Null(action.Result);

            var nextState = action.Execute(new ActionContext()
            {
                PreviousStates = state,
                Signer         = _agentAddress,
                Random         = new TestRandom(),
                Rehearsal      = false,
            });

            var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
            var newWeeklyState  = nextState.GetWeeklyArenaState(0);

            Assert.NotNull(action.Result);

            Assert.NotEmpty(action.Result.OfType <GetReward>());
            Assert.Equal(BattleLog.Result.Win, action.Result.result);
            Assert.Equal(contains, newWeeklyState.ContainsKey(_avatarAddress));
            Assert.True(nextAvatarState.worldInformation.IsStageCleared(stageId));

            var value = nextState.GetState(_rankingMapAddress);

            var rankingMapState = new RankingMapState((Dictionary)value);
            var info            = rankingMapState.GetRankingInfos(null).First();

            Assert.Equal(info.AgentAddress, _agentAddress);
            Assert.Equal(info.AvatarAddress, _avatarAddress);
        }
        public ActionMutation()
        {
            Field <NonNullGraphType <BooleanGraphType> >("createAvatar",
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress   = privatekey.PublicKey.ToAddress();
                    Address avatarAddress = userAddress.Derive("avatar_0");

                    var action = new CreateAvatar
                    {
                        avatarAddress = avatarAddress,
                        index         = 0,
                        hair          = 0,
                        lens          = 0,
                        ear           = 0,
                        tail          = 0,
                        name          = "createbymutation",
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });

            Field <NonNullGraphType <BooleanGraphType> >("hackAndSlash",
                                                         arguments: new QueryArguments(
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "weeklyArenaAddress",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "rankingArenaAddress",
            }),
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress         = privatekey.PublicKey.ToAddress();
                    Address avatarAddress       = userAddress.Derive("avatar_0");
                    Address weeklyArenaAddress  = new Address(context.GetArgument <string>("weeklyArenaAddress"));
                    Address rankingArenaAddress = new Address(context.GetArgument <string>("rankingArenaAddress"));

                    var action = new HackAndSlash
                    {
                        avatarAddress      = avatarAddress,
                        worldId            = 1,
                        stageId            = 1,
                        WeeklyArenaAddress = weeklyArenaAddress,
                        RankingMapAddress  = rankingArenaAddress,
                        costumes           = new List <int>(),
                        equipments         = new List <Guid>(),
                        foods = new List <Guid>(),
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });

            Field <NonNullGraphType <BooleanGraphType> >("combinationEquipment",
                                                         arguments: new QueryArguments(
                                                             new QueryArgument <NonNullGraphType <DecimalGraphType> >
            {
                Name = "recipeId",
            },
                                                             new QueryArgument <NonNullGraphType <DecimalGraphType> >
            {
                Name = "slotIndex",
            },
                                                             new QueryArgument <DecimalGraphType>
            {
                Name = "subRecipeId",
            }),
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress   = privatekey.PublicKey.ToAddress();
                    Address avatarAddress = userAddress.Derive("avatar_0");
                    int recipeId          = context.GetArgument <int>("recipeId");
                    int slotIndex         = context.GetArgument <int>("slotIndex");
                    int?subRecipeId       = context.GetArgument <int>("subRecipeId");

                    var action = new CombinationEquipment
                    {
                        AvatarAddress = avatarAddress,
                        RecipeId      = recipeId,
                        SlotIndex     = slotIndex,
                        SubRecipeId   = subRecipeId
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });

            Field <NonNullGraphType <BooleanGraphType> >("itemEnhancement",
                                                         arguments: new QueryArguments(
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "itemId",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "materialIds",
            }),
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress   = privatekey.PublicKey.ToAddress();
                    Address avatarAddress = userAddress.Derive("avatar_0");
                    Guid itemId           = Guid.Parse(context.GetArgument <string>("itemId"));
                    Guid materialId       = Guid.Parse(context.GetArgument <string>("materialIds"));

                    var action = new ItemEnhancement
                    {
                        avatarAddress = avatarAddress,
                        slotIndex     = 0,
                        itemId        = itemId,
                        materialIds   = new[] { materialId }
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });

            Field <NonNullGraphType <BooleanGraphType> >("buy",
                                                         arguments: new QueryArguments(
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "sellerAgentAddress",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "sellerAvatarAddress",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "productId",
            }),
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress         = privatekey.PublicKey.ToAddress();
                    Address avatarAddress       = userAddress.Derive("avatar_0");
                    Address sellerAgentAddress  = new Address(context.GetArgument <string>("sellerAgentAddress"));
                    Address sellerAvatarAddress = new Address(context.GetArgument <string>("sellerAvatarAddress"));
                    Guid productId = Guid.Parse(context.GetArgument <string>("productId"));

                    var action = new Buy
                    {
                        buyerAvatarAddress  = avatarAddress,
                        sellerAgentAddress  = sellerAgentAddress,
                        sellerAvatarAddress = sellerAvatarAddress,
                        productId           = productId,
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });
            Field <NonNullGraphType <BooleanGraphType> >("sell",
                                                         arguments: new QueryArguments(
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "sellerAvatarAddress",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "productId",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "itemId",
            },
                                                             new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "price",
            }),
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address sellerAvatarAddress = new Address(context.GetArgument <string>("sellerAvatarAddress"));
                    Guid itemId  = Guid.Parse(context.GetArgument <string>("itemId"));
                    var currency = new GoldCurrencyState(
                        (Dictionary)blockChain.GetState(GoldCurrencyState.Address)
                        ).Currency;
                    FungibleAssetValue price =
                        FungibleAssetValue.Parse(currency, context.GetArgument <string>("price"));

                    var action = new Sell
                    {
                        sellerAvatarAddress = sellerAvatarAddress,
                        itemId = itemId,
                        price  = price
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });

            Field <NonNullGraphType <BooleanGraphType> >("dailyReward",
                                                         resolve: context =>
            {
                try
                {
                    NineChroniclesNodeService service = context.Source;
                    PrivateKey privatekey             = service.PrivateKey;
                    BlockChain <NineChroniclesActionType> blockChain = service.Swarm.BlockChain;
                    Address userAddress   = privatekey.PublicKey.ToAddress();
                    Address avatarAddress = userAddress.Derive("avatar_0");

                    var action = new DailyReward
                    {
                        avatarAddress = avatarAddress
                    };

                    var actions = new PolymorphicAction <ActionBase>[] { action };
                    blockChain.MakeTransaction(privatekey, actions);
                }
                catch (Exception e)
                {
                    var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
                    context.Errors.Add(new ExecutionError(msg, e));
                    Log.Error(msg, e);
                    return(false);
                }

                return(true);
            });
        }