|
@@ -22,31 +22,26 @@ import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.GregorianCalendar;
|
|
|
-import java.util.logging.Level;
|
|
|
|
|
|
import com.l2jserver.Config;
|
|
|
import com.l2jserver.L2DatabaseFactory;
|
|
|
-import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
|
|
|
import com.l2jserver.gameserver.instancemanager.MailManager;
|
|
|
import com.l2jserver.gameserver.model.entity.Message;
|
|
|
-import com.l2jserver.gameserver.model.itemcontainer.Mail;
|
|
|
import com.l2jserver.gameserver.taskmanager.Task;
|
|
|
import com.l2jserver.gameserver.taskmanager.TaskManager;
|
|
|
import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;
|
|
|
import com.l2jserver.gameserver.taskmanager.TaskTypes;
|
|
|
-import com.l2jserver.gameserver.util.Util;
|
|
|
|
|
|
/**
|
|
|
- * @author Nyaran
|
|
|
+ * Birthday Gift task.
|
|
|
+ * @author Zoey76
|
|
|
*/
|
|
|
public class TaskBirthday extends Task
|
|
|
{
|
|
|
private static final String NAME = "birthday";
|
|
|
- private static final String QUERY = "SELECT charId, createDate FROM characters WHERE createDate LIKE ?";
|
|
|
- private static final Calendar _today = Calendar.getInstance();
|
|
|
- private int _count = 0;
|
|
|
+ /** Get all players that have had a birthday since last check. */
|
|
|
+ private static final String SELECT_PENDING_BIRTHDAY_GIFTS = "SELECT charId, char_name, createDate, (YEAR(NOW()) - YEAR(createDate)) AS age " //
|
|
|
+ + "FROM characters WHERE (YEAR(NOW()) - YEAR(createDate) > 0) AND (DATE_FORMAT(createDate, '%m-%d') > DATE_FORMAT(FROM_UNIXTIME(?), '%m-%d'))";
|
|
|
|
|
|
@Override
|
|
|
public String getName()
|
|
@@ -57,85 +52,38 @@ public class TaskBirthday extends Task
|
|
|
@Override
|
|
|
public void onTimeElapsed(ExecutedTask task)
|
|
|
{
|
|
|
- Calendar lastExecDate = Calendar.getInstance();
|
|
|
- long lastActivation = task.getLastActivation();
|
|
|
+ final int birthdayGiftCount = giveBirthdayGifts(task.getLastActivation());
|
|
|
|
|
|
- if (lastActivation > 0)
|
|
|
- {
|
|
|
- lastExecDate.setTimeInMillis(lastActivation);
|
|
|
- }
|
|
|
-
|
|
|
- String rangeDate = "[" + Util.getDateString(lastExecDate.getTime()) + "] - [" + Util.getDateString(_today.getTime()) + "]";
|
|
|
-
|
|
|
- for (; !_today.before(lastExecDate); lastExecDate.add(Calendar.DATE, 1))
|
|
|
- {
|
|
|
- checkBirthday(lastExecDate.get(Calendar.YEAR), lastExecDate.get(Calendar.MONTH), lastExecDate.get(Calendar.DATE));
|
|
|
- }
|
|
|
-
|
|
|
- _log.info("BirthdayManager: " + _count + " gifts sent. " + rangeDate);
|
|
|
+ _log.info("BirthdayManager: " + birthdayGiftCount + " gifts sent.");
|
|
|
}
|
|
|
|
|
|
- private void checkBirthday(int year, int month, int day)
|
|
|
+ private int giveBirthdayGifts(long lastActivation)
|
|
|
{
|
|
|
+ int birthdayGiftCount = 0;
|
|
|
try (Connection con = L2DatabaseFactory.getInstance().getConnection();
|
|
|
- PreparedStatement statement = con.prepareStatement(QUERY))
|
|
|
+ PreparedStatement ps = con.prepareStatement(SELECT_PENDING_BIRTHDAY_GIFTS))
|
|
|
{
|
|
|
- statement.setString(1, "%-" + getNum(month + 1) + "-" + getNum(day));
|
|
|
- try (ResultSet rset = statement.executeQuery())
|
|
|
+ ps.setLong(1, lastActivation);
|
|
|
+ try (ResultSet rs = ps.executeQuery())
|
|
|
{
|
|
|
- while (rset.next())
|
|
|
+ while (rs.next())
|
|
|
{
|
|
|
- int playerId = rset.getInt("charId");
|
|
|
- Calendar createDate = Calendar.getInstance();
|
|
|
- createDate.setTime(rset.getDate("createDate"));
|
|
|
-
|
|
|
- int age = year - createDate.get(Calendar.YEAR);
|
|
|
- if (age <= 0)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
String text = Config.ALT_BIRTHDAY_MAIL_TEXT;
|
|
|
+ text = text.replaceAll("$c1", rs.getString("char_name"));
|
|
|
+ text = text.replaceAll("$s1", Integer.toString(rs.getInt("age")));
|
|
|
|
|
|
- if (text.contains("$c1"))
|
|
|
- {
|
|
|
- text = text.replace("$c1", CharNameTable.getInstance().getNameById(playerId));
|
|
|
- }
|
|
|
- if (text.contains("$s1"))
|
|
|
- {
|
|
|
- text = text.replace("$s1", String.valueOf(age));
|
|
|
- }
|
|
|
-
|
|
|
- Message msg = new Message(playerId, Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, Message.SendBySystem.ALEGRIA);
|
|
|
-
|
|
|
- Mail attachments = msg.createAttachments();
|
|
|
- attachments.addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
|
|
|
-
|
|
|
+ final Message msg = new Message(rs.getInt("charId"), Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, Message.SendBySystem.ALEGRIA);
|
|
|
+ msg.createAttachments().addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
|
|
|
MailManager.getInstance().sendMessage(msg);
|
|
|
- _count++;
|
|
|
+ birthdayGiftCount++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (SQLException e)
|
|
|
{
|
|
|
- _log.log(Level.WARNING, "Error checking birthdays. ", e);
|
|
|
+ _log.warning("Error checking birthdays: " + e.getMessage());
|
|
|
}
|
|
|
-
|
|
|
- // If character birthday is 29-Feb and year isn't leap, send gift on 28-feb
|
|
|
- GregorianCalendar calendar = new GregorianCalendar();
|
|
|
- if ((month == Calendar.FEBRUARY) && (day == 28) && !calendar.isLeapYear(_today.get(Calendar.YEAR)))
|
|
|
- {
|
|
|
- checkBirthday(year, Calendar.FEBRUARY, 29);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param num the number to format.
|
|
|
- * @return the formatted number starting with a 0 if it is lower or equal than 10.
|
|
|
- */
|
|
|
- private String getNum(int num)
|
|
|
- {
|
|
|
- return (num <= 9) ? "0" + num : String.valueOf(num);
|
|
|
+ return birthdayGiftCount;
|
|
|
}
|
|
|
|
|
|
@Override
|