Пример #1
0
 public static string GetTorrentPath(string name, string md5)
 {
     return(Global.MapPath(String.Format("~/Resources/{0}", (object)GetTorrentFileName(name, md5))));
 }
Пример #2
0
        static void StoreMetadata(string md5,
                                  Resource resource,
                                  byte[] serializedData,
                                  byte[] torrentData,
                                  byte[] minimap,
                                  byte[] metalMap,
                                  byte[] heightMap)
        {
            var file = String.Format("{0}/{1}", Global.MapPath("~/Resources"), resource.InternalName.EscapePath());

            resource.LastChange = DateTime.UtcNow;

            if (minimap != null)
            {
                File.WriteAllBytes(String.Format("{0}.minimap.jpg", file), minimap);
            }
            if (metalMap != null)
            {
                File.WriteAllBytes(String.Format("{0}.metalmap.jpg", file), metalMap);
            }
            if (heightMap != null)
            {
                File.WriteAllBytes(String.Format("{0}.heightmap.jpg", file), heightMap);
            }
            if (torrentData != null)
            {
                File.WriteAllBytes(GetTorrentPath(resource.InternalName, md5), torrentData);
            }
            if (serializedData != null)
            {
                File.WriteAllBytes(String.Format("{0}.metadata.xml.gz", file), serializedData);
                if (minimap != null)
                {
                    var map = (Map) new XmlSerializer(typeof(Map)).Deserialize(new MemoryStream(serializedData.Decompress()));

                    if (string.IsNullOrEmpty(resource.AuthorName))
                    {
                        if (!string.IsNullOrEmpty(map.Author))
                        {
                            resource.AuthorName = map.Author;
                        }
                        else
                        {
                            var m = Regex.Match(map.Description, "by ([\\w]+)", RegexOptions.IgnoreCase);
                            if (m.Success)
                            {
                                resource.AuthorName = m.Groups[1].Value;
                            }
                        }
                    }

                    if (resource.MapIsSpecial == null)
                    {
                        resource.MapIsSpecial = map.ExtractorRadius > 120 || map.MaxWind > 40;
                    }
                    resource.MapSizeSquared = (map.Size.Width / 512) * (map.Size.Height / 512);
                    resource.MapSizeRatio   = (float)map.Size.Width / map.Size.Height;
                    resource.MapWidth       = map.Size.Width / 512;
                    resource.MapHeight      = map.Size.Height / 512;

                    using (var im = Image.FromStream(new MemoryStream(minimap)))
                    {
                        int w, h;
                        if (resource.MapSizeRatio > 1)
                        {
                            w = ThumbnailSize;
                            h = (int)(w / resource.MapSizeRatio);
                        }
                        else
                        {
                            h = ThumbnailSize;
                            w = (int)(h * resource.MapSizeRatio);
                        }

                        using (var correctMinimap = new Bitmap(w, h, PixelFormat.Format24bppRgb))
                        {
                            using (var graphics = Graphics.FromImage(correctMinimap))
                            {
                                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics.DrawImage(im, 0, 0, w, h);
                            }

                            var jgpEncoder    = ImageCodecInfo.GetImageEncoders().First(x => x.FormatID == ImageFormat.Jpeg.Guid);
                            var encoderParams = new EncoderParameters(1);
                            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

                            var target = String.Format("{0}/{1}.thumbnail.jpg",
                                                       Global.MapPath("~/Resources"),
                                                       resource.InternalName.EscapePath());
                            correctMinimap.Save(target, jgpEncoder, encoderParams);
                        }
                    }
                }
            }
        }
Пример #3
0
        public static void ProgressCampaign(int accountID, int missionID, string missionVars = "")
        {
            ZkDataContext  db     = new ZkDataContext();
            CampaignPlanet planet = db.CampaignPlanets.FirstOrDefault(p => p.MissionID == missionID);

            if (planet != null)
            {
                Account acc = db.Accounts.First(x => x.AccountID == accountID);

                bool     alreadyCompleted = false;
                int      campID           = planet.CampaignID;
                Campaign camp             = planet.Campaign;
                List <CampaignPlanet>  unlockedPlanets  = new List <CampaignPlanet>();
                List <CampaignJournal> unlockedJournals = new List <CampaignJournal>();

                // start with processing the mission vars, if there are any
                byte[] missionVarsAsByteArray = System.Convert.FromBase64String(missionVars);
                string missionVarsDecoded     = System.Text.Encoding.UTF8.GetString(missionVarsAsByteArray);
                foreach (string kvpRaw in missionVarsDecoded.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string   kvpRaw2 = kvpRaw.Trim();
                    string   key = "", value = "";
                    string[] kvpSplit = kvpRaw2.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (kvpSplit.Length == 2)
                    {
                        key   = kvpSplit[0].Trim();
                        value = kvpSplit[1].Trim();
                    }
                    else
                    {
                        throw new Exception("Invalid key-value pair in decoded mission vars: " + missionVarsDecoded);
                    }
                    if (!(string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)))
                    {
                        CampaignVar        cv  = camp.CampaignVars.First(x => x.KeyString == key);
                        AccountCampaignVar acv = acc.AccountCampaignVars.FirstOrDefault(x => x.CampaignID == campID && x.VarID == cv.VarID);
                        if (acv == null)
                        {
                            db.AccountCampaignVars.InsertOnSubmit(new AccountCampaignVar()
                            {
                                AccountID = accountID, CampaignID = campID, VarID = cv.VarID, Value = value
                            });
                        }
                        else
                        {
                            acv.Value = value;
                        }
                    }
                }

                //reload DB - this allows the vars submitted this session to be used by the following code
                db.SubmitChanges();
                db     = new ZkDataContext();
                acc    = db.Accounts.First(x => x.AccountID == accountID);
                camp   = planet.Campaign;
                planet = db.CampaignPlanets.FirstOrDefault(p => p.MissionID == missionID);


                // now we unlock planets and journal entries
                // first mark this planet as completed - but only if it's already unlocked
                AccountCampaignProgress progress = acc.AccountCampaignProgress.FirstOrDefault(x => x.PlanetID == planet.PlanetID && x.CampaignID == planet.CampaignID);
                if (progress != null)
                {
                    alreadyCompleted = progress.IsCompleted;
                }
                else if (planet.StartsUnlocked)
                {
                    progress = new AccountCampaignProgress()
                    {
                        AccountID = accountID, CampaignID = campID, PlanetID = planet.PlanetID, IsCompleted = false, IsUnlocked = true
                    };
                    db.AccountCampaignProgress.InsertOnSubmit(progress);
                }

                if (progress != null && planet.IsUnlocked(accountID))
                {
                    progress.IsCompleted = true;


                    // unlock planets made available by completing this one
                    var links = camp.CampaignLinks.Where(x => x.UnlockingPlanetID == planet.PlanetID);
                    foreach (CampaignLink link in links)
                    {
                        CampaignPlanet toUnlock     = link.PlanetToUnlock;
                        bool           proceed      = true;
                        var            requiredVars = toUnlock.CampaignPlanetVars;
                        if (requiredVars.Count() == 0)
                        {
                            proceed = true;
                        }
                        else
                        {
                            foreach (CampaignPlanetVar variable in requiredVars)
                            {
                                AccountCampaignVar accountVar = acc.AccountCampaignVars.FirstOrDefault(x => x.CampaignID == campID && x.VarID == variable.RequiredVarID);
                                if (!(accountVar != null && accountVar.Value == variable.RequiredValue))
                                {
                                    proceed = false;
                                    break;  // failed to meet var requirement, stop here
                                }
                            }
                        }

                        if (proceed)    // met requirements for unlocking planet
                        {
                            AccountCampaignProgress progress2 = toUnlock.AccountCampaignProgress.FirstOrDefault(x => x.CampaignID == campID && x.AccountID == accountID);
                            if (progress2 == null)
                            {
                                progress2 = new AccountCampaignProgress()
                                {
                                    AccountID = accountID, CampaignID = campID, PlanetID = toUnlock.PlanetID, IsCompleted = false, IsUnlocked = true
                                };
                                db.AccountCampaignProgress.InsertOnSubmit(progress2);
                                unlockedPlanets.Add(toUnlock);
                            }
                            else if (!progress2.IsUnlocked)
                            {
                                progress2.IsUnlocked = true;
                                unlockedPlanets.Add(toUnlock);
                            }
                        }
                    }
                }
                // unlock journals
                var journalsWithVars = db.CampaignJournals.Where(x => x.CampaignID == campID && x.CampaignJournalVars.Any());
                foreach (CampaignJournal journal in journalsWithVars)
                {
                    bool proceed = true;

                    var requiredVars = journal.CampaignJournalVars.Where(x => x.CampaignID == campID).ToList();
                    foreach (CampaignJournalVar variable in requiredVars)
                    {
                        AccountCampaignVar accountVar = acc.AccountCampaignVars.FirstOrDefault(x => x.CampaignID == campID && x.VarID == variable.RequiredVarID);
                        if (!(accountVar != null && accountVar.Value == variable.RequiredValue))
                        {
                            proceed = false;
                            break;  // failed to meet var requirement, stop here
                        }
                    }

                    if (proceed)    // met requirements for unlocking journal
                    {
                        System.Console.WriteLine(journal.Title);
                        AccountCampaignJournalProgress jp = journal.AccountCampaignJournalProgress.FirstOrDefault(x => x.AccountID == accountID);
                        if (jp == null)
                        {
                            jp = new AccountCampaignJournalProgress()
                            {
                                AccountID = accountID, CampaignID = campID, JournalID = journal.JournalID, IsUnlocked = true
                            };
                            db.AccountCampaignJournalProgress.InsertOnSubmit(jp);
                            unlockedJournals.Add(journal);
                        }
                        else if (!jp.IsUnlocked)
                        {
                            jp.IsUnlocked = true;
                            unlockedJournals.Add(journal);
                        }
                    }
                }

                if (!alreadyCompleted)
                {
                    db.CampaignEvents.InsertOnSubmit(Global.CreateCampaignEvent(accountID, campID, "Planet completed: {0}", planet));
                    foreach (CampaignJournal journal in db.CampaignJournals.Where(x => x.CampaignID == campID && x.PlanetID == planet.PlanetID && x.UnlockOnPlanetCompletion))
                    {
                        unlockedJournals.Add(journal);
                    }
                }
                foreach (CampaignPlanet unlocked in unlockedPlanets)
                {
                    db.CampaignEvents.InsertOnSubmit(Global.CreateCampaignEvent(accountID, campID, "Planet unlocked: {0}", unlocked));
                    foreach (CampaignJournal journal in db.CampaignJournals.Where(x => x.CampaignID == campID && x.PlanetID == unlocked.PlanetID && x.UnlockOnPlanetUnlock))
                    {
                        unlockedJournals.Add(journal);
                    }
                }
                foreach (CampaignJournal uj in unlockedJournals)
                {
                    db.CampaignEvents.InsertOnSubmit(Global.CreateCampaignEvent(accountID, campID, "{1} - Journal entry unlocked: {0}", uj, uj.Planet));
                }
            }
            db.SubmitChanges();
        }