Addition of BuyList task manager.

This commit is contained in:
MobiusDevelopment 2021-01-23 12:30:50 +00:00
parent f8d64089d8
commit b1ad584b76
4 changed files with 252 additions and 74 deletions

View File

@ -26,11 +26,11 @@ import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.l2jmobius.Config; import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory; import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.data.ItemTable; import org.l2jmobius.gameserver.data.ItemTable;
import org.l2jmobius.gameserver.model.StoreTradeList; import org.l2jmobius.gameserver.model.StoreTradeList;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance; import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
import org.l2jmobius.gameserver.taskmanager.BuyListTaskManager;
/** /**
* @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $ * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
@ -43,31 +43,6 @@ public class TradeController
private final Map<Integer, StoreTradeList> _lists; private final Map<Integer, StoreTradeList> _lists;
private final Map<Integer, StoreTradeList> _listsTaskItem; private final Map<Integer, StoreTradeList> _listsTaskItem;
/** Task launching the function for restore count of Item (Clan Hall) */
public class RestoreCount implements Runnable
{
private final int _timer;
public RestoreCount(int time)
{
_timer = time;
}
@Override
public void run()
{
try
{
restoreCount(_timer);
dataTimerSave(_timer);
ThreadPool.schedule(new RestoreCount(_timer), _timer * 60 * 60 * 1000);
}
catch (Throwable t)
{
}
}
}
protected TradeController() protected TradeController()
{ {
boolean limitedItem = false; boolean limitedItem = false;
@ -214,11 +189,11 @@ public class TradeController
savetimer = rset2.getLong("savetimer"); savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0) if ((savetimer - currentMillis) > 0)
{ {
ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis()); BuyListTaskManager.getInstance().addTime(time, savetimer);
} }
else else
{ {
ThreadPool.schedule(new RestoreCount(time), 0); BuyListTaskManager.getInstance().addTime(time, 0);
} }
} }
rset2.close(); rset2.close();
@ -375,11 +350,11 @@ public class TradeController
savetimer = rset2.getLong("savetimer"); savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0) if ((savetimer - currentMillis) > 0)
{ {
ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis()); BuyListTaskManager.getInstance().addTime(time, savetimer);
} }
else else
{ {
ThreadPool.schedule(new RestoreCount(time), 0); BuyListTaskManager.getInstance().addTime(time, 0);
} }
} }
rset2.close(); rset2.close();
@ -436,7 +411,7 @@ public class TradeController
return lists; return lists;
} }
protected void restoreCount(int time) public void restoreCount(int time)
{ {
if (_listsTaskItem == null) if (_listsTaskItem == null)
{ {
@ -449,12 +424,12 @@ public class TradeController
} }
} }
protected void dataTimerSave(int time) public void dataTimerSave(int time)
{ {
final long timerSave = System.currentTimeMillis() + (time * 60 * 60 * 1000); final long timerSave = System.currentTimeMillis() + (time * 60 * 60 * 1000);
try (Connection con = DatabaseFactory.getConnection()) try (Connection con = DatabaseFactory.getConnection())
{ {
final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer =? WHERE time =?"); final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer=? WHERE time=?");
statement.setLong(1, timerSave); statement.setLong(1, timerSave);
statement.setInt(2, time); statement.setInt(2, time);
statement.executeUpdate(); statement.executeUpdate();
@ -485,13 +460,13 @@ public class TradeController
} }
listId = list.getListId(); listId = list.getListId();
for (ItemInstance Item : list.getItems()) for (ItemInstance item : list.getItems())
{ {
if (Item.getCount() < Item.getInitCount()) // needed? if (item.getCount() < item.getInitCount()) // needed?
{ {
statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?"); statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
statement.setInt(1, Item.getCount()); statement.setInt(1, item.getCount());
statement.setInt(2, Item.getItemId()); statement.setInt(2, item.getItemId());
statement.setInt(3, listId); statement.setInt(3, listId);
statement.executeUpdate(); statement.executeUpdate();
statement.close(); statement.close();

View File

@ -0,0 +1,114 @@
/*
* 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 org.l2jmobius.gameserver.taskmanager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.gameserver.TradeController;
/**
* @author Mobius
*/
public class BuyListTaskManager
{
private static final Map<Integer, Long> REFRESH_TIME = new ConcurrentHashMap<>();
private static final List<Integer> PENDING_UPDATES = new ArrayList<>();
private static boolean _workingTimes = false;
private static boolean _workingSaves = false;
public BuyListTaskManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
if (_workingTimes)
{
return;
}
_workingTimes = true;
final long currentTime = System.currentTimeMillis();
for (Entry<Integer, Long> entry : REFRESH_TIME.entrySet())
{
if (currentTime > entry.getValue().longValue())
{
final Integer time = entry.getKey();
synchronized (PENDING_UPDATES)
{
PENDING_UPDATES.add(time);
}
REFRESH_TIME.put(time, currentTime + (time.intValue() * 60 * 60 * 1000L));
}
}
_workingTimes = false;
}, 1000, 60000);
ThreadPool.scheduleAtFixedRate(() ->
{
if (_workingSaves)
{
return;
}
_workingSaves = true;
if (!PENDING_UPDATES.isEmpty())
{
final Integer time;
synchronized (PENDING_UPDATES)
{
time = PENDING_UPDATES.get(0);
PENDING_UPDATES.remove(time);
}
TradeController.getInstance().restoreCount(time.intValue());
TradeController.getInstance().dataTimerSave(time.intValue());
}
_workingSaves = false;
}, 50, 50);
}
public void addTime(int time, long refreshTime)
{
if (refreshTime == 0)
{
synchronized (PENDING_UPDATES)
{
PENDING_UPDATES.add(time);
}
REFRESH_TIME.put(time, System.currentTimeMillis() + (time * 60 * 60 * 1000L));
}
else
{
REFRESH_TIME.put(time, refreshTime);
}
}
public static BuyListTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final BuyListTaskManager INSTANCE = new BuyListTaskManager();
}
}

View File

@ -26,11 +26,11 @@ import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.l2jmobius.Config; import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory; import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.data.ItemTable; import org.l2jmobius.gameserver.data.ItemTable;
import org.l2jmobius.gameserver.model.StoreTradeList; import org.l2jmobius.gameserver.model.StoreTradeList;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance; import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
import org.l2jmobius.gameserver.taskmanager.BuyListTaskManager;
/** /**
* @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $ * @version $Revision: 1.5.4.13 $ $Date: 2005/04/06 16:13:38 $
@ -43,31 +43,6 @@ public class TradeController
private final Map<Integer, StoreTradeList> _lists; private final Map<Integer, StoreTradeList> _lists;
private final Map<Integer, StoreTradeList> _listsTaskItem; private final Map<Integer, StoreTradeList> _listsTaskItem;
/** Task launching the function for restore count of Item (Clan Hall) */
public class RestoreCount implements Runnable
{
private final int _timer;
public RestoreCount(int time)
{
_timer = time;
}
@Override
public void run()
{
try
{
restoreCount(_timer);
dataTimerSave(_timer);
ThreadPool.schedule(new RestoreCount(_timer), _timer * 60 * 60 * 1000);
}
catch (Throwable t)
{
}
}
}
protected TradeController() protected TradeController()
{ {
boolean limitedItem = false; boolean limitedItem = false;
@ -214,11 +189,11 @@ public class TradeController
savetimer = rset2.getLong("savetimer"); savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0) if ((savetimer - currentMillis) > 0)
{ {
ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis()); BuyListTaskManager.getInstance().addTime(time, savetimer);
} }
else else
{ {
ThreadPool.schedule(new RestoreCount(time), 0); BuyListTaskManager.getInstance().addTime(time, 0);
} }
} }
rset2.close(); rset2.close();
@ -375,11 +350,11 @@ public class TradeController
savetimer = rset2.getLong("savetimer"); savetimer = rset2.getLong("savetimer");
if ((savetimer - currentMillis) > 0) if ((savetimer - currentMillis) > 0)
{ {
ThreadPool.schedule(new RestoreCount(time), savetimer - System.currentTimeMillis()); BuyListTaskManager.getInstance().addTime(time, savetimer);
} }
else else
{ {
ThreadPool.schedule(new RestoreCount(time), 0); BuyListTaskManager.getInstance().addTime(time, 0);
} }
} }
rset2.close(); rset2.close();
@ -436,7 +411,7 @@ public class TradeController
return lists; return lists;
} }
protected void restoreCount(int time) public void restoreCount(int time)
{ {
if (_listsTaskItem == null) if (_listsTaskItem == null)
{ {
@ -449,12 +424,12 @@ public class TradeController
} }
} }
protected void dataTimerSave(int time) public void dataTimerSave(int time)
{ {
final long timerSave = System.currentTimeMillis() + (time * 60 * 60 * 1000); final long timerSave = System.currentTimeMillis() + (time * 60 * 60 * 1000);
try (Connection con = DatabaseFactory.getConnection()) try (Connection con = DatabaseFactory.getConnection())
{ {
final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer =? WHERE time =?"); final PreparedStatement statement = con.prepareStatement("UPDATE merchant_buylists SET savetimer=? WHERE time=?");
statement.setLong(1, timerSave); statement.setLong(1, timerSave);
statement.setInt(2, time); statement.setInt(2, time);
statement.executeUpdate(); statement.executeUpdate();
@ -485,13 +460,13 @@ public class TradeController
} }
listId = list.getListId(); listId = list.getListId();
for (ItemInstance Item : list.getItems()) for (ItemInstance item : list.getItems())
{ {
if (Item.getCount() < Item.getInitCount()) // needed? if (item.getCount() < item.getInitCount()) // needed?
{ {
statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?"); statement = con.prepareStatement("UPDATE merchant_buylists SET currentCount=? WHERE item_id=? AND shop_id=?");
statement.setInt(1, Item.getCount()); statement.setInt(1, item.getCount());
statement.setInt(2, Item.getItemId()); statement.setInt(2, item.getItemId());
statement.setInt(3, listId); statement.setInt(3, listId);
statement.executeUpdate(); statement.executeUpdate();
statement.close(); statement.close();

View File

@ -0,0 +1,114 @@
/*
* 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 org.l2jmobius.gameserver.taskmanager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.gameserver.TradeController;
/**
* @author Mobius
*/
public class BuyListTaskManager
{
private static final Map<Integer, Long> REFRESH_TIME = new ConcurrentHashMap<>();
private static final List<Integer> PENDING_UPDATES = new ArrayList<>();
private static boolean _workingTimes = false;
private static boolean _workingSaves = false;
public BuyListTaskManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
if (_workingTimes)
{
return;
}
_workingTimes = true;
final long currentTime = System.currentTimeMillis();
for (Entry<Integer, Long> entry : REFRESH_TIME.entrySet())
{
if (currentTime > entry.getValue().longValue())
{
final Integer time = entry.getKey();
synchronized (PENDING_UPDATES)
{
PENDING_UPDATES.add(time);
}
REFRESH_TIME.put(time, currentTime + (time.intValue() * 60 * 60 * 1000L));
}
}
_workingTimes = false;
}, 1000, 60000);
ThreadPool.scheduleAtFixedRate(() ->
{
if (_workingSaves)
{
return;
}
_workingSaves = true;
if (!PENDING_UPDATES.isEmpty())
{
final Integer time;
synchronized (PENDING_UPDATES)
{
time = PENDING_UPDATES.get(0);
PENDING_UPDATES.remove(time);
}
TradeController.getInstance().restoreCount(time.intValue());
TradeController.getInstance().dataTimerSave(time.intValue());
}
_workingSaves = false;
}, 50, 50);
}
public void addTime(int time, long refreshTime)
{
if (refreshTime == 0)
{
synchronized (PENDING_UPDATES)
{
PENDING_UPDATES.add(time);
}
REFRESH_TIME.put(time, System.currentTimeMillis() + (time * 60 * 60 * 1000L));
}
else
{
REFRESH_TIME.put(time, refreshTime);
}
}
public static BuyListTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final BuyListTaskManager INSTANCE = new BuyListTaskManager();
}
}