Ejemplo n.º 1
0
        /// Please read this links before reading code:
        /// http://www.jdconley.com/blog/archive/2007/11/28/linq-to-sql-surprise-performance-hit.aspx
        /// http://blog.linqexchange.com/index.php/how-to-use-compiled-queries-in-linq-to-sql-for-high-demand-asp-net-websites/

        /// <summary>
        /// Main search function
        /// </summary>
        /// <param name="usi">Insert UserSearchInfo object</param>
        /// <param name="pagelimit">Insert resultlimit</param>
        /// <param name="pageindex">Insert index</param>
        /// <param name="langId">Insert langid for what langauge you want the result in</param>
        /// <param name="fromLastSeconds"></param>
        /// <returns>return SearchObject object</returns>
        private List <SearchObject> SearchMatchs(UserSearchInfo usi,
                                                 int pagelimit,
                                                 int pageindex,
                                                 int langId,
                                                 int fromLastSeconds)
        {
            Searchwar_netEntities db = new Searchwar_netEntities();

            List <Guid> lastIds = new List <Guid>();

            if (_context.Session["searchresult"] != null)
            {
                lastIds = (List <Guid>)_context.Session["searchresult"];
            }

            SearchColumns parameters = new SearchColumns
            {
                Usi             = usi,
                LangId          = langId,
                PageIndex       = pageindex,
                PageLimit       = pagelimit,
                FromLastSeconds = fromLastSeconds,
                DateTimeNow     = TimeZoneManager.DateTimeNow,
                LastIds         = lastIds
            };

            List <SearchObject> matchsFound = SearchResults(db, parameters).Skip(parameters.PageIndex == 0 ? (parameters.PageIndex * parameters.PageLimit) : 0).Take(
                parameters.PageLimit).ToList <SearchObject>();

            return(matchsFound);
        }
Ejemplo n.º 2
0
        // updating user activity
        private SearchNoticeMessage UpdateUserActivity(UserSearchInfo usi,
                                                       int userActivityMinSeconds,
                                                       HttpContext context,
                                                       SearchNoticeMessage noticemessage)
        {
            DateTime DateTimeNow = TimeZoneManager.DateTimeNow;

            Guid?searchId = ANOProfile.GetCookieValues(usi.UserIpAddress, context).SearchMatchID;

            SW_SearchWar getSearch =
                db.SW_SearchWar.SingleOrDefault <SW_SearchWar>(
                    s =>
                    s.SearchWarUserID == usi.UserID &&
                    (s.SearchWarId == searchId ||
                     s.SearchWarId == usi.SearchMatchID));

            if (getSearch != null)
            {
                if (getSearch.SearchWarEditDate.AddSeconds(userActivityMinSeconds) > DateTimeNow)
                {
                    getSearch.SearchWarEditDate = DateTimeNow.AddSeconds(5);
                    db.SaveChanges();

                    return(noticemessage);
                }
            }

            return(SearchNoticeMessage.NoConnection);
        }
Ejemplo n.º 3
0
        private InsertusiResult CreateUserSearch(UserSearchInfo usi)
        {
            TimeZoneManager mngInfo     = new TimeZoneManager(usi.UserIpAddress);
            DateTime        nowDatetime = TimeZoneManager.DateTimeNow;


            List <SW_SearchWar> getUserMatchs = (from s in db.SW_SearchWar
                                                 where s.SearchWarUserID == usi.UserID &&
                                                 s.SearchWarIsRunning == true
                                                 select s).ToList();

            foreach (SW_SearchWar s in getUserMatchs)
            {
                s.SearchWarIsRunning = false;
            }

            SW_SearchWar createusi = new SW_SearchWar
            {
                SearchWarId                = Guid.NewGuid(),
                SearchWarClanName          = usi.ClanName,
                SearchWarClanContinentId   = usi.ClanContinentID,
                SearchWarClanCountryId     = usi.ClanCountryID,
                SearchWarClanSkillId       = usi.SearchSkillID,
                SearchWarSearchGameId      = usi.SearchGameID,
                SearchWarSearchGameTypeId  = usi.SearchGameModeID,
                SearchWarSearchContinentId = usi.SearchContinentID,
                SearchWarSearchCountryId   = usi.SearchCountryID,
                SearchWarSearchMap         = usi.SearchMap,
                SearchWarIsRunning         = true,
                SearchWarSearchSkillId     = usi.SearchSkillID,
                SearchWarSearchvsX         = usi.SearchvsX,
                SearchWarSearchXvs         = usi.SearchXvs,
                SearchWarSearchMatchStart  = usi.SearchMatchStart,
                SearchWarUserIpAddress     = usi.UserIpAddress,
                SearchWarUserID            = usi.UserID,
                SearchWarAddedDate         = nowDatetime,
                SearchWarEditDate          = nowDatetime
            };

            db.SW_SearchWar.AddObject(createusi);
            db.SaveChanges();

            usi.SearchMatchID       = createusi.SearchWarId;
            _context.Session["usi"] = usi;

            return(InsertusiResult.Done);
        }
Ejemplo n.º 4
0
        public Boolean GetUserMatchInfo(UserSearchInfo usi)
        {
            SW_SearchWar getSearch = null;

            if (usi.SearchMatchID.HasValue && usi.UserID != null)
            {
                GetUserMatchInfoColumnNoPosition parameters = new GetUserMatchInfoColumnNoPosition
                {
                    SearchWarId     = usi.SearchMatchID.Value,
                    SearchWarUserId = usi.UserID
                };

                getSearch = GetUserSearchInfoFast(db, parameters);
            }

            if (getSearch != null)
            {
                usi.SearchGameID     = getSearch.SearchWarSearchGameId;
                usi.SearchMatchStart = getSearch.SearchWarSearchMatchStart;
                usi.ClanSkillID      = getSearch.SearchWarClanSkillId;
                usi.ClanName         = getSearch.SearchWarClanName;
                if (getSearch.SearchWarClanSkillId.HasValue)
                {
                    usi.ClanCountryID = getSearch.SearchWarClanCountryId.Value;
                }
                usi.ClanContinentID = getSearch.SearchWarClanContinentId.Value;
                if (getSearch.SearchWarSearchContinentId.HasValue)
                {
                    usi.SearchContinentID = getSearch.SearchWarSearchContinentId.Value;
                }
                usi.SearchCountryID  = getSearch.SearchWarSearchCountryId;
                usi.SearchGameModeID = getSearch.SearchWarSearchGameTypeId;
                usi.SearchMap        = getSearch.SearchWarSearchMap;
                usi.SearchSkillID    = getSearch.SearchWarSearchSkillId;
                usi.SearchvsX        = getSearch.SearchWarSearchvsX;
                usi.SearchXvs        = getSearch.SearchWarSearchXvs;

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        // check max idle time
        private SearchNoticeMessage CheckUserIdleActivity(UserSearchInfo usi,
                                                          int maxIdleSearch_Seconds,
                                                          HttpContext context,
                                                          SearchNoticeMessage noticemessage)
        {
            Guid?searchID = ANOProfile.GetCookieValues(usi.UserIpAddress, context).SearchMatchID;

            SW_SearchWar obj =
                db.SW_SearchWar.SingleOrDefault <SW_SearchWar>(s => s.SearchWarUserID == usi.UserID && (s.SearchWarId == searchID));

            DateTime getAddedDateTime;

            if (obj != null)
            {
                getAddedDateTime = obj.SearchWarAddedDate;
            }
            else
            {
                return(SearchNoticeMessage.NoResults);
            }

            return(getAddedDateTime.AddSeconds(maxIdleSearch_Seconds) > TimeZoneManager.DateTimeNow ? noticemessage : SearchNoticeMessage.NoResults);
        }
        public void ProcessRequest(HttpContext context)
        {
            // Set defaults values
            string            data          = null;
            string            currentUserIp = "62.107.21.129";
            HttpRequest       currentR      = context.Request;
            HttpServerUtility currentS      = context.Server;
            int    currentLangId;
            bool   isValid            = true;
            bool   isUpdateInfoSucces = true;
            string errorMessage       = "succes";

            _currentUserIp = currentUserIp;

            string sLangId = context.Request.QueryString["li"];

            if (string.IsNullOrEmpty(sLangId))
            {
                isValid      = false;
                errorMessage = "You forgot langid (querystring li)";
            }

            string userAction = context.Request.QueryString["a"];

            if (string.IsNullOrEmpty(userAction))
            {
                isValid      = false;
                errorMessage = "You forgot action (querystring a)";
            }

            // Convert langid querystring to int32
            if (!Int32.TryParse(sLangId, out currentLangId))
            {
                isValid      = false;
                errorMessage = "Problem with converting langauge ID (querystring li)";
            }



            #region Go search

            SearchEngine engine = new SearchEngine();

            // default option
            SearchEngine.UserSearchOption userOption = SearchEngine.UserSearchOption.UpdateUserSearch;


            #region Set default user data

            var otherData = new
            {
                UserID        = (Guid)ANOProfile.GetCookieValues(currentUserIp, context).UserID,
                UserIpAddress = (string)currentUserIp
            };

            #endregion

            // Create user default information
            UserSearchInfo usi = new UserSearchInfo
            {
                UserID        = otherData.UserID,
                UserIpAddress = otherData.UserIpAddress
            };

            // CREATE match search!
            if (userAction == "1")
            {
                #region Set ints data (Get data by QueryStrings)

                var dataConvertToInts = new
                {
                    clanSkillID       = (string)currentR.QueryString["cs"],
                    clanContinentID   = (string)currentR.QueryString["cct"],
                    clanCountryID     = (string)currentR.QueryString["cc"],
                    searchContinentID = (string)currentR.QueryString["sct"],
                    searchCountryID   = (string)currentR.QueryString["sc"],
                    searchGameID      = (string)currentR.QueryString["sg"],
                    searchGameModeID  = (string)currentR.QueryString["sgt"],
                    searchXvs         = (string)currentR.QueryString["sxv"],
                    searchvsX         = (string)currentR.QueryString["svx"]
                }.ToAnonymousObjectCollection();

                int MaxIntValue = int.MaxValue;
                var intdata     = new
                {
                    clanSkillID       = (int?)null,
                    clanContinentID   = (int)MaxIntValue,
                    clanCountryID     = (int)MaxIntValue,
                    searchContinentID = (int)MaxIntValue,
                    searchCountryID   = (int?)null,
                    searchGameID      = (int)MaxIntValue,
                    searchGameModeID  = (int?)null,
                    searchXvs         = (int?)null,
                    searchvsX         = (int?)null,
                    searchYearTo      = (int)MaxIntValue,
                    searchYearFrom    = (int)MaxIntValue,
                    searchDayTo       = (int)MaxIntValue,
                    searchDayFrom     = (int)MaxIntValue,
                    searchMonthTo     = (int)MaxIntValue,
                    searchMonthFrom   = (int)MaxIntValue,
                    searchHourFrom    = (int)MaxIntValue,
                    searchHourTo      = (int)MaxIntValue,
                    searchMinutesFrom = (int)MaxIntValue,
                    searchMinutesTo   = (int)MaxIntValue
                }.ToAnonymousObjectCollection();

                #endregion

                #region validate and convert properties to ints

                for (int i = 0; i < dataConvertToInts.Count; i++)
                {
                    AnonymousObject o = dataConvertToInts.GetAnonymousObject(i);

                    if (!string.IsNullOrEmpty(o.GetValue <string>()))
                    {
                        int result;
                        if (int.TryParse(o.GetValue <string>(), out result))
                        {
                            intdata.GetAnonymousObject(o.KeyName).SetValue(result);
                        }
                    }

                    if (intdata.GetAnonymousObject(o.KeyName).GetValue_UnknownObject() != null
                        &&
                        Convert.ToInt32(intdata.GetAnonymousObject(o.KeyName).GetValue_UnknownObject()) == MaxIntValue)
                    {
                        isValid      = false;
                        errorMessage = "'" + o.KeyName +
                                       "' much be more than empty";
                    }
                }

                #endregion

                #region Set strings data (convert to HtmlEncode strings)

                var stringData = new
                {
                    ClanName  = (string)currentS.HtmlEncode(currentS.UrlDecode(currentR.QueryString["cn"])),
                    SearchMap = (string)currentS.HtmlEncode(currentS.UrlDecode(currentR.QueryString["sm"])),
                };

                #endregion

                #region Set datetime data (Replace + and . (This chars is used to avoid problems))

                var datetimeData = new
                {
                    SearchMatchStart = (DateTime)DateTime.ParseExact(currentS.UrlDecode(currentR.QueryString["sfd"]), "dd-MM-yyyy HH:mm:ss", new DateTimeFormatInfo())
                };

                #endregion

                // Edit/Create user search information
                usi.ClanName          = stringData.ClanName;
                usi.ClanSkillID       = intdata.GetAnonymousObject("clanSkillID").GetValue <int?>();
                usi.ClanContinentID   = intdata.GetAnonymousObject("clanContinentID").GetValue <int>();
                usi.ClanCountryID     = intdata.GetAnonymousObject("clanCountryID").GetValue <int>();
                usi.SearchContinentID = intdata.GetAnonymousObject("searchContinentID").GetValue <int>();
                usi.SearchCountryID   = intdata.GetAnonymousObject("searchCountryID").GetValue <int?>();
                usi.SearchGameID      = intdata.GetAnonymousObject("searchGameID").GetValue <int>();
                usi.SearchGameModeID  = intdata.GetAnonymousObject("searchGameModeID").GetValue <int?>();
                usi.SearchMap         = stringData.SearchMap;
                usi.SearchXvs         = intdata.GetAnonymousObject("searchXvs").GetValue <int>();
                usi.SearchvsX         = intdata.GetAnonymousObject("searchvsX").GetValue <int>();
                usi.SearchMatchStart  = new TimeZoneManager(currentUserIp).ConvertDateTimeToUtc(datetimeData.SearchMatchStart);

                userOption = SearchEngine.UserSearchOption.CreateUserSearch;
            }


            // Update user activity
            if (userAction == "2")
            {
                userOption = SearchEngine.UserSearchOption.UpdateUserSearch;

                // Get UserSearchInfo from database
                usi.SearchMatchID  = ANOProfile.GetCookieValues(usi.UserIpAddress, context).SearchMatchID;
                isUpdateInfoSucces = engine.GetUserMatchInfo(usi);
            }

            if (userAction == "3")
            {
                userOption = SearchEngine.UserSearchOption.UpdateActivity;

                // Get UserSearchInfo from database
                usi.SearchMatchID  = ANOProfile.GetCookieValues(usi.UserIpAddress, context).SearchMatchID;
                isUpdateInfoSucces = engine.GetUserMatchInfo(usi);

                if (isValid == true)
                {
                    errorMessage = "UpdatingUserActivity";
                }
            }


            // Validate DateTimes
            if (isUpdateInfoSucces == true)
            {
                if (TimeNowValidate(usi.SearchMatchStart) == true)
                {
                    if (DateNowValidate(usi.SearchMatchStart) != true)
                    {
                        isValid      = false;
                        errorMessage = "FromDateIsSmallerThanDateNow";
                    }
                }
                else
                {
                    isValid      = false;
                    errorMessage = "FromTimeIsSmallerThanTimeNow";
                }
            }
            else
            {
                isValid      = false;
                errorMessage = "UserSearchMatchIsNotOnline";
            }

            SearchEngine.SearchNoticeMessage searchNotice     = SearchEngine.SearchNoticeMessage.Searching;
            const int           defaultMaxResult              = 10;
            const int           defaultPageIndex              = 0;
            const int           defaultMaxSearchTimeSeconds   = 1200;
            const int           defaultMinUserActivitySeconds = 10;
            const int           defaultFromLastSeconds        = 10;
            List <SearchObject> searchResult = engine.UserSearchMatch(usi,
                                                                      userOption,
                                                                      defaultMaxResult,
                                                                      defaultPageIndex,
                                                                      currentLangId,
                                                                      defaultMaxSearchTimeSeconds,
                                                                      defaultMinUserActivitySeconds,
                                                                      out searchNotice,
                                                                      defaultFromLastSeconds,
                                                                      context);


            #endregion
            LangaugeSystem ls      = new LangaugeSystem();
            string         getLang = ls.GetLang(Convert.ToInt32(sLangId)).LangShortname;
            Thread.CurrentThread.CurrentCulture   = CultureInfo.CreateSpecificCulture(getLang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(getLang);


            // Create documet and first element called "ss" for "Searchs"
            XDocument createXmlSearchs = new XDocument(
                new XDeclaration("1.0", "iso-8859-1", "true"),
                new XElement("ssi"));

            // Get element "ssi" for "Searchs"
            XElement getSearchsElement = createXmlSearchs.Descendants("ssi").Single();

            // items element
            getSearchsElement.Add(new XElement("is"));

            // Get element "is" for "Items"
            XElement getItemsElement = getSearchsElement.Descendants("is").Single();


            // check the result is  0
            if (searchResult != null)
            {
                if (searchResult.Count() > 0)
                {
                    TimeZoneManager mngInfo = new TimeZoneManager(currentUserIp);

                    // Insert/Create data as xml
                    for (int i = 0; i < searchResult.Count(); i++)
                    {
                        var s = searchResult[i];

                        // Create element data
                        getItemsElement.Add(
                            new XElement("i",
                                         new XAttribute("id", s.SearchWarID.ToString()),
                                         new XElement("cn", s.ClanName),
                                         new XElement("ct", new XAttribute("i", s.ClanContinentData.SearchWarContinentId),
                                                      s.ClanContinentData.SearchWarContinentName),
                                         new XElement("cy", new XAttribute("i", s.ClanCountryData.SearchWarCountrytId),
                                                      new XAttribute("t", s.ClanCountryData.SearchWarCountryTLD),
                                                      s.ClanCountryData.SearchWarCountryName),
                                         new XElement("cs",
                                                      new XAttribute("i",
                                                                     s.ClanSkillData != null
                                                                         ? s.ClanSkillData.SearchWarSkillId.ToString()
                                                                         : ""),
                                                      s.ClanSkillData != null ? s.ClanSkillData.SearchWarSkillName : ""),
                                         new XElement("st", new XAttribute("i", s.SearchContinentData.SearchWarContinentId),
                                                      s.SearchContinentData.SearchWarContinentName),
                                         new XElement("sy",
                                                      new XAttribute("i",
                                                                     s.SearchCountryData != null
                                                                         ? s.SearchCountryData.SearchWarCountrytId.ToString()
                                                                         : ""),
                                                      new XAttribute("t",
                                                                     s.SearchCountryData != null
                                                                         ? s.SearchCountryData.SearchWarCountryTLD
                                                                         : ""),
                                                      s.SearchCountryData != null
                                                          ? s.SearchCountryData.SearchWarCountryName
                                                          : ""),
                                         new XElement("ss",
                                                      new XAttribute("i",
                                                                     s.SearchSkillData != null
                                                                         ? s.SearchSkillData.SearchWarSkillId.ToString()
                                                                         : ""),
                                                      s.SearchSkillData != null ? s.SearchSkillData.SearchWarSkillName : ""),
                                         new XElement("g", new XAttribute("i", s.SearchGame.SearchWarGameId),
                                                      s.SearchGame.SearchWarGameName),
                                         new XElement("gt",
                                                      new XAttribute("i",
                                                                     s.SearchGameType != null
                                                                         ? s.SearchGameType.SearchWarGameTypeId.ToString()
                                                                         : ""),
                                                      s.SearchGameType != null ? s.SearchGameType.SearchWarGameTypeName : ""),
                                         new XElement("fd", mngInfo.ConvertDateTimeFromUtc(s.SearchMatchStart).ToString()),
                                         new XElement("x", s.SearchXvs),
                                         new XElement("y", s.SearchvsX),
                                         new XElement("m", s.SearchMap)));
                    }
                }
                else
                {
                    if (isValid == true)
                    {
                        isValid      = false;
                        errorMessage = "NoResults";


                        if (userAction == "3")
                        {
                            isValid      = true;
                            errorMessage = "UpdatingUserActivity";
                        }
                    }
                }
            }
            else
            {
                if (isValid == true)
                {
                    isValid      = false;
                    errorMessage = "NoResults";


                    if (userAction == "3")
                    {
                        isValid      = true;
                        errorMessage = "UpdatingUserActivity";
                    }
                }
            }

            // write status for result
            getSearchsElement.Add(new XElement("status", new XAttribute("bool", isValid.ToString()), errorMessage));

            // Write/save data
            StringWriter sw = new StringWriter();
            XmlWriter    w  = XmlWriter.Create(sw);
            createXmlSearchs.Save(w);

            w.Close();

            data = sw.ToString();
            sw.Close();

            context.Response.ContentType     = "text/xml";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.Write(XmlWhiteSpaceModule.RemoveWhitespace(data));

            // release it
            context.Response.Flush();

            engine.CleanUpAndClose();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Searching and matching other matchs/clanwars
        /// </summary>
        /// <param name="userSearchInfo">insert UserSearchInfo object</param>
        /// <param name="option">are you starting searching OR just update search results?</param>
        /// <param name="maxResults">max results you want</param>
        /// <param name="pageIndex">page index (fx. what page you are on)</param>
        /// <param name="langId">insert langid for langauge</param>
        /// <param name="minNoUserActivityTimeSeconds">If there is no activity for the user (fx. internet connection lost)</param>
        /// <param name="noticemessage">notice if user still can search OR errors</param>
        /// <param name="fromLastSeconds">Write the number of seconds you want to get all "Searchs" from</param>
        /// <param name="context">current httpcontext</param>
        /// <param name="maxIdleTimeSeconds">How long the user can max search in time</param>
        /// <returns>anynmous object</returns>
        public List <SearchObject> UserSearchMatch(UserSearchInfo userSearchInfo,
                                                   UserSearchOption option,
                                                   int maxResults,
                                                   int pageIndex,
                                                   int langId,
                                                   int maxIdleTimeSeconds,
                                                   int minNoUserActivityTimeSeconds,
                                                   out SearchNoticeMessage noticemessage,
                                                   int fromLastSeconds,
                                                   HttpContext context)
        {
            _context = context;

            noticemessage = SearchNoticeMessage.Searching; // default status value

            // Create search
            if (option == UserSearchOption.CreateUserSearch)
            {
                InsertusiResult result = CreateUserSearch(userSearchInfo);
                if (result == InsertusiResult.Error)
                {
                    noticemessage = SearchNoticeMessage.UnknownError;
                }
            }



            // Check max idle/searching time
            if (option == UserSearchOption.UpdateUserSearch)
            {
                // Update user activity
                noticemessage = UpdateUserActivity(userSearchInfo,
                                                   minNoUserActivityTimeSeconds,
                                                   context,
                                                   noticemessage);

                noticemessage = CheckUserIdleActivity(userSearchInfo,
                                                      maxIdleTimeSeconds,
                                                      context,
                                                      noticemessage);
            }

            if (option == UserSearchOption.UpdateActivity)
            {
                // Update user activity
                noticemessage = UpdateUserActivity(userSearchInfo,
                                                   minNoUserActivityTimeSeconds,
                                                   context,
                                                   noticemessage);

                noticemessage = CheckUserIdleActivity(userSearchInfo,
                                                      maxIdleTimeSeconds,
                                                      context,
                                                      noticemessage);

                return(null);
            }

            // SEARCH NOW!!!!
            var itemResult = noticemessage == SearchNoticeMessage.Searching
                             ? SearchMatchs(userSearchInfo,
                                            maxResults,
                                            pageIndex,
                                            langId,
                                            fromLastSeconds)
                             : null;


            // no result change noticemsg
            if (itemResult == null)
            {
                noticemessage = SearchNoticeMessage.NoResults;
            }


            // return search/match results
            return(itemResult);
        }