Sort the scores

This commit is contained in:
Sam 2017-05-04 17:16:16 +01:00
parent 808885ea04
commit 8032133f4d

View File

@ -3,9 +3,11 @@ package nautilus.game.arcade.game.games.alieninvasion;
import mineplex.core.common.Pair;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.utils.UtilVariant;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.events.GameStateChangeEvent;
@ -16,14 +18,19 @@ import nautilus.game.arcade.kit.Kit;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
public class AlienInvasion extends SoloGame
@ -33,6 +40,19 @@ public class AlienInvasion extends SoloGame
"Something something",
"Add this soon"
};
private static final Comparator<DragonScore> SCORE_SORTER = (o1, o2) ->
{
if (o1.Score > o2.Score)
{
return 1;
}
else if (o1.Score < o2.Score)
{
return -1;
}
return 0;
};
private final List<BeamSource> _sources = new ArrayList<>(5);
private final List<Location> _targets = new ArrayList<>();
@ -40,6 +60,7 @@ public class AlienInvasion extends SoloGame
private int _lastBeamId;
private long _lastBeam;
private long _nextBeam;
private final List<DragonScore> _score = new ArrayList<>(16);
@ -55,21 +76,19 @@ public class AlienInvasion extends SoloGame
@EventHandler
public void ScoreboardUpdate(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST || !IsLive())
if (event.getType() != UpdateType.FAST || !InProgress())
{
return;
}
Scoreboard.writeNewLine();
AtomicInteger index = new AtomicInteger(0);
List<DragonScore> scores = new ArrayList<>(_score);
Collections.reverse(scores);
sortScores();
Scoreboard.writeGroup(scores.subList(0, Math.min(16, scores.size())), score ->
Scoreboard.writeGroup(_score.subList(0, Math.min(14, _score.size())), score ->
{
ChatColor col = IsAlive(score.Player) ? ChatColor.GREEN : ChatColor.RED;
return Pair.create(col + score.Player.getName(), index.incrementAndGet());
return Pair.create(col + score.Player.getName(), (int) score.Score);
}, true);
Scoreboard.writeNewLine();
@ -111,11 +130,11 @@ public class AlienInvasion extends SoloGame
int id = 0;
for (Location pathPoint : _path)
for (Location target : _targets)
{
BeamSource source = getClosestSource(pathPoint);
BeamSource source = getClosestSource(target);
source.addBeam(Manager, id++, pathPoint);
source.addBeam(Manager, id++, target);
}
}
@ -139,9 +158,9 @@ public class AlienInvasion extends SoloGame
}
@EventHandler
public void live(GameStateChangeEvent event)
public void prepare(GameStateChangeEvent event)
{
if (event.GetState() != GameState.Live)
if (event.GetState() != GameState.Prepare)
{
return;
}
@ -150,8 +169,31 @@ public class AlienInvasion extends SoloGame
{
_score.add(new DragonScore(player, 0));
}
}
@EventHandler
public void live(GameStateChangeEvent event)
{
if (event.GetState() != GameState.Live)
{
return;
}
_lastBeam = System.currentTimeMillis();
_nextBeam = 5000;
ItemStack sword = new ItemStack(Material.STONE_SWORD);
ItemStack helmet = new ItemStack(Material.GLASS);
CreatureAllowOverride = true;
for (Location location : WorldData.GetDataLocs("BLUE"))
{
Skeleton skeleton = UtilVariant.spawnWitherSkeleton(location);
skeleton.getEquipment().setHelmet(helmet);
skeleton.getEquipment().setItemInHand(sword);
}
CreatureAllowOverride = false;
}
@EventHandler
@ -170,6 +212,7 @@ public class AlienInvasion extends SoloGame
}
_lastBeam = System.currentTimeMillis();
_nextBeam -= 50;
_lastBeamId++;
Manager.runSyncTimer(new BukkitRunnable()
@ -179,6 +222,11 @@ public class AlienInvasion extends SoloGame
{
if (beam.update())
{
for (Entry<Player, Double> entry : UtilPlayer.getInRadius(beam.getLastLocation(), 20).entrySet())
{
Manager.GetDamage().NewDamageEvent(entry.getKey(), null, null, DamageCause.CUSTOM, 20 * entry.getValue(), false, true, false, "Alien Invasion", "Phaser");
}
for (DragonScore score : _score)
{
if (score.Score <= _lastBeamId)
@ -195,7 +243,7 @@ public class AlienInvasion extends SoloGame
private Beam getSuitableBeam()
{
if (!UtilTime.elapsed(_lastBeam, 5000))
if (!UtilTime.elapsed(_lastBeam, _nextBeam))
{
return null;
}
@ -254,15 +302,38 @@ public class AlienInvasion extends SoloGame
@Override
public void EndCheck()
{
if (!IsLive())
{
return;
}
List<Player> alive = GetPlayers(true);
for (DragonScore score : _score)
{
if (score.Score == _path.size() - 1)
if (score.Score == _path.size() - 1 || alive.isEmpty())
{
sortScores();
List<Player> players = new ArrayList<>(_score.size());
for (DragonScore score1 : _score)
{
players.add(score1.Player);
}
Collections.reverse(players);
AnnounceEnd(players);
SetState(GameState.End);
return;
}
}
}
private void sortScores()
{
_score.sort(SCORE_SORTER);
}
private DragonScore getScore(Player player)
{
for (DragonScore score : _score)