コード例 #1
0
  @RequestMapping(value = "/combat/pve/Attack", method = RequestMethod.POST)
  public @ResponseBody CombatObject increaseAttack(@RequestBody CombatObject data) {
    data = CombatLogic.playerAttack(data);

    if (data.getThePlayer().getCurrentHealth() > 0 && data.getTheEnemy().getHealth() <= 0) {
      data = CombatLogic.survivingPlayer(data);
    } else {
      data = CombatLogic.enemyAttack(data);
    }

    if (data.getThePlayer().getCurrentHealth() <= 0) {
      data = CombatLogic.dieingPlayer(data);
    }

    try {
      Connection connection = DatabaseUrl.extract().getConnection();
      Statement stmtUser = connection.createStatement();
      stmtUser.execute(
          "UPDATE Characters SET currenthealth = '"
              + data.getThePlayer().getCurrentHealth()
              + "'  WHERE characterid = '"
              + data.getThePlayer().getCharacterID()
              + "'");
      stmtUser.execute(
          "UPDATE Characters SET zeni = '"
              + data.getThePlayer().getMoney()
              + "'  WHERE characterid = '"
              + data.getThePlayer().getCharacterID()
              + "'");
    } catch (Exception e) {
      System.out.println(e);
    }

    return data;
  }
コード例 #2
0
  @RequestMapping(value = "/buy/Potion", method = RequestMethod.POST)
  public @ResponseBody MarketObject addPotions(@RequestBody MarketObject data) {
    data = MarketLogic.buyPotion(data);

    try {
      Connection connection = DatabaseUrl.extract().getConnection();
      Statement stmtUser = connection.createStatement();
      stmtUser.execute(
          "UPDATE Characters SET zeni = '"
              + data.getPlayer().getMoney()
              + "'  WHERE characterid = '"
              + data.getPlayer().getCharacterID()
              + "'");
      stmtUser.execute(
          "UPDATE Characters SET healItems = '"
              + data.getPlayer().getHealItems()
              + "'  WHERE characterid = '"
              + data.getPlayer().getCharacterID()
              + "'");
    } catch (Exception e) {
      System.out.println(e);
    }

    return data;
  }
コード例 #3
0
  public static void main(String[] args) {

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    // get("/hello", (req, res) -> "Hello World");

    // get("/hello", (req, res) -> {
    //   RelativisticModel.select();

    //   String energy = System.getenv().get("ENERGY");

    //   Amount<Mass> m = Amount.valueOf(energy).to(KILOGRAM);
    //   return "E=mc^2: " + energy + " = " + m.toString();
    // });

    // get("/", (request, response) -> {
    //         Map<String, Object> attributes = new HashMap<>();
    //         attributes.put("message", "Hello World!");

    //         return new ModelAndView(attributes, "index.ftl");
    //     }, new FreeMarkerEngine());

    get(
        "/db",
        (req, res) -> {
          Connection connection = null;
          Map<String, Object> attributes = new HashMap<>();
          try {
            connection = DatabaseUrl.extract().getConnection();

            Statement stmt = connection.createStatement();
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
            stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
            ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

            ArrayList<String> output = new ArrayList<String>();
            while (rs.next()) {
              output.add("Read from DB: " + rs.getTimestamp("tick"));
            }

            attributes.put("results", output);
            return new ModelAndView(attributes, "db.ftl");
          } catch (Exception e) {
            attributes.put("message", "There was an error: " + e);
            return new ModelAndView(attributes, "error.ftl");
          } finally {
            if (connection != null)
              try {
                connection.close();
              } catch (SQLException e) {
              }
          }
        },
        new FreeMarkerEngine());
  }
コード例 #4
0
  @RequestMapping(value = "/combat/pve/Heal", method = RequestMethod.POST)
  public @ResponseBody CombatObject increaseHealth(@RequestBody CombatObject data) {
    data = CombatLogic.healPlayer(data);

    if (data.getThePlayer().getCurrentHealth() > 0 && data.getTheEnemy().getHealth() <= 0) {
      data = CombatLogic.survivingPlayer(data);
    } else {
      data = CombatLogic.enemyAttack(data);
    }

    if (data.getThePlayer().getCurrentHealth() <= 0) {
      data = CombatLogic.dieingPlayer(data);
    }

    try {
      Connection connection = DatabaseUrl.extract().getConnection();
      Statement stmtUser = connection.createStatement();
      // Table that relates characters to users:		usercharacters
      // Columns:		characterid		chactername		health		attack		healingitems	zeni
      // DataTypes:	int				string			int			int			int				int
      // ResultSet character = stmtUser.executeQuery("SELECT health FROM Characters WHERE
      // characterid = '" + data.getThePlayer().getCharacterID() + "'");
      stmtUser.execute(
          "UPDATE Characters SET currenthealth = '"
              + data.getThePlayer().getCurrentHealth()
              + "'  WHERE characterid = '"
              + data.getThePlayer().getCharacterID()
              + "'");
      stmtUser.execute(
          "UPDATE Characters SET healthitems = '"
              + data.getThePlayer().getHealItems()
              + "'  WHERE characterid = '"
              + data.getThePlayer().getCharacterID()
              + "'");
      stmtUser.execute(
          "UPDATE Characters SET zeni = '"
              + data.getThePlayer().getMoney()
              + "'  WHERE characterid = '"
              + data.getThePlayer().getCharacterID()
              + "'");
      // while(character.next())
      // {
      // int health = character.getInt(1);
      // }
    } catch (Exception e) {
      System.out.println(e);
    }

    return data;
  }
コード例 #5
0
  @RequestMapping(value = "/combat/pve", method = RequestMethod.POST)
  public @ResponseBody CombatObject startCombat(@RequestBody Player data) {
    CombatObject combat = new CombatObject(data, CombatLogic.createEnemy(data));

    try {
      Connection connection = DatabaseUrl.extract().getConnection();
      Statement stmtUser = connection.createStatement();
      stmtUser.execute(
          "UPDATE Characters SET town = '"
              + data.getTown()
              + "'  WHERE characterid = '"
              + data.getCharacterID()
              + "'");
    } catch (Exception e) {
      System.out.println(e);
    }

    return combat;
  }
コード例 #6
0
 @RequestMapping(value = "/login", method = RequestMethod.POST)
 public @ResponseBody LoginValidation loginGet(@RequestBody LoginData data) {
   LoginValidation code = new LoginValidation();
   try {
     Connection connection = DatabaseUrl.extract().getConnection();
     Statement stmtCount = connection.createStatement();
     Statement stmtUser = connection.createStatement();
     Statement stmtUserCharacter = connection.createStatement();
     Statement stmtCharacter = connection.createStatement();
     ResultSet user =
         stmtCount.executeQuery(
             "SELECT count(*) FROM Users WHERE username = '******' AND password = '******'");
     while (user.next()) {
       if (user.getInt(1) == 0) {
         code.IncorrectUsernameOrPassword = true;
       }
     }
     if (!code.IncorrectUsernameOrPassword) {
       ResultSet userInfo =
           stmtUser.executeQuery(
               "SELECT * FROM Users WHERE username = '******' AND password = '******'");
       while (userInfo.next()) {
         code.playerData.setUserId(userInfo.getInt(1));
       }
       ResultSet userCharacterInfo =
           stmtUserCharacter.executeQuery(
               "SELECT * FROM userCharacters WHERE userid = '"
                   + code.playerData.getUserId()
                   + "'");
       while (userCharacterInfo.next()) {
         if (userCharacterInfo.getInt(1) == code.playerData.getUserId()) {
           code.playerData.setCharacterID(userCharacterInfo.getInt(2));
         }
       }
       ResultSet characterInfo =
           stmtCharacter.executeQuery(
               "SELECT * FROM Characters WHERE characterid = '"
                   + code.playerData.getCharacterID()
                   + "'");
       code.playerData.setCurrentHealth(characterInfo.getInt(2));
       while (characterInfo.next()) {
         if (characterInfo.getInt(1) == code.playerData.getCharacterID()) {
           code.playerData.setCharacterName(characterInfo.getString(2));
           code.playerData.setCurrentHealth(characterInfo.getInt(3));
           code.playerData.setMaxHealth(characterInfo.getInt(4));
           code.playerData.setAttack(characterInfo.getInt(5));
           code.playerData.setHealItems(characterInfo.getInt(6));
           code.playerData.setMoney(characterInfo.getInt(7));
           code.playerData.setTown(characterInfo.getString(8));
         }
       }
     }
   } catch (Exception e) {
     code.databaseError = true;
   }
   return code;
   //		return new ResponseEntity<String>(HttpStatus.ACCEPTED);
 }
コード例 #7
0
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public @ResponseBody ValidationCodes registerTransfer(@RequestBody RegisterData data) {
    ValidationCodes code = new ValidationCodes();
    try {
      if (data.userName.isEmpty()) {
        code.UsernameEmpty = true;
      }
      if (data.email.isEmpty()) {
        code.EmailEmpty = true;
      }
      if (data.password.isEmpty() || data.confirmPassword.isEmpty()) {
        code.PasswordMismatch = true;
      }
      if (!code.UsernameEmpty && !code.EmailEmpty && !code.PasswordMismatch) {
        Connection connection = DatabaseUrl.extract().getConnection();
        Statement stmtUser = connection.createStatement();
        Statement stmtEmail = connection.createStatement();
        Statement stmtInsert = connection.createStatement();
        Statement stmtCharacterCreate = connection.createStatement();
        Statement stmtUserData = connection.createStatement();
        Statement stmtCharacterData = connection.createStatement();
        Statement stmtCharacterRelate = connection.createStatement();
        ResultSet userName =
            stmtUser.executeQuery(
                "SELECT count(*) FROM Users WHERE username = '******'");
        ResultSet email =
            stmtEmail.executeQuery("SELECT count(*) FROM Users WHERE email = '" + data.email + "'");

        while (userName.next()) {
          if (userName.getInt(1) != 0) {
            code.UsernameTaken = true;
          }
        }
        while (email.next()) {
          if (email.getInt(1) != 0) {
            code.EmailTaken = true;
          }
        }
        String newPassword = data.password;
        String newConfirmPassword = data.confirmPassword;
        if (!newPassword.equals(newConfirmPassword)) {
          code.PasswordMismatch = true;
        }
        if (!code.UsernameTaken && !code.EmailTaken && !code.PasswordMismatch) {
          stmtInsert.execute(
              "Insert into Users (username, email, password) values ('"
                  + data.userName
                  + "','"
                  + data.email
                  + "','"
                  + data.password
                  + "')");
          stmtCharacterCreate.execute(
              "Insert into Characters (charactername, attack, maxhealth, currenthealth) values ('"
                  + data.userName
                  + "', 10, 10, 10)");
          ResultSet userData =
              stmtUserData.executeQuery(
                  "SELECT userID FROM users WHERE username = '******'");
          ResultSet characterData =
              stmtCharacterData.executeQuery(
                  "SELECT characterID FROM Characters WHERE charactername = '"
                      + data.userName
                      + "'");
          int userID = -1;
          int characterID = -1;
          while (userData.next()) {
            userID = userData.getInt(1);
          }
          while (characterData.next()) {
            characterID = characterData.getInt(1);
          }
          stmtCharacterRelate.execute(
              "Insert into userCharacters (userid, characterid) values ("
                  + userID
                  + ", "
                  + characterID
                  + ")");
        }
      }
    } catch (Exception e) {
      code.databaseError = true;
    }
    return code;
  }