Beispiel #1
0
        public void CreateManuallyWithDateTime()
        {
            var address  = new Address(Generator.Integer(0, 10), Generator.Integer(0, 10), Generator.Integer(10, 20));
            var dateTime = DateTime.UtcNow;

            var(seconds, nanos) = Epoch.FromDate(dateTime);

            var txId = new TxId(address, dateTime);

            Assert.Equal(address, txId.Address);
            Assert.Equal(seconds, txId.ValidStartSeconds);
            Assert.Equal(nanos, txId.ValidStartNanos);
        }
Beispiel #2
0
    public void CanCreateAPendingTransactionIdMannually()
    {
        var address  = new Address(Generator.Integer(0, 10), Generator.Integer(0, 10), Generator.Integer(10, 20));
        var dateTime = DateTime.UtcNow;

        var(seconds, nanos) = Epoch.FromDate(dateTime);
        var nonce = Generator.Integer(5, 20);

        var txId = new TxId(address, dateTime, true, nonce);

        Assert.Equal(address, txId.Address);
        Assert.Equal(seconds, txId.ValidStartSeconds);
        Assert.Equal(nanos, txId.ValidStartNanos);
        Assert.True(txId.Pending);
        Assert.Equal(nonce, txId.Nonce);
    }
Beispiel #3
0
    public void CreateManuallyWithNonce()
    {
        var address  = new Address(Generator.Integer(0, 10), Generator.Integer(0, 10), Generator.Integer(10, 20));
        var dateTime = DateTime.UtcNow;

        var(seconds, nanos) = Epoch.FromDate(dateTime);
        var nonce = Generator.Integer(5, 20);

        var txId = new TxId(address, dateTime, false, nonce);

        Assert.Equal(address, txId.Address);
        Assert.Equal(seconds, txId.ValidStartSeconds);
        Assert.Equal(nanos, txId.ValidStartNanos);
        Assert.False(txId.Pending);
        Assert.Equal(nonce, txId.Nonce);
    }
Beispiel #4
0
        public async Task TickNeighborhoods(DateTime now)
        {
            var config = Context.Config.Neighborhoods;
            //process the neighbourhoods for this city
            var endDate = new DateTime(now.Year, now.Month, 1).AddMonths(1);

            if (config.Election_Week_Align)
            {
                endDate = FindLastWeek(endDate);
            }
            var timeToNextMonth = (endDate - now);
            var ds = Kernel.Get <IDataService>();

            var epochNow = Epoch.FromDate(now);

            using (var da = DAFactory.Get())
            {
                var midnight          = LotVisitUtils.Midnight(); //gets this morning's midnight (basically current date, with day reset)
                var activityBeginning = midnight - new TimeSpan(7, 0, 0, 0);

                var range      = da.LotVisits.StreamBetweenPlusNhood(Context.ShardId, activityBeginning, midnight);
                var enumerator = range.GetEnumerator();

                var nhoodHours = new Dictionary <uint, double>();

                while (enumerator.MoveNext())
                {
                    var visit = enumerator.Current;
                    var span  = LotVisitUtils.CalculateDateOverlap(activityBeginning, midnight, visit.time_created, visit.time_closed.Value);
                    if (nhoodHours.ContainsKey(visit.neighborhood_id))
                    {
                        nhoodHours[visit.neighborhood_id] += span.TotalMinutes;
                    }
                    else
                    {
                        nhoodHours.Add(visit.neighborhood_id, span.TotalMinutes);
                    }
                }

                //
                var nhoodOrder = nhoodHours.OrderByDescending(x => x.Value);

                var nhoods = da.Neighborhoods.All(Context.ShardId);

                foreach (var nhood in nhoods)
                {
                    var nhoodDS = await ds.Get <Neighborhood>((uint)nhood.neighborhood_id);

                    if (nhoodDS == null)
                    {
                        continue;
                    }
                    //placement within the top neighbourhoods for activity
                    var placement = nhoodOrder.ToList().FindIndex(x => x.Key == nhood.neighborhood_id);
                    if (placement == -1)
                    {
                        placement = nhoods.Count;
                    }

                    nhoodDS.Neighborhood_ActivityRating = (uint)placement + 1;

                    //is there an active cycle for this neighbourhood?
                    var stillActive = false;
                    if (nhood.election_cycle_id != null)
                    {
                        var cycle = da.Elections.GetCycle(nhood.election_cycle_id.Value);
                        if (cycle != null)
                        {
                            if (cycle.current_state == DbElectionCycleState.shutdown ||
                                cycle.current_state == DbElectionCycleState.failsafe)
                            {
                                long timeToEnd = (long)cycle.end_date - epochNow;
                                if (timeToEnd <= 0 && nhood.mayor_id != null)
                                {
                                    await SetMayor(da, 0, (uint)nhood.neighborhood_id);
                                }
                                stillActive = epochNow < cycle.end_date; //can't switch eligibility til we end
                            }
                            else if (cycle.current_state < DbElectionCycleState.ended)
                            {
                                var active = (epochNow >= cycle.start_date && epochNow < cycle.end_date);

                                long timeToEnd = (long)cycle.end_date - epochNow;

                                DbElectionCycleState targetState;
                                if (timeToEnd <= 0)
                                {
                                    targetState = DbElectionCycleState.ended;
                                }
                                else if (timeToEnd <= 60 * 60 * 24 * 3) //last 3 days are the full election
                                {
                                    targetState = DbElectionCycleState.election;
                                }
                                else //all other time is the nomination
                                {
                                    targetState = DbElectionCycleState.nomination;
                                }

                                if (targetState != cycle.current_state)
                                {
                                    await ChangeElectionState(da, nhood, cycle, targetState);

                                    nhoodDS.Neighborhood_ElectionCycle.ElectionCycle_CurrentState = (byte)targetState;
                                }
                                //important: if we are in failsafe mode we can't switch eligibility or start a new cycle.
                                if (cycle.current_state != DbElectionCycleState.ended)
                                {
                                    stillActive = true;
                                }
                            }
                        }
                    }

                    //do we need to start a new cycle?
                    if (!stillActive && timeToNextMonth.TotalDays < 7 && (nhood.flag & 1) == 0)
                    {
                        //update eligibility
                        if ((nhood.flag & 2) > 0)
                        {
                            //not eligibile for elections (temp)
                            //is our placement within bounds?
                            if (placement != -1 && placement < config.Mayor_Elegibility_Limit)
                            {
                                //make us eligible.
                                nhood.flag &= ~(uint)2;
                                nhoodDS.Neighborhood_Flag = nhood.flag;
                                da.Neighborhoods.UpdateFlag((uint)nhood.neighborhood_id, nhood.flag);

                                SendBulletinPost(da, nhood.neighborhood_id, "f123", (int)NeighBulletinStrings.ElectionBeginSubject, (int)NeighBulletinStrings.ElectionBegin,
                                                 0, nhood.name, config.Mayor_Elegibility_Limit.ToString());
                            }
                        }
                        else
                        {
                            //is our placement outwith bounds?
                            if (placement == -1 || placement >= config.Mayor_Elegilility_Falloff)
                            {
                                //make us ineligible.
                                nhood.flag |= 2;
                                nhoodDS.Neighborhood_Flag = nhood.flag;
                                da.Neighborhoods.UpdateFlag((uint)nhood.neighborhood_id, nhood.flag);

                                //start a shutdown cycle
                                var dbCycle = new DbElectionCycle
                                {
                                    current_state = DbElectionCycleState.shutdown,
                                    election_type = DbElectionCycleType.shutdown,
                                    start_date    = Epoch.FromDate(midnight),
                                    end_date      = Epoch.FromDate(endDate)
                                };
                                var cycleID = da.Elections.CreateCycle(dbCycle);
                                nhoodDS.Neighborhood_ElectionCycle = new ElectionCycle()
                                {
                                    ElectionCycle_CurrentState = (byte)dbCycle.current_state,
                                    ElectionCycle_ElectionType = (byte)dbCycle.election_type,
                                    ElectionCycle_StartDate    = dbCycle.start_date,
                                    ElectionCycle_EndDate      = dbCycle.end_date
                                };
                                da.Neighborhoods.UpdateCycle((uint)nhood.neighborhood_id, cycleID);

                                SendBulletinPost(da, nhood.neighborhood_id, "f123", (int)NeighBulletinStrings.ElectionCancelledSubject, (int)NeighBulletinStrings.ElectionCancelled,
                                                 0, nhood.name);
                            }
                        }

                        var eligible = (nhood.flag & 2) == 0;

                        if (eligible || nhood.election_cycle_id == null)
                        {
                            //yes
                            var dbCycle = new DbElectionCycle
                            {
                                current_state = (eligible) ? DbElectionCycleState.nomination : DbElectionCycleState.shutdown,
                                election_type = (eligible) ? DbElectionCycleType.election : DbElectionCycleType.shutdown,
                                start_date    = Epoch.FromDate(midnight),
                                end_date      = Epoch.FromDate(endDate)
                            };
                            var cycleID = da.Elections.CreateCycle(dbCycle);

                            nhoodDS.Neighborhood_ElectionCycle = new ElectionCycle()
                            {
                                ElectionCycle_CurrentState = (byte)dbCycle.current_state,
                                ElectionCycle_ElectionType = (byte)dbCycle.election_type,
                                ElectionCycle_StartDate    = dbCycle.start_date,
                                ElectionCycle_EndDate      = dbCycle.end_date
                            };
                            da.Neighborhoods.UpdateCycle((uint)nhood.neighborhood_id, cycleID);

                            if (eligible)
                            {
                                //notify current mayor
                                if (nhood.mayor_id != null)
                                {
                                    var mail = Kernel.Get <MailHandler>();
                                    mail.SendSystemEmail("f116", (int)NeighMailStrings.TermLengthSubject, (int)NeighMailStrings.TermLength,
                                                         1, MessageSpecialType.Nominate, dbCycle.end_date, nhood.mayor_id.Value, nhood.name, dbCycle.end_date.ToString());
                                }

                                //post to bulletin
                                var nomEndDate = dbCycle.end_date - 60 * 60 * 24 * 3; //nomination ends 3 days before end of cycle
                                SendBulletinPost(da, nhood.neighborhood_id, "f123", (int)NeighBulletinStrings.NominateSubject, (int)NeighBulletinStrings.Nominate, nomEndDate,
                                                 nhood.name, nomEndDate.ToString());
                            }
                        }
                    }
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Convenience constructor converting the
 /// entered <code>DateTime</code> object into
 /// the proper seconds and nanoseconds since the
 /// Epoch for use by the network.
 /// </summary>
 /// <param name="address">
 /// Address associated with
 /// and paying for this transaction.
 /// </param>
 /// <param name="dateTime">
 /// The Date & Time stamp associated with the
 /// transaction.
 /// </param>
 public TxId(Address address, DateTime dateTime)
 {
     Address = address;
     (ValidStartSeconds, ValidStartNanos) = Epoch.FromDate(dateTime);
 }
Beispiel #6
0
 internal Timestamp(DateTime dateTime) : this()
 {
     (Seconds, Nanos) = Epoch.FromDate(dateTime);
 }