This commit is contained in:
Shaun Bennett 2015-10-21 14:48:27 -04:00
parent 112f34c77f
commit 00a689ea03
15 changed files with 309 additions and 154 deletions

View File

@ -76,7 +76,7 @@ public abstract class CommandBase<PluginType extends MiniPlugin> implements ICom
return null;
}
protected List<String> getMatches(String start, List<String> possibleMatches)
protected List<String> getMatches(String start, Collection<String> possibleMatches)
{
List<String> matches = new ArrayList<String>();

View File

@ -21,7 +21,6 @@ import mineplex.core.database.column.ColumnTimestamp;
import mineplex.core.database.column.ColumnVarChar;
import mineplex.database.tables.records.ClansRecord;
import mineplex.game.clans.core.repository.tokens.ClanAllianceToken;
import mineplex.game.clans.core.repository.tokens.ClanEnemyToken;
import mineplex.game.clans.core.repository.tokens.ClanMemberToken;
import mineplex.game.clans.core.repository.tokens.ClanTerritoryToken;
import mineplex.game.clans.core.repository.tokens.ClanToken;
@ -29,6 +28,8 @@ import mineplex.game.clans.core.repository.tokens.SimpleClanToken;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.game.clans.core.repository.tokens.ClanWarToken;
import org.jooq.DSLContext;
import static mineplex.database.Tables.*;
@ -49,6 +50,7 @@ public class ClanRepository extends RepositoryBase
private static String RETRIEVE_CLAN_ALLIANCE_INFO = "SELECT c.name, cOther.name, ca.trusted FROM clanAlliances AS ca INNER JOIN clans AS c ON c.id = ca.clanId INNER JOIN clans as cOther ON cOther.id = ca.otherClanId WHERE c.serverId = ?;";
// private static String RETRIEVE_CLAN_ENEMY_INFO = "SELECT c.name, cOther.name, clanScore, otherClanScore, clanKills, otherClanKills, timeFormed FROM clanEnemies AS ce INNER JOIN clans AS c ON c.id = ce.clanId INNER JOIN clans as cOther ON cOther.id = ce.otherClanId WHERE c.serverId = ?;";
private static String RETRIEVE_CLAN_WAR_INFO = "SELECT c.name, cOther.name, cw.score, cw.created, cw.ended, cw.lastUpdated FROM clanWar AS cw INNER JOIN clans AS c on c.id = cw.initiatorId INNER JOIN clans as cOther ON cOther.id = cw.clanId WHERE c.serverId = ? AND completed = FALSE";
private static String RETRIEVE_CLAN_WAR_FROM_CLAN_IDS = "SELECT c.name, cOther.name, cw.score, cw.created, cw.ended, cw.lastUpdated FROM clanWar AS cw INNER JOIN clans AS c on c.id = cw.initiatorId INNER JOIN clans as cOther ON cOther.id = cw.clanId WHERE cw.initiatorId = ? AND cw.clanId = ? AND completed = FALSE";
private static String DELETE_CLAN_MEMBER = "DELETE aC FROM accountClan AS aC INNER JOIN accounts ON accounts.id = aC.accountId WHERE aC.clanId = ? AND accounts.name = ?;";
private static String DELETE_CLAN_MEMBERS = "DELETE FROM accountClan WHERE clanId = ?;";
@ -359,6 +361,30 @@ public class ClanRepository extends RepositoryBase
System.out.println("3");
executeQuery(RETRIEVE_CLAN_WAR_INFO, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
ClanWarToken warToken = new ClanWarToken();
warToken.ClanA = resultSet.getString(1);
warToken.ClanB = resultSet.getString(2);
warToken.Score = resultSet.getInt(3);
warToken.Created = resultSet.getTimestamp(4);
warToken.Ended = resultSet.getTimestamp(5);
warToken.Updated = resultSet.getTimestamp(6);
if (clans.containsKey(warToken.ClanA) && clans.containsKey(warToken.ClanB))
{
clans.get(warToken.ClanA).WarsInitiated.add(warToken);
clans.get(warToken.ClanB).WarsInitiatedOn.add(warToken);
}
}
}
}, new ColumnInt("serverId", _serverId));
System.out.println("Finished loading clans from database...");
return clans.values();
}

View File

@ -22,9 +22,9 @@ public class ClanToken
public Timestamp DateCreated;
public Timestamp LastOnline;
public ClanEnemyToken EnemyToken;
public List<ClanMemberToken> Members = new ArrayList<ClanMemberToken>();
public List<ClanTerritoryToken> Territories = new ArrayList<ClanTerritoryToken>();
public List<ClanAllianceToken> Alliances = new ArrayList<ClanAllianceToken>();
public List<ClanWarToken> WarsInitiated = new ArrayList<ClanWarToken>();
public List<ClanWarToken> WarsInitiatedOn = new ArrayList<ClanWarToken>();
}

View File

@ -0,0 +1,13 @@
package mineplex.game.clans.core.repository.tokens;
import java.sql.Timestamp;
public class ClanWarToken
{
public String ClanA;
public String ClanB;
public int Score;
public Timestamp Created;
public Timestamp Ended;
public Timestamp Updated;
}

View File

@ -31,6 +31,7 @@ import mineplex.game.clans.core.repository.tokens.ClanTerritoryToken;
import mineplex.game.clans.core.repository.tokens.ClanToken;
import mineplex.game.clans.clans.tntGenerator.TntGenerator;
import mineplex.game.clans.core.repository.tokens.ClanWarToken;
public class ClanInfo
{
@ -58,7 +59,6 @@ public class ClanInfo
private NautHashMap<UUID, ClansPlayer> _memberMap = new NautHashMap<UUID, ClansPlayer>();
private NautHashMap<String, Boolean> _allyMap = new NautHashMap<String, Boolean>();
private HashSet<String> _claimSet = new HashSet<String>();
private EnemyData _enemyData;
/**
* When a war is formed, a clan can either be the initiator, or be initiated on. We seperate these
@ -132,15 +132,11 @@ public class ClanInfo
_allyMap.put(allianceToken.ClanName, allianceToken.Trusted);
}
updateEnemy(token.EnemyToken);
}
public void updateEnemy(ClanEnemyToken enemyToken)
{
if (enemyToken != null)
for (ClanWarToken warToken : token.WarsInitiated)
{
_enemyData = new EnemyData(enemyToken.EnemyName, enemyToken.Initiator, enemyToken.Score, enemyToken.Kills, enemyToken.TimeFormed);
ClanWar war = new ClanWar(warToken);
}
}
public int getClaims()
@ -213,7 +209,7 @@ public class ClanInfo
public boolean isNeutral(String other)
{
return (!isAlly(other) && !isSelf(other) && !isEnemy(other));
return (!isAlly(other) && !isSelf(other) && !isAtWar(other));
}
public long getTimer()
@ -303,27 +299,6 @@ public class ClanInfo
return stringList;
}
public LinkedList<String> mEnemy()
{
LinkedList<String> stringList = new LinkedList<String>();
if (_enemyData == null)
{
stringList.add(F.main("Clans", "You do not have an enemy!"));
}
else
{
stringList.add(F.main("Clans", "Enemy Details"));
String enemyName = _enemyData.getEnemyName();
EnemyData otherEnemyData = Clans.getClanUtility().getClanByClanName(enemyName).getEnemyData();
stringList.add(F.value("Enemy", _enemyData.getEnemyName()));
stringList.add(F.value("Formed", _enemyData.getRelativeTimeFormed()));
stringList.add(F.value("ClanWar Score", _enemyData.getScore() + " : " + otherEnemyData.getScore()));
}
return stringList;
}
public LinkedList<String> mTerritory()
{
LinkedList<String> stringList = new LinkedList<String>();
@ -548,21 +523,6 @@ public class ClanInfo
_warLosses += amount;
}
public EnemyData getEnemyData()
{
return _enemyData;
}
public boolean hasEnemy()
{
return _enemyData != null;
}
public boolean isEnemy(String clanName)
{
return hasEnemy() && clanName != null && clanName.equals(_enemyData.getEnemyName());
}
public ClanWar getInitiatedWar(ClanInfo againstClan)
{
return getInitiatedWar(againstClan.getName());
@ -588,6 +548,18 @@ public class ClanInfo
return _initiatedOnWars.get(againstClan);
}
public void addWar(ClanWar clanWar)
{
if (equals(clanWar.getClanA()))
{
_initiatedWars.put(clanWar.getClanB().getName(), clanWar);
}
else if (equals(clanWar.getClanB()))
{
_initiatedOnWars.put(clanWar.getClanA().getName(), clanWar);
}
}
public ClanWar getWar(String againstClan)
{
ClanWar initiated = getInitiatedWar(againstClan);

View File

@ -1,5 +1,6 @@
package mineplex.game.clans.clans;
import java.sql.Timestamp;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -17,6 +18,8 @@ import mineplex.game.clans.clans.event.ClanDeleteEvent;
import mineplex.game.clans.clans.scoreboard.ClansScoreboardManager;
import mineplex.game.clans.clans.scoreboard.ClansScoreboardManager;
import mineplex.game.clans.clans.tntGenerator.TntGenerator;
import mineplex.game.clans.clans.war.ClanWar;
import mineplex.game.clans.clans.war.WarManager;
import mineplex.game.clans.core.repository.ClanRepository;
import mineplex.game.clans.core.repository.ClanTerritory;
import mineplex.game.clans.core.repository.tokens.ClanToken;
@ -627,6 +630,36 @@ public class ClansDataAccessLayer
});
}
public void war(final ClanInfo clanA, final ClanInfo clanB, final Callback<ClanWar> warCallback)
{
final ClanWar war = new ClanWar(clanA, clanB, WarManager.WAR_START_SCORE, false, new Timestamp(System.currentTimeMillis()), null, new Timestamp(System.currentTimeMillis()));
runAsync(new Runnable()
{
@Override
public void run()
{
_repository.addWar(clanA.getId(), clanB.getId(), WarManager.WAR_START_SCORE);
runSync(new Runnable()
{
@Override
public void run()
{
// Memory
clanA.addWar(war);
clanB.addWar(war);
_manager.log("Initiator war for [" + clanA.getName() + "] against [" + clanB.getName() + "].");
if (warCallback != null) warCallback.run(war);
}
});
}
});
}
/*
public void updateEnemy(ClanInfo clan, ClanInfo otherClan)
{

View File

@ -1,6 +1,7 @@
package mineplex.game.clans.clans;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
@ -329,6 +330,11 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
return _clanMap;
}
public Set<String> getClanNameSet()
{
return _clanMap.keySet();
}
public NautHashMap<UUID, ClanInfo> getClanMemberUuidMap()
{
return _clanMemberUuidMap;
@ -359,17 +365,6 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
return _clanMemberUuidMap.get(player.getUniqueId());
}
public ClanInfo getEnemy(ClanInfo clanInfo)
{
ClanInfo enemy = null;
EnemyData enemyData = clanInfo.getEnemyData();
if (enemyData != null)
enemy = getClan(enemyData.getEnemyName());
return enemy;
}
public boolean isInClan(Player player)
{
return _clanMemberUuidMap.containsKey(player.getUniqueId());

View File

@ -35,6 +35,7 @@ public class ClansUtility
SELF(C.xSelf, C.xdSelf),
ALLY(C.xAlly, C.xdAlly),
ALLY_TRUST(C.xAlly, C.xdAlly),
WAR(C.xWar, C.xdWar),
NEUTRAL(C.xNeutral, C.xdNeutral),
ADMIN(C.xAdmin, C.xAdmin),
SAFE(C.xSafe, C.xSafe);
@ -280,6 +281,14 @@ public class ClansUtility
String chunkTag = UtilWorld.chunkToStr(chunk);
return Clans.getClaimMap().get(chunkTag);
}
/**
* Get formatted name of display as shown for clanB
*/
public String name(ClanInfo display, ClanInfo clanB)
{
return rel(display, clanB).getPrefix() + display.getName();
}
public ClanTerritory getClaim(Location loc)
{
@ -504,6 +513,9 @@ public class ClansUtility
if (cA.isAlly(cB.getName()))
return ClanRelation.ALLY;
if (cA.isAtWar(cB))
return ClanRelation.WAR;
//Enemy
return ClanRelation.NEUTRAL;
}

View File

@ -1,65 +0,0 @@
package mineplex.game.clans.clans;
import java.util.Date;
import mineplex.core.common.util.UtilTime;
public class EnemyData
{
private String _enemy;
private boolean _initiator;
private int _score;
private int _kills;
private Date _timeFormed;
public EnemyData(String enemy, boolean initiator, int score, int kills, Date timeFormed)
{
_enemy = enemy;
_initiator = initiator;
_score = score;
_kills = kills;
_timeFormed = timeFormed;
}
public String getEnemyName()
{
return _enemy;
}
public int getScore()
{
return _score;
}
public int getKills()
{
return _kills;
}
public void addScore(int add)
{
int newScore = _score + add;
_score = Math.max(-40, Math.min(40, newScore)); // Cap range of score to [-40, 40]
}
public void addKill()
{
_kills++;
}
public boolean isInitiator()
{
return _initiator;
}
public Date getTimeFormed()
{
return _timeFormed;
}
public String getRelativeTimeFormed()
{
long timeFormed = _timeFormed.getTime();
return UtilTime.convertString(System.currentTimeMillis() - timeFormed, 1, UtilTime.TimeUnit.FIT);
}
}

View File

@ -125,30 +125,7 @@ public class ClansCommand extends CommandBase<ClansManager>
else
infoClan(caller, args[0]);
}
/*
* public void enemy(Player caller, String[] args) { if (args.length == 2) {
* ClanInfo playerClan = Plugin.getClanUtility().getClanByPlayer(caller);
* ClanInfo otherClan = Plugin.getClanUtility().searchClan(caller, args[1],
* true); if (playerClan == null) { UtilPlayer.message(caller,
* F.main("Clans", "You are not in a Clan.")); return; } else if (otherClan
* == null) { return; } attemptEnemy(caller, playerClan, otherClan); } else
* { infoEnemy(caller); } }
*/
/*
* private void attemptEnemy(Player caller, ClanInfo initiatorClan, ClanInfo
* otherClan) { Plugin.getWarManager().attemptEnemy(caller, initiatorClan,
* otherClan); }
*/
private void infoEnemy(Player caller)
{
ClanInfo playerClan = Plugin.getClanUtility().getClanByPlayer(caller);
UtilPlayer.message(caller, playerClan.mEnemy());
}
public void commandChat(Player caller, String[] args)
{
if (args.length == 0)
@ -710,9 +687,9 @@ public class ClansCommand extends CommandBase<ClansManager>
return;
}
if (cA.isEnemy(cB.getName()))
if (cA.isAtWar(cB.getName()))
{
UtilPlayer.message(caller, F.main("Clans", "You cannot form an alliance with an enemy clan."));
UtilPlayer.message(caller, F.main("Clans", "You cannot form an alliance with a clan you are at war with."));
return;
}
@ -817,9 +794,9 @@ public class ClansCommand extends CommandBase<ClansManager>
{
UtilPlayer.message(caller, F.main("Clans", "You prefer to think of yourself positively..."));
}
else if (cB.isEnemy(cA.getName()))
else if (cB.isAtWar(cA.getName()))
{
UtilPlayer.message(caller, F.main("Clans", "You are currently enemies with " + F.elem("Clan " + cB.getName()) + "."));
UtilPlayer.message(caller, F.main("Clans", "You are currently at war with " + F.elem("Clan " + cB.getName()) + "."));
}
else if (cB.isNeutral(cA.getName()))
{

View File

@ -109,7 +109,7 @@ public class ItemMapRenderer extends MapRenderer
{
clanColor = Color.YELLOW;
}
else if (owningClan.hasEnemy() && owningClan.getEnemyData().getEnemyName().equals(clan.getName()))
else if (clan.isAtWar(owningClan))
{
clanColor = Color.RED;
}

View File

@ -3,6 +3,7 @@ package mineplex.game.clans.clans.war;
import java.sql.Timestamp;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.core.repository.tokens.ClanWarToken;
public class ClanWar
{
@ -14,6 +15,10 @@ public class ClanWar
private Timestamp _ended;
private Timestamp _lastUpdated;
public ClanWar(ClanWarToken warToken)
{
}
public ClanWar(ClanInfo clanA, ClanInfo clanB, int score, boolean completed, Timestamp created, Timestamp ended, Timestamp lastUpdated)
{
_clanA = clanA;
@ -107,4 +112,17 @@ public class ClanWar
_score--;
}
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof ClanWar)
{
ClanWar war = ((ClanWar) obj);
return _clanA.equals(war.getClanA()) && _clanB.equals(war.getClanB()) && _completed == war.isCompleted() && _score == war.getScore()
&& _created.equals(war.getCreated());
}
return false;
}
}

View File

@ -14,11 +14,16 @@ import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.event.ClansPlayerDeathEvent;
import mineplex.game.clans.clans.war.command.DominanceCommand;
import mineplex.game.clans.clans.war.command.WarCommand;
import mineplex.game.clans.clans.war.event.WarEndEvent;
import mineplex.game.clans.clans.war.event.WarInvasionEndEvent;
import mineplex.game.clans.clans.war.event.WarInvasionStartEvent;
@ -42,6 +47,42 @@ public class WarManager extends MiniPlugin
_activeInvasions = new HashMap<String, List<WarInvasion>>();
}
public void attemptWar(Player caller, ClanInfo clanA, ClanInfo clanB, boolean inform, final Callback<ClanWar> callback)
{
// Check Self
if (clanA.equals(clanB))
{
if (inform) UtilPlayer.message(caller, F.main("War", "You cannot go to war against yourself!"));
if (callback != null) callback.run(null);
return;
}
// Check Ally
if (clanA.isAlly(clanB))
{
if (inform) UtilPlayer.message(caller, F.main("War", "You cannot go to war against an ally"));
if (callback != null) callback.run(null);
return;
}
// Check Already at War
if (clanA.isAtWar(clanB))
{
if (inform) UtilPlayer.message(caller, F.main("War", "You are already at war with " + _clansManager.getClanUtility().name(clanB, clanA)));
if (callback != null) callback.run(null);
return;
}
_clansManager.getClanDataAccess().war(clanA, clanB, new Callback<ClanWar>()
{
@Override
public void run(ClanWar data)
{
if (callback != null) callback.run(data);
}
});
}
public ClansManager getClansManager()
{
return _clansManager;
@ -172,4 +213,11 @@ public class WarManager extends MiniPlugin
iterator.remove();
}
}
@Override
public void addCommands()
{
addCommand(new DominanceCommand(this));
addCommand(new WarCommand(this));
}
}

View File

@ -0,0 +1,59 @@
package mineplex.game.clans.clans.war.command;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilTime;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.war.ClanWar;
import mineplex.game.clans.clans.war.WarManager;
public class DominanceCommand extends CommandBase<WarManager>
{
public DominanceCommand(WarManager plugin)
{
super(plugin, Rank.ALL, "dominance", "dom");
}
@Override
public void Execute(Player caller, String[] args)
{
ClansManager clansManager = Plugin.getClansManager();
ClanInfo clan = clansManager.getClan(caller);
if (clan == null)
{
UtilPlayer.message(caller, F.main("War", "You are not in a clan"));
return;
}
if (args.length == 1)
{
ClanInfo search = clansManager.getClanUtility().searchClanPlayer(caller, args[0], true);
if (search == null) return;
String searchName = clansManager.getClanUtility().name(search, clan);
String selfName = clansManager.getClanUtility().name(clan, clan);
ClanWar war = clan.getWar(search);
if (war == null)
{
UtilPlayer.message(caller, F.main("War", "You are not at war with " + searchName));
}
else
{
UtilPlayer.message(caller, F.main("War", "War Status with " + searchName));
UtilPlayer.message(caller, F.main("War", "Initiated by: " + (war.getClanA() == clan ? selfName : searchName)));
UtilPlayer.message(caller, F.main("War", "Initiator Dominance: " + F.elem("" + war.getScore())));
UtilPlayer.message(caller, F.main("War", "Age: " + F.elem(UtilTime.convertString(System.currentTimeMillis() - war.getCreated().getTime(), 1, UtilTime.TimeUnit.FIT))));
}
}
}
}

View File

@ -0,0 +1,67 @@
package mineplex.game.clans.clans.war.command;
import java.util.Arrays;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.war.ClanWar;
import mineplex.game.clans.clans.war.WarManager;
public class WarCommand extends CommandBase<WarManager>
{
public WarCommand(WarManager plugin)
{
super(plugin, Rank.ALL, "war");
}
@Override
public void Execute(Player caller, String[] args)
{
final ClanInfo clan = Plugin.getClansManager().getClan(caller);
if (clan == null)
{
UtilPlayer.message(caller, F.main("War", "You are not in a clan"));
return;
}
if (args == null || args.length != 1)
{
UtilPlayer.message(caller, F.main("War", "Usage: /war <clan>"));
return;
}
final ClanInfo search = Plugin.getClansManager().getClanUtility().searchClanPlayer(caller, args[0], true);
if (search == null) return;
Plugin.attemptWar(caller, clan, search, true, new Callback<ClanWar>()
{
@Override
public void run(ClanWar data)
{
if (data != null)
{
Plugin.getClansManager().messageClan(clan, "You are now at war with " + search.getName());
Plugin.getClansManager().messageClan(search, "You are now at war with " + clan.getName());
}
}
});
}
@Override
public List<String> onTabComplete(CommandSender sender, String commandLabel, String[] args)
{
System.out.println("Tab Complete!");
System.out.println("Label: " + commandLabel);
System.out.println("args: " + Arrays.toString(args));
return args.length == 1 ? getMatches(args[0], Plugin.getClansManager().getClanNameSet()) : null;
}
}