Example #1
0
        /// <summary>
        /// Creating a legal unit with a local unit and an enterprise
        /// </summary>
        /// <param name="legalUnit"></param>
        /// <returns></returns>
        public async Task CreateLegalWithEnterpriseAndLocal(LegalUnit legalUnit)
        {
            LegalUnit      createdLegal;
            EnterpriseUnit createdEnterprise = null;
            LocalUnit      createdLocal      = null;

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    createdLegal = await CreateStatUnitAsync(legalUnit);

                    if (legalUnit.EnterpriseUnitRegId == null || legalUnit.EnterpriseUnitRegId == 0)
                    {
                        var sameStatIdEnterprise =
                            _dbContext.EnterpriseUnits.FirstOrDefault(eu => eu.StatId == legalUnit.StatId);

                        if (sameStatIdEnterprise != null)
                        {
                            await LinkEnterpriseToLegalAsync(sameStatIdEnterprise, createdLegal);
                        }
                        else
                        {
                            createdEnterprise = await CreateEnterpriseForLegalAsync(createdLegal);
                        }
                    }

                    var addressIds    = legalUnit.LocalUnits.Where(x => x.AddressId != null).Select(x => x.AddressId).ToList();
                    var addresses     = _dbContext.Address.Where(x => addressIds.Contains(x.Id)).ToList();
                    var sameAddresses = addresses.Where(x =>
                                                        x.RegionId == legalUnit.Address.RegionId &&
                                                        x.AddressPart1 == legalUnit.Address.AddressPart1 &&
                                                        x.AddressPart2 == legalUnit.Address.AddressPart2 &&
                                                        x.AddressPart3 == legalUnit.Address.AddressPart3 &&
                                                        x.Latitude == legalUnit.Address.Latitude &&
                                                        x.Longitude == legalUnit.Address.Longitude).ToList();
                    if (!sameAddresses.Any())
                    {
                        createdLocal = await CreateLocalForLegalAsync(createdLegal);
                    }

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    throw new BadRequestException(nameof(Resource.SaveError), e);
                }
            }

            await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLegal));

            if (createdLocal != null)
            {
                await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLocal));
            }
            if (createdEnterprise != null)
            {
                await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdEnterprise));
            }
        }
Example #2
0
        private void ClearingOrders(LocalUnits lh)
        {
            for (int index = 0; index < N.Count; index++)
            {
                for (int o = 0; o < lh.N.Count; o++)
                {
                    LocalUnit h = lh.N[o];
                    if (h.CurrentOrder is BuildOrder && Main.iss.n[(h.CurrentOrder as BuildOrder).blockID].count < 1)
                    {
                        h.CurrentOrder.complete = true;
                        h.CurrentOrder          = new NothingOrder();
                    }

                    if (h.CurrentOrder is MoveOrder && IsNear(h.CurrentOrder.dest, h.Pos))
                    {
                        h.CurrentOrder.complete = true;
                        h.CurrentOrder          = new NothingOrder();
                    }
                }

                if (N[index].complete)
                {
                    N.RemoveAt(index);
                }
            }
        }
Example #3
0
        private void ParseIfNotMappedPropIsIgnored()
        {
            const string expected = "some name";
            var unit = new LocalUnit {Name = expected};
            var mapping = new Dictionary<string, string[]>();
            var raw = new Dictionary<string, object> {["emptyNotes"] = nameof(unit.Notes)};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.Name);
        }
Example #4
0
        private void ParseNullableIntProp()
        {
            var unit = new LocalUnit {AddressId = 100500};
            const string sourceProp = "address_id";
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.AddressId)}};
            int? expected = null;
            var raw = new Dictionary<string, object> {[sourceProp] = string.Empty};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.AddressId);
        }
Example #5
0
        private void ParseBoolProp()
        {
            var unit = new LocalUnit {FreeEconZone = false};
            const string sourceProp = "isFreeEconZone";
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.FreeEconZone)}};
            const bool expected = true;
            var raw = new Dictionary<string, object> {[sourceProp] = expected.ToString()};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.FreeEconZone);
        }
Example #6
0
        private void ParseStringProp()
        {
            var unit = new LocalUnit {Name = "ku"};
            const string sourceProp = "name";
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.Name)}};
            const string expected = "qwerty";
            var raw = new Dictionary<string, object> {[sourceProp] = expected};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.Name);
        }
Example #7
0
        private void ParseComplexFieldShouldPassForLegalForm()
        {
            const string expected = "some", sourceProp = "legalForm";
            var propPath = $"{nameof(StatisticalUnit.LegalForm)}.{nameof(LegalForm.Code)}";
            var unit = new LocalUnit();
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {propPath}};
            var raw = new Dictionary<string, object> {[sourceProp] = expected};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.NotNull(unit.LegalForm);
            Assert.Equal(expected, unit.LegalForm.Code);
        }
Example #8
0
        /// <summary>
        /// Creation of a local unit together with a legal unit, if there is none
        /// </summary>
        /// <param name="localUnit"></param>
        /// <returns></returns>
        public async Task CreateLocalUnit(LocalUnit localUnit)
        {
            try
            {
                _dbContext.LocalUnits.Add(localUnit);
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new BadRequestException(nameof(Resource.SaveError), e);
            }

            await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(localUnit));
        }
Example #9
0
        private async Task CheckIfUnitExistsOnAbsentUnit()
        {
            var unit = new LocalUnit {
                StatId = "2"
            };
            bool exists;

            using (var ctx = CreateDbContext())
            {
                ctx.LocalUnits.Add(unit);
                await ctx.SaveChangesAsync();

                exists = await new QueueService(ctx).CheckIfUnitExists(StatUnitTypes.LocalUnit, "42");
            }

            Assert.False(exists);
        }
Example #10
0
 public void Update(GameTime gt)
 {
     for (int i = 0; i < N.Count; i++)
     {
         LocalUnit lh = N[i];
         lh.PrePos = lh.Pos;
         lh.MoveUnit(gt);
         if (lh.Pos == lh.PrePos)
         {
             lh.IddleTime += gt.ElapsedGameTime;
         }
         else
         {
             lh.IddleTime = TimeSpan.Zero;
         }
     }
 }
Example #11
0
        private void ParseMultipleFieldsFromOneSourceAttribute()
        {
            const string source = "source", expectedName = "name";
            var unit = new LocalUnit();
            var mapping = new Dictionary<string, string[]>
            {
                [source] = new[] {nameof(unit.Name), nameof(unit.ShortName)},
            };
            var raw = new Dictionary<string, object>
            {
                [source] = expectedName,
            };

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expectedName, unit.Name);
            Assert.Equal(expectedName, unit.ShortName);
        }
Example #12
0
        private async Task <LocalUnit> CreateLocalForLegalAsync(LegalUnit legalUnit)
        {
            var localUnit = new LocalUnit
            {
                AddressId       = legalUnit.AddressId,
                ActualAddressId = legalUnit.ActualAddressId,
                LegalUnitId     = legalUnit.RegId
            };

            Mapper.Map(legalUnit, localUnit);
            _dbContext.LocalUnits.Add(localUnit);
            await _dbContext.SaveChangesAsync();

            legalUnit.HistoryLocalUnitIds = localUnit.RegId.ToString();
            _dbContext.LegalUnits.Update(legalUnit);
            await _dbContext.SaveChangesAsync();

            CreateActivitiesAndPersonsAndForeignParticipations(legalUnit.Activities, legalUnit.PersonsUnits, legalUnit.ForeignParticipationCountriesUnits, localUnit.RegId);
            await _dbContext.SaveChangesAsync();

            return(localUnit);
        }
Example #13
0
        private void MakingOrders(GameTime gt, LocalUnits lh, SectorMap smap)
        {
            for (int o = 0; o < lh.N.Count; o++)
            {
                LocalUnit h = lh.N[o];
                if (h.CurrentOrder is DestroyOrder && IsNear(h.Pos, h.CurrentOrder.dest))
                {
                    smap.At(h.CurrentOrder.dest.X, h.CurrentOrder.dest.Y, h.CurrentOrder.dest.Z).Health -=
                        (float)(10 * gt.TotalGameTime.TotalSeconds);
                    if (
                        smap.At(h.CurrentOrder.dest.X, h.CurrentOrder.dest.Y, h.CurrentOrder.dest.Z).Health <= 0)
                    {
                        Main.smap.KillBlock((int)h.CurrentOrder.dest.X, (int)h.CurrentOrder.dest.Y,
                                            (int)h.CurrentOrder.dest.Z);
                        h.CurrentOrder.complete = true;
                        h.CurrentOrder          = new NothingOrder();
                    }
                }

                if (h.CurrentOrder is CollectOrder && IsNear(h.Pos, h.CurrentOrder.dest))
                {
                    //var temp = Main.localitems.GetNearItem(h.pos);
                    if ((h.CurrentOrder as CollectOrder).tocollect.id != 0 && h.Carry.id == 0)
                    {
                        //if (Main.localitems.n.Contains((h.current_order as CollectOrder).tocollect))
                        //{
                        h.Carry.id    = (h.CurrentOrder as CollectOrder).tocollect.id;
                        h.Carry.count = (h.CurrentOrder as CollectOrder).tocollect.count;

                        Main.localitems.n.Remove((h.CurrentOrder as CollectOrder).tocollect);

                        h.CurrentOrder.complete = true;

                        if (Main.globalstorage.n.Count > 0)
                        {
                            Vector3 st = Main.globalstorage.GetFreeStorage();
                            if (st != new Vector3(-1))
                            {
                                h.CurrentOrder = new ToStoreOrder {
                                    dest = st
                                };
                                Main.AddToLog("ToStoreOrder patch find for " + h.Pos + " " + h.Patch.Count);
                                h.Patch = Main.smap.FindPatch(h.Pos, st);
                                (h.CurrentOrder as ToStoreOrder).storagepos = st;
                            }
                        }
                        //}
                    }
                }

                //if (h.current_order is ToStoreOrder && IsNear(h.pos, h.current_order.dest))
                //{
                //    if (h.carry.id != 0)
                //    {
                //        Vector3 st = (h.current_order as ToStoreOrder).storagepos;
                //        LocalItems tempstor = Main.mmap.n[(int)st.X, (int)st.Y, (int)st.Z].tags["storage"] as LocalItems;
                //        if (tempstor.n.Count < tempstor.carp)
                //        {
                //            tempstor.n.Add(new LocalItem() { count = h.carry.count, id = h.carry.id });
                //            h.carry = new LocalItem();
                //            h.current_order = new NothingOrder();
                //        }
                //        else
                //        {
                //            Main.localitems.n.Add(new LocalItem() { count = h.carry.count, id = h.carry.id });
                //            h.carry = new LocalItem();
                //            h.current_order = new NothingOrder();
                //        }
                //    }
                //}

                if (h.CurrentOrder is BuildOrder && IsNear(h.Pos, h.CurrentOrder.dest))
                {
                    bool allow = true;
                    foreach (LocalUnit he in lh.N)
                    {
                        if ((int)he.Pos.X == (int)h.CurrentOrder.dest.X &&
                            (int)he.Pos.Z == (int)h.CurrentOrder.dest.Z &&
                            (int)he.Pos.Z == (int)h.CurrentOrder.dest.Z)
                        {
                            allow = false;
                        }
                    }
                    if (Main.smap.At(h.CurrentOrder.dest).BlockID == 0 && allow &&
                        Main.iss.n[(h.CurrentOrder as BuildOrder).blockID].count >= 1)
                    {
                        Main.smap.SetBlock(h.CurrentOrder.dest, (h.CurrentOrder as BuildOrder).blockID);
                        Main.iss.n[(h.CurrentOrder as BuildOrder).blockID].count--;
                        h.CurrentOrder.complete = true;
                        h.CurrentOrder          = new NothingOrder();
                    }
                }

                if (h.IddleTime >= TimeSpan.FromSeconds(10))
                {
                    h.CurrentOrder.taken = false;
                    h.CurrentOrder.sleep = true;
                    h.CurrentOrder       = new NothingOrder();
                }
            }
        }