Advertisement
Guest User

RankManager

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.23 KB | None | 0 0
  1. package com.holojava.managers;
  2.  
  3. import com.holojava.core.console.Output;
  4. import com.holojava.database.DatabaseManipulation;
  5.  
  6. import java.util.Hashtable;
  7.  
  8. /**
  9.  * Summary: Provides information about the various user ranks/levels.
  10.  *          Also provides information for game ranks, games such as 'BattleBall' and 'SnowStorm'.
  11.  */
  12. public class RankManager {
  13.     private static Hashtable userRanks;
  14.     private GameRank[] gameRanksBB;
  15.     private GameRank[] gameRanksSS;
  16.  
  17.     private DatabaseManipulation dbManipulate;
  18.     private Output output;
  19.  
  20.     /**
  21.      * Summary: Initializes an instance of RankManager and makes calls to initialize UserRanks, BattleBallRanks,
  22.      *          and SnowStormRanks.
  23.      */
  24.     public RankManager() {
  25.         dbManipulate = new DatabaseManipulation();
  26.         output = new Output();
  27.  
  28.         initUserRanks();
  29.         initBattleBallRanks();
  30.         initSnowStormRanks();
  31.     }
  32.  
  33.     /**
  34.      * Summary - Initializes the set amount of UserRanks from table system_config in the database.
  35.      */
  36.     private void initUserRanks() {
  37.         // Initializes all fuse rights for the set user ranks.
  38.         output.writeLine("Initializing user rank fuserights...");
  39.  
  40.         int fuseMaxRank = Integer.parseInt(dbManipulate.runRead
  41.                 ("SELECT sval FROM system_config WHERE skey = '" + "fuse_rank_amount" + "'"));
  42.        
  43.         userRanks = new Hashtable();
  44.  
  45.         for (byte i = 1; i <= fuseMaxRank; i++) {
  46.             userRanks.put(i, new UserRank(i));
  47.         }
  48.  
  49.         output.writeLine("Fuserights for " + fuseMaxRank + " ranks loaded.");
  50.         System.out.println();
  51.     }
  52.  
  53.     /**
  54.      * Summary - Initializes the set GameRanks, from table games_ranks in the database, for BattleBall.
  55.      */
  56.     private void initBattleBallRanks() {
  57.         output.writeLine("Initializing BattleBall ranks...");
  58.  
  59.         // Sets GameRanks for BattleBall.
  60.         String[] titles = dbManipulate.runReadColumn
  61.                 ("SELECT title FROM games_ranks WHERE type = 'bb' ORDER BY id ASC", 0);
  62.         Integer[] minPoints = dbManipulate.runReadColumn
  63.                 ("SELECT minpoints FROM games_ranks WHERE type = 'bb' ORDER BY id ASC", 0, null);
  64.         Integer[] maxPoints = dbManipulate.runReadColumn
  65.                 ("SELECT maxpoints FROM games_ranks WHERE type = 'bb' ORDER BY id ASC", 0, null);
  66.  
  67.         this.gameRanksBB = new GameRank[titles.length];
  68.  
  69.         for (int i = 0; i < this.gameRanksBB.length; i++) {
  70.             this.gameRanksBB[i] = new GameRank(titles[i], minPoints[i], maxPoints[i]);
  71.             output.writeLine("Loaded gamerank '" + titles[i] + "' [" + minPoints[i] + "-" + maxPoints[i] +
  72.                     "] for game 'BattleBall'.");
  73.         }
  74.  
  75.         output.writeLine("Loaded " + titles.length + " ranks for game 'BattleBall'.");
  76.     }
  77.  
  78.     /**
  79.      * Summary - Initializes the set GameRanks, from table games_ranks in the database, for SnowStorm.
  80.      */
  81.     private void initSnowStormRanks() {
  82.         output.writeLine("Initializing SnowStorm ranks...");
  83.  
  84.         // Sets GameRanks for SnowStorm.
  85.         String[] titles = dbManipulate.runReadColumn
  86.                 ("SELECT title FROM games_ranks WHERE type = 'ss' ORDER BY id ASC", 0);
  87.         Integer[] minPoints = dbManipulate.runReadColumn
  88.                 ("SELECT minpoints FROM games_ranks WHERE type = 'ss' ORDER BY id ASC", 0, null);
  89.         Integer[] maxPoints = dbManipulate.runReadColumn
  90.                 ("SELECT maxpoints FROM games_ranks WHERE type = 'ss' ORDER BY id ASC", 0, null);
  91.  
  92.         this.gameRanksSS = new GameRank[titles.length];
  93.  
  94.         for (int i = 0; i < this.gameRanksSS.length; i++) {
  95.             this.gameRanksSS[i] = new GameRank(titles[i], minPoints[i], maxPoints[i]);
  96.             output.writeLine("Loaded gamerank '" + titles[i] + "' [" + minPoints[i] + "-" + maxPoints[i] +
  97.                     "] for game 'SnowStorm'.");
  98.         }
  99.  
  100.         output.writeLine("Loaded " + titles.length + " ranks for game 'SnowStorm'.");
  101.     }
  102.  
  103.     /**
  104.      * Summary: Returns the fuse rights, as a string, for a certain user rank.
  105.      * @param rankID - The ID of the user rank.
  106.      * @return - The string of fuserights that a certain user rank possesses.
  107.      */
  108.     public String getFuseRights(byte rankID) {
  109.         String[] fuseRights = ((UserRank)userRanks.get(rankID)).fuseRights ;
  110.         StringBuilder sBuilder = new StringBuilder();
  111.  
  112.         for (String right : fuseRights) {
  113.             sBuilder.append(right);
  114.         }
  115.  
  116.         /*
  117.         for (int i = 0; i < fuseRights.length; i++) {
  118.             sBuilder.append(fuseRights[i] + '2');
  119.         }
  120.         */
  121.  
  122.         return sBuilder.toString();
  123.     }
  124.  
  125.     /**
  126.      * Summary: Returns a bool that indicates if a certain user rank contains a certain fuseright.
  127.      * @param rankID - The ID of the user rank.
  128.      * @param fuseRight - The fuseright to look for.
  129.      * @return - True/False, depending on if the intended user rank possesed the fuseRight.
  130.      */
  131.     public boolean containsRight(byte rankID, String fuseRight) {
  132.         UserRank objRank = ((UserRank)userRanks.get(rankID));
  133.  
  134.         for (int i = 0; i < objRank.fuseRights.length; i++) {
  135.             if (objRank.fuseRights[i].equals(fuseRight)) {
  136.                 return true;
  137.             }
  138.         }
  139.  
  140.         return false;
  141.     }
  142.  
  143.     /**
  144.      * Summary: Returns the game rank title as a string for a certain game type ('BattleBall' or 'SnowStorm')
  145.      * @param isBattleBall - True/False, if the game type is BattleBall or SnowStorm, respectively.
  146.      * @param title - The title to get the rank for.
  147.      * @return - The rank with the target title, or 'holo.cast.gamerank.null'.
  148.      */
  149.     public GameRank getGameRank(boolean isBattleBall, String title) {
  150.         GameRank[] ranks;
  151.  
  152.         if (isBattleBall) {
  153.             ranks = gameRanksBB.clone();
  154.         } else {
  155.             ranks = gameRanksSS.clone();
  156.         }
  157.  
  158.         for (GameRank rank : ranks) {
  159.             if (rank.title.equals(title)) {
  160.                 return rank;
  161.             }
  162.         }
  163.  
  164.         // No game rank found with that title.
  165.         return new GameRank("holo.cast.gamerank.null", 0, 0);
  166.     }
  167.  
  168.  
  169.     /**
  170.      * Summary: Returns the game rank title as a string for a certain game type ('BattleBall' or 'SnowStorm') and score.
  171.      * @param isBattleBall - True/False, if the game type is BattleBall or SnowStorm, respectively.
  172.      * @param score - The score to get the rank for.
  173.      * @return - The rank with the target score, or 'holo.cast.gamerank.null'.
  174.      */
  175.     public String getGameRankTitle(boolean isBattleBall, int score) {
  176.         GameRank[] ranks;
  177.  
  178.         if (isBattleBall) {
  179.             ranks = gameRanksBB.clone();
  180.         } else {
  181.             ranks = gameRanksSS.clone();
  182.         }
  183.  
  184.         for (GameRank rank : ranks) {
  185.             if (score >= rank.minPoints && (rank.maxPoints == 0 || score <= rank.maxPoints)) {
  186.                 return rank.title;
  187.             }
  188.         }
  189.  
  190.         // No game rank with that score.
  191.         return "holo.cast.gamerank.null";
  192.     }
  193.  
  194.  
  195.     /**
  196.      * Summary: Represents a user rank.
  197.      */
  198.     private class UserRank {
  199.         String[] fuseRights;
  200.  
  201.         /**
  202.          * Summary: Initializes a UserRank.
  203.          * @param rankID - The ID of the rank to initialize.
  204.          */
  205.         UserRank(byte rankID) {
  206.             this.fuseRights = dbManipulate.runReadColumn
  207.                     ("SELECT fuseright FROM system_fuserights WHERE minrank <= " + rankID + "", 0);
  208.         }
  209.     }
  210.  
  211.     /**
  212.      * Summary: Represents a game rank, containing the min and max score and the rank name.
  213.      */
  214.     private class GameRank {
  215.         int minPoints;
  216.         int maxPoints;
  217.         String title;
  218.  
  219.         /**
  220.          * Summary: Initializes the GameRank with given values.
  221.          * @param title - The title of this rank.
  222.          * @param minPoints - The minimum amount of points needed to be in this rank.
  223.          * @param maxPoints - The maximum amount of points one can have to be in this rank.
  224.          */
  225.         GameRank(String title, int minPoints, int maxPoints) {
  226.             this.title = title;
  227.             this.minPoints = minPoints;
  228.             this.maxPoints = maxPoints;
  229.         }
  230.     }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement