예제 #1
0
 public void addNewGamer(SocketThread st, String name) {
   System.out.println("Do gry " + this.name + " do��czy� si� " + name);
   synchronized (gamers) {
     Gamer g = gamers.get(countGamers);
     g.st = st;
     g.name = name;
     // gamers.add(countGamers, g);
     countGamers++;
   }
   if (countGamers == 3) {
     broadcast(jsonRedy());
     return;
   }
   broadcast(jsonWaitingForGamer(countGamers + 1));
 }
예제 #2
0
  void feedback(String s) {
    JSONObject json = new JSONObject();

    json.put("type", "serverMessage");
    json.put("message", s);
    broadcast(json.toJSONString());
  }
예제 #3
0
 void listen(Gamer g) {
   // System.out.println("GET() w listen");
   String m = g.st.bm.get();
   String command = unPack(m, "command");
   // System.out.println("command = " + command);
   if (command.equals("getPlayersAndCards")) {
     ready++;
     System.out.println("Wysy�am jednemu " + jsonGetGameState(g));
     g.st.send(jsonGetGameState(g));
     if (ready == 3) {
       broadcast(jsonWhoseTurn(whoseTourn));
     }
   }
   if (command.equals("guess")) {
     guess(m);
     whoseTourn++;
     whoseTourn %= 3;
     broadcast(jsonWhoseTurn(whoseTourn));
   }
   ifEnd();
 }
예제 #4
0
 void ifEnd() {
   int max = gamers.get(0).numberOfPoinst;
   int who = 0;
   for (int x = 1; x < 3; x++) {
     if (gamers.get(x).numberOfPoinst > max) {
       max = gamers.get(x).numberOfPoinst;
       who = x;
     }
   }
   if (whoseTourn == 0) {
     System.out.println("ifEnd " + max);
     if (max >= 15) {
       broadcast(whoWin(who));
       win = true;
     }
   }
 }
예제 #5
0
  /** Run game server */
  public void run() {

    // Server socket
    try {
      this.listener = new ServerSocket(Integer.parseInt(this.config.getConfig("gamePort")));
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Listen for new connections
    try {
      while (true) {
        Game game = new Game();

        System.out.println("Waiting for players...");

        // Wait for red user
        game.addPlayer(game.new Player(listener.accept(), "red"));

        // Wait for blue user
        game.addPlayer(game.new Player(listener.accept(), "blue"));

        // Wait for yellow user
        game.addPlayer(game.new Player(listener.accept(), "yellow"));

        // Wait for green user
        game.addPlayer(game.new Player(listener.accept(), "green"));

        game.broadcast("STARTGAME");

        // Start game
        game.start();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        listener.close();
      } catch (IOException e) {
        System.out.println("Error closing game server socket");
        e.printStackTrace();
      }
    }
  }
예제 #6
0
  void guess(String m) {
    try {
      String ownerGuessedCard = unPack(m, "ownerGuessedCard");
      int[] playerTriedGuess = {-1, -1};
      JSONParser jsonParser = new JSONParser();
      JSONObject jsonObject = (JSONObject) jsonParser.parse(m);
      JSONArray arr = (JSONArray) jsonObject.get("playerTriedGuess");
      playerTriedGuess[0] = Integer.parseInt(arr.get(0).toString());
      if (arr.get(1) != null) playerTriedGuess[1] = Integer.parseInt(arr.get(1).toString());
      // System.out.println(playerTriedGuess[0] + "  --  " + playerTriedGuess[1]);
      Integer cardName = Integer.parseInt(unPack(m, "cardName"));
      String myName = unPack(m, "myName");
      String state = unPack(m, "state");
      int cardOlder = whichThisCardIs(ownerGuessedCard, cardName);
      int numberOfVisible = gamers.get(whoIs(ownerGuessedCard)).numberOfVisible;
      System.out.println("numberOfVisible " + numberOfVisible);
      int pointsForYou = 3;
      if (numberOfVisible == 9) pointsForYou = 2;
      if (numberOfVisible == 10) pointsForYou = 1;
      Boolean isVisible = false;
      if (state.equals("ONE_TRY_FAIL_ANS")) {
        pointsForYou = 0;
        isVisible = false;
        feedback(
            "Gracz_"
                + myName
                + "_�le_typowa�_karte_"
                + cardOlder
                + "_gracza_"
                + ownerGuessedCard
                + "_jako_karte_"
                + playerTriedGuess[0]);
      }
      if (state.equals("TWO_TRY_FAIL_ANS")) {
        if (playerTriedGuess[1] == -1) {
          whoseTourn += 4;
          whoseTourn %= 3;
          return;
        }
        pointsForYou = -1;
        isVisible = false;
        feedback(
            "Gracz_"
                + myName
                + "_�le_typowa�_karte_"
                + cardOlder
                + "_gracza_"
                + ownerGuessedCard
                + "_jako_karty_"
                + playerTriedGuess[0]
                + "_i_"
                + playerTriedGuess[1]);
      }
      if (state.equals("ONE_TRY_GOOD_ANS")) {
        gamers.get(whoIs(ownerGuessedCard)).numberOfVisible++;
        isVisible = true;
        feedback(
            "Gracz_"
                + myName
                + "_poprawnie_zgad�_karte_"
                + cardOlder
                + "_gracza_"
                + ownerGuessedCard
                + "_kt�ra_mia�a_warto��_"
                + playerTriedGuess[0]);
      }
      if (state.equals("TWO_TRY_GOOD_ANS")) {
        gamers.get(whoIs(ownerGuessedCard)).numberOfVisible++;
        pointsForYou -= 1;
        isVisible = true;
        feedback(
            "Gracz_"
                + myName
                + "_�le_typowa�_karte_"
                + cardOlder
                + "_gracza_"
                + ownerGuessedCard
                + "_jako_karte_"
                + playerTriedGuess[0]
                + "_ale_poprawi�_odpowiedz_na_"
                + playerTriedGuess[1]);
      }
      getHimPoints(myName, pointsForYou);
      JSONObject json = new JSONObject();
      json.put("type", "cardGuess");
      json.put("WhoTryGuess", myName);
      json.put("isVisible", isVisible);
      Integer i = pointsForYou;
      json.put("addPointForPlayer", i.toString());
      json.put("cardName", cardName.toString());
      broadcast(json.toJSONString());

    } catch (ParseException pe) {
      System.err.println("Blad PARSOWANIA!" + pe.getMessage());
    }
  }