private void ReportScoreMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            var displayMatch = item.DataContext as DisplayMatch;

            //Find whether the player 1 panel or the player 2 panel was clicked. This is used to set victor
            var border = (Border)((ContextMenu)item.Parent).PlacementTarget;
            var button = (Button)border.TemplatedParent;
            var grid = (Grid)button.Parent;

            //Get index of button in the grid's children. If index is 0, player 1 panel was clicked, index 1 is player 2
            var index = grid.Children.OfType<Button>().Select((b, i) => new { Button = b, Index = i }).First(a => a.Button == button).Index;

            var reportScoreWindow = new ReportScoreWindow()
            {
                Owner = this,
                DataContext = item.DataContext,
                Player1Victory = index == 0
            };

            if (displayMatch != null && reportScoreWindow.ShowDialog() == true)
            {
                var scores = new SetScore[] { SetScore.Create(reportScoreWindow.Player1Score, reportScoreWindow.Player2Score) };

                if (reportScoreWindow.Player1Victory) displayMatch.Player1WinsScored.Execute(scores);
                else displayMatch.Player2WinsScored.Execute(scores);
            }
        }
        public void ReportMatchWinner(int tournamentId, int matchId, int winnerId, params SetScore[] scores)
        {
            var request = new RestRequest(string.Format("tournaments/{0}/matches/{1}.xml", tournamentId, matchId), Method.PUT);

            request.AddParameter("api_key", ApiKey);
            request.AddParameter("match[winner_id]", winnerId);

            if (scores == null || scores.Length == 0)
            {
                scores = new SetScore[] { new SetScore {
                                              Player1Score = 0, Player2Score = 0
                                          } }
            }
            ;

            request.AddParameter("match[scores_csv]", scores.Select(ss => ss.ToString()).Aggregate((one, two) => string.Format("{0},{1}", one, two)));

            var response = client.Execute(request);

            throwOnError(response);
        }
        private void submitScore_Click(object sender, RoutedEventArgs e)
        {
            var vm = this.DataContext as OrganizerViewModel;
            Station primaryStation = null;
            DisplayMatch primaryMatch = null;

            foreach (KeyValuePair<string, Station> entry in Stations.Instance.Dict)
            {
                Station station = entry.Value;
                if (station.isPrimaryStream())
                    primaryStation = station;
            }

            if (primaryStation != null)
            {
                foreach (DisplayMatch match in vm.OpenMatches)
                {
                    if (match.Match.IsMatchInProgress && match.Match.StationAssignment == primaryStation.Name)
                    {
                        primaryMatch = match;
                        break;
                    }
                }

                if (primaryMatch != null)
                {
                    int p1ScoreValue = 0;
                    int p2ScoreValue = 0;
                    int.TryParse(p1Score.Text, out p1ScoreValue);
                    int.TryParse(p2Score.Text, out p2ScoreValue);

                    if (playersSwapped)
                    {
                        int tmpValue = p1ScoreValue;
                        p1ScoreValue = p2ScoreValue;
                        p2ScoreValue = tmpValue;
                    }

                    var scores = new SetScore[] { SetScore.Create(p1ScoreValue, p2ScoreValue) };

                    if (p1ScoreValue > p2ScoreValue)
                        primaryMatch.Player1WinsScored.Execute(scores);
                    else if (p1ScoreValue < p2ScoreValue)
                        primaryMatch.Player2WinsScored.Execute(scores);
                    else
                        MessageBox.Show("Both players are tied with the same score. Increase one player's score first so a winner can be determined.", "Cannot Report Score", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                    MessageBox.Show("There is no match in progress on the primary streaming station.", "Cannot Report Score", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
                MessageBox.Show("There is no primary streaming station. Please set at least one station to type Stream. The first station in the list of type Stream will be the primary streaming station.", "Cannot Report Score", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        public void ReportMatchWinner(int tournamentId, int matchId, int winnerId, params SetScore[] scores)
        {
            var request = new RestRequest(string.Format("tournaments/{0}/matches/{1}.xml", tournamentId, matchId), Method.PUT);
            request.AddParameter("api_key", ApiKey);
            request.AddParameter("match[winner_id]", winnerId);

            if (scores == null || scores.Length == 0) scores = new SetScore[] { new SetScore { Player1Score = 0, Player2Score = 0 } };

            request.AddParameter("match[scores_csv]", scores.Select(ss => ss.ToString()).Aggregate((one, two) => string.Format("{0},{1}", one, two)));

            var response = client.Execute(request);
            throwOnError(response);
        }