Ejemplo n.º 1
0
        public void SendMission(Mission mission, List <MissionSlot> slots, string author, string password, Mod modInfo)
        {
            if (mission == null)
            {
                throw new ApplicationException("Mission is null");
            }

            Account acc = null;
            var     db  = new ZkDataContext();

            if (Debugger.IsAttached)
            {
                acc = db.Accounts.SingleOrDefault(x => x.Name == "Testor303");
            }
            else
            {
                acc = AuthServiceClient.VerifyAccountPlain(author, password);
            }

            if (acc == null)
            {
                throw new ApplicationException("Cannot verify user account");
            }


            Mission prev = db.Missions.SingleOrDefault(x => x.MissionID == mission.MissionID || (x.Name == mission.Name && x.AccountID == acc.AccountID)); // previous mission by id or name + account

            if (prev == null && db.Missions.Any(x => x.Name == mission.Name))
            {
                throw new ApplicationException("Mission name must be unique");
            }
            var map = db.Resources.SingleOrDefault(x => x.InternalName == mission.Map && x.TypeID == ZkData.ResourceType.Map);

            if (map == null)
            {
                throw new ApplicationException("Map name is unknown");
            }
            var mod = db.Resources.SingleOrDefault(x => x.InternalName == mission.Mod && x.TypeID == ZkData.ResourceType.Mod);

            if (mod == null)
            {
                throw new ApplicationException("Mod name is unknown");
            }
            //if (db.Resources.Any(x => x.InternalName == mission.Name && x.MissionID != null)) throw new ApplicationException("Name already taken by other mod/map");

            modInfo.MissionMap = mission.Map;

            if (prev != null)
            {
                if (prev.AccountID != acc.AccountID && !acc.IsLobbyAdministrator && !acc.IsZeroKAdmin)
                {
                    throw new ApplicationException("Invalid author or password");
                }
                prev.Description      = mission.Description;
                prev.DescriptionStory = mission.DescriptionStory;
                prev.Mod                  = mission.Mod;
                prev.Map                  = mission.Map;
                prev.Name                 = mission.Name;
                prev.ScoringMethod        = mission.ScoringMethod;
                prev.ModRapidTag          = mission.ModRapidTag;
                prev.ModOptions           = mission.ModOptions;
                prev.Image                = mission.Image;
                prev.MissionEditorVersion = mission.MissionEditorVersion;
                prev.SpringVersion        = mission.SpringVersion;
                prev.Revision++;
                prev.Mutator           = mission.Mutator;
                prev.ForumThread.Title = mission.Name;
                mission = prev;
            }
            else
            {
                mission.CreatedTime = DateTime.UtcNow;
                mission.ForumThread = new ForumThread()
                {
                    Title = mission.Name, ForumCategory = db.ForumCategories.FirstOrDefault(x => x.IsMissions), CreatedAccountID = acc.AccountID, LastPostAccountID = acc.AccountID
                };
                mission.ForumThread.UpdateLastRead(acc.AccountID, true);
                db.Missions.InsertOnSubmit(mission);
            }
            mission.AccountID    = acc.AccountID;
            mission.Script       = Regex.Replace(mission.Script, "GameType=([^;]+);", (m) => { return(string.Format("GameType={0};", mission.NameWithVersion)); });
            mission.MinHumans    = slots.Count(x => x.IsHuman && x.IsRequired);
            mission.MaxHumans    = slots.Count(x => x.IsHuman);
            mission.ModifiedTime = DateTime.UtcNow;
            mission.IsDeleted    = true;
            mission.IsCoop       = slots.Where(x => x.IsHuman).GroupBy(x => x.AllyID).Count() == 1;

            db.SubmitChanges();

            var updater = new MissionUpdater();

            updater.UpdateMission(db, mission, modInfo);

            mission.IsDeleted = false;
            db.SubmitChanges();
        }
Ejemplo n.º 2
0
        static void Downloader_PackagesChanged(object sender, EventArgs e)
        {
            foreach (var ver in Downloader.PackageDownloader.Repositories.SelectMany(x => x.VersionsByTag.Keys))
            {
                if (ver.EndsWith(":test") || ver.EndsWith(":latest"))
                {
                    if (!Downloader.PackageDownloader.SelectedPackages.Contains(ver))
                    {
                        Trace.TraceInformation("Selecting package: {0}", ver);
                        Downloader.PackageDownloader.SelectPackage(ver);
                        Downloader.GetResource(DownloadType.MOD, ver);
                    }
                }
            }

            var waiting = false;

            do
            {
                var downs = Downloader.Downloads.ToList().Where(x => x.IsComplete == null && !x.IsAborted).ToList();
                if (downs.Any())
                {
                    waiting = true;
                    var d = downs.First();
                    Trace.TraceInformation("Waiting for: {0} - {1} {2}", d.Name, d.TotalProgress, d.TimeRemaining);
                }
                else if (Scanner.GetWorkCost() > 0)
                {
                    waiting = true;
                    Trace.TraceInformation("Waiting for scanner: {0}", Scanner.GetWorkCost());
                }
                else
                {
                    waiting = false;
                }
                if (waiting)
                {
                    Thread.Sleep(10000);
                }
            } while (waiting);



            lock (Locker)
            {
                foreach (var id in new ZkDataContext(false).Missions.Where(x => !x.IsScriptMission && x.ModRapidTag != "" && !x.IsDeleted).Select(x => x.MissionID).ToList())
                {
                    using (var db = new ZkDataContext(false))
                    {
                        var mis = db.Missions.Single(x => x.MissionID == id);
                        try
                        {
                            if (!string.IsNullOrEmpty(mis.ModRapidTag))
                            {
                                var latestMod = Downloader.PackageDownloader.GetByTag(mis.ModRapidTag);
                                if (latestMod != null && mis.Mod != latestMod.InternalName)
                                {
                                    mis.Mod = latestMod.InternalName;
                                    Trace.TraceInformation("Updating mission {0} {1} to {2}", mis.MissionID, mis.Name, mis.Mod);
                                    var mu      = new MissionUpdater();
                                    Mod modInfo = null;
                                    Scanner.MetaData.GetMod(mis.NameWithVersion, m => { modInfo = m; }, (er) => { });
                                    mis.Revision++;
                                    mu.UpdateMission(db, mis, modInfo);
                                    db.SubmitChanges();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("Failed to update mission {0}: {1}", mis.MissionID, ex);
                        }
                    }
                }
            }
        }