Experienced Elementalist
- Joined
- Mar 12, 2015
- Messages
- 238
- Reaction score
- 43
So I was making a thread for a release.
I put some code in
I put some code in
PHP:
tags and suddenly this happened:
[php]/* * Bingo Event for MapleStory! * (ragezone-link) */package net.custom;
import client.MapleCharacter;import client.MapleClient;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Arrays;import java.util.HashMap;import java.util.LinkedHashSet;import java.util.Map;import java.util.Random;import java.util.Set;import java.util.concurrent.ScheduledFuture;import java.util.logging.Level;import java.util.logging.Logger;import net.server.Server;import server.TimerManager;import tools.DatabaseConnection;import tools.MaplePacketCreator;
/** * * @author Las Systos */public class BingoHandler {
private static BingoHandler instance = null; private final Map<Integer, Boolean> bingoCalls = new HashMap<>(); private ScheduledFuture<?> bingoTimer; private final int interval = 1; // interval between new bingo numbers in minutes
/** * The constructor for BingoHandler, can only be 'constructed' once due to * getInstance method. loadBingoCalls method should only be used once on * server startup, for updating bingoCalls use updateBingoCalls method. */ public BingoHandler() { loadBingoCalls(); bingoTimer(); }
/** * This method makes a new BingoHandler just once, and always returns this * bingo handler. (leeched from Server.java) * * [USER=850422]return[/USER] The BingoHandler Object */ public static BingoHandler getInstance() { if (instance == null) { instance = new BingoHandler(); } return instance; }
/** * The makeBingoCard method generates 24 random non-recurring numbers and * updates the new bingo card into the database. * * [USER=2000183830]para[/USER]m c - MapleClient */ public static void makeBingoCard(MapleClient c) { // generating numbers int[] arr = new int[24]; Random rand = new Random(); Set<Integer> set = new LinkedHashSet<>(); while (set.size() < arr.length) { set.add(rand.nextInt(89) + 10); } int pos = 0; for (Integer v : set) { arr[pos++] = v; } String numbers = Arrays.toString(arr); numbers = numbers.substring(1, numbers.length() - 1);
// adding generated numbers to db (too lazy to make 2 seperate methods for this) try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE `characters` SET `bingocard` = ? WHERE `name` = ?")) { ps.setString(1, numbers); ps.setString(2, c.getPlayer().getName()); ps.execute(); } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); }
// setting 'havecard' to true c.getPlayer().setCard(true);
// adding card to MapleCharacter loadCard(c); }
/** * The readBingoCard method returns all the bingo numbers from the players * card that are stored in the database, the check if someone has a bingo * card occurs in the script! * * [USER=2000183830]para[/USER]m c - MapleClient * [USER=850422]return[/USER] BingoCard numbers */ private static String readBingoCard(MapleClient c) { try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT `bingocard` FROM `characters` WHERE `name` = ?")) { ps.setString(1, c.getPlayer().getName()); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { String card = rs.getString("bingocard"); return card; } } } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); } return null; }
/** * Method to check if the player has a bingo card. * * [USER=2000183830]para[/USER]m c MapleClient * [USER=850422]return[/USER] True or False whether the player has a card or not */ public static boolean hasCard(MapleClient c) { return readBingoCard(c) != null; }
/** * Stores the bingo card in MapleCharacter * * [USER=2000183830]para[/USER]m c - MapleClient */ public static void loadCard(MapleClient c) { if (readBingoCard(c) != null) { String[] numbers_string = readBingoCard(c).split(", "); int[] numbers_int = new int[24]; for (int i = 0; i < numbers_string.length; i++) { numbers_int[i] = Integer.valueOf(numbers_string[i]); } c.getPlayer().fillBingoNumbers(numbers_int); } }
/** * This method makes the bingo card readable for players. * * [USER=2000183830]para[/USER]m c - MapleClient * [USER=850422]return[/USER] The String that shows players their bingo card */ public static String convertCard(MapleClient c) { int[] playerNumbers = c.getPlayer().getBingoNumbers(); String[] redNumbers = {"#i3990009#", "#i3990000#", "#i3990001#", "#i3990002#", "#i3990003#", "#i3990004#", "#i3990005#", "#i3990006#", "#i3990007#", "#i3990008#"}; // I can't find a better way to do this :( String[] greenNumbers = {"#i3990019#", "#i3990010#", "#i3990011#", "#i3990012#", "#i3990013#", "#i3990014#", "#i3990015#", "#i3990016#", "#i3990017#", "#i3990018#"}; // ...
String str = new String(); // new String(); looks fancy, don't hate :) int i = 0; for (int number : playerNumbers) { i++; int first = number / 10; // can't be 0 int second = number % 10; // I'm completely improvising at this point T_T if (BingoHandler.getInstance().isCalled(number)) { // called = green str += greenNumbers[first]; str += greenNumbers[second]; } else { // not called = red str += redNumbers[first]; str += redNumbers[second]; } str += "\t"; if (i == 12) { // the 2 ++ (empty space in the middle) str += "#i3990022##i3990022#\t"; } } return str; }
/** * Checks if the player has a full card or not. * * [USER=2000183830]para[/USER]m c - MapleClient * [USER=850422]return[/USER] True or False whether you have a full card or not. */ public static boolean checkFullCard(MapleClient c) { int[] playerNumbers = c.getPlayer().getBingoNumbers(); for (int number : playerNumbers) { if (!BingoHandler.getInstance().isCalled(number)) { // as soon as it sees a number that is not true, it can't be a full card anymore return false; // no full card } } return true; // full card }
/** * A method that reads all bingo numbers from the database and puts them * into a HashMap. */ private void loadBingoCalls() { for (int i = 10; i < 100; i++) { bingoCalls.put(i, isCalledDB(i)); System.out.println(i + ": " + isCalledDB(i)); } }
/** * This method updates the bingoCalls HashMap and the database. * * [USER=2000183830]para[/USER]m number the number to change * [USER=2000183830]para[/USER]m call set the number to true or false, since people are retarded I * build a failsafe */ public void updateBingoCalls(int number, boolean call) { if (bingoCalls.replace(number, isCalled(number), call)) { try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE `bingo` SET `called` = ? WHERE `number` = ?")) { ps.setBoolean(1, call); ps.setInt(2, number); ps.execute(); } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("Derp derp, derpy dy derp"); } }
/** * Reads the number directly from the database. * * [USER=2000183830]para[/USER]m number - The number that the database has to read * [USER=850422]return[/USER] Boolean whether the specified number is called or not */ private boolean isCalledDB(int number) { try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT `called` FROM `bingo` WHERE `number` = ?")) { ps.setInt(1, number); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { return rs.getBoolean("called"); } } } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); } return false; // ??? }
/** * A method to see if a certain number is called or not. * * [USER=2000183830]para[/USER]m number - The number to read from the HashMap * [USER=850422]return[/USER] Boolean whether the specified number is called or not */ private boolean isCalled(int number) { return bingoCalls.get(number); }
/** * Method to reset the bingo event with the option to reset player bingo * cards * * [USER=2000183830]para[/USER]m playerCards True if you want to reset bingo cards */ public void resetBingo(boolean playerCards) { for (int i = 10; i < 100; i++) { bingoCalls.put(i, false); // there has to be an easier way to do this? }
try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE `bingo` SET `called` = 0")) { ps.execute(); } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); }
if (playerCards) { try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("UPDATE `characters` SET `bingocard` = ?")) { ps.setString(1, null); ps.execute(); } catch (SQLException ex) { Logger.getLogger(BingoHandler.class.getName()).log(Level.SEVERE, null, ex); }
for (MapleCharacter chr : Server.getInstance().getWorld(0).getPlayerStorage().getAllCharacters()) { // if you want multiworld support then you can probably code this yourself chr.setCard(false); chr.fillBingoNumbers(null); // Technically this isn't required, but I'm not 100% sure... } } }
public void startBingoTimer() { bingoTimer(); }
public void destroyBingoTimer() { if (bingoTimer != null) { bingoTimer.cancel(false); bingoTimer = null; } else { System.out.println("Some crappy GM tried to stop a non-existing bingo timer. Fire him."); } }
private void bingoTimer() { if (bingoTimer == null) { bingoTimer = TimerManager.getInstance().register(() -> { if (this != null) { // huh Random rand = new Random(); boolean called = false; int number = 0; // java is too stupid to see that number will always be initialized while (!called) { number = rand.nextInt(89) + 10; called = !isCalled(number); } updateBingoCalls(number, true); Server.getInstance().broadcastMessage(MaplePacketCreator.serverNotice(6, "[Bingo] Number " + number + " has been called!")); } else { System.out.println("If you are seeing this you are either high or you did something really weird."); } }, interval * 1000 * 60, interval * 1000 * 60); } else { System.out.println("Some crappy GM tried to run 2 bingo timers at once. Fire him."); } }}
Code:
tags look so dull and are kinda hard to read, any solution?