Merged with released L2J-Unity files.

This commit is contained in:
mobiusdev
2016-06-12 01:34:09 +00:00
parent e003e87887
commit 635557f5da
18352 changed files with 3245113 additions and 2892959 deletions

View File

@ -1,89 +1,145 @@
/*
* This file is part of the L2J Mobius project.
*
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.taskmanager.tasks;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import com.l2jmobius.Config;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.gameserver.enums.MailType;
import com.l2jmobius.gameserver.instancemanager.MailManager;
import com.l2jmobius.gameserver.model.entity.Message;
import com.l2jmobius.gameserver.taskmanager.Task;
import com.l2jmobius.gameserver.taskmanager.TaskManager;
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
import com.l2jmobius.gameserver.taskmanager.TaskTypes;
/**
* Birthday Gift task.
* @author Zoey76
*/
public class TaskBirthday extends Task
{
private static final String NAME = "birthday";
/** 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_ADD(createDate, INTERVAL (YEAR(NOW()) - YEAR(createDate)) YEAR)) BETWEEN FROM_UNIXTIME(?) AND NOW())";
@Override
public String getName()
{
return NAME;
}
@Override
public void onTimeElapsed(ExecutedTask task)
{
_log.info("BirthdayManager: " + giveBirthdayGifts(task.getLastActivation()) + " gifts sent.");
}
private int giveBirthdayGifts(long lastActivation)
{
int birthdayGiftCount = 0;
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement(SELECT_PENDING_BIRTHDAY_GIFTS))
{
ps.setLong(1, TimeUnit.SECONDS.convert(lastActivation, TimeUnit.MILLISECONDS));
try (ResultSet rs = ps.executeQuery())
{
while (rs.next())
{
final String text = Config.ALT_BIRTHDAY_MAIL_TEXT.replaceAll("$c1", rs.getString("char_name")).replaceAll("$s1", Integer.toString(rs.getInt("age")));
final Message msg = new Message(rs.getInt("charId"), Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, MailType.BIRTHDAY);
msg.createAttachments().addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
MailManager.getInstance().sendMessage(msg);
birthdayGiftCount++;
}
}
}
catch (SQLException e)
{
_log.warning("Error checking birthdays: " + e.getMessage());
}
return birthdayGiftCount;
}
@Override
public void initializate()
{
super.initializate();
TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, "1", "06:30:00", "");
}
}
/*
* This file is part of the L2J Mobius project.
*
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.taskmanager.tasks;
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.l2jmobius.Config;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.gameserver.data.sql.impl.CharNameTable;
import com.l2jmobius.gameserver.enums.MailType;
import com.l2jmobius.gameserver.instancemanager.MailManager;
import com.l2jmobius.gameserver.model.entity.Message;
import com.l2jmobius.gameserver.model.itemcontainer.Mail;
import com.l2jmobius.gameserver.taskmanager.Task;
import com.l2jmobius.gameserver.taskmanager.TaskManager;
import com.l2jmobius.gameserver.taskmanager.TaskManager.ExecutedTask;
import com.l2jmobius.gameserver.taskmanager.TaskTypes;
import com.l2jmobius.gameserver.util.Util;
/**
* @author Nyaran
*/
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;
@Override
public String getName()
{
return NAME;
}
@Override
public void onTimeElapsed(ExecutedTask task)
{
final Calendar lastExecDate = Calendar.getInstance();
final long lastActivation = task.getLastActivation();
if (lastActivation > 0)
{
lastExecDate.setTimeInMillis(lastActivation);
}
final 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));
}
LOGGER.info("BirthdayManager: " + _count + " gifts sent. " + rangeDate);
}
private void checkBirthday(int year, int month, int day)
{
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement(QUERY))
{
statement.setString(1, "%-" + getNum(month + 1) + "-" + getNum(day));
try (ResultSet rset = statement.executeQuery())
{
while (rset.next())
{
final int playerId = rset.getInt("charId");
final Calendar createDate = Calendar.getInstance();
createDate.setTime(rset.getDate("createDate"));
final int age = year - createDate.get(Calendar.YEAR);
if (age <= 0)
{
continue;
}
String text = Config.ALT_BIRTHDAY_MAIL_TEXT;
if (text.contains("$c1"))
{
text = text.replace("$c1", CharNameTable.getInstance().getNameById(playerId));
}
if (text.contains("$s1"))
{
text = text.replace("$s1", String.valueOf(age));
}
final Message msg = new Message(playerId, Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, MailType.BIRTHDAY);
final Mail attachments = msg.createAttachments();
attachments.addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
MailManager.getInstance().sendMessage(msg);
_count++;
}
}
}
catch (SQLException e)
{
LOGGER.log(Level.WARNING, "Error checking birthdays. ", e);
}
// If character birthday is 29-Feb and year isn't leap, send gift on 28-feb
final 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);
}
@Override
public void initializate()
{
TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, "1", "06:30:00", "");
}
}