Ejemplo n.º 1
0
        public FindMatchResponse FindMatch(LuisResponse luisResults, bool fromContext = false)
        {
            // If requested to get last discussed game, return it
            if (fromContext)
            {
                return(LastGameInMemory);
            }

            var home     = luisResults.Entities.ContainsKey("Home") ? luisResults.Entities["Home"] : null;
            var away     = luisResults.Entities.ContainsKey("Away") ? luisResults.Entities["Away"] : null;
            var team     = luisResults.Entities.ContainsKey("Team") ? luisResults.Entities["Team"] : null;
            var relative = luisResults.Entities.ContainsKey("Relative") ? luisResults.Entities["Relative"] : null;

            if (home != null && away == null && team != null && home != team)
            {
                away = team;
            }
            else if (home == null && away != null && team != null && away != team)
            {
                home = team;
            }

            MatchJSON matchInfo = GetMatchInfo(home, away, team, relative);

            var findMatchResult = new FindMatchResponse()
            {
                Home         = matchInfo.HomeTeam,
                Away         = matchInfo.AwayTeam,
                MatchDate    = matchInfo.Date,
                ResponseText = matchInfo.MatchDescription,
                Team         = team,
                PictureType  = IsChampionsLeague ? PictureType.ChampionsLeague : PictureType.LaLiga,
            };

            CoreferenceTeam   = team ?? home ?? away; // Save last team in context
            LastGameInMemory  = findMatchResult;      // Save last game found
            IsChampionsLeague = false;                // Reset Champions League flag
            return(findMatchResult);
        }
Ejemplo n.º 2
0
        private MatchJSON GetMatchInfo(string home, string away, string team, string relative = "Previous")
        {
            var Game = new MatchJSON();

            if (home != null && away != null && home != away) // If 2 teams are detected in the query
            {
                // If game is found, return game info plus a description generated from language generation
                Game = AllGames.Where(match => match.HomeTeam == home && match.AwayTeam == away).SingleOrDefault();
                if (Game != null)
                {
                    var result = Game.FTR == "H" ? $"a home win for **{home}**" : Game.FTR == "A" ? $"an away win for **{away}**" : "a draw";
                    Game.MatchDescription = lgEngine.EvaluateTemplate("FindMatchResult", new { home = home, away = away, date = Game.Date, stadium = stadiums[home], result = result, homeGoals = Game.FTHG, awayGoals = Game.FTAG, });
                }
                else // If game is not found, check future games
                {
                    if (IsChampionsLeague)
                    {
                        Game = ChampionsLeagueGames.Where(match => match.HomeTeam == home && match.AwayTeam == away).SingleOrDefault();
                    }
                    else
                    {
                        Game = FutureGames.Where(match => match.HomeTeam == home && match.AwayTeam == away).SingleOrDefault();
                    }

                    if (Game == null)
                    {
                        Game.MatchDescription = "Sorry we cannot find the match you requested";
                    }

                    Game.MatchDescription = lgEngine.EvaluateTemplate("NextGameTemplate", new { team = home, awayOrHome = "at home against", opponent = away, date = Game.Date, home = home, stadium = stadiums[home] });
                }
            }
            else // If one team is detected in the query
            {
                var singleTeam = team ?? home ?? away;
                if (relative == "Previous")                                                                              // If query is about team's last game, look for its last game
                {
                    Game = AllGames.Where(match => match.AwayTeam == singleTeam || match.HomeTeam == singleTeam).Last(); // Get the team's last game
                    if (Game != null)                                                                                    // If team's last game is found
                    {
                        var result = Game.FTR == "H" ? $"a home win for **{Game.HomeTeam}**" : Game.FTR == "A" ? $"an away win for **{Game.AwayTeam}**" : "a draw";
                        Game.MatchDescription = lgEngine.EvaluateTemplate("FindMatchResult", new { home = Game.HomeTeam, away = Game.AwayTeam, date = Game.Date, stadium = stadiums[Game.HomeTeam], result = result, homeGoals = Game.FTHG, awayGoals = Game.FTAG, });
                    }
                    else // If team's last game is not found
                    {
                        Game.MatchDescription = "Sorry we cannot find the match you requested";
                    }
                }
                else // Find team's next game
                {
                    if (IsChampionsLeague)
                    {
                        Game = ChampionsLeagueGames.Where(match => match.HomeTeam == singleTeam || match.AwayTeam == singleTeam).FirstOrDefault();
                    }
                    else
                    {
                        Game = FutureGames.Where(match => match.HomeTeam == singleTeam || match.AwayTeam == singleTeam).FirstOrDefault();
                    }

                    var awayOrHome = singleTeam == Game.HomeTeam ? "at home" : "away";
                    var otherTeam  = singleTeam == Game.HomeTeam ? Game.AwayTeam : Game.HomeTeam;
                    Game.MatchDescription = lgEngine.EvaluateTemplate("NextGameTemplate", new { team = singleTeam, awayOrHome = awayOrHome, opponent = otherTeam, date = Game.Date, home = Game.HomeTeam, stadium = stadiums[Game.HomeTeam] });
                }
            }

            return(Game);
        }