public ActionResult Play(HomePageViewModel viewModel) { if (!Enum.IsDefined(typeof(Region), viewModel.PlayerInput.Region) || string.IsNullOrWhiteSpace(viewModel.PlayerInput.SummonerName)) { return(RedirectToAction("Index", new { error = "Something is wrong with the provided summoner name or region." })); } var region = (Region)viewModel.PlayerInput.Region; var summonerName = viewModel.PlayerInput.SummonerName; var trimmedSummonerName = Regex.Replace(summonerName, @"\s+", ""); var apiConnection = new RiotAPIConnection(); var summonerRequest = new GetSummonerByNameRequest(region, summonerName); var summonersResult = apiConnection.PerformRequest <Dictionary <string, Summoner> >(summonerRequest); if (summonersResult.isSuccess && summonersResult.value != null && summonersResult.value.ContainsKey(trimmedSummonerName)) { var summoner = summonersResult.value[trimmedSummonerName]; return(RedirectToAction("Play", "Game", new { region = region, summonerId = summoner.id })); } else { return(RedirectToAction("Index", new { error = "Could not find Summoner." })); } }
// GET: Play public ActionResult Play(Region?region = null, long?summonerId = null) { if (!summonerId.HasValue || !region.HasValue) { return(RedirectToAction("Index", "Home")); } var apiConnection = new RiotAPIConnection(); // get last game(s) of given summoner var recentGamesRequest = new RecentGamesRequest(region.Value, summonerId.Value); var recentGamesResult = apiConnection.PerformRequest <Games>(recentGamesRequest); // generate game seed var gameSeed = GameSeed.GenerateGameSeed(recentGamesResult.value.games); if (!recentGamesResult.isSuccess || gameSeed == null) { return(RedirectToAction("Index", "Home", new { error = ErrorMessage })); } // get main champion of player from seed var topMasteryRequest = new TopChampionMasteryRequest(region.Value, gameSeed.FellowPlayerId); var topMasteryResult = apiConnection.PerformRequest <List <Mastery> >(topMasteryRequest); if (!topMasteryResult.isSuccess || topMasteryResult.value.Count == 0) { return(RedirectToAction("Index", "Home", new { error = ErrorMessage })); } var targetChampionId = topMasteryResult.value.FirstOrDefault().championId; gameSeed.TargetChampionId = targetChampionId; Session[TargetChampionId] = targetChampionId; // get name of player from seed var summonerRequest = new GetSummonerRequest(region.Value, gameSeed.FellowPlayerId); var summonersResult = apiConnection.PerformRequest <Dictionary <string, Summoner> >(summonerRequest); if (!summonersResult.isSuccess || summonersResult.value == null || summonersResult.value.Count == 0) { return(RedirectToAction("Index", "Home", new { error = ErrorMessage })); } var fellowName = summonersResult.value[gameSeed.FellowPlayerId.ToString()].name; Session[FellowName] = fellowName; var fellowIconId = summonersResult.value[gameSeed.FellowPlayerId.ToString()].profileIconId; Session[FellowIcon] = fellowIconId; // prepare view model var allChampionsRequest = new AllChampionsRequest(region.Value); var allChampionsResult = apiConnection.PerformRequest <Champions>(allChampionsRequest); if (!allChampionsResult.isSuccess) { return(RedirectToAction("Index", "Home", new { error = ErrorMessage })); } var gameVersionsRequest = new GameVersionsRequest(region.Value); var gameVersionsResult = apiConnection.PerformRequest <List <string> >(gameVersionsRequest); if (!gameVersionsResult.isSuccess || gameVersionsResult.value.Count == 0) { return(RedirectToAction("Index", "Home", new { error = ErrorMessage })); } var gameVersion = gameVersionsResult.value.First(); Session[GameVersion] = gameVersion; return(View(new GameViewModel() { Champions = allChampionsResult.value.data.Values.OrderBy(x => x.name), PlayerChampionName = KnownChampionUtility.ChampionIdToName(gameSeed.PlayerChampionId), FellowPlayerChampionName = KnownChampionUtility.ChampionIdToName(gameSeed.FellowPlayerChampionId), FellowPlayerName = fellowName, FellowPlayerIconId = fellowIconId, SameTeam = gameSeed.SameTeam, NGamesAgo = gameSeed.NGamesAgo, GameVersion = gameVersion })); }