Esempio n. 1
0
 public StepComputationResult(bool isSuccess, bool hasEnded, bool hasNextOp, IOp nextOp)
 {
     IsSuccess = isSuccess;
     HasEnded  = hasEnded;
     HasNextOp = hasNextOp;
     NextOp    = nextOp;
 }
Esempio n. 2
0
        public static void ComputeRemain(IEl <int> target,
                                         IOp <Ops.Tick> tick)
        {
            int remain = target.Read();

            if (remain == 0)
            {
                return;
            }

            Ops.Tick t;
            if (tick.TryRead(out t))
            {
                remain -= t.Dt;
            }

            if (remain < 0)
            {
                remain = 0;
            }

            if (remain != target.Read())
            {
                target.Write(remain);
            }
        }
Esempio n. 3
0
        public static void Items(
            ILi <ITodoItem> target,
            IEl <int> nextId,
            IMultiOp <string> newItem_,
            IOp <Empty> deleteCompletedItems_,
            IMultiOp <string> deleteItem_,
            ITodoItemFactory factory)
        {
            var   newItem = newItem_.Read();
            Empty tmp;
            bool  deleteCompletedItems = deleteCompletedItems_.TryRead(out tmp);
            var   deleteItem           = deleteItem_.Read();

            if (newItem.Count <= 0 &&
                !deleteCompletedItems &&
                deleteItem.Count <= 0)
            {
                return;
            }

            var items = target.AsWrite();

            if (newItem.Count > 0)
            {
                for (int i = 0, n = newItem.Count; i < n; ++i)
                {
                    items.Add(factory.Create((nextId.Read() + i).ToString(), newItem[i]));
                }
                nextId.Write(nextId.Read() + newItem.Count);
            }

            if (deleteCompletedItems)
            {
                items.RemoveAll(it =>
                {
                    if (it.IsCompleted.Read())
                    {
                        factory.Dispose(it);
                        return(true);
                    }
                    return(false);
                });
            }

            if (deleteItem.Count > 0)
            {
                items.RemoveAll(it =>
                {
                    if (deleteItem.Contains(it.Id))
                    {
                        factory.Dispose(it);
                        return(true);
                    }
                    return(false);
                });
            }
        }
Esempio n. 4
0
        public void Emit(IOp op)
        {
            if (op is MsgOp msgOp)
            {
                _msgOpStream.Emit(msgOp);
            }

            _opStream.Emit(op);
        }
Esempio n. 5
0
        public State(CompositeDisposable cd, IEngine engine)
        {
            NextId               = engine.El(1);
            CreateNewItem        = engine.MultiOp <string>();
            Items                = engine.Li(new List <ITodoItem>());
            ToggleItemComplete   = engine.MultiOp <string>();
            UncompletedCount     = engine.El(0);
            DeleteCompletedItems = engine.Op <Empty>();
            DeleteItem           = engine.MultiOp <string>();
            EditItem             = engine.Op <string>();
            EditingItemId        = engine.El <string>(null);
            FinishEditItem       = engine.Op <string>();
            ItemFactory          = new TodoItem.Factory(cd, engine,
                                                        ToggleItemComplete, EditingItemId, FinishEditItem);

            engine.Computer(cd,
                            new object[] {
                Items,
                ToggleItemComplete,
            },
                            () => Computers.UncompletedCount(
                                UncompletedCount,
                                Items
                                )
                            );

            engine.Computer(cd,
                            new object[]
            {
                CreateNewItem,
                DeleteCompletedItems,
                DeleteItem
            },
                            () => Computers.Items(
                                Items,
                                NextId,
                                CreateNewItem,
                                DeleteCompletedItems,
                                DeleteItem,
                                ItemFactory
                                )
                            );

            engine.Computer(cd,
                            new object[]
            {
                EditItem,
                FinishEditItem
            },
                            () => Computers.EditingItemId(
                                EditingItemId,
                                EditItem,
                                FinishEditItem
                                )
                            );
        }
Esempio n. 6
0
            public Factory(
                CompositeDisposable cd,
                IEngine engine,
                IMultiOp <string> toggleComplete,
                IEl <string> editingItemId,
                IOp <string> finishEdit
                )
            {
                this.engine         = engine;
                this.toggleComplete = toggleComplete;
                this.editingItemId  = editingItemId;
                this.finishEdit     = finishEdit;

                cd.Add(this);
            }
Esempio n. 7
0
        public bool TryReadOp(out IOp result)
        {
            try
            {
                var b  = _reader.ReadByte();
                var op = GetOp(b);

                result = op.ReadArguments(_reader);
                return(true);
            }
            catch (Exception)
            {
                result = null;
                return(false);
            }
        }
Esempio n. 8
0
        public static void ComputeElapsed(IEl <int> target,
                                          IOp <Ops.Tick> tick)
        {
            int elapsed = target.Read();

            Ops.Tick t;
            if (tick.TryRead(out t))
            {
                elapsed += t.Dt;
            }

            if (elapsed != target.Read())
            {
                target.Write(elapsed);
            }
        }
Esempio n. 9
0
        public static void ComputeList(ILi <IModifierItem> target,
                                       IEntity entity, IOp <Ops.Tick> tick, IList <Ops.Hit> hit,
                                       IModifierItemFactory itemFactory)
        {
            Ops.Tick t;
            if (!tick.TryRead(out t) && hit.Count <= 0)
            {
                return;
            }

            var items = target.AsWrite();

            for (int i = 0, n = hit.Count; i < n; ++i)
            {
                var h = hit[i];
                if (h.To != entity)
                {
                    continue;
                }

                var a = h.From.Hitters.AddModifier;
                if (a == null)
                {
                    continue;
                }

                for (int j = 0, m = a.Modifiers.Count; j < m; ++j)
                {
                    items.Add(itemFactory.Create(a.Modifiers[j]));
                }
            }

            if (tick.TryRead(out t))
            {
                items.RemoveAll(it =>
                {
                    if (it.Remain.Read() == 0)
                    {
                        itemFactory.Dispose(it);
                        return(true);
                    }
                    return(false);
                });
            }
        }
Esempio n. 10
0
        public static void EditingItemId(IEl <string> target, IOp <string> edit_, IOp <string> finish)
        {
            string edit;
            string tmp;
            var    editingItemId = target.Read();

            if (finish.TryRead(out tmp))
            {
                editingItemId = null;
            }
            else if (edit_.TryRead(out edit))
            {
                editingItemId = edit;
            }

            if (editingItemId != target.Read())
            {
                target.Write(editingItemId);
            }
        }
Esempio n. 11
0
        public TodoItem(
            CompositeDisposable cd,
            IEngine engine,
            IMultiOp <string> toggleComplete,
            IEl <string> editingItemId,
            IOp <string> finishEdit,
            string id,
            string content)
        {
            this.id      = id;
            this.content = engine.El(content);
            isCompleted  = engine.El(false);

            cd = new CompositeDisposable();

            engine.Computer(cd,
                            new object[] {
                toggleComplete
            },
                            () => Computers.TodoItem.IsCompleted(
                                isCompleted,
                                toggleComplete,
                                this.id
                                )
                            );

            engine.Computer(cd,
                            new object[] {
                editingItemId,
                finishEdit
            },
                            () => Computers.TodoItem.Content(
                                this.content,
                                editingItemId,
                                finishEdit,
                                this.id
                                )
                            );
        }
Esempio n. 12
0
 /// <summary>
 /// Constructs a new pending binary operation
 /// </summary>
 /// <param name="operation">The operation to perform</param>
 /// <param name="output">The output operand</param>
 /// <param name="in1">An input operand</param>
 /// <param name="in2">An input operand</param>
 public PendingBinaryConversionOperation(IOp <Ta> operation, NdArray <Ta> output, NdArray <Tb> in1, NdArray <Tb> in2)
     : base(operation, output, in1)
 {
     InputOperandRhs = in2;
 }
Esempio n. 13
0
 /// <summary>
 /// Constructs a new pending unary operation
 /// </summary>
 /// <param name="operation">The operation to perform</param>
 /// <param name="output">The output operand</param>
 /// <param name="input">The input operand</param>
 public PendingUnaryConversionOperation(IOp <Ta> operation, NdArray <Ta> output, NdArray <Tb> input)
     : base(operation, output)
 {
     InputOperand = input;
 }
Esempio n. 14
0
 /// <summary>
 /// Constructs a new pending operation
 /// </summary>
 /// <param name="operation">The operation to perform</param>
 /// <param name="operands">The operands involved</param>
 public PendingOperation(IOp <T> operation, params NdArray <T>[] operands)
 {
     this.m_clock   = LazyAccessorCollector.ClockTick;
     this.Operation = operation;
     this.Operands  = operands;
 }
Esempio n. 15
0
 /// <summary>
 /// Register a pending operation on the underlying array
 /// </summary>
 /// <param name="operation">The operation performed</param>
 /// <param name="operands">The operands involved, operand 0 is the target</param>
 public virtual void AddOperation(IOp <T> operation, params NdArray <T>[] operands)
 {
     m_clock = LazyAccessorCollector.AddOperation(new PendingOperation <T>(operation, operands));
 }
Esempio n. 16
0
 public bool TryGetFollowingOp(out IOp op)
 {
     op = null;
     return(false);
 }
Esempio n. 17
0
        //internal CursorState State { get; set; }

        #endregion Cursor state

        #region Constructors

        internal Comparison(TCursor cursor, TValue value, IOp <TValue, bool> op) : this()
        {
            _op     = op;
            _value  = value;
            _cursor = cursor;
        }
Esempio n. 18
0
 public override bool TryGetFollowingOp(out IOp followingOp)
 {
     followingOp = new Verify();
     return(true);
 }
    private static IEnumerator WaitThenRestart(IOp <Empty> restart)
    {
        yield return(new WaitForSeconds(1f));

        restart.Fire(Empty.Instance);
    }
Esempio n. 20
0
 public virtual bool TryGetFollowingOp(out IOp followingOp)
 {
     followingOp = null;
     return(false);
 }
Esempio n. 21
0
 public void EnqueueSend(IOp op)
 {
     _writeQueue.Enqueue(op);
 }
Esempio n. 22
0
 public static StepComputationResult DefaultSuccessNextOp(IOp nextOp)
 {
     return(new StepComputationResult(true, false, true, nextOp));
 }
Esempio n. 23
0
        public IOp ReadOneOp()
        {
            IOp op = null;

            using var workspace = new MemoryStream();
            var opMarkerChars = new byte[4];
            var i             = -1;

            while (true)
            {
                var curr = _stream.ReadByte();
                if (curr == -1)
                {
                    return(null);
                }

                var c = (byte)curr;
                if (!IsDelimMarker(c) && c != Cr && c != Lf)
                {
                    opMarkerChars[++i] = c;
                    continue;
                }

                if (i == -1)
                {
                    continue;
                }

                if (opMarkerChars[0] == M)
                {
                    op = ParseMsgOp(_stream, workspace);
                }
                else if (opMarkerChars[0] == P)
                {
                    if (opMarkerChars[1] == I)
                    {
                        op = ParsePingOp(_stream);
                    }
                    else if (opMarkerChars[1] == O)
                    {
                        op = ParsePongOp(_stream);
                    }
                }
                else if (opMarkerChars[0] == I)
                {
                    op = ParseInfoOp(_stream, workspace);
                }
                else if (opMarkerChars[0] == Plus)
                {
                    op = ParseOkOp(_stream);
                }
                else if (opMarkerChars[0] == Minus)
                {
                    op = ParseErrorOp(_stream, workspace);
                }

                if (op == null)
                {
                    var opMarker = string.Create(i + 1, opMarkerChars, (t, v) =>
                    {
                        if (t.Length == 4)
                        {
                            t[3] = (char)v[3];
                        }

                        t[2] = (char)v[2];
                        t[1] = (char)v[1];
                        t[0] = (char)v[0];
                    });

                    throw NatsException.OpParserUnsupportedOp(opMarker);
                }

                i = -1;
                opMarkerChars[0]   = EmptyOpMarker;
                opMarkerChars[1]   = EmptyOpMarker;
                opMarkerChars[2]   = EmptyOpMarker;
                opMarkerChars[3]   = EmptyOpMarker;
                workspace.Position = 0;

                return(op);
            }
        }
Esempio n. 24
0
        public static void ComputeCurrent(IEl <int> target,
                                          int max, int regenSpeed, IEntity entity, int armorValue,
                                          IOp <Ops.Tick> tick, IList <Ops.Hit> hit, IList <IStickHitItem> stickHits,
                                          int randomSeed)
        {
            if (target.Read() <= 0)
            {
                return;
            }

            int add = 0;
            int sub = 0;

            int ticks = 0;

            Ops.Tick t;
            if (tick.TryRead(out t))
            {
                ticks = t.Dt;
            }

            add += ticks * regenSpeed;

            for (int i = 0, n = hit.Count; i < n; ++i)
            {
                var h = hit[i];
                if (h.From.HasOwner.Owner != entity)
                {
                    continue;
                }

                var hitters = h.From.Hitters;
                if (hitters.Damage == null)
                {
                    continue;
                }

                int lifeStealPercent = hitters.Damage.LifeStealPercent.Read();
                if (lifeStealPercent <= 0)
                {
                    continue;
                }

                // TODO Still need hit random seed, beside world random seed
                // to make sure 2 hits in same update
                // are different when calc critical
                int dealtDamage = CalcDealtDamage(hitters.Damage,
                                                  h.To.Armor.Value.Read(), randomSeed, h.RandomSeed);
                int canStealAmount = Math.Min(dealtDamage, h.To.Health.Current.Read());
                add += (int)Math.Ceiling(canStealAmount * (lifeStealPercent / 100f));
            }

            for (int i = 0, n = stickHits.Count; i < n; ++i)
            {
                var s = stickHits[i];
                var h = s.Hit;
                if (h.From.HasOwner.Owner != entity)
                {
                    continue;
                }

                var hitters = h.From.Hitters;
                if (hitters.Damage == null)
                {
                    continue;
                }

                int lifeStealPercent = hitters.Damage.LifeStealPercent.Read();
                if (lifeStealPercent <= 0)
                {
                    continue;
                }

                int dealtDamage = CalcDealtDot(ticks, s, hitters.Damage,
                                               h.To.Armor.Value.Read(), randomSeed, h.RandomSeed);
                int canStealAmount = Math.Min(dealtDamage, h.To.Health.Current.Read());
                add += (int)Math.Ceiling(canStealAmount * (lifeStealPercent / 100f));
            }

            for (int i = 0, n = hit.Count; i < n; ++i)
            {
                var h = hit[i];
                if (h.To != entity)
                {
                    continue;
                }

                var hitters = h.From.Hitters;
                if (hitters.Damage == null)
                {
                    continue;
                }

                sub += CalcDealtDamage(hitters.Damage, armorValue,
                                       randomSeed, h.RandomSeed);
            }

            for (int i = 0, n = hit.Count; i < n; ++i)
            {
                var h = hit[i];
                if (h.From.HasOwner.Owner != entity)
                {
                    continue;
                }

                var reflectDamagePercent = h.To.DamageReflector.Percent.Read();
                if (reflectDamagePercent <= 0)
                {
                    continue;
                }

                var hitters = h.From.Hitters;
                if (hitters.Damage == null)
                {
                    continue;
                }

                int dealtDamage = CalcDealtDamage(hitters.Damage,
                                                  h.To.Armor.Value.Read(), randomSeed, h.RandomSeed);
                sub += (int)Math.Ceiling(dealtDamage * (reflectDamagePercent / 100f));
            }

            for (int i = 0, n = stickHits.Count; i < n; ++i)
            {
                var s = stickHits[i];
                var h = s.Hit;
                if (h.To != entity)
                {
                    continue;
                }

                var hitters = h.From.Hitters;
                if (hitters.Damage == null)
                {
                    continue;
                }

                sub += CalcDealtDot(ticks, s, hitters.Damage,
                                    armorValue, randomSeed, h.RandomSeed);
            }

            // Can be very flexible here, prefer add over sub, sub over add or fair
            int current = target.Read();

            current = Math.Min(max, current + add);
            current = Math.Max(0, current - sub);
            if (current != target.Read())
            {
                target.Write(current);
            }
        }
Esempio n. 25
0
            public static void Content(IEl <string> target, IEl <string> editingItemId, IOp <string> finishEdit_, string myId)
            {
                string finishEdit;

                if (editingItemId.Read() == myId && finishEdit_.TryRead(out finishEdit))
                {
                    string newContent = finishEdit;
                    if (!string.IsNullOrEmpty(newContent))
                    {
                        target.Write(newContent);
                    }
                }
            }