Example #1
0
  protected void loadGameInfo() throws Exception {
    Engine engine = Engine.getSharedInstance(); // Singleton!
    GameInfo gameInfo = new HelloWorldGameInfo();

    engine.pushInputController((InputController) gameInfo);
    gameInfo.onGameStart();
    engine.setGameInfo(gameInfo);
  }
Example #2
0
 public void addPlayer(ServerPlayer p) {
   if (myInfo.addPlayer(p) && !inProgress) {
     myGame = new GameState(myInfo.getPlayers());
     myModel = new GameModel(myGame, this);
     this.updateGame(myGame);
     this.updateGame(new PromptUnits());
     inProgress = true;
   } else if (inProgress) {
     this.updateGame();
   }
 }
Example #3
0
 public void receiveCommandList(CommandList ls) {
   turnCommands.addCommands(ls.getCommands());
   commandsReceived++;
   System.out.println(commandsReceived + " " + myInfo.getPlayers().size());
   if (commandsReceived >= myInfo.getPlayers().size()) {
     this.processCommands();
     if (!unitsPlaced) {
       unitsPlaced = true;
       this.start();
     }
   }
 }
Example #4
0
 public ServerGame(GameInfo info, GameState game, ObjectServer os) {
   myInfo = info;
   myInfo.getPlayers().clear();
   myGame = game;
   myServer = os;
   myModel = new GameModel(myGame, this);
   if (!myInfo.getOriginalPlayers().isEmpty()) {
     unitsPlaced = true;
     inProgress = true;
     this.start();
   }
 }
Example #5
0
  private JSONObject specialMove(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    int game;
    int addPoints;
    int special;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);
    gameInfo = gameRecord.get(game);
    // Checks if all specials are used
    if (gameInfo.checkSpecial()) {
      return moveUser(gameCount);
    }
    // Gets random number to denote which special move to use
    special = randomNum.nextInt(4);
    while (gameInfo.checkSpecialMove(special)) {
      special = randomNum.nextInt(4);
    }
    gameInfo.useSpecialMove(special);

    addPoints = randomNum.nextInt(41) - 20;
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();

    try {
      jObj.put("game", game);

      action.put("actionType", "specialMove");
      action.put("actionNumber", gameInfo.getCount());
      action.put("pointsAdded", addPoints);
      if (special == SHUFFLE) {
        action.put("move", "Shuffle");
      } else if (special == CLEAR) {
        action.put("move", "Clear");
      } else if (special == INVERT) {
        action.put("move", "Invert");
      } else {
        action.put("move", "Rotate");
      }
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
 public static void processCommand(Command c, PlayerConnection pc) {
   byte commandType = c.getCommandType();
   System.out.println("Command Type: " + commandType);
   if (commandType == GAME_INFO) {
     switch (c.getCommandSpecific()) {
       case GAME_INFO_HEARTBEAT:
         GameInfo.processHeartbeat(c, pc);
       case GAME_INFO_LOGIN:
         GameInfo.processLogin(c, pc);
       case GAME_INFO_IMAGESET_REQUEST:
         GameInfo.processImageSetRequest(c, pc);
         // case GAME_INFO_LOGOUT: GameInfo.processLogout(c,pc);
     }
   }
   if (commandType == GAME_LOOKUP) {
     switch (c.getCommandSpecific()) {
         // case GAME_LOOKUP_PLAYER_ID: GameLookup.lookupPlayerID(c,pc);break;
         // case GAME_LOOKUP_PLAYER_NAME: GameLookup.lookupPlayerName(c,pc);break;
         // case GAME_LOOKUP_CHARACTER_ID: GameLookup.lookupCharacterID(c,pc);break;
         // case GAME_LOOKUP_CHARACTER_NAME: GameLookup.lookupCharacterName(c,pc);break;
         // case GAME_LOOKUP_MAP_ID: GameLookup.lookupMapID(c,pc);break;
         // case GAME_LOOKUP_MAP_NAME: GameLookup.lookupMapName(c,pc);break;
         // case GAME_LOOKUP_MAP_REQUEST: GameLookup.lookupMap(c,pc);break;
     }
   }
   if (commandType == GAME_UPDATE) {
     switch (c.getCommandSpecific()) {
       case GAME_UPDATE_PHYSICS:
         GameUpdate.updatePhysics(c, pc);
         break;
         // case GAME_UPDATE_BASE_STATS: GameUpdate.updateBaseStats(c,pc);break;
         // case GAME_UPDATE_DERIVED_STATS: GameUpdate.updateDerivedStats(c,pc);break;
         // case GAME_UPDATE_PLAYER_INFO: GameUpdate.updatePlayerInfo(c,pc);break;
         // case GAME_UPDATE_QUEST: GameUpdate.updateQuest(c,pc);
         // case GAME_UPDATE_CREATURE: GameUpdate.updateCreature(c,pc);
         // case GAME_UPDATE_SCRIPT: GameUpdate.updateScript(c,pc);
         // case GAME_UPDATE_BUFFS: GameUpdate.updateBuffs(c,pc);
     }
   }
   if (commandType == GAME_ITEM) {
     switch (c.getCommandSpecific()) {
     }
   }
 }
Example #7
0
 private void processRace(GameInfo gameInfo, int race, long xp) {
   xp = raceXP.get(race) + xp;
   raceXP.put(race, xp);
   if (gameInfo.won()) {
     switch (gameInfo.getGameType()) {
       case GameInfo.DEFENSE:
         int defenseWon = defensiveGamesWon.get(race);
         defensiveGamesWon.put(race, defenseWon + 1);
         break;
       case GameInfo.BATTLE:
         int battleWon = battleGamesWon.get(race);
         battleGamesWon.put(race, battleWon + 1);
         break;
       default:
         Modules.LOG.error(
             "PlayerStats", "Unknown game type in player stats " + gameInfo.getGameType());
     }
   }
 }
Example #8
0
      @Override
      public void update(GameManager manager) {
        if (manager.allPlayersReady()) {
          GameInfo gameInformation = manager.game.advanceAndGetState();

          // We can only wait for a choice to be played if there is a choice
          manager.isChoicePlayed = gameInformation.getChoices().isEmpty();

          // Unready the current player for the next state
          Player currentPlayer = manager.players.get(gameInformation.getPlayerColor());

          // Send the game information to everyone
          for (Player player : manager.players.values()) {
            player.updateGameInfo(gameInformation, currentPlayer == player);
          }

          manager.unreadyPlayer(currentPlayer);

          manager.state = GameManagerState.WAIT_TO_REVEAL_DICE;
        }
      }
Example #9
0
  @Override
  public void visitGameNotification(GameNotification n) {
    synchronized (matches) {
      if (!matches.isEmpty()) {
        return;
      }
    }

    try {
      GameInfo info = n.getInfo();

      int matchCount = info.getMaps().length;
      int matchNumber = 0;
      for (String map : info.getMaps()) {
        if (map.endsWith(".xml")) map = map.substring(0, map.indexOf('.'));
        Match match = new Match(info, map, options, matchNumber++, matchCount);
        debug("queuing match " + match);
        matches.add(match);
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail("couldn't start the match: " + e.getMessage());
    }
  }
Example #10
0
  private JSONObject endGame(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    int game;
    String status;

    game = getRandomGame(gameCount);
    gameInfo = gameRecord.get(game);
    gameInfo.incrementCount();
    if (gameInfo.getCount() < 9) {
      return moveUser(gameCount);
    }
    if (gameInfo.getPoints() > 40 || gameInfo.getPoints() < -40) {
      status = "WIN";
    } else {
      status = "LOSS";
    }
    try {
      jObj.put("game", game);

      action.put("actionType", "gameEnd");
      action.put("gameStatus", status);
      action.put("actionNumber", gameInfo.getCount());
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
      userIds[gameInfo.getId()] = false;
      gameRecord.remove(game);
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
 public void saveGameInfo(GameInfo gameInfo) throws IOException, JSONException {
   BufferedWriter writer = null;
   try {
     JSONArray jsonArray = new JSONArray();
     loadGameInfo();
     for (int i = 0; i < mGameInfoList.size(); i++) {
       JSONObject jsonObject = mGameInfoList.get(i).toJson();
       jsonArray.put(jsonObject);
     }
     jsonArray.put(gameInfo.toJson());
     OutputStream outputStream = mContext.openFileOutput(mInfoFileName, Context.MODE_PRIVATE);
     writer = new BufferedWriter(new OutputStreamWriter(outputStream));
     writer.write(jsonArray.toString());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (writer != null) {
       writer.close();
     }
   }
 }
Example #12
0
  public long increaseXP(GameInfo info) {

    long xp = info.getXP();

    long xpToNextLevel = getLevelXP(currentLevel + 1) - totalXP;

    totalXP += xp;

    long temp = xp - xpToNextLevel;
    if (xp >= 0) {
      currentLevel++;

      long nextLevel = currentLevel + 1;

      while ((temp -= nextLevel) >= 0) {
        currentLevel++;
        nextLevel++;
      }
    }
    return xp;
  }
Example #13
0
  private JSONObject moveUser(int gameCount) {
    Random randomNum = new Random();
    GameInfo gameInfo;
    JSONObject jObj = new JSONObject();
    JSONObject action = new JSONObject();
    JSONObject coords = new JSONObject();
    int game;
    int xCoord;
    int yCoord;
    int addPoints;
    // Gets random number to denote which game to use action on
    game = getRandomGame(gameCount);

    xCoord = randomNum.nextInt(20) + 1;
    yCoord = randomNum.nextInt(20) + 1;
    addPoints = randomNum.nextInt(41) - 20;
    gameInfo = gameRecord.get(game);
    gameInfo.addPoints(addPoints);
    gameInfo.incrementCount();
    try {
      jObj.put("game", game);

      coords.put("x", xCoord);
      coords.put("y", yCoord);

      action.put("actionType", "Move");
      action.put("actionNumber", gameInfo.getCount());
      action.put("location", coords);
      action.put("pointsAdded", addPoints);
      action.put("points", gameInfo.getPoints());

      jObj.put("action", action);
      jObj.put("user", gameInfo.getUser());
    } catch (JSONException e) {
      System.out.println("could not put");
    }
    return jObj;
  }
Example #14
0
  public void update(GameInfo gameInfo) {
    int races = gameInfo.getLocalPlayerRaces();
    CampaignLevel campaignLevel = gameInfo.getCampaignLevel();
    if (campaignLevel != null && gameInfo.won()) {
      int stars = gameInfo.getStarCount();
      campaignLevel.update(stars, gameInfo.getComputerLevel());
    }

    long xp = increaseXP(gameInfo);
    for (int race : Races.asArray(races)) {
      processRace(gameInfo, race, xp);
    }
    if (gameInfo.won()) wins++;
    else losses++;

    if (longestGame < gameInfo.getLength()) longestGame = gameInfo.getLength();
    if (shortestGame > gameInfo.getLength()) shortestGame = gameInfo.getLength();
    if (highestLevelBeaten < gameInfo.getComputerLevel())
      highestLevelBeaten = gameInfo.getComputerLevel();
    if (highestScore < xp) highestScore = xp;

    for (Achievement achievement : achievements) {
      if (achievement.executeIfNotCompleted(this)) {
        // add achievement to list if it was executed for the first time
        gameInfo.addAchievement(achievement);
      }
    }
  }
Example #15
0
 private void setGameInfo(int gameNo, boolean isFavorite) {
   this.gameInfo = new GameInfo();
   try {
     gameInfo.setEvent(getSanitizedString(DataBase.getEvent()));
     if (gameInfo.getEvent().equals("?")) {
       gameInfo.setEvent("");
     }
     gameInfo.setSite(getSanitizedString(DataBase.getSite()));
     if (gameInfo.getSite().equals("?")) {
       gameInfo.setSite("");
     }
     String date = DataBase.getDate();
     if (date == null) {
       date = "";
     } else if (date.endsWith(".??.??")) {
       date = date.substring(0, date.length() - 6);
     } else if (date.endsWith(".??")) {
       date = date.substring(0, date.length() - 3);
     }
     if (date.equals("?") || date.equals("????")) {
       date = "";
     }
     gameInfo.setDate(date);
     gameInfo.setRound(getSanitizedString(DataBase.getRound()));
     if (gameInfo.getRound().equals("?")) {
       gameInfo.setRound("");
     }
     gameInfo.setWhite(getSanitizedString(DataBase.getWhite()));
     gameInfo.setBlack(getSanitizedString(DataBase.getBlack()));
     String[] results = {"*", "1-0", "0-1", "1/2"};
     gameInfo.setResult(results[DataBase.getResult()]);
     byte[] dbPgn = DataBase.getPGN();
     if (dbPgn != null) {
       gameInfo.setPgn(loadPGN ? new String(DataBase.getPGN(), DataBase.SCID_ENCODING) : null);
     }
   } catch (UnsupportedEncodingException e) {
     Log.e("SCID", "Error converting byte[] to String", e);
   }
   gameInfo.setId(gameNo);
   gameInfo.setFavorite(isFavorite);
   gameInfo.setDeleted(DataBase.isDeleted());
 }
Example #16
0
 public void endGame() {
   myServer.endGame(myInfo.getName());
 }