public MapInstanceProvider(List <NpcMonsterDto> npcMonsters, List <Map.Map> maps,
                                   IMapItemProvider mapItemProvider, IMapNpcProvider mapNpcProvider,
                                   IMapMonsterProvider mapMonsterProvider)
        {
            var mapPartitioner = Partitioner.Create(maps, EnumerablePartitionerOptions.NoBuffering);
            var mapList        = new ConcurrentDictionary <short, Map.Map>();
            var npccount       = 0;
            var monstercount   = 0;

            Parallel.ForEach(mapPartitioner, new ParallelOptions {
                MaxDegreeOfParallelism = 8
            }, map =>
            {
                var guid           = Guid.NewGuid();
                mapList[map.MapId] = map;
                var newMap         = new MapInstance(map, guid, map.ShopAllowed, MapInstanceType.BaseMapInstance, npcMonsters,
                                                     mapItemProvider, mapNpcProvider, mapMonsterProvider);
                MapInstances.TryAdd(guid, newMap);
                newMap.LoadMonsters();
                newMap.LoadNpcs();
                newMap.StartLife();
                monstercount += newMap.Monsters.Count;
                npccount     += newMap.Npcs.Count;
            });
            var mapInstancePartitioner =
                Partitioner.Create(MapInstances.Values, EnumerablePartitionerOptions.NoBuffering);

            Parallel.ForEach(mapInstancePartitioner, new ParallelOptions {
                MaxDegreeOfParallelism = 8
            }, mapInstance =>
            {
                var partitioner = Partitioner.Create(
                    DaoFactory.PortalDao.Where(s => s.SourceMapId.Equals(mapInstance.Map.MapId)),
                    EnumerablePartitionerOptions.None);
                var portalList = new ConcurrentDictionary <int, Portal>();
                Parallel.ForEach(partitioner, portalDto =>
                {
                    Portal portal = portalDto.Adapt <Portal>();
                    portal.SourceMapInstanceId      = mapInstance.MapInstanceId;
                    portal.DestinationMapInstanceId = GetBaseMapInstanceIdByMapId(portal.DestinationMapId);
                    portalList[portal.PortalId]     = portal;
                });
                mapInstance.Portals.AddRange(portalList.Select(s => s.Value));
            });
            maps.AddRange(mapList.Select(s => s.Value));
        }
Example #2
0
 public MapInstance(Map.Map map, Guid guid, bool shopAllowed, MapInstanceType type,
                    List <NpcMonsterDto> npcMonsters, IMapItemProvider mapItemProvider,
                    IMapNpcProvider mapNpcProvider, IMapMonsterProvider mapMonsterProvider)
 {
     _npcMonsters    = npcMonsters;
     XpRate          = 1;
     DropRate        = 1;
     ShopAllowed     = shopAllowed;
     MapInstanceType = type;
     Map             = map;
     MapInstanceId   = guid;
     Portals         = new List <Portal>();
     _monsters       = new ConcurrentDictionary <long, MapMonster>();
     _npcs           = new ConcurrentDictionary <long, MapNpc>();
     MapItems        = new ConcurrentDictionary <long, MapItem>();
     _isSleeping     = true;
     LastUnregister  = SystemTime.Now().AddMinutes(-1);
     ExecutionEnvironment.TryGetCurrentExecutor(out var executor);
     Sessions            = new DefaultChannelGroup(executor);
     _mapItemProvider    = mapItemProvider;
     _mapNpcProvider     = mapNpcProvider;
     _mapMonsterProvider = mapMonsterProvider;
 }