/* This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * http://www.gnu.org/copyleft/gpl.html */ package net.sf.l2j.gameserver.quote; import net.sf.l2j.Config; import net.sf.l2j.L2DatabaseFactory; import net.sf.l2j.gameserver.ThreadPoolManager; import net.sf.l2j.gameserver.model.L2World; import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance; import java.sql.*; /** * This class pulls a random quote out of a database. * Make sure you have some quotes in * the SQL table "quotes" * Don't bother with the id field, it's unused * (primary key) * @version $Revision: 1 $Date: March 12 2007 * @author AresXP */ public class QuotesHandler { private static QuotesHandler _instance; /** * Resets quote status to 0... (not last) */ public static void resetQuotes() { java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("UPDATE quotes SET last = 0"); statement.executeUpdate(); statement.close(); } catch(Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch(Exception e) { } } } /** * Sets last quote used to prevent * same quoted to be used twice in * a row. * @param int id (quote id) */ public static void setLast(int id) { java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("UPDATE quotes SET last = 1 WHERE id = ?"); statement.setInt(1, id); statement.executeUpdate(); statement.close(); } catch(Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch(Exception e) { } } } /** * Grabs a quote randomly from the database. * Make sure to have more than one quote * or it'll always select the same one. * (>15-20 should be good) * @return String quote */ public static String GrabRandomQuote() { java.sql.Connection con = null; String quote = null; int id = 0; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("SELECT id, quote FROM quotes WHERE last = 0 ORDER BY RAND() LIMIT 1"); ResultSet rset = statement.executeQuery(); while(rset.next()) { quote = rset.getString("quote"); id = rset.getInt("id"); } resetQuotes(); setLast(id); statement.close(); } catch(Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch(Exception e) { } } return quote; } class MessageQuotes implements Runnable { public String quote = null; /** * Heart and soul of this class * Sends a message to all logged in players * with the selected quote. * @see java.lang.Runnable#run() */ public void run() { // grab the quote from db quote = QuotesHandler.GrabRandomQuote(); if(quote.length() > 0) //make sure there's something to send { for(L2PcInstance player : L2World.getInstance().getAllPlayers()) { //if there's a newline, send a message for each line. if(quote.contains("\n")) { for(String newstring : quote.split("\n")) { player.sendMessage(newstring); } } else { player.sendMessage(quote); } } } else { System.out.println("Quote empty... problem."); } ThreadPoolManager.getInstance().scheduleGeneral(new MessageQuotes(), Config.RANDOM_QUOTES_INTERVAL); } } public static QuotesHandler getInstance() { if ( _instance == null ) _instance = new QuotesHandler(); return _instance; } private QuotesHandler() { // schedule this to run every 20 mins by default (1200000 msec) // to determine msecs --> min*60000 (20*60000 = 1200000) ThreadPoolManager.getInstance().scheduleGeneral(new MessageQuotes(), Config.RANDOM_QUOTES_INTERVAL); } }