//Crear jugador y comprobar si ya existe en BD private bool CreatePlayer(string nickname) { var dt = DataBaseController.ExecuteQuery($"SELECT * FROM PLAYER WHERE nickname = '{nickname}'"); if (dt.Rows.Count > 0) { var dt2 = DataBaseController.ExecuteQuery($"SELECT idPlayer FROM PLAYER WHERE nickname = '{nickname}'"); foreach (DataRow dr in dt2.Rows) { GameData.idPlayer = Convert.ToInt32(dr[0]); } return(true); } else { DataBaseController.ExecuteNonQuery("INSERT INTO PLAYER(nickname) VALUES" + $"('{nickname}')"); var dt3 = DataBaseController.ExecuteQuery($"SELECT idPlayer FROM PLAYER WHERE nickname = '{nickname}'"); foreach (DataRow dr in dt3.Rows) { GameData.idPlayer = Convert.ToInt32(dr[0]); } return(false); } }
//Comprobante de bloques golpeados, puntos y rebotes private void timer1_Tick(object sender, EventArgs e) { if (!GameData.gamestarted) { return; } try { ball.Left += GameData.dirX; ball.Top += GameData.dirY; bounceball(); CheckWin(); } //La pelota cae hasta el fondo catch (OutOfBoundsException ex) { try { timer1.Stop(); GameData.livesleft--; //Juego Perdido if (GameData.livesleft == 0) { throw new GameOverException("Has perdido!"); } ball.Top = Height - pictureBox1.Height * 2 - ball.Height - 15; ball.Left = pictureBox1.Left + pictureBox1.Width / 2; GameData.gamestarted = false; } //Juego Perdido catch (GameOverException ex2) { //Guardar el punatje en BD DataBaseController.ExecuteNonQuery("INSERT INTO SCORES(idPlayer, score) VALUES" + $"({GameData.idPlayer}, {GameData.points})"); MessageBox.Show(ex2.Message); } } //Juego Ganado catch (GameWinException ex) { timer1.Stop(); GameData.gamestarted = false; //Guardar el puntaje en BD DataBaseController.ExecuteNonQuery("INSERT INTO SCORES(idPlayer, score) VALUES" + $"({GameData.idPlayer}, {GameData.points})"); MessageBox.Show(ex.Message); GameData.gamewon = true; } }
//Obtener la lista Top 10 de BD private List <Player> ObtainTopPlayers() { var topPlayers = new List <Player>(); DataTable dt = DataBaseController.ExecuteQuery("SELECT pl.nickname, sc.score " + "FROM PLAYER pl, SCORES sc " + "WHERE pl.idPlayer = sc.idPlayer " + "ORDER BY sc.score DESC " + "LIMIT 10"); foreach (DataRow dr in dt.Rows) { topPlayers.Add(new Player(dr[0].ToString(), Convert.ToInt32(dr[1]))); } return(topPlayers); }