public OfflineMessages(TasClient client)
 {
     this.client              = client;
     client.UserAdded        += client_UserAdded;
     client.Said             += client_Said;
     client.LoginAccepted    += client_LoginAccepted;
     client.ChannelUserAdded += client_ChannelUserAdded;
     using (var db = new ZkDataContext())
     {
         db.ExecuteCommand("DELETE FROM LobbyMessages WHERE Created < {0}", DateTime.UtcNow.AddDays(-14));
     }
 }
        public static bool GetLinksAndTorrent(string internalName,
                                              out List <string> links,
                                              out byte[] torrent,
                                              out List <string> dependencies,
                                              out ResourceType resourceType,
                                              out string torrentFileName)
        {
            var db = new ZkDataContext();

            var resource = db.Resources.SingleOrDefault(x => x.InternalName == internalName);

            if (resource == null)
            {
                torrent         = null;
                links           = null;
                dependencies    = null;
                resourceType    = ResourceType.Map;
                torrentFileName = null;
                return(false);
            }

            dependencies = resource.ResourceDependencies.Select(x => x.NeedsInternalName).ToList();
            resourceType = resource.TypeID;

            var bestOld = resource.ResourceContentFiles.FirstOrDefault(x => x.LinkCount == resource.ResourceContentFiles.Max(y => y.LinkCount));

            if (bestOld != null && bestOld.LinkCount > 0 && (resource.MissionID != null ||
                                                             (resource.LastLinkCheck != null && DateTime.UtcNow.Subtract(resource.LastLinkCheck.Value).TotalHours < 2)))
            {
                // use cached values for missions or resources checked less than 1 day ago
                links           = PlasmaServer.GetLinkArray(bestOld);
                torrent         = PlasmaServer.GetTorrentData(bestOld);
                torrentFileName = PlasmaServer.GetTorrentFileName(bestOld);
                if (links.Count > 0)
                {
                    db.ExecuteCommand("UPDATE Resource SET DownloadCount = DownloadCount+1 WHERE ResourceID={0}", resource.ResourceID);
                }
                else
                {
                    db.ExecuteCommand("UPDATE Resource SET NoLinkDownloadCount = NoLinkDownloadCount+1 WHERE ResourceID={0}", resource.ResourceID);
                }

                return(true);
            }

            RequestData data;
            var         isNew = false;

            lock (Requests)
            {
                if (!Requests.TryGetValue(resource.ResourceID, out data))
                {
                    data  = new RequestData(resource.ResourceID);
                    isNew = true;
                    Requests.Add(resource.ResourceID, data);
                }
            }

            if (!isNew)
            {
                // request is ongoing, wait for completion
                data.WaitHandle.WaitOne();
                torrentFileName = PlasmaServer.GetTorrentFileName(data.ContentFile);
                links           = PlasmaServer.GetLinkArray(data.ContentFile);
                torrent         = PlasmaServer.GetTorrentData(data.ContentFile);
                if (links.Count > 0)
                {
                    db.ExecuteCommand("UPDATE Resource SET DownloadCount = DownloadCount+1 WHERE ResourceID={0}", resource.ResourceID);
                }
                else
                {
                    db.ExecuteCommand("UPDATE Resource SET NoLinkDownloadCount = NoLinkDownloadCount+1 WHERE ResourceID={0}", resource.ResourceID);
                }
                return(true);
            }
            else
            {
                // new request - actually perform it
                try
                {
                    if (resource.ResourceContentFiles.Count > 1) // multiple content fiels - perform updates in paralell
                    {
                        var toCheck = from x in resource.ResourceContentFiles
                                      group x by new { x.FileName, x.Length }
                        into g where !g.Key.FileName.EndsWith(".sdp") select g.First();

                        Task.WaitAll(toCheck.Select(x => Task.Factory.StartNew(() => UpdateLinks(x))).ToArray());
                    }
                    else
                    {
                        foreach (var content in resource.ResourceContentFiles)
                        {
                            UpdateLinks(content);
                        }
                    }

                    db.SubmitChanges();

                    // find best content file - the one with most links
                    var best = resource.ResourceContentFiles.FirstOrDefault(x => x.LinkCount == resource.ResourceContentFiles.Max(y => y.LinkCount));

                    if (best != null)
                    {
                        data.ContentFile = best;
                    }
                    else
                    {
                        data.ContentFile = resource.ResourceContentFiles.First();  // all content files sux, reurn any
                    }
                    links           = PlasmaServer.GetLinkArray(data.ContentFile);
                    torrent         = PlasmaServer.GetTorrentData(data.ContentFile);
                    torrentFileName = PlasmaServer.GetTorrentFileName(data.ContentFile);
                    if (links.Count > 0)
                    {
                        resource.DownloadCount++;
                    }
                    else
                    {
                        resource.NoLinkDownloadCount++;
                    }
                    db.SubmitChanges();
                    return(true);
                }
                finally
                {
                    lock (Requests) Requests.Remove(data.ResourceID);
                    data.WaitHandle.Set(); // notify other waiting Requests that its done
                }
            }
        }
Esempio n. 3
0
        public static void PurgeGalaxy(int galaxyID, bool resetclans = false, bool resetroles = false)
        {
            System.Console.WriteLine("Purging galaxy " + galaxyID);
            using (var db = new ZkDataContext())
            {
                db.CommandTimeout = 300;

                var gal = db.Galaxies.Single(x => x.GalaxyID == galaxyID);
                foreach (var p in gal.Planets)
                {
                    //p.ForumThread = null;
                    p.OwnerAccountID = null;
                    p.Faction        = null;
                    p.Account        = null;
                }
                foreach (var f in db.Factions)
                {
                    f.Metal = 0;
                    f.EnergyDemandLastTurn   = 0;
                    f.EnergyProducedLastTurn = 0;
                    f.Bombers   = 0;
                    f.Dropships = 0;
                    f.Warps     = 0;
                }
                db.SubmitChanges();

                db.ExecuteCommand("update account set pwbombersproduced=0, pwbombersused=0, pwdropshipsproduced=0, pwdropshipsused=0, pwmetalproduced=0, pwmetalused=0, pwattackpoints=0, pwwarpproduced=0, pwwarpused=0, elopw=1500");
                if (resetclans)
                {
                    db.ExecuteCommand("update account set clanid=null");
                }
                db.ExecuteCommand("delete from event");
                db.ExecuteCommand("delete from planetownerhistory");
                db.ExecuteCommand("delete from planetstructure");
                db.ExecuteCommand("delete from planetfaction");
                db.ExecuteCommand("delete from accountplanet");
                if (resetroles)
                {
                    db.ExecuteCommand("delete from accountrole where clanID is null");
                }
                db.ExecuteCommand("delete from factiontreaty");
                db.ExecuteCommand("delete from treatyeffect");

                db.ExecuteCommand("delete from forumthread where forumcategoryid={0}", db.ForumCategories.Single(x => x.IsPlanets).ForumCategoryID);

                if (resetclans)
                {
                    db.ExecuteCommand("delete from clan");
                    db.ExecuteCommand("delete from forumthread where forumcategoryid={0}", db.ForumCategories.Single(x => x.IsClans).ForumCategoryID);
                }
            }
        }