All pastes #392602 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-03-12 22:58 UTC
rendered paste body
/* 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;
    /**
     * 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 void resetQuotes()
    {
        java.sql.Connection con = null;
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con.prepareStatement("UPDATE quotes SET last = 0");
            statement.executeUpdate();
        } catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    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();
        } catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    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);
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        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(quote.contains("\n")) {
                        for(String newstring : quote.split("\n"))
                        {
                            player.sendMessage(newstring);
                        }
                    } else {
                        player.sendMessage(quote);
                    }
                }
            } else {
                System.out.println("Quote empty... problem.");
            }
        }
    }
    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().scheduleGeneralAtFixedRate(new MessageQuotes(), 0, Config.RANDOM_QUOTES_INTERVAL);
    }
}