Make LevelPicker class more extensible

This commit is contained in:
UnknownShadow200 2023-12-15 22:39:07 +11:00
parent 9ead72b356
commit abfdcdf076
8 changed files with 64 additions and 43 deletions

View File

@ -21,29 +21,58 @@ using System.Threading;
namespace MCGalaxy.Games
{
public class LevelPicker
public abstract class LevelPicker
{
public string QueuedMap;
public List<string> RecentMaps = new List<string>();
public int VoteTime = 20;
public bool Voting;
internal string Candidate1 = "", Candidate2 = "", Candidate3 = "";
internal int Votes1, Votes2, Votes3;
const int MIN_MAPS = 3;
public void AddRecentMap(string map) {
public abstract List<string> GetCandidateMaps(RoundsGame game);
public virtual string GetRandomMap(Random r, List<string> maps) {
int i = r.Next(0, maps.Count);
string map = maps[i];
maps.RemoveAt(i);
return map;
}
public abstract string ChooseNextLevel(RoundsGame game);
public virtual void AddRecentMap(string map) {
if (RecentMaps.Count >= 20)
RecentMaps.RemoveAt(0);
RecentMaps.Add(map);
}
public void Clear() {
public virtual void Clear() {
QueuedMap = null;
RecentMaps.Clear();
}
public abstract bool HandlesMessage(Player p, string message);
public abstract void SendVoteMessage(Player p);
public abstract void ResetVoteMessage(Player p);
}
public class SimpleLevelPicker : LevelPicker
{
public int VoteTime = 20;
public string ChooseNextLevel(RoundsGame game) {
internal string Candidate1 = "", Candidate2 = "", Candidate3 = "";
internal int Votes1, Votes2, Votes3;
const int MIN_MAPS = 3;
public override List<string> GetCandidateMaps(RoundsGame game) {
return new List<string>(game.GetConfig().Maps);
}
public override string ChooseNextLevel(RoundsGame game) {
if (QueuedMap != null) return QueuedMap;
try {
@ -90,7 +119,8 @@ namespace MCGalaxy.Games
void DoLevelVote(IGame game) {
Voting = true;
Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players) {
foreach (Player pl in players)
{
if (pl.level != game.Map) continue;
SendVoteMessage(pl);
}
@ -141,18 +171,17 @@ namespace MCGalaxy.Games
}
}
internal static string GetRandomMap(Random r, List<string> maps) {
int i = r.Next(0, maps.Count);
string map = maps[i];
maps.RemoveAt(i);
return map;
public override bool HandlesMessage(Player p, string message) {
if (!Voting) return false;
return
Player.CheckVote(message, p, "1", "one", ref Votes1) ||
Player.CheckVote(message, p, "2", "two", ref Votes2) ||
Player.CheckVote(message, p, "3", "three", ref Votes3);
}
public virtual List<string> GetCandidateMaps(RoundsGame game) {
return new List<string>(game.GetConfig().Maps);
}
public void SendVoteMessage(Player p) {
public override void SendVoteMessage(Player p) {
const string line1 = "&eLevel vote - type &a1&e, &b2&e or &c3";
string line2 = "&a" + Candidate1 + "&e, &b" + Candidate2 + "&e, &c" + Candidate3;
@ -165,19 +194,10 @@ namespace MCGalaxy.Games
}
}
public void ResetVoteMessage(Player p) {
public override void ResetVoteMessage(Player p) {
p.SendCpeMessage(CpeMessageType.BottomRight3, "");
p.SendCpeMessage(CpeMessageType.BottomRight2, "");
p.SendCpeMessage(CpeMessageType.BottomRight1, "");
}
public bool HandlesMessage(Player p, string message) {
if (!Voting) return false;
return
Player.CheckVote(message, p, "1", "one", ref Votes1) ||
Player.CheckVote(message, p, "2", "two", ref Votes2) ||
Player.CheckVote(message, p, "3", "three", ref Votes3);
}
}
}

View File

@ -101,7 +101,7 @@ namespace MCGalaxy.Games
List<string> maps = Picker.GetCandidateMaps(this);
if (maps == null || maps.Count == 0) return null;
return LevelPicker.GetRandomMap(new Random(), maps);
return Picker.GetRandomMap(new Random(), maps);
}
void RunGame() {

View File

@ -61,7 +61,7 @@ namespace MCGalaxy.Modules.Games.CTF
CtfTeam Blue = new CtfTeam("Blue", Colors.blue);
public static CTFGame Instance = new CTFGame();
public CTFGame() { Picker = new LevelPicker(); }
public CTFGame() { Picker = new SimpleLevelPicker(); }
protected override string WelcomeMessage {
get { return "&9Capture the Flag &Sis running! Type &T/CTF go &Sto join"; }

View File

@ -46,7 +46,7 @@ namespace MCGalaxy.Modules.Games.Countdown
public CountdownSpeed SpeedType;
public static CountdownGame Instance = new CountdownGame();
public CountdownGame() { Picker = new LevelPicker(); }
public CountdownGame() { Picker = new SimpleLevelPicker(); }
public override void UpdateMapConfig() { }

View File

@ -53,7 +53,7 @@ namespace MCGalaxy.Modules.Games.LS
static bool hooked;
public static LSGame Instance = new LSGame();
public LSGame() { Picker = new LevelPicker(); }
public LSGame() { Picker = new SimpleLevelPicker(); }
public static LSData Get(Player p) {
object data;

View File

@ -95,7 +95,7 @@ namespace MCGalaxy.Modules.Games.TW
TNTImmuneFilter tntImmuneFilter;
public static TWGame Instance = new TWGame();
public TWGame() { Picker = new LevelPicker(); }
public TWGame() { Picker = new SimpleLevelPicker(); }
const string twExtrasKey = "MCG_TW_DATA";
static TWData Get(Player p) {

View File

@ -77,7 +77,7 @@ namespace MCGalaxy.Modules.Games.ZS
public override RoundsGameConfig GetConfig() { return Config; }
public static ZSGame Instance = new ZSGame();
public ZSGame() { Picker = new LevelPicker(); }
public ZSGame() { Picker = new SimpleLevelPicker(); }
protected override string WelcomeMessage {
get { return "&2Zombie Survival &Sis running! Type &T/ZS go &Sto join"; }

View File

@ -77,9 +77,9 @@ namespace MCGalaxy.Modules.Warps
if (p.level.name.CaselessEq(warp.Level)) {
p.SendPosition(warp.Pos, new Orientation(warp.Yaw, warp.Pitch));
p.Message("Sent you to waypoint/warp");
p.Message("Sent you to waypoint/warp {0}", warp.Name);
} else {
p.Message("Unable to send you to the warp as the map it is on is not loaded.");
p.Message("&WUnable to send you to the warp as the map it is on is not loaded.");
}
}
@ -102,7 +102,7 @@ namespace MCGalaxy.Modules.Warps
string line;
while ((line = r.ReadLine()) != null) {
line = line.Trim();
if (line.StartsWith("#") || !line.Contains(":")) continue;
if (line.IsCommentLine()) continue;
string[] parts = line.Split(':');
Warp warp = new Warp();
@ -116,7 +116,7 @@ namespace MCGalaxy.Modules.Warps
warp.Pitch = byte.Parse(parts[6]);
warps.Add(warp);
} catch (Exception ex) {
Logger.LogError("Error loading warp from " + Filename, ex);
Logger.LogError("Error loading warp " + line + " from " + Filename, ex);
}
}
}
@ -126,8 +126,9 @@ namespace MCGalaxy.Modules.Warps
public void Save() {
using (StreamWriter w = new StreamWriter(Filename)) {
foreach (Warp warp in Items) {
w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" +
foreach (Warp warp in Items)
{
w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" +
warp.Pos.Y + ":" + warp.Pos.Z + ":" + warp.Yaw + ":" + warp.Pitch);
}
}