Exemple #1
0
        public ActionResult Edit(Guid id)
        {
            NodeWrapper nodeWrapper = _unnoService.LoadNode(id);
            UnitWrapper unitWrapper = null;

            if (nodeWrapper == null)
            {
                unitWrapper = _unnoService.LoadUnit(id);
                if (unitWrapper == null)
                {
                    return(new HttpNotFoundResult());
                }
                nodeWrapper = new NodeWrapper
                {
                    NodeId = unitWrapper.UnitId,
                    UnitId = unitWrapper.UnitId,
                    Node   = new Node(unitWrapper.Unit.Key)
                };
            }
            else
            {
                unitWrapper = _unnoService.LoadUnit(nodeWrapper.UnitId);
            }
            ViewBag.Unit = unitWrapper.Unit;
            ViewBag.Node = nodeWrapper.Node;
            return(View(Path.Combine("Edit", nodeWrapper.UnitId.ToString()), nodeWrapper));
        }
Exemple #2
0
        public UnitWrapper LoadUnit(Guid id)
        {
            using (MySqlConnection connection = OpenConnection())
            {
                UnitWrapper wrapper = connection.Query <UnitWrapper>(
                    @"SELECT `Name`, `Version`, `UpdateAt` FROM `unno_unit_wrapper` WHERE `UnitId` = @UnitId;",
                    new { UnitId = id }).SingleOrDefault();
                if (wrapper == null)
                {
                    return(null);
                }
                wrapper.UnitId = id;
                var units = connection.Query <UnitRow>(
                    @"SELECT `Id`, `ParentId`, `Key`, `Name` FROM `unno_unit` WHERE `UnitId` = @UnitId ORDER BY Id;",
                    new { UnitId = id });
                var specs = connection.Query <SpecRow>(
                    @"SELECT `Id`, `Type`, `Min`, `Max`, `Options` FROM `unno_unit_specs` WHERE `UnitId` = @UnitId ORDER BY `Id`, `SpecId`;",
                    new { UnitId = id });

                var dummy = new Unit("dummy", null);
                BindUnit(units, specs, dummy, null);
                wrapper.Unit = dummy.Children.FirstOrDefault();
                return(wrapper);
            }
        }
Exemple #3
0
        private void SetNextText(BattleData battleData, int standardActivityPoint, List <UnitWrapper> otherUnits)
        {
            for (int i = 0; i < otherUnits.Count; i += 1)
            {
                UnitWrapper unit = otherUnits[i];
                if (GetActivityPoint(battleData, unit) >= standardActivityPoint)
                {
                    continue;
                }

                if (nextTurnTexts.Count <= i)
                {
                    Debug.Log("Unit is to many for this APBar");
                    return;
                }

                GameObject nextTurnText = nextTurnTexts[i];
                nextTurnText.GetComponent <Image>().enabled = true;

                // Does not make space no units left in current turn.
                if (i != 0)
                {
                    nextTurnText.GetComponent <RectTransform>().anchoredPosition += new Vector2(seperationSpace * 2, 0);
                }

                return;
            }
        }
Exemple #4
0
        private bool SaveWrapper(MySqlConnection connection, UnitWrapper wrapper)
        {
            UnitWrapper found = connection.Query <UnitWrapper>(
                @"SELECT `Version` FROM `unno_unit_wrapper` WHERE `UnitId` = @UnitId FOR UPDATE;",
                new { UnitId = wrapper.UnitId }).SingleOrDefault();

            if (found == null)
            {
                connection.Execute(
                    @"INSERT INTO `unno_unit_wrapper` (`UnitId`, `Name`, `Version`, `UpdateAt`)
VALUES (@UnitId, @Name, @Version, @UpdateAt);", wrapper);
            }
            else
            {
                wrapper.Version++;
                connection.Execute(
                    @"UPDATE `unno_unit_wrapper` SET
`Name` = @Name, 
`Version` = @Version, 
`UpdateAt` = @UpdateAt 
WHERE `UnitId` = @UnitId;", wrapper);

                connection.Execute(
                    @"DELETE FROM `unno_unit` WHERE `UnitId` = @UnitId;
DELETE FROM `unno_unit_specs` WHERE `UnitId` = @UnitId;", new { UnitId = wrapper.UnitId });
            }
            return(true);
        }
Exemple #5
0
        private void SetProfiles(BattleData battleData, int standardActivityPoint, List <UnitWrapper> otherUnits)
        {
            if (otherUnits.Count == 0)
            {
                return;
            }

            for (int index = 0; index < otherUnits.Count; index += 1)
            {
                GameObject profileGameObject = otherProfiles[index];

                profileGameObject.GetComponent <Image>().enabled = true;
                SetProfile(battleData, profileGameObject, otherUnits[index], standardActivityPoint);
            }

            // Does not make space no units left in current turn.
            if (otherUnits[0].GetUnit().GetCurrentActivityPoint() < standardActivityPoint)
            {
                return;
            }

            for (int index = 0; index < otherUnits.Count; index += 1)
            {
                GameObject  profileGameObject = otherProfiles[index];
                UnitWrapper unit = otherUnits[index];

                if (GetActivityPoint(battleData, unit) < standardActivityPoint)
                {
                    profileGameObject.GetComponent <RectTransform>().anchoredPosition += new Vector2(seperationSpace * 2, 0);
                }
            }
        }
Exemple #6
0
 public UnitWrapper4Mongo(UnitWrapper wrapper)
 {
     this.UnitId   = wrapper.UnitId;
     this.Name     = wrapper.Name;
     this.UpdateAt = wrapper.UpdateAt;
     this.Version  = wrapper.Version;
     this.Unit     = wrapper.Unit;
 }
Exemple #7
0
        public ActionResult Index(Guid id)
        {
            NodeWrapper nodeWrapper = _unnoService.LoadNode(id);
            UnitWrapper unitWrapper = _unnoService.LoadUnit(nodeWrapper.UnitId);

            ViewBag.Unit = unitWrapper.Unit;
            ViewBag.Node = nodeWrapper.Node;
            return(View(nodeWrapper.UnitId.ToString(), nodeWrapper));
        }
Exemple #8
0
        private int GetActivityPoint(BattleData battleData, UnitWrapper wrapper)
        {
            int activityPoint = wrapper.GetUnit().GetCurrentActivityPoint();

            if (wrapper.IsPreviewUnit() && battleData.previewAPAction != null)
            {
                activityPoint -= battleData.previewAPAction.requiredAP;
            }
            return(activityPoint);
        }
Exemple #9
0
        public void SaveUnit(UnitWrapper wrapper)
        {
            UnitWrapper4Mongo wrapper4Mongo = new UnitWrapper4Mongo(wrapper);
            var reload = LoadUnit(wrapper.UnitId);

            if (reload != null)
            {
                wrapper4Mongo.Id = (reload as UnitWrapper4Mongo).Id;
            }
            _proxy.Unit.Save(wrapper4Mongo);
        }
        /// <summary>
        /// Adds a unit.
        /// </summary>
        public void Add(TKey key, T unit, IConvex2D shape)
        {
            UnitWrapper wrapper = new UnitWrapper(unit, shape);

            foreach (var cell in _getOrCreateSupercover(shape))
            {
                cell.Add(key, wrapper);
            }

            wrappers.Add(key, wrapper);
            Count++;
        }
        /// <summary>
        /// Finds nearest neighbour within maxDistance for which a predicate evaluates to true
        /// </summary>
        protected T _nearest(Vector2 position, float maxDistance, Predicate <T> predicate, out float distSquared)
        {
            UnitWrapper nearestWrapper = null;
            float       nearestDist    = float.PositiveInfinity;
            float       radius         = Mathf.Min(CellSize.x, CellSize.y); // Intial search radius

            // Handles a cell
            Action <TCell> handleCell = (cell) =>
            {
                float nearestDistInCell;
                var   nearestInCell = cell.Nearest(position, radius, predicate, out nearestDistInCell);

                if (nearestDistInCell < nearestDist)
                {
                    nearestDist    = nearestDistInCell;
                    nearestWrapper = nearestInCell;
                }
            };

            // Keep searching and doubling our radius until we've found a unit or when we've searched beyond the limit
            while (nearestWrapper == null && radius <= maxDistance)
            {
                foreach (Vector2Int cellIndex in new Circle(position, radius).Supercover(this))
                {
                    if (_cells[cellIndex.x, cellIndex.y] != null)
                    {
                        handleCell(_cells[cellIndex.x, cellIndex.y]);
                    }
                }

                radius *= 2;
            }

            // In case our radius expanded beyond the limit we need to search one more time exactly at limit
            if (radius > maxDistance && nearestWrapper == null)
            {
                radius = maxDistance;

                foreach (Vector2Int cellIndex in new Circle(position, radius).Supercover(this))
                {
                    if (_cells[cellIndex.x, cellIndex.y] != null)
                    {
                        handleCell(_cells[cellIndex.x, cellIndex.y]);
                    }
                }
            }

            distSquared = nearestDist;
            return(nearestWrapper == null ? default(T) : nearestWrapper.Unit);
        }
Exemple #12
0
        private void InitUnit(Unit unit)
        {
            _instcount++;
            reattachFormationToNewParentKey = new StringBuilder("reattachformationtonewparent").Append(_instcount).ToString();

            _unitWrapper = new UnitWrapper(unit);
            GetUnit().SubscribeOnParentChange(reattachFormationToNewParentKey,
                                              () =>
            {
                ChangeParentTo(GetUnit().GetParentActorAsUnit()?.GetFormation());
            });

            //SubscribeOnDestruction("unsubreattachtonewparent", () => GetUnit().UnsubscribeOnDestruction(reattachFormationToNewParentKey));
        }
Exemple #13
0
 public void SaveUnit(UnitWrapper wrapper)
 {
     using (MySqlConnection connection = OpenConnection())
     {
         using (IDbTransaction transaction = connection.BeginTransaction())
         {
             if (SaveWrapper(connection, wrapper))
             {
                 int id = 0;
                 SaveUnit(connection, wrapper.Unit, wrapper.UnitId, null, ref id);
                 transaction.Commit();
             }
         }
     }
 }
Exemple #14
0
        /// <summary>
        /// Updates the specified unit.
        /// </summary>
        /// <param name="unit">The unit.</param>
        /// <param name="token">The token.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// The task result contains the updated unit.
        /// </returns>
        /// <exception cref="ArgumentException">Thrown when the parameter check fails.</exception>
        /// <exception cref="NotAuthorizedException">Thrown when not authorized to access this resource.</exception>
        /// <exception cref="NotFoundException">Thrown when the resource url could not be found.</exception>
        public async Task <Unit> EditAsync(Unit unit, CancellationToken token = default)
        {
            if (unit.Id <= 0)
            {
                throw new ArgumentException("invalid unit id", nameof(unit));
            }

            var wrappedUnit = new UnitWrapper
            {
                Unit = unit.ToApi()
            };

            var jsonModel = await PutAsync($"/api/units/{unit.Id}", wrappedUnit, token);

            return(jsonModel.ToDomain());
        }
        /// <summary>
        /// Updates a unit's position / shape
        /// </summary>
        public void Update(TKey key, IConvex2D newShape)
        {
            UnitWrapper wrapper = wrappers[key];

            foreach (var cell in _getOrCreateSupercover(wrapper.Shape))
            {
                cell.Remove(key);
            }

            var newWrapper = new UnitWrapper(wrapper.Unit, newShape);

            foreach (var cell in _getOrCreateSupercover(newShape))
            {
                cell.Add(key, newWrapper);
            }
            wrappers[key] = newWrapper;
        }
Exemple #16
0
        public ActionResult Edit(Guid id, int version, string name = null)
        {
            NodeWrapper nodeWrapper = _unnoService.LoadNode(id);
            UnitWrapper unitWrapper = null;

            if (nodeWrapper == null)
            {
                unitWrapper = _unnoService.LoadUnit(id);
                if (unitWrapper == null)
                {
                    return(new HttpNotFoundResult());
                }
                nodeWrapper = new NodeWrapper {
                    NodeId = Guid.NewGuid(), UnitId = unitWrapper.UnitId
                };
            }
            else
            {
                unitWrapper = _unnoService.LoadUnit(nodeWrapper.UnitId);
            }

            Node node   = _unnoService.Parse(unitWrapper.Unit, Request.Form);
            var  errors = new Dictionary <string, string>();

            node.Validate(errors);
            if (errors.Count > 0)
            {
                foreach (var error in errors)
                {
                    ModelState.AddModelError(error.Key, error.Value);
                }
            }

            nodeWrapper.Name    = name;
            nodeWrapper.Version = version;
            nodeWrapper.Node    = node;

            if (ModelState.IsValid)
            {
                _unnoService.SaveNode(nodeWrapper);
            }

            ViewBag.Unit = unitWrapper.Unit;
            ViewBag.Node = nodeWrapper.Node;
            return(RedirectToAction("Edit", new { id = nodeWrapper.NodeId }));
        }
Exemple #17
0
        private void SetCurrentText(BattleData battleData, int standardActivityPoint, List <UnitWrapper> otherUnits)
        {
            if (otherUnits.Count == 0)
            {
                return;
            }

            UnitWrapper firstUnit = otherUnits[0];

            if (GetActivityPoint(battleData, firstUnit) >= standardActivityPoint)
            {
                currentTurnTexts[0].GetComponent <Image>().enabled = true;
            }
            else
            {
                return;
            }
        }
Exemple #18
0
        /// <summary>
        /// Creates a unit.
        /// </summary>
        /// <param name="value">The unit to create.</param>
        /// <param name="token">The cancellation token.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// The task result contains the new unit.
        /// </returns>
        /// <exception cref="ArgumentException">Thrown when the parameter check fails.</exception>
        /// <exception cref="NotAuthorizedException">Thrown when not authorized to access this resource.</exception>
        /// <exception cref="NotFoundException">Thrown when the resource url could not be found.</exception>
        public async Task <Unit> CreateAsync(Unit value, CancellationToken token = default)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (string.IsNullOrEmpty(value.Name) || value.Id != 0)
            {
                throw new ArgumentException("invalid property values for unit", nameof(value));
            }

            var wrappedUnit = new UnitWrapper
            {
                Unit = value.ToApi()
            };
            var jsonModel = await PostAsync("/api/units", wrappedUnit, token).ConfigureAwait(false);

            return(jsonModel.ToDomain());
        }
            /// <summary>
            /// Returns the nearest unit wrapper to position that is within limit and conforms to predicate
            /// </summary>
            public UnitWrapper Nearest(Vector2 position, float limit, Predicate <T> predicate, out float nearestDistSquared)
            {
                float       limitSquared   = limit * limit;
                UnitWrapper nearestWrapper = null;

                nearestDistSquared = float.PositiveInfinity;

                foreach (var wrapper in _unitWrappers)
                {
                    float d = wrapper.Shape.DistanceSquared(position);

                    if (d < nearestDistSquared && d < limitSquared && predicate(wrapper.Unit))
                    {
                        nearestDistSquared = d;
                        nearestWrapper     = wrapper;
                    }
                }

                return(nearestWrapper);
            }
Exemple #20
0
        public NodeWrapper LoadNode(Guid id)
        {
            NodeWrapper      wrapper     = null;
            int              rootId      = 0;
            UnitWrapper      unitWrapper = null;
            List <NodeTable> tables      = new List <NodeTable>();

            using (MySqlConnection connection = OpenConnection())
            {
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"SELECT `NodeId`, `UnitId`, `RootId`, `Version`, `UpdateAt` FROM `unno_node_wrapper` WHERE `NodeId` = @NodeId;";
                    cmd.Parameters.AddWithValue("@NodeId", id);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            wrapper          = new NodeWrapper();
                            rootId           = (int)reader["RootId"];
                            wrapper.NodeId   = id;
                            wrapper.UnitId   = Guid.Parse(reader["UnitId"].ToString());
                            wrapper.Version  = (int)reader["Version"];
                            wrapper.UpdateAt = (DateTime)reader["UpdateAt"];
                        }
                    }
                }
                if (wrapper != null)
                {
                    unitWrapper = _unitRepository.LoadUnit(wrapper.UnitId);
                    LoadDataFromDB(connection, rootId, unitWrapper.Unit, UNNO_TABLE_PREFIX, tables);
                }
            }

            if (wrapper == null)
            {
                return(null);
            }
            wrapper.Node = BuildNode(unitWrapper.Unit, tables, UNNO_TABLE_PREFIX, 0);
            return(wrapper);
        }
Exemple #21
0
        private void SetSeperateBar(BattleData battleData, int standardActivityPoint, List <UnitWrapper> otherUnits)
        {
            int nextTurnUnitIndex = -1;

            for (int i = 0; i < otherUnits.Count; i += 1)
            {
                UnitWrapper unit = otherUnits[i];
                if (GetActivityPoint(battleData, unit) < standardActivityPoint)
                {
                    nextTurnUnitIndex = i;
                    break;
                }
            }

            if (nextTurnUnitIndex == -1)
            {
                return;
            }

            if (nextTurnUnitIndex == 0)
            {
                return;
            }

            int barIndex = nextTurnUnitIndex - 1;

            if (turnSeperateBars.Count <= barIndex)
            {
                Debug.LogWarning("Unit is to many for this APBar");
                return;
            }

            GameObject turnSeperateBar = turnSeperateBars[barIndex];

            turnSeperateBar.GetComponent <Image>().enabled = true;
            turnSeperateBar.GetComponent <RectTransform>().anchoredPosition += new Vector2(seperationSpace, 0);
        }
Exemple #22
0
        private void SetProfile(BattleData battleData, GameObject profileGO, UnitWrapper unitWrapper, int standardActivityPoint)
        {
            Unit unit = unitWrapper.GetUnit();

            profileGO.GetComponent <Image>().sprite = FindUnitProfileImage(unit);

            GameObject apTextGO = profileGO.transform.Find("apText").gameObject;

            apTextGO.GetComponent <CustomUIText>().enabled = true;

            int activityPoint = GetActivityPoint(battleData, unitWrapper);

            if (activityPoint < standardActivityPoint)
            {
                activityPoint += unit.GetRegenerationAmount();
            }
            apTextGO.GetComponent <CustomUIText>().text = activityPoint.ToString();

            if (unitWrapper.IsPreviewUnit())
            {
                GameObject arrow = profileGO.transform.Find("arrow").gameObject;
                arrow.GetComponent <Image>().enabled = true;
            }
        }
Exemple #23
0
 public Unit ApiToDomain(UnitWrapper value)
 {
     return(ApiToDomain(value?.Unit));
 }
Exemple #24
0
 public virtual void Add(UnitWrapper wrapper)
 {
     _wrappedUnitList.Add(wrapper);
 }
Exemple #25
0
 public void Init(Unit unit, RectTransform dragArea, RectTransform containerOfWholeTreeView)
 {
     this.dragArea = dragArea;
     this.containerOfWholeTreeView = containerOfWholeTreeView;
     _associatedUnitWrapper        = new UnitWrapper(unit);
 }
 public void Add(TKey key, UnitWrapper wrapper)
 {
     _unitDictionary.Add(key, wrapper);
 }
 internal static Unit ToDomain(this UnitWrapper value)
 {
     return(s_unitMapper.ApiToDomain(value));
 }
Exemple #28
0
        public void SaveUnit(UnitWrapper wrapper)
        {
            string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented, _settings);

            System.IO.File.WriteAllText(Path.Combine(_unitPath, wrapper.UnitId.ToString()), json, Encoding.UTF8);
        }