示例#1
0
        public Factory AddFactory(Factory factory)
        {
            if (!factoryRegistry.Factories.Contains(factory))
            {
                throw new ArgumentException("Factory: {0} not contained in the FactoryRegistry.", factory.name);
            }

            bool canAfford = true;

            foreach (ResourceAmount resource in factory.cost)
            {
                if (!ConsumeResource(resource, true))
                {
                    canAfford = false;
                    break;
                }
            }

            if (canAfford)
            {
                foreach (ResourceAmount resource in factory.cost)
                {
                    ConsumeResource(resource, false);
                }

                Factory newFactory = FactoryManager.AddFactory(factory);
                UpdateCheckpoint(new ResourceCheckpoint(GetResourceAmounts()));

                return(newFactory);
            }
            return(null);
        }
示例#2
0
        static void Main(string[] args)
        {
            /*
             * int[] r = new int[20];
             * for (int j = 0; j < 10; j++)
             * {
             *  var rnd = new FastRandom2(j);
             *  for (int i = 0; i < 100000000; i++)
             *  {
             *      int result = rnd.Range(1, 20);
             *      r[result]++;
             *  }
             * }
             *
             * for (int i = 0; i < r.Length; i++)
             * {
             *  Console.WriteLine(i + " -> " + r[i]);
             * }*/

            FactoryManager.SetDefaultFactory(new DefaultFactory());

            FactoryManager.AddFactory(typeof(IRandomImplementation), new Factory())
            .AddVariant("fast", typeof(FastRandom));

            FactoryManager.AddFactory(typeof(Price), new Factory())
            .AddVariant("simple-resource", typeof(SimpleResourcePrice))

            .AddVariant("simple", typeof(SimpleResourcePrice))
            .AddVariant("composite", typeof(CompositeReward));

            FactoryManager.AddFactory(typeof(PathChoice), new Factory())
            .AddVariant("rle", typeof(RleSetPathChoice))
            .AddVariant("simple", typeof(SimplePathChoice));

            FactoryManager.AddFactory(typeof(Reward), new Factory())
            .AddVariant("simple-resource", typeof(SimpleResourceReward))
            .AddVariant("composite", typeof(CompositeReward))
            .AddVariant("random", typeof(RandomReward));

            FactoryManager.AddFactory(typeof(RewardResult), new Factory())
            .AddVariant("simple-resource", typeof(SimpleResourceRewardResult))
            .AddVariant("composite", typeof(CompositeRewardResult));

            FactoryManager.AddFactory(typeof(Requirement), new Factory())
            .AddVariant("simple-resource", typeof(SimpleResourceRequirement))
            .AddVariant("and", typeof(AndRequirement))
            .AddVariant("not", typeof(NotRequirement))
            .AddVariant("or", typeof(OrRequirement))
            .AddVariant("composite", typeof(CompositeRequirement))
            .AddVariant("false", typeof(FalseRequirement))
            .AddVariant("city", typeof(CityRequirement))
            .AddVariant("building", typeof(BuildingRequirement));

            FactoryManager.AddFactory(typeof(Amount), new Factory())
            .AddVariant("simple", typeof(SimpleAmount))
            .AddVariant("set", typeof(SetAmount))
            .AddVariant("rle", typeof(RleSetAmount))
            .AddVariant("range", typeof(RangeAmount))
            .AddVariant("critical", typeof(CriticalAmount));


            var mockNode = new RawNode(JSON.Instance.Parse(File.ReadAllText("mock.json")));

            var simpleResourceNode = JSON.Instance.Parse(File.ReadAllText("simple-resources.json"));
            var randomNode         = JSON.Instance.Parse(File.ReadAllText("random.json"));
            var objectsNode        = JSON.Instance.Parse(File.ReadAllText("objects.json"));
            var dealsNode          = JSON.Instance.Parse(File.ReadAllText("deals.json"));

            var citiesNode = JSON.Instance.Parse(File.ReadAllText("cities.json"));

            var dict = SerializeUtil.Dict().SetArgs("simple-resources", simpleResourceNode, "objects", objectsNode, "random",
                                                    randomNode, "deals", dealsNode, "cities", citiesNode);

            var player = new Player(mockNode, new RawNode(dict));

            var territories = new Territories("territories", new TerritoryCategory(), player);

            territories.Attach(territories.category.tank.fire, OnTerritories);
            territories["0"].Attach(territories.category.tank.fire, OnTerritory);
            territories["0"].tanks.Attach(territories.category.tank.fire, OnTanks);
            territories["0"].tanks["0"].Attach(territories.category.tank.fire, OnTank);
            territories["0"].tanks["0"].Fire();

            var cities = player.GetChild <CityCollection>("cities");
            var model  = cities["0"];

            cities.Attach(cities.categories.buildings.complete, Handler);
            model.buildings["0"].Complete();

            Console.ReadKey();
        }
示例#3
0
 public Factory LoadFactory(Factory factory)
 {
     return(FactoryManager.AddFactory(factory));
 }
示例#4
0
        private bool StartInternal(Bot bot)
        {
            if (CheckStatus(bot) != PluginStatus.Ready)
            {
                throw new InvalidOperationException("This plugin has not yet been prepared");
            }

            try
            {
                switch (Type)
                {
                case PluginType.None:
                    throw new InvalidOperationException("A 'None' plugin cannot be loaded");

                case PluginType.BotPlugin:
                    if (bot is null)
                    {
                        Log.Error("This plugin needs to be activated on a bot instance.");
                        status = PluginStatus.Error;
                        return(false);
                    }
                    if (pluginObjectList.ContainsKey(bot))
                    {
                        throw new InvalidOperationException("Plugin is already instantiated on this bot");
                    }
                    var pluginInstance = (IBotPlugin)Activator.CreateInstance(coreType);
                    if (pluginObjectList.Count == 0)
                    {
                        RegisterCommands(pluginInstance, coreType);
                    }
                    pluginObjectList.Add(bot, pluginInstance);
                    if (!bot.Injector.TryInject(pluginInstance))
                    {
                        Log.Warn("Some dependencies are missing for this plugin");
                    }
                    pluginInstance.Initialize();
                    break;

                case PluginType.CorePlugin:
                    pluginObject = (ICorePlugin)Activator.CreateInstance(coreType);
                    RegisterCommands(pluginObject, coreType);
                    if (!CoreInjector.TryInject(pluginObject))
                    {
                        Log.Warn("Some dependencies are missing for this plugin");
                    }
                    pluginObject.Initialize();
                    break;

                case PluginType.Factory:
                    factoryObject = (IFactory)Activator.CreateInstance(coreType);
                    FactoryManager.AddFactory(factoryObject);
                    break;

                case PluginType.Commands:
                    RegisterCommands(null, coreType);
                    break;

                default:
                    throw Util.UnhandledDefault(Type);
                }
            }
            catch (Exception ex)
            {
                if (ex is MissingMethodException)
                {
                    Log.Error(ex, "Plugins and Factories needs a parameterless constructor.");
                }
                else
                {
                    Log.Error(ex, "Plugin '{0}' failed to load: {1}.", Name, ex.Message);
                }
                Stop(bot);
                if (Type != PluginType.BotPlugin)
                {
                    status = PluginStatus.Error;
                }
                return(false);
            }

            if (Type != PluginType.BotPlugin)
            {
                status = PluginStatus.Active;
            }

            return(true);
        }