Skip to content

Commit

Permalink
Make LevelPicker class more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Dec 15, 2023
1 parent 9ead72b commit abfdcdf
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
80 changes: 50 additions & 30 deletions MCGalaxy/Games/RoundsGame/LevelPicker.cs
Expand Up @@ -21,29 +21,58 @@

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;

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 string ChooseNextLevel(RoundsGame game) {
public override string ChooseNextLevel(RoundsGame game) {
if (QueuedMap != null) return QueuedMap;

try {
Expand Down Expand Up @@ -90,7 +119,8 @@ public class LevelPicker
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);
}
Expand Down Expand Up @@ -141,18 +171,17 @@ public class LevelPicker
}
}

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 virtual List<string> GetCandidateMaps(RoundsGame game) {
return new List<string>(game.GetConfig().Maps);
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 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;

Expand All @@ -165,19 +194,10 @@ public class LevelPicker
}
}

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);
}
}
}
2 changes: 1 addition & 1 deletion MCGalaxy/Games/RoundsGame/RoundsGame.cs
Expand Up @@ -101,7 +101,7 @@ public abstract partial class RoundsGame : IGame
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() {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Games/CTF/CtfGame.cs
Expand Up @@ -61,7 +61,7 @@ public partial class CTFGame : RoundsGame
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"; }
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Games/Countdown/CountdownGame.cs
Expand Up @@ -46,7 +46,7 @@ public partial class CountdownGame : RoundsGame
public CountdownSpeed SpeedType;

public static CountdownGame Instance = new CountdownGame();
public CountdownGame() { Picker = new LevelPicker(); }
public CountdownGame() { Picker = new SimpleLevelPicker(); }

public override void UpdateMapConfig() { }

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Games/LavaSurvival/LSGame.cs
Expand Up @@ -53,7 +53,7 @@ public partial class LSGame : RoundsGame
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;
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Games/TNTWars/TWGame.cs
Expand Up @@ -95,7 +95,7 @@ public partial class TWGame : RoundsGame
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) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Modules/Games/ZombieSurvival/ZSGame.cs
Expand Up @@ -77,7 +77,7 @@ public partial class ZSGame : RoundsGame
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"; }
Expand Down
13 changes: 7 additions & 6 deletions MCGalaxy/Modules/Warps/Warp.cs
Expand Up @@ -77,9 +77,9 @@ public sealed class WarpList

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.");
}
}

Expand All @@ -102,7 +102,7 @@ public sealed class WarpList
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();
Expand All @@ -116,7 +116,7 @@ public sealed class WarpList
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);
}
}
}
Expand All @@ -126,8 +126,9 @@ public sealed class WarpList

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);
}
}
Expand Down

0 comments on commit abfdcdf

Please sign in to comment.