private void Button_MouseClick(Object sender, RoutedEventArgs e) { /* GameButton b = (GameButton)sender; * String path = b.Path; * GameManager.LaunchGame(path);*/ GameButton b = (GameButton)sender; SelectedTitle.FontSize = 48; SelectedTitle.Text = b.Title + "\n"; SelectedDescription.FontSize = 30; SelectedDescription.TextWrapping = TextWrapping.Wrap; SelectedDescription.Text = b.Description + "\n"; toPlay = b; playButton = new Button(); playButton.Content = "PLAY"; playButton.Background = Brushes.Red; playButton.Foreground = Brushes.White; playButton.MaxWidth = 200; playButton.MaxHeight = 125; playButton.Click += new RoutedEventHandler(Play_MouseClick); this.PlayGrid.Children.Add(playButton); //add high scores from database String scoreList = ""; ScoreAPIResponse scores = ScoreAPI.ScoreAPI.RequestScores(b.Title, 5, 0); if (scores.ErrCode == 0) { int scoreCount = 1; foreach (Score s in scores.ScoreSet) { scoreList += scoreCount + ": " + s.Name + s.Value; //not sure if this will format correctly scoreList += "\n"; scoreCount++; } } HighScores.FontSize = 30; HighScores.Text = scoreList; }
//Code is more or less identical for both API calls so just this one will be commented. public static ScoreAPIResponse RequestScores(string GameName, int Count, int StartPos) { //This is the parameter string to pass the arguments to the endpoint string URLParam = String.Format("?methodName=requestScores&gameName={0}&count={1}&startPos={2}", GameName, Count, StartPos); //Connections may fail at several points during the call. //Catch any failure and return a valid ScoreAPIResponse try { //Create Reques with a 1.5 second time out. Default timeout is much to long and the calls do block HttpWebRequest request = WebRequest.Create(URL + URLParam) as HttpWebRequest; request.Timeout = 1500; //Make the request and get the HTTP Respons from the server using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { //If the server returned anything other than HTTP 200(Ok) //Report the HTTP error code and a message in the ScoreAPI Response if (response.StatusCode != HttpStatusCode.OK) { ScoreAPIResponse sr = new ScoreAPIResponse(); sr.ErrCode = (int)response.StatusCode; sr.ErrMsg = response.StatusDescription; return sr; } //Using the reponse convert th JSON returned to a ScoreAPIResponse object and return it DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ScoreAPIResponse)); object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); ScoreAPIResponse jsonResponse = objResponse as ScoreAPIResponse; return jsonResponse; } //Connection failures, JSON converstion errors and anything else is caught here and returned with a status of -1 // Along with a message of the error } catch(Exception e) { ScoreAPIResponse sr = new ScoreAPIResponse(); sr.ErrCode = -1; sr.ErrMsg = e.Message; return sr; } }
//change to "welcome" when time has elapsed private void OnTimedEvent(Object sender, ElapsedEventArgs e) { //have list of buttons loop through buttonsList[buttonsList.Count % callCount]; // need to check if works after somebody leaves then enters again. // if (isNull) // { Console.WriteLine("null"); this.Dispatcher.Invoke((Action)(() => { if (isNull) { String scoreList = ""; ScoreAPIResponse scores = ScoreAPI.ScoreAPI.RequestScores(buttonsList[callCount - (buttonsList.Count * numLoop)].Title, 5, 0); if (scores.ErrCode == 0) { int scoreCount = 1; foreach (Score s in scores.ScoreSet) { scoreList += scoreCount + ": " + s.Name + s.Value; //not sure if this will format correctly scoreList += "\n"; scoreCount++; } } HighScores.FontSize = 30; HighScores.Text = scoreList; SelectedTitle.Text = buttonsList[callCount - (buttonsList.Count * numLoop)].Title; SelectedDescription.Text = buttonsList[callCount - (buttonsList.Count * numLoop)].Description; HighScores.Text = ""; // })); callCount++; if (callCount % buttonsList.Count == 0) { numLoop++; } } })); }
public static ScoreAPIResponse SubmitScore(string GameName, string Name, int Value) { string URLParam = String.Format("?methodName=submitScore&gameName={0}&userName={1}&value={2}", GameName, Name, Value); try { HttpWebRequest request = WebRequest.Create(URL + URLParam) as HttpWebRequest; request.Timeout = 1500; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { ScoreAPIResponse sr = new ScoreAPIResponse(); sr.ErrCode = (int)response.StatusCode; sr.ErrMsg = response.StatusDescription; return sr; } DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ScoreAPIResponse)); object objResponse = jsonSerializer.ReadObject(response.GetResponseStream()); ScoreAPIResponse jsonResponse = objResponse as ScoreAPIResponse; return jsonResponse; } } catch (Exception e) { ScoreAPIResponse sr = new ScoreAPIResponse(); sr.ErrCode = -1; sr.ErrMsg = e.Message; return sr; } }