Ejemplo n.º 1
0
        private async Task GenerateActorTasksAndPutInDictAsync(IEnumerable <ActorInSector> actorDataList,
                                                               CancellationToken cancelToken)
        {
            var actorDataListMaterialized = actorDataList.ToArray();

            var actorGrouppedBySector = actorDataListMaterialized.GroupBy(x => x.Sector).ToArray();

            foreach (var sectorGroup in actorGrouppedBySector)
            {
                foreach (var actorDataItem in sectorGroup)
                {
                    var actor  = actorDataItem.Actor;
                    var sector = actorDataItem.Sector;

                    var taskSource = actor.TaskSource;

                    var context = new SectorTaskSourceContext(sector, cancelToken);

                    try
                    {
                        if (taskSource is IHumanActorTaskSource <SectorTaskSourceContext> humanTaskSource)
                        {
                            // Drop intention for human controlled task source.
                            cancelToken.Register(() => { humanTaskSource.DropIntentionWaiting(); });
                        }

                        var actorTask = await taskSource.GetActorTaskAsync(actor, context).ConfigureAwait(false);

                        var state = new TaskState(actor, sector, actorTask, taskSource);

                        _taskDict.AddOrUpdate(actor, state, (a, t) => state);
                    }
                    catch (TaskCanceledException)
                    {
                        // Do nothing for his actor. His task was cancelled.
                        _taskDict.TryRemove(actor, out var _);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private async Task GenerateActorTasksAndPutInDictAsync(IEnumerable <ActorInSector> actorDataList)
        {
            var actorDataListMaterialized = actorDataList.ToArray();

            var actorGrouppedBySector = actorDataListMaterialized.GroupBy(x => x.Sector).ToArray();

            foreach (var sectorGroup in actorGrouppedBySector)
            {
                foreach (var actorDataItem in sectorGroup)
                {
                    var actor  = actorDataItem.Actor;
                    var sector = actorDataItem.Sector;

                    var taskSource = actor.TaskSource;

                    var context = new SectorTaskSourceContext(sector);

                    try
                    {
                        var actorTask = await taskSource.GetActorTaskAsync(actor, context).ConfigureAwait(false);

                        var state = new TaskState(actor, sector, actorTask, taskSource);
                        if (!_taskDict.TryAdd(actor, state))
                        {
                            // Это происходит, когда игрок пытается присвоить новую команду,
                            // когда старая еще не закончена и не может быть заменена.
                            throw new InvalidOperationException(
                                      "Попытка назначить задачу, когда старая еще не удалена.");
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // Do nothing for his actor. His task was cancelled.
                        _taskDict.TryRemove(actor, out var _);
                    }
                }
            }
        }