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();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
updateThread.join(3000);
|
|
|
|
} catch(InterruptedException e){ /* exception ignored */ }
|
|
|
|
if(!updateRunnable.isFinished())
|
|
|
|
{
|
|
|
|
// TODO this is an ugly fix to a still-not-found freeze bug in Battle.update()
|
2018-10-19 08:18:02 +00:00
|
|
|
TurnBasedMinecraftMod.logger.error("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update timed out!");
|
2018-10-18 07:26:09 +00:00
|
|
|
updateThread.stop();
|
|
|
|
}
|
|
|
|
else if(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
|
|
|
}
|
|
|
|
}
|