테스트

aion-server 4.8

Gitteol
최고관리자 · 1 · 💬 0 클론/새로받기
 4.8 61f661d · 1 commits 새로받기(Pull)
game-server/src/com/aionemu/gameserver/taskmanager/tasks/housing/AuctionEndTask.java
package com.aionemu.gameserver.taskmanager.tasks.housing;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import com.aionemu.gameserver.configs.main.HousingConfig;
import com.aionemu.gameserver.services.HousingBidService;
import com.aionemu.gameserver.taskmanager.AbstractCronTask;
import com.aionemu.gameserver.utils.ThreadPoolManager;

/**
 * Handles housing auction end and potential prolongations if there are new bids just before auction end.
 * 
 * @author Neon
 */
public class AuctionEndTask extends AbstractCronTask {

	private static final long PROLONGATION_MILLIS = TimeUnit.MINUTES.toMillis(5);
	private static final long MAX_PROLONGATION_MILLIS = TimeUnit.MINUTES.toMillis(30);
	private static final AuctionEndTask instance = new AuctionEndTask();
	private Map<Integer, ProlongedAuction> prolongedAuctions = new ConcurrentHashMap<>();

	public static AuctionEndTask getInstance() {
		return instance;
	}

	private AuctionEndTask() {
		super(HousingConfig.HOUSE_AUCTION_END_TIME);
	}

	@Override
	protected boolean shouldRunOnStart() {
		if (super.shouldRunOnStart()) // true if the server was down when auctions should have ended (SERVER_STOP_MILLIS < lastPlannedRun)
			return true;
		if (SERVER_STOP_MILLIS != null) // trigger auction end if the server shut down in the prolongation time frame (30min after regular auction end)
			return SERVER_STOP_MILLIS - getLastPlannedRun().getTime() <= MAX_PROLONGATION_MILLIS;
		return false;
	}

	@Override
	protected void executeTask() {
		HousingBidService.getInstance().endAuctions();
	}

	public int getRemainingAuctionSeconds(int houseObjectId) {
		ProlongedAuction prolongedAuction = prolongedAuctions.get(houseObjectId);
		long auctionEndMillis = prolongedAuction == null ? getNextRun().getTime() : prolongedAuction.auctionEndMillis;
		return (int) ((auctionEndMillis - System.currentTimeMillis()) / 1000);
	}

	public void onAuctionEnd(int houseObjectId) {
		ProlongedAuction prolongedAuction = prolongedAuctions.remove(houseObjectId);
		if (prolongedAuction != null)
			prolongedAuction.task.cancel(false);
	}

	/**
	 * @return True if the auction did not need to be prolonged or was prolonged successfully. False if the auction just ended.
	 */
	public boolean tryProlongAuction(int houseObjectId) {
		long millisUntilAuctionEnd = getMillisUntilNextRun();
		long millisSinceLastAuctionEnd = getMillisSinceLastRun();
		long delayMillis = 0;
		if (millisUntilAuctionEnd <= TimeUnit.MINUTES.toMillis(5)) // initial extension is 5 minutes after regular auction end
			delayMillis = millisUntilAuctionEnd + PROLONGATION_MILLIS;
		else if (millisSinceLastAuctionEnd != -1 && millisSinceLastAuctionEnd < MAX_PROLONGATION_MILLIS) // max extension is 30 minutes
			delayMillis = Math.min(TimeUnit.MINUTES.toMillis(30) - millisSinceLastAuctionEnd, PROLONGATION_MILLIS);
		return delayMillis == 0 || prolongAuction(houseObjectId, delayMillis);
	}

	public boolean isAuctionProlonged(int houseObjectId) {
		return prolongedAuctions.containsKey(houseObjectId);
	}

	/**
	 * @return True if the auction could be prolonged. False if it just ended.
	 */
	private boolean prolongAuction(int houseObjectId, long delayMillis) {
		ProlongedAuction prolongedAuction = prolongedAuctions.compute(houseObjectId, (key, oldValue) -> {
			if (oldValue == null)
				oldValue = new ProlongedAuction(houseObjectId, delayMillis);
			else if (!oldValue.prolong(delayMillis))
				return null;
			return oldValue;
		});
		return prolongedAuction != null;
	}

	private class ProlongedAuction {

		private final int houseObjectId;
		private long auctionEndMillis;
		private Future<?> task;

		private ProlongedAuction(int houseObjectId, long delayMillis) {
			this.houseObjectId = houseObjectId;
			prolong(delayMillis);
		}

		private boolean prolong(long delayMillis) {
			if (task != null && !task.cancel(false))
				return false;
			auctionEndMillis = System.currentTimeMillis() + delayMillis;
			task = ThreadPoolManager.getInstance().schedule(() -> HousingBidService.getInstance().endAuction(houseObjectId), delayMillis);
			return true;
		}
	}
}

📎 첨부파일

댓글 작성 권한이 없습니다.
🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 102,949
2 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 63,733
3 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,771
4 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 36,923
5 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 35,011
6 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 29,470
7 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 29,077
8 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
9 no_profile 보거스쪽지보내기 자기소개 아이디로 검색 전체게시물 26,731
10 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 17,020
알림 0