Exemple #1
0
        private async Task ScheduledJob <T>(TimeSpan span, Job.JobPriority priority) where T : AJobPayload, new()
        {
            try
            {
                var jobs = await Db.Jobs.AsQueryable()
                           .Where(j => j.Payload is T)
                           .OrderByDescending(j => j.Created)
                           .Take(10)
                           .ToListAsync();

                foreach (var job in jobs)
                {
                    if (job.Started == null || job.Ended == null)
                    {
                        return;
                    }
                    if (DateTime.Now - job.Ended < span)
                    {
                        return;
                    }
                }
                await Db.Jobs.InsertOneAsync(new Job
                {
                    Priority = priority,
                    Payload  = new T()
                });
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, ex, $"Error in JobScheduler when scheduling {typeof(T).Name}");
            }
        }
Exemple #2
0
        private async Task ScheduledJob <T>(TimeSpan span, Job.JobPriority priority) where T : AJobPayload, new()
        {
            if (!Settings.RunBackEndJobs && typeof(T).ImplementsInterface(typeof(IBackEndJob)))
            {
                return;
            }
            if (!Settings.RunFrontEndJobs && typeof(T).ImplementsInterface(typeof(IFrontEndJob)))
            {
                return;
            }
            try
            {
                var jobs = (await Sql.GetUnfinishedJobs())
                           .Where(j => j.Payload is T)
                           .OrderByDescending(j => j.Created)
                           .Take(10);

                foreach (var job in jobs)
                {
                    if (job.Started == null || job.Ended == null)
                    {
                        return;
                    }
                    if (DateTime.Now - job.Ended < span)
                    {
                        return;
                    }
                }
                await Sql.EnqueueJob(new Job
                {
                    Priority = priority,
                    Payload  = new T()
                });
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, ex, $"Error in JobScheduler when scheduling {typeof(T).Name}");
            }
        }
Exemple #3
0
    public Job(Tile tile, TileType jobTileType, Action <Job> jobCompleted, float jobTime, RequestedItem[] requestedItems, Job.JobPriority jobPriority, bool jobRepeats = false, bool adjacent = false)
    {
        this.tile            = tile;
        this.JobTileType     = jobTileType;
        this.Type            = jobTileType.LocalizationCode;
        this.OnJobCompleted += jobCompleted;
        this.jobTimeRequired = this.JobTime = jobTime;
        this.jobRepeats      = jobRepeats;
        this.Priority        = jobPriority;
        this.adjacent        = adjacent;
        this.Description     = "job_error_missing_desc";

        jobWorkedLua    = new List <string>();
        jobCompletedLua = new List <string>();

        this.DeliveredItems = new Dictionary <string, Inventory>();
        this.RequestedItems = new Dictionary <string, RequestedItem>();
        if (requestedItems != null)
        {
            foreach (RequestedItem item in requestedItems)
            {
                this.RequestedItems[item.Type] = item.Clone();
            }
        }
    }
Exemple #4
0
    public Job(Tile tile, TileType jobTileType, Action <Job> jobCompleted, float jobTime, Inventory[] inventoryRequirements, Job.JobPriority jobPriority, bool jobRepeats = false, bool adjacent = false)
    {
        this.tile            = tile;
        this.JobTileType     = jobTileType;
        this.OnJobCompleted += jobCompleted;
        this.jobTimeRequired = this.JobTime = jobTime;
        this.jobRepeats      = jobRepeats;
        this.Priority        = jobPriority;
        this.adjacent        = adjacent;
        this.JobDescription  = "job_error_missing_desc";

        jobWorkedLua    = new List <string>();
        jobCompletedLua = new List <string>();

        this.inventoryRequirements = new Dictionary <string, Inventory>();
        if (inventoryRequirements != null)
        {
            foreach (Inventory inv in inventoryRequirements)
            {
                this.inventoryRequirements[inv.ObjectType] = inv.Clone();
            }
        }
    }
Exemple #5
0
    public Job(Tile tile, string jobObjectType, Action <Job> jobComplete, float jobTime, RequestedItem[] requestedItems, Job.JobPriority jobPriority, bool jobRepeats = false, bool need = false, bool critical = false, bool adjacent = false)
    {
        this.tile            = tile;
        this.JobObjectType   = jobObjectType;
        this.OnJobCompleted += jobComplete;
        this.jobTimeRequired = this.JobTime = jobTime;
        this.jobRepeats      = jobRepeats;
        this.IsNeed          = need;
        this.Critical        = critical;
        this.Priority        = jobPriority;
        this.adjacent        = adjacent;
        this.JobDescription  = "job_error_missing_desc";

        jobWorkedLua    = new List <string>();
        jobCompletedLua = new List <string>();

        this.HeldInventory  = new Dictionary <string, Inventory>();
        this.RequestedItems = new Dictionary <string, RequestedItem>();

        if (requestedItems != null)
        {
            foreach (RequestedItem item in requestedItems)
            {
                this.RequestedItems[item.Type] = item.Clone();
            }
        }

        this.HeldInventory = new Dictionary <string, Inventory>();
    }
    public Job(Tile tile, string type, Action <Job> jobComplete, float jobTime, RequestedItem[] requestedItems, Job.JobPriority jobPriority, JobCategory category, bool jobRepeats = false, bool need = false, bool critical = false, bool adjacent = false)
    {
        this.tile            = tile;
        this.Type            = type;
        this.OnJobCompleted += jobComplete;
        this.jobTimeRequired = this.JobTime = jobTime;
        this.jobRepeats      = jobRepeats;
        this.IsNeed          = need;
        this.Critical        = critical;
        this.Priority        = jobPriority;
        this.Category        = category;
        this.adjacent        = adjacent;
        this.IsActive        = true;
        this.Description     = "job_error_missing_desc";

        jobWorkedLua    = new List <string>();
        jobCompletedLua = new List <string>();

        this.DeliveredItems = new Dictionary <string, Inventory>();
        this.RequestedItems = new Dictionary <string, RequestedItem>();

        if (requestedItems != null)
        {
            foreach (RequestedItem item in requestedItems)
            {
                this.RequestedItems[item.Type] = item.Clone();
            }
        }

        if (this.Category == null)
        {
            UnityDebugger.Debugger.LogError("Invalid category detected.");
        }
    }
 public Job(Tile tile, string type, Action <Job> jobComplete, float jobTime, RequestedItem[] requestedItems, Job.JobPriority jobPriority, string category, bool jobRepeats = false, bool need = false, bool critical = false, bool adjacent = false) :
     this(tile, type, jobComplete, jobTime, requestedItems, jobPriority, PrototypeManager.JobCategory.Get(category), jobRepeats, need, critical, adjacent)
 {
     // This is identical to the next structure, except the category is a string. Intended primarily for Lua
 }
Exemple #8
0
    public Job(Tile tile, TileType jobTileType, Action <Job> jobCompleted, float jobTime, RequestedItem[] requestedItems, Job.JobPriority jobPriority, bool jobRepeats = false, bool adjacent = false)
    {
        this.tile            = tile;
        this.JobTileType     = jobTileType;
        this.JobObjectType   = "tile_" + jobTileType.Name;
        this.OnJobCompleted += jobCompleted;
        this.jobTimeRequired = this.JobTime = jobTime;
        this.jobRepeats      = jobRepeats;
        this.Priority        = jobPriority;
        this.adjacent        = adjacent;
        this.JobDescription  = "Job Error: Missing Job!";

        jobWorkedLua    = new List <string>();
        jobCompletedLua = new List <string>();

        this.HeldInventory  = new Dictionary <string, Inventory>();
        this.RequestedItems = new Dictionary <string, RequestedItem>();
        if (requestedItems != null)
        {
            foreach (RequestedItem item in requestedItems)
            {
                this.RequestedItems[item.Type] = item.Clone();
            }
        }

        this.HeldInventory = new Dictionary <string, Inventory>();
    }
    /// <summary>
    /// Search for a job that can be performed by the specified character. Tests that the job can be reached and there is enough inventory to complete it, somewhere.
    /// </summary>
    public Job GetJob(Character character)
    {
        UnityDebugger.Debugger.LogFormat("JobManager", "{0},{1} GetJob() (Queue size: {2})", character.GetName(), character.ID, jobQueue.Count);
        if (jobQueue.Count == 0)
        {
            return(null);
        }

        foreach (CharacterJobPriority charPriority in characterPriorityLevels)
        {
            List <JobCategory> jobTypes = character.CategoriesOfPriority(charPriority);
            foreach (JobCategory category in categories)
            {
                if (jobTypes.Contains(category) == false)
                {
                    continue;
                }

                UnityDebugger.Debugger.LogFormat("JobManager", "{0} Looking for job of category {1} - {2} options available", character.Name, category.Type, jobQueue[category].Count);

                Job.JobPriority bestJobPriority = Job.JobPriority.Low;

                foreach (Job job in jobQueue[category])
                {
                    if (job.IsActive == false || job.IsBeingWorked == true)
                    {
                        continue;
                    }

                    // Lower numbers indicate higher priority.
                    if (bestJobPriority > job.Priority)
                    {
                        bestJobPriority = job.Priority;
                    }
                }

                Job   bestJob         = null;
                float bestJobPathtime = int.MaxValue;

                foreach (Job job in jobQueue[category])
                {
                    if (job.IsActive == false || job.IsBeingWorked == true || job.Priority != bestJobPriority)
                    {
                        continue;
                    }

                    if (CanJobRun(job))
                    {
                        float pathtime = Pathfinder.FindMinPathTime(character.CurrTile, job.tile, job.adjacent, bestJobPathtime);
                        if (pathtime < bestJobPathtime)
                        {
                            bestJob         = job;
                            bestJobPathtime = pathtime;
                        }
                    }
                }

                if (bestJob != null)
                {
                    UnityDebugger.Debugger.LogFormat("JobManager", "{0} Job Assigned {1} at {2}", character.ID, bestJob, bestJob.tile);
                    if (JobModified != null)
                    {
                        JobModified(bestJob);
                    }

                    return(bestJob);
                }
            }
        }

        return(null);
    }