Example #1
0
        public void UpdateFill(FillEvent fill)
        {
            var fillDir    = GetNumericDirection(fill.Direction);
            var closePrice = this.bars.GetLastClosePrice(fill.Symbol);

            if (closePrice == null)
            {
                throw new InvalidOperationException($"Cannot find last price for {fill.Symbol}");
            }

            var cost = fillDir * closePrice.Value * fill.Quantity;

            this.currentPositions[fill.Symbol] += fillDir * fill.Quantity;
            this.currentComission += fill.Comission;
            this.currentCash      -= (cost + fill.Comission);
        }
        public void ExecuteOrder(OrderEvent orderEvent)
        {
            // Simulate order execution delay
            var dateTime = orderEvent.OrderTime.AddSeconds(CONST_ExecutionDelaySeconds);

            var closePrice = this.bars.GetLastClosePrice(orderEvent.Symbol);
            var fillCost   = closePrice * orderEvent.Quantity ?? decimal.Zero;

            var fillEvent = new FillEvent(
                dateTime,
                orderEvent.Symbol,
                "ARCA",
                orderEvent.Quantity,
                orderEvent.OrderDirection,
                fillCost);

            this.eventBus.Put(fillEvent);
        }
    /// <summary>
    /// Fill all the element with fill event. If To = from then hexagon element is new instantiated. Use object pooling.
    /// If To != from then move hexagon gameobject from location to to location.
    /// Check if there is any move left for player to play. If there is not, then game is over.
    /// If there is more moves left, set non player match elkements to see if there is any match to ready to expldoe.
    /// </summary>
    /// <param name="fillEvent"></param>
    /// <returns></returns>
    private IEnumerator Fill(FillEvent fillEvent)
    {
        foreach (List <MoveElement> list in fillEvent.fillLists)
        {
            foreach (MoveElement moveElemnt in list)
            {
                if (moveElemnt.to == moveElemnt.from)
                {
                    SetNewType(itemComponentsPool[0], moveElemnt.to, moveElemnt.hexagonElement.matchType, moveElemnt.hexagonElement.bombInfo);
                    if (!gridComponentHolder.ContainsKey(moveElemnt.to))
                    {
                        gridComponentHolder.Add(moveElemnt.to, itemComponentsPool[0]);
                    }
                    else
                    {
                        gridComponentHolder[moveElemnt.to] = itemComponentsPool[0];
                    }
                    itemComponentsPool.RemoveAt(0);
                }
                else
                {
                    gridComponentHolder[moveElemnt.from].move.CreateMoveToCoroutine(positionCalculator.CoordToAnchored(moveElemnt.to));
                    gridComponentHolder[moveElemnt.to]   = gridComponentHolder[moveElemnt.from];
                    gridComponentHolder[moveElemnt.from] = null;
                }
            }
            yield return(new WaitForSeconds(0.05f));
        }

        // Sets null match element to get successive explode events.
        if (fillEvent.leftMove)
        {
            SetMatchElement();
        }
        else
        {
            Debug.Log("No move left move! Game over.");
            GameOver?.Invoke();
        }
        yield return(null);
    }
    /// <summary>
    /// Explodes all the explode element in the explode event, add explodede gameobjects to object pool to reuse them.
    /// Invoke increase score after all explosions and start fill event for vacancies of exploded gameobjects.
    /// </summary>
    /// <param name="explodeEvent"></param>
    /// <returns></returns>
    private IEnumerator Explode(ExplodeEvent explodeEvent)
    {
        yield return(new WaitForSeconds(0.3f));

        foreach (ExplodeElement explodeElement in explodeEvent.explodeElements)
        {
            foreach (Vector2 coord in explodeElement.positions)
            {
                gridComponentHolder[coord].explode.SetExplode();
                itemComponentsPool.Add(gridComponentHolder[coord]);
                gridComponentHolder[coord] = null;
            }
        }
        yield return(new WaitForSeconds(1f));

        IncreaseScore?.Invoke(hexagonLogic.GetCurrentScore());
        FillEvent fillEvent = hexagonLogic.Fill();

        SetFill(fillEvent);
        yield return(null);
    }
 /// <summary>
 /// Start fill coroutine.
 /// </summary>
 /// <param name="fillEvent"></param>
 private void SetFill(FillEvent fillEvent)
 {
     StopAllCoroutines();
     StartCoroutine(Fill(fillEvent));
 }