public Dictionary<string, string> GetResult(Guid MatchId, int CurrentRound) { try { using (RockPaperScissorsContainer Entities = new RockPaperScissorsContainer()) { int HumanId = GetPlayerTypeId("Human", Entities); int ComputerId = GetPlayerTypeId("Computer", Entities); Dictionary<string, string> WinnerDict = new Dictionary<string, string>(); string HumanChoice = Entities.tblTurns .Where(t => t.MatchId == MatchId && t.PlayerTypeId == HumanId && t.TurnNumber == CurrentRound) .FirstOrDefault().Choice; string ComputerChoice = Entities.tblTurns .Where(t => t.MatchId == MatchId && t.PlayerTypeId == ComputerId && t.TurnNumber == CurrentRound) .FirstOrDefault().Choice; string Winner = this.GetWinner(Entities, HumanChoice, ComputerChoice); WinnerDict.Add("HumanChoice", HumanChoice); WinnerDict.Add("ComputerChoice", ComputerChoice); WinnerDict.Add("Winner", Winner); return WinnerDict; } } catch (Exception ex) { throw ex; } }
public string GetWinner(RockPaperScissorsContainer Entities, string HumanChoice, string ComputerChoice) { if (Entities.vw_WinnersLosers.Where(v => v.Winner == HumanChoice && v.Loser == ComputerChoice).Count() == 1) { return "HumanWinner"; } else if (Entities.vw_WinnersLosers.Where(v => v.Winner == ComputerChoice && v.Loser == HumanChoice).Count() == 1) { return "ComputerWinner"; } else { return "Draw"; } }
public List<tblPlayerType> GetOpponents() { try { using (RockPaperScissorsContainer entities = new RockPaperScissorsContainer()) { return entities.tblPlayerTypes .Where(p => p.PlayerTypeId != 1) .ToList(); } } catch(Exception ex) { // TODO: Handle SQL error throw ex; } }
public Dictionary<Guid, bool> CreateMatchId(string numOfRounds) { try { using (RockPaperScissorsContainer entities = new RockPaperScissorsContainer()) { tblMatch tblMatch = new tblMatch(); tblMatch.MatchId = Guid.NewGuid(); tblMatch.MatchType = numOfRounds; entities.tblMatches.Add(tblMatch); entities.SaveChanges(); Dictionary<Guid, bool> Outcome = new Dictionary<Guid, bool>(); Outcome.Add(tblMatch.MatchId, true); return Outcome; } } catch (Exception ex) { throw ex; } }
public int GetPlayerTypeId(string Type, RockPaperScissorsContainer Entities) { return Entities.tblPlayerTypes .Where(p => p.PlayerType == Type) .FirstOrDefault().PlayerTypeId; }
public string GetLastMoveHuman(Guid MatchId, int RoundNumber) { try { using (RockPaperScissorsContainer Entities = new RockPaperScissorsContainer()) { RoundNumber = RoundNumber-1; return Entities.tblTurns .Where(t => t.MatchId == MatchId && t.TurnNumber == RoundNumber && t.PlayerTypeId == 1) .FirstOrDefault().Choice; } } catch (Exception ex) { throw ex; } }
public void SaveMove(string Choice, Guid MatchId, string Player, int currentRound) { try { using (RockPaperScissorsContainer entities = new RockPaperScissorsContainer()) { int OpponentId = entities.tblPlayerTypes .Where(p => p.PlayerType == Player) .FirstOrDefault() .PlayerTypeId; tblTurn tblTurn = new tblTurn(); tblTurn.TurnId = Guid.NewGuid(); tblTurn.MatchId = MatchId; tblTurn.Choice = Choice; tblTurn.PlayerTypeId = OpponentId; tblTurn.TurnNumber = currentRound; entities.tblTurns.Add(tblTurn); entities.SaveChanges(); } } catch (Exception ex) { throw ex; } }
// public list public List<tblLookupMoveType> GetStandardMoveSet() { try { using (RockPaperScissorsContainer entities = new RockPaperScissorsContainer()) { return entities.tblLookupMoveTypes .Where(m => m.MoveTypeName == "Rock" || m.MoveTypeName == "Paper" || m.MoveTypeName == "Scissors") .ToList(); } } catch (Exception ex) { // TODO: Handle SQL error throw ex; } }