2018-08-28 06:13:14 +00:00
|
|
|
package com.seodisparate.TurnBasedMinecraft.common;
|
|
|
|
|
2018-10-18 04:34:57 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2018-09-20 06:15:34 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2018-09-06 08:08:36 +00:00
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
public class BattleUpdater implements Runnable
|
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
private BattleManager manager;
|
2018-09-20 06:15:34 +00:00
|
|
|
private AtomicBoolean isRunning;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
2018-10-18 07:26:09 +00:00
|
|
|
private class UpdateRunnable implements Runnable
|
|
|
|
{
|
|
|
|
private Battle battle;
|
|
|
|
private AtomicBoolean finished = new AtomicBoolean(false);
|
|
|
|
private AtomicBoolean battleFinished = new AtomicBoolean(false);
|
|
|
|
|
|
|
|
public UpdateRunnable(Battle battle)
|
|
|
|
{
|
|
|
|
this.battle = battle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFinished()
|
|
|
|
{
|
|
|
|
return finished.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBattleFinished()
|
|
|
|
{
|
|
|
|
return battleFinished.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
battleFinished.set(battle.update());
|
|
|
|
finished.set(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
public BattleUpdater(BattleManager manager)
|
|
|
|
{
|
|
|
|
this.manager = manager;
|
2018-09-20 06:15:34 +00:00
|
|
|
isRunning = new AtomicBoolean(true);
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2018-09-20 06:15:34 +00:00
|
|
|
while(isRunning.get())
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-10-18 04:34:57 +00:00
|
|
|
synchronized(manager.battleMap)
|
2018-08-29 06:09:44 +00:00
|
|
|
{
|
2018-10-18 04:34:57 +00:00
|
|
|
for(Iterator<Map.Entry<Integer, Battle>> iter = manager.battleMap.entrySet().iterator(); iter.hasNext();)
|
2018-09-06 08:08:36 +00:00
|
|
|
{
|
2018-10-18 04:34:57 +00:00
|
|
|
Map.Entry<Integer, Battle> entry = iter.next();
|
2018-10-18 07:26:09 +00:00
|
|
|
UpdateRunnable updateRunnable = new UpdateRunnable(entry.getValue());
|
|
|
|
Thread updateThread = new Thread(updateRunnable);
|
|
|
|
updateThread.start();
|
2018-10-27 04:58:19 +00:00
|
|
|
try { updateThread.join(2000); } catch(InterruptedException e){ /* exception ignored */ }
|
2018-10-18 07:26:09 +00:00
|
|
|
if(!updateRunnable.isFinished())
|
|
|
|
{
|
2018-10-27 04:58:19 +00:00
|
|
|
TurnBasedMinecraftMod.logger.warn("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update hanged for 2 seconds!");
|
|
|
|
try { updateThread.join(2000); } catch(InterruptedException e){ /* exception ignored */ }
|
|
|
|
if(!updateRunnable.isFinished())
|
|
|
|
{
|
|
|
|
TurnBasedMinecraftMod.logger.warn("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update hanged for 4 seconds!");
|
|
|
|
try { updateThread.join(4000); } catch(InterruptedException e){ /* exception ignored */ }
|
|
|
|
if(!updateRunnable.isFinished())
|
|
|
|
{
|
|
|
|
// TODO this is an ugly fix to a still-not-found freeze bug in Battle.update()
|
|
|
|
TurnBasedMinecraftMod.logger.error("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update timed out!");
|
|
|
|
updateThread.stop();
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 07:26:09 +00:00
|
|
|
}
|
2018-10-27 04:58:19 +00:00
|
|
|
if(updateRunnable.isFinished() && updateRunnable.isBattleFinished())
|
2018-10-18 04:34:57 +00:00
|
|
|
{
|
|
|
|
iter.remove();
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 09:28:47 +00:00
|
|
|
manager.updateRecentlyLeftBattle();
|
2018-09-25 05:55:24 +00:00
|
|
|
try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ }
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setIsRunning(boolean isRunning)
|
|
|
|
{
|
2018-09-20 06:15:34 +00:00
|
|
|
this.isRunning.set(isRunning);
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
}
|