Esempio n. 1
0
        public ActionResult <bool> StartMatch([FromForm] string matchName, [FromForm] string datetime)
        {
            try
            {
                // check if match is not already created
                if (_isOngoingMatch(matchName))
                {
                    return(true);
                }

                // create pub/sub topic on match start
                _cache.MessagingService.CreateTopic(matchName);

                // create start update
                MatchUpdate start = new MatchUpdate(datetime, "match_start", "stadium", "Match has Started");

                // send update for storing in cache
                _storeHandler.SaveUpdates(matchName, start);
            }
            catch (Exception e)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// update match with any event
        /// </summary>
        /// <param name="matchName">name of match</param>
        /// <param name="datetime">time of match</param>
        /// <param name="type">type of event</param>
        /// <param name="entity">entity of event</param>
        /// <param name="data">data of event</param>
        /// <returns>true or false</returns>
        public ActionResult <bool> UpdateMatch([FromForm] string matchName, [FromForm] string datetime, [FromForm] string type, [FromForm] string entity, [FromForm] string data)
        {
            try
            {
                // send update to live match handler
                MatchUpdate matchUpdate = new MatchUpdate(datetime, type, entity, data);

                // check if the match is already created
                if (!_isOngoingMatch(matchName))
                {
                    return(false);
                }

                // create new Message packet with the match update
                Message message = new Message(matchUpdate);

                // fetch topic handler from cache
                ITopic topic = _cache.MessagingService.GetTopic(matchName);
                // publish Message on topic
                topic.Publish(message, DeliveryOption.All);

                // store update to cache
                _storeHandler.SaveUpdates(matchName, new MatchUpdate(datetime, type, entity, data));
            }
            catch (Exception e)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        public ActionResult <bool> StopMatch([FromForm] string matchName, [FromForm] string datetime)
        {
            try
            {
                // create stop update
                MatchUpdate stop = new MatchUpdate(datetime, "match_end", "stadium", "Match has Stopped");

                // check if the match is already created
                if (!_isOngoingMatch(matchName))
                {
                    return(true);
                }

                // create new Message packet with the match update
                Message message = new Message(stop);

                // fetch topic handler from cache
                ITopic topic = _cache.MessagingService.GetTopic(matchName);
                // publish Message on topic
                topic.Publish(message, DeliveryOption.All);

                // send update for storing in cache
                _storeHandler.SaveUpdates(matchName, stop);
                // clear all stored updates
                _storeHandler.ClearUpdates(matchName);
            }
            catch (Exception e)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        private static long AddMatchUpdate(MatchUpdate update, StffContext.MatchRow match, StffContext context)
        {
            var playerRow = context.Player.Where(p => p.ExternalId == update.Player.ExternalId).Single();

            var insertedRow = context.MatchUpdate.AddMatchUpdateRow(update.Type.ToString(), update.Minute, playerRow, match, update.HomeTeam);

            insertedRow.AcceptChanges();
            context.AcceptChanges();
            return(insertedRow.Id);
        }
Esempio n. 5
0
        private static List <MatchUpdate> GetUpdates(string eventsHtml, IEnumerable <Player> players)
        {
            List <MatchUpdate> updates = new List <MatchUpdate>();
            var   events = eventsHtml.Split(new string[] { "<li" }, StringSplitOptions.RemoveEmptyEntries);
            Regex timeEx = new Regex("<time>(.*?)</time>");
            Regex pidEx  = new Regex("fplid=(.*?)\"");

            foreach (var e in events)
            {
                bool isHome    = e.Contains("hometeam");
                var  timeMatch = timeEx.Match(e);
                var  pidMatch  = pidEx.Match(e);
                if (!timeMatch.Success || !pidMatch.Success)
                {
                    continue;
                }
                var stringMinute = timeMatch.Value.Substring(6, timeMatch.Value.Length - 14);
                if (stringMinute.Contains("+"))
                {
                    stringMinute = stringMinute.Split('+')[0];
                }
                var         stringPid = pidMatch.Value.Substring(6, pidMatch.Value.Length - 7);
                long        pid       = long.Parse(stringPid);
                MatchUpdate mu        = null;
                if (e.Contains("yellow-card"))
                {
                    mu = new YellowCardUpdate();
                }
                else if (e.Contains("red-card"))
                {
                    mu = new RedCardUpdate();
                }
                else if (e.Contains("goal"))
                {
                    mu = new GoalUpdate();
                }
                else
                {
                    continue; // ?
                }
                mu.Minute   = int.Parse(stringMinute);
                mu.HomeTeam = isHome;
                var player = players.Where(p => p.ExternalId == pid);
                var tmp    = new List <Player>(player);
                if (tmp.Count != 1)
                {
                    throw new InvalidOperationException();
                }
                mu.Player = player.Single();
                updates.Add(mu);
            }
            return(updates);
        }
Esempio n. 6
0
        public MatchChallonge UpdateMatch(string tournamentUrl, int matchID, MatchUpdate mu)
        {
            //I return
            var jsonMatch = new JsonMatchUpdate()
            {
                json = mu
            };

            return(ProcessJson <MatchChallonge, JsonMatchChallonge>(
                       JsonConvert.DeserializeObject <JsonMatchChallonge>(Send(jsonMatch, "PUT", "/" + tournamentUrl + "/matches/" + matchID.ToString()))
                       ));
        }
Esempio n. 7
0
        public static Dictionary <long, Match> GetAllMatches(
            Dictionary <long, Referee> refCache,
            Dictionary <long, Player> playerCache,
            Dictionary <long, Team> teamCache)
        {
            Dictionary <long, Match> matches = new Dictionary <long, Match>();

            using (var context = new StffContext())
            {
                foreach (var match in context.Match.ToList())
                {
                    var   updates = context.MatchUpdate.Where(mu => mu.MatchId == match.Id);
                    Match m       = new Match();
                    m.AssRef1         = refCache[match.AssRefereeId1];
                    m.AssRef2         = refCache[match.AssRefereeId2];
                    m.AwayTeam        = teamCache[match.AwayTeamid].ExternalId;
                    m.AwayTeamPlayers = new List <Player>();
                    foreach (var player in context.Player.Where(p => p.TeamId == match.AwayTeamid))
                    {
                        m.AwayTeamPlayers.Add(playerCache[player.Id]);
                    }
                    m.Completed       = match.Completed;
                    m.ExternalId      = match.ExternalMatchId;
                    m.GameWeek        = match.GameWeek;
                    m.HomeTeam        = teamCache[match.HomeTeamId].ExternalId;
                    m.HomeTeamPlayers = new List <Player>();
                    foreach (var player in context.Player.Where(p => p.TeamId == match.HomeTeamId))
                    {
                        m.HomeTeamPlayers.Add(playerCache[player.Id]);
                    }
                    m.MatchUpdates = new List <MatchUpdate>();
                    foreach (var mu in context.MatchUpdate.Where(update => update.MatchId == match.Id))
                    {
                        m.MatchUpdates.Add(MatchUpdate.GetUpdate(mu.HomeTeam, mu.Minute, playerCache[mu.PlayerId], (MatchUpdateType)Enum.Parse(typeof(MatchUpdateType), mu.Type)));
                    }

                    m.Ref       = refCache[match.RefereeId];
                    m.StartTime = m.StartTime;
                    matches.Add(m.ExternalId, m);
                }
            }
            return(matches);
        }
 /// <summary>
 /// send update to all connected clients on the "MatchUpdate" event
 /// </summary>
 /// <param name="update">update to send</param>
 /// <returns></returns>
 public async Task SendUpdate(MatchUpdate update)
 {
     await Clients.All.SendAsync("MatchUpdate", new JsonResult(update));
 }