Join.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package bs;
  7. import bswfx.ScreenManager;
  8. import org.json.JSONArray;
  9. import bswfx.BomberStudent;
  10. import org.json.JSONObject;
  11. /**
  12. *
  13. * @author loquicom
  14. */
  15. public class Join {
  16. protected JSONArray games;
  17. protected int selected = 0;
  18. public Join() {
  19. ScreenManager.setScreen(ScreenManager.JOIN_SCREEN);
  20. BomberStudent.bsc.send("GET", "game/list");
  21. JSONObject res = BomberStudent.bsc.receive();
  22. if (!res.has("games")) {
  23. BomberStudent.menu = new Menu();
  24. BomberStudent.menu.show();
  25. } else {
  26. this.games = res.getJSONArray("games");
  27. if (this.games.length() == 0) {
  28. BomberStudent.menu = new Menu();
  29. BomberStudent.menu.show();
  30. }
  31. }
  32. }
  33. public void up() {
  34. if (this.selected == 0) {
  35. this.selected = this.games.length();
  36. }
  37. this.selected--;
  38. }
  39. public void down() {
  40. if (this.selected == this.games.length() - 1) {
  41. this.selected = -1;
  42. }
  43. this.selected++;
  44. }
  45. public void connect() {
  46. JSONObject game = this.games.getJSONObject(this.selected);
  47. JSONObject param = new JSONObject();
  48. param.put("name", game.getString("name"));
  49. BomberStudent.bsc.send("POST", "game/join", param);
  50. JSONObject res = BomberStudent.bsc.receive();
  51. if (res.getInt("status") != 201) {
  52. System.err.println("Impossible de rejoindre la partie");
  53. BomberStudent.end();
  54. }
  55. //Creation game
  56. BomberStudent.game = new Game(res);
  57. ScreenManager.setScreen(ScreenManager.GAME_SCREEN);
  58. }
  59. public String toHtml() {
  60. String html = "<!DOCTYPE html><html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"file:" + System.getProperty("user.dir") + "/file/css/menu.css\"></head><body>";
  61. for (int i = 0; i < this.games.length(); i++) {
  62. JSONObject jo = this.games.getJSONObject(i);
  63. html += "<button id=\"id" + i + "\" disabled>" + jo.getString("name") + " - " + jo.getString("map") + " (" + jo.getInt("nbPlayer") + "j.)</button>";
  64. }
  65. html += "</body></html>";
  66. return html.replaceAll("id" + this.selected, "select");
  67. }
  68. public void show() {
  69. BomberStudent.wfx.loadHtml(this.toHtml());
  70. }
  71. }