コード例 #1
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
        /// <param name="id">what incident to execute the given procedure on</param>
        /// <param name="procedure">what to do for each of the count iterations</param>
        /// <param name="count">how many times to execute the given procedure</param>
        /// <param name="onLast">special logic to do in addition to the standard procedure on the last iteration</param>
        /// <exception cref="Exception"></exception>
        public void OnIncident(Proc.Id id, Proc.edure procedure, int count = -1, Proc.edure onLast = null)
        {
            if (count == 0)
            {
                return;
            }
            List <Proc.edure> responses = incidentResponseTable[id.Value];

            if (count < 0)
            {
                responses.Add(procedure);
                return;
            }
            Proc.edure countLimitedProcedure = incident => {
                if (count <= 0)
                {
                    throw new Exception("how was count decremented outside of this function?");
                }
                Proc.Result result = procedure.Invoke(incident);
                --count;
                if (count > 0)
                {
                    return(result);
                }
                if (onLast != null && result == Proc.Result.Success)
                {
                    result = onLast.Invoke(incident);
                }
                RemoveIncident(id.Value, procedure);
                return(result);
            };
            responseAilias[procedure] = countLimitedProcedure;
            responses.Add(countLimitedProcedure);
        }
コード例 #2
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
 public void NotifyIncident(Proc.Id id, Incident incident)
 {
     // make an array copy of the list because the list might be modified by the execution of elements in the list.
     Proc.edure[] responses = incidentResponseTable[id.Value].ToArray();
     ++codeToIncident[id.Value].Count;
     responses.ForEach(response => response.Invoke(incident));
 }
コード例 #3
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
 public int GetResponseIndex(Proc.Id id, Proc.edure response)
 {
     if (!IsValidCode(id.Value))
     {
         throw new Exception("bad incident code");
     }
     return(incidentResponseTable[id.Value].IndexOf(response));
 }
コード例 #4
0
ファイル: Strategy.cs プロジェクト: mvaganov/20210131_ggj2021
        /// <summary>
        /// keep executing procedure until it returns Proc.Result.Success.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="procedure">incident listener is removed if result is <see cref="Proc.Result.Success"/>. should return <see cref="Proc.Result.Halt"/> until it should stop</param>
        public Strategy ThenWhileIncident(Proc.Id id, Proc.edure procedure = null)
        {
            Strategy deferringStrategy = null;

            deferringStrategy = ThenImmediately("(defer)" + id, incident => {
                Strategy deferredStrategy   = new Strategy("(deferred)" + id, procedure, this);
                deferredStrategy.Next       = deferringStrategy.Next;
                deferredStrategy.IsDeferred = true;
                Proc.WhileIncident(id, deferredStrategy.Invoke, deferringStrategy.ContinueChain);
                return(Proc.Result.Halt);
            });
            return(deferringStrategy);
        }
コード例 #5
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
        /// <summary>
        /// keep executing procedure until it returns Proc.Result.Success.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="procedure">incident listener is removed if result is <see cref="Proc.Result.Success"/>. should return <see cref="Proc.Result.Halt"/> until it should stop</param>
        /// <param name="onFinish"></param>
        public void WhileIncident(Proc.Id id, Proc.edure procedure, Proc.edure onFinish)
        {
            List <Proc.edure> responses = incidentResponseTable[id.Value];

            Proc.edure resultLimitedProcedure = incident => {
                Proc.Result result = procedure.Invoke(incident);
                if (result == Proc.Result.Success || result == Proc.Result.Failure)
                {
                    RemoveIncident(id, procedure);
                    onFinish.Invoke(incident);
                    return(Proc.Result.Success);
                }
                return(result);
            };
            responseAilias[procedure] = resultLimitedProcedure;
            responses.Add(resultLimitedProcedure);
        }
コード例 #6
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
        public bool RemoveIncident(Proc.Id id, Proc.edure procedure)
        {
            bool removed = false;

            if (IsValidCode(id.Value) && !incidentResponseTable[id.Value].Remove(procedure))
            {
                if (responseAilias.TryGetValue(procedure, out Proc.edure alias))
                {
                    //RemoveIncident(incidentCode, alias);
                    incidentResponseTable[id.Value].Remove(alias);
                    removed |= responseAilias.Remove(procedure);
                }
                else
                {
                    // Debug.LogWarning("the given response is not in the response table");
                }
            }
            return(removed);
        }
コード例 #7
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
        /// <summary>
        /// if a non-<see cref="Proc.edure"/> is used as a response to the incident, this should clear it
        /// </summary>
        public bool RemoveIncident(Proc.Id id, object procedure)
        {
            Proc.edure r       = procedure as Proc.edure;
            bool       removed = false;

            if (r == null)
            {
                if (responseAilias.TryGetValue(procedure, out r))
                {
                    removed = responseAilias.Remove(procedure);
                }
                else
                {
                    // Debug.LogWarning("the given response is not in the response table");
                }
            }
            removed |= RemoveIncident(id, r);
            return(removed);
        }
コード例 #8
0
 // functions so we can pass more straight forward Actions instead of more detailed Reactions
 public static void OnIncident(Proc.Id id, Action procedure, int count = -1, Action onLast = null)
 {
     Main.OnIncident(id, ConvertR(procedure, true), count, ConvertR(onLast, false));
 }
コード例 #9
0
 // functions so we don't need to include "return Procedure.Result.Success;" at the end of each lambda
 public static void OnIncident(Proc.Id id, Proc.edure procedure, int count = -1, Proc.edure onLast = null)
 {
     Main.OnIncident(id, procedure, count, onLast);
 }
コード例 #10
0
 public static Incident Enqueue(Proc.Id incidentId)
 {
     return(SystemClock.Delay(0, incidentId.Value));
 }
コード例 #11
0
 public static bool RemoveIncident(Proc.Id id, object procedure)
 {
     return(Main.RemoveIncident(id, procedure));
 }
コード例 #12
0
 public static void WhileIncident(Proc.Id id, Proc.edure procedure, Proc.edure onFinish)
 {
     Main.WhileIncident(id, procedure, onFinish);
 }
コード例 #13
0
 public static void NotifyIncident(Proc.Id id, Incident incident)
 {
     Main.NotifyIncident(id, incident);
 }
コード例 #14
0
ファイル: Processr.cs プロジェクト: mvaganov/20210131_ggj2021
 /// <summary>
 /// convenience method: creates and <see cref="Incident"/> and passes it to any <see cref="Proc.edure"/>s waiting for it
 /// </summary>
 /// <param name="id"></param>
 /// <param name="source"></param>
 /// <param name="detail"></param>
 /// <param name="identifier"></param>
 public void NotifyIncident(Proc.Id id, object source = null, object detail = null, string identifier = null)
 {
     NotifyIncident(id, new Incident(SystemClock.GetTime(), identifier, source, detail));
 }