Advertisement
Guest User

ShopHandler.java

a guest
Aug 7th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. package server.world;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8. import server.Config;
  9. import server.Server;
  10. import server.util.Misc;
  11.  
  12. /**
  13. * Shops
  14. **/
  15.  
  16. public class ShopHandler {
  17.  
  18.     public static int MaxShops = 101;
  19.     public static int MaxShopItems = 101;
  20.     public static int MaxInShopItems = 20;
  21.     public static int MaxShowDelay = 10;
  22.     public static int MaxSpecShowDelay = 60;
  23.     public static int TotalShops = 0;
  24.     public static int[][] ShopItems = new int[MaxShops][MaxShopItems];
  25.     public static int[][] ShopItemsN = new int[MaxShops][MaxShopItems];
  26.     public static int[][] ShopItemsDelay = new int[MaxShops][MaxShopItems];
  27.     public static int[][] ShopItemsSN = new int[MaxShops][MaxShopItems];
  28.     public static int[] ShopItemsStandard = new int[MaxShops];
  29.     public static String[] ShopName = new String[MaxShops];
  30.     public static int[] ShopSModifier = new int[MaxShops];
  31.     public static int[] ShopBModifier = new int[MaxShops];
  32.    
  33.     public ShopHandler() {
  34.         for(int i = 0; i < MaxShops; i++) {
  35.             for(int j = 0; j < MaxShopItems; j++) {
  36.                 ResetItem(i, j);
  37.                 ShopItemsSN[i][j] = 0;
  38.             }
  39.             ShopItemsStandard[i] = 0;
  40.             ShopSModifier[i] = 0;
  41.             ShopBModifier[i] = 0;
  42.             ShopName[i] = "";
  43.         }
  44.         TotalShops = 0;
  45.         loadShops("shops.cfg");
  46.     }
  47.  
  48.     public static void shophandler() {
  49.     Misc.println("Shop Handler class successfully loaded");
  50.     }
  51.    
  52.     public void process() {
  53.         boolean DidUpdate = false;
  54.         for(int i = 1; i <= TotalShops; i++) {
  55.             for(int j = 0; j < MaxShopItems; j++) {
  56.                 if (ShopItems[i][j] > 0) {
  57.                     if (ShopItemsDelay[i][j] >= MaxShowDelay) {
  58.                         if (j <= ShopItemsStandard[i] && ShopItemsN[i][j] <= ShopItemsSN[i][j]) {
  59.                             if (ShopItemsN[i][j] < ShopItemsSN[i][j]) {
  60.                                 ShopItemsN[i][j] += 1;
  61.                                 DidUpdate = true;
  62.                                 ShopItemsDelay[i][j] = 1;
  63.                                 ShopItemsDelay[i][j] = 0;
  64.                                 DidUpdate = true;
  65.                             }
  66.                         } else if (ShopItemsDelay[i][j] >= MaxSpecShowDelay) {
  67.                             DiscountItem(i, j);
  68.                             ShopItemsDelay[i][j] = 0;
  69.                             DidUpdate = true;
  70.                         }
  71.                     }
  72.                     ShopItemsDelay[i][j]++;
  73.                 }
  74.             }
  75.             if (DidUpdate == true) {
  76.                 for (int k = 1; k < Config.MAX_PLAYERS; k++) {
  77.                     if (Server.playerHandler.players[k] != null) {
  78.                         if (Server.playerHandler.players[k].isShopping == true && Server.playerHandler.players[k].myShopId == i) {
  79.                             Server.playerHandler.players[k].updateShop = true;
  80.                             DidUpdate =false;
  81.                             Server.playerHandler.players[k].updateshop(i);
  82.                         }
  83.                     }
  84.                 }
  85.                 DidUpdate = false;
  86.             }
  87.         }
  88.     }
  89.  
  90.     public void DiscountItem(int ShopID, int ArrayID) {
  91.         ShopItemsN[ShopID][ArrayID] -= 1;
  92.         if (ShopItemsN[ShopID][ArrayID] <= 0) {
  93.             ShopItemsN[ShopID][ArrayID] = 0;
  94.             ResetItem(ShopID, ArrayID);
  95.         }
  96.     }
  97.    
  98.     public void ResetItem(int ShopID, int ArrayID) {
  99.         ShopItems[ShopID][ArrayID] = 0;
  100.         ShopItemsN[ShopID][ArrayID] = 0;
  101.         ShopItemsDelay[ShopID][ArrayID] = 0;
  102.     }
  103.  
  104.     public boolean loadShops(String FileName) {
  105.         String line = "";
  106.         String token = "";
  107.         String token2 = "";
  108.         String token2_2 = "";
  109.         String[] token3 = new String[(MaxShopItems * 2)];
  110.         boolean EndOfFile = false;
  111.         int ReadMode = 0;
  112.         BufferedReader characterfile = null;
  113.         try {
  114.             characterfile = new BufferedReader(new FileReader("./Data/CFG/"+FileName));
  115.         } catch(FileNotFoundException fileex) {
  116.             Misc.println(FileName+": file not found.");
  117.             return false;
  118.         }
  119.         try {
  120.             line = characterfile.readLine();
  121.         } catch(IOException ioexception) {
  122.             Misc.println(FileName+": error loading file.");
  123.             return false;
  124.         }
  125.         while(EndOfFile == false && line != null) {
  126.             line = line.trim();
  127.             int spot = line.indexOf("=");
  128.             if (spot > -1) {
  129.                 token = line.substring(0, spot);
  130.                 token = token.trim();
  131.                 token2 = line.substring(spot + 1);
  132.                 token2 = token2.trim();
  133.                 token2_2 = token2.replaceAll("\t\t", "\t");
  134.                 token2_2 = token2_2.replaceAll("\t\t", "\t");
  135.                 token2_2 = token2_2.replaceAll("\t\t", "\t");
  136.                 token2_2 = token2_2.replaceAll("\t\t", "\t");
  137.                 token2_2 = token2_2.replaceAll("\t\t", "\t");
  138.                 token3 = token2_2.split("\t");
  139.                 if (token.equals("shop")) {
  140.                     int ShopID = Integer.parseInt(token3[0]);
  141.                     ShopName[ShopID] = token3[1].replaceAll("_", " ");
  142.                     ShopSModifier[ShopID] = Integer.parseInt(token3[2]);
  143.                     ShopBModifier[ShopID] = Integer.parseInt(token3[3]);
  144.                     for (int i = 0; i < ((token3.length - 4) / 2); i++) {
  145.                         if (token3[(4 + (i * 2))] != null) {
  146.                             ShopItems[ShopID][i] = (Integer.parseInt(token3[(4 + (i * 2))]) + 1);
  147.                             ShopItemsN[ShopID][i] = Integer.parseInt(token3[(5 + (i * 2))]);
  148.                             ShopItemsSN[ShopID][i] = Integer.parseInt(token3[(5 + (i * 2))]);
  149.                             ShopItemsStandard[ShopID]++;
  150.                         } else {
  151.                             break;
  152.                         }
  153.                     }
  154.                     TotalShops++;
  155.                 }
  156.             } else {
  157.                 if (line.equals("[ENDOFSHOPLIST]")) {
  158.                     try { characterfile.close(); } catch(IOException ioexception) { }
  159.                     return true;
  160.                 }
  161.             }
  162.             try {
  163.                 line = characterfile.readLine();
  164.             } catch(IOException ioexception1) { EndOfFile = true; }
  165.         }
  166.         try { characterfile.close(); } catch(IOException ioexception) { }
  167.         return false;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement