/** * Constructor for the random events. * * @param playerArg * the player to affect * @param strat * the strategy used by this random event */ public RandomEvent(Player playerArg) { this.player = playerArg; EventType et = (new Random().NextDouble() <= EventChance) ? GetNonNullEvent() : EventType.NULL_EVENT; switch (et) { case EventType.NULL_EVENT: this.effect = new NullStrategy(); break; case EventType.FUEL_EVENT: this.effect = new FuelStrategy(); break; case EventType.MONEY_EVENT: this.effect = new MoneyStrategy(); break; case EventType.GOODS_EVENT: this.effect = new GoodsStrategy(); break; default: this.effect = new NullStrategy(); break; } }
/// <summary> /// Build a event factory with an event publisher. /// </summary> /// <param name="name">Name to identify the event publisher.</param> /// <param name="publisher">Resolved event publisher will be used.</param> /// <returns>Event factory created.</returns> public static IEventFactory Builder(string name, IEventStrategy publisher) { var factory = new EventFactory(); factory.AddStrategy(name, publisher); return(factory); }
/// <summary> /// Add event with IoC. /// </summary> /// <param name="services"><see cref="IServiceCollection"/> injection.</param> /// <param name="name">Name to identify the event publisher.</param> /// <param name="publisher">Resolved event publisher will be used.</param> /// <returns><see cref="IServiceCollection"/> with event plugin injected.</returns> public static IServiceCollection AddEventPlugin( this IServiceCollection services, string name, IEventStrategy publisher) { factory.AddStrategy(name, publisher); services.AddSingleton <IEventFactory>(factory); return(services); }
private static void ValidateParameters(string name, IEventStrategy publisher) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException(string.Format(Messages.REQUIRED_PARAMETER, nameof(name)), nameof(name)); } if (publisher == null) { throw new ArgumentException(string.Format(Messages.REQUIRED_PARAMETER, nameof(publisher)), nameof(publisher)); } if (strategies.ContainsKey(name)) { throw new ArgumentException(string.Format(Messages.NAME_ALREADY_EXISTS, name), nameof(name)); } }
internal void AddStrategy(string name, IEventStrategy publisher) { ValidateParameters(name, publisher); strategies.Add(name, publisher); }