Example #1
0
        public long GenerateUnitId(int zone)
        {
            if (zone > MaxZone)
            {
                throw new Exception($"zone > MaxZone: {zone}");
            }
            uint time = TimeSince2020();

            if (time > this.lastUnitIdTime)
            {
                this.lastUnitIdTime = time;
                this.unitIdValue    = 0;
            }
            else
            {
                ++this.unitIdValue;

                if (this.unitIdValue > ushort.MaxValue - 1)
                {
                    this.unitIdValue = 0;
                    ++this.lastUnitIdTime; // 借用下一秒
                    Log.Error($"unitid count per sec overflow: {time} {this.lastUnitIdTime}");
                }
            }

            UnitIdStruct unitIdStruct = new UnitIdStruct(zone, GlobalDefine.Options.Process, this.lastUnitIdTime, this.unitIdValue);

            return(unitIdStruct.ToLong());
        }
Example #2
0
        public long GenerateUnitId(int zone)
        {
            if (zone > MaxZone)
            {
                throw new Exception($"zone > MaxZone: {zone}");
            }
            uint time = TimeSince2020();


            if (time == this.lastUnitIdTime)
            {
                ++this.unitIdThisSecCount;
            }
            else
            {
                this.lastUnitIdTime     = time;
                this.unitIdThisSecCount = 1;
            }
            if (this.unitIdThisSecCount == ushort.MaxValue)
            {
                Log.Error($"unitid count per sec overflow: {this.unitIdThisSecCount}");
            }

            if (++this.unitIdValue > ushort.MaxValue - 1)
            {
                this.unitIdValue = 0;
            }

#if SERVER
            UnitIdStruct unitIdStruct = new UnitIdStruct(zone, Game.Options.Process, time, this.unitIdValue);
#else
            UnitIdStruct unitIdStruct = new UnitIdStruct(zone, 0, time, this.unitIdValue);
#endif
            return(unitIdStruct.ToLong());
        }