Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds a couple of new commands for GMs on games. #6183

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions megamek/i18n/megamek/common/options/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ GameOptionsInfo.option.allow_nukes.displayableName=Allow command-line nukes.
GameOptionsInfo.option.allow_nukes.description=This must be checked to allow players to throw nukes from the command line. If this is not checked, nukes are still available as advanced munitions.
GameOptionsInfo.option.really_allow_nukes.displayableName=REALLY allow command-line nukes.
GameOptionsInfo.option.really_allow_nukes.description=This must be checked to allow players to throw nukes from the command line. If this is not checked, nukes are still available as advanced munitions.
GameOptionsInfo.option.gm_can_kill_units.displayableName=Allow command-line kill (GM only).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest lumping this together with command line nukes. I doubt the distinction is required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe one new specifically to enable/disable GM commands in game? Because now I have created 4 new GM-only commands, or maybe just not add the option at all, since you are turning yourself into a GM, requesting the blessing of the other players, maybe having access to all commands should be a given instead of having multiple flags blocking you from doing that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would go with "GM gets access to all GM commands by default". Nominally, the point is to give them access to various tools that let them arbitrarily alter the scenario as needed. The GM status itself already has some safeguards applied to prevent abuse (can't just activate it midgame without the players voting to allow it, etc.).

GameOptionsInfo.option.gm_can_kill_units.description=This must be checked to allow the GM to kill units using command line. If this is not checked, the command is disabled.


GameOptionsInfo.group.advancedRules.displayableName=Advanced Rules
Expand Down
4 changes: 4 additions & 0 deletions megamek/i18n/megamek/common/report-messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
1241=Blue Shield Field Dampener <span class='warning'>fails</span>!
1242=no effect

#1300s - Orbital Bombardment related
1300=<newline><newline><newline>An orbital bombardment hit hex <data>!!!
1301=<span class='info'>End of orbital bombardment resolution</span>

# 1500s - ammo handling related
1500=<data> (<data>) selected <data> <data>
1501= due to <span class='warning'>out of ammo!</span>
Expand Down
1 change: 1 addition & 0 deletions megamek/src/megamek/common/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ public void setLastPhase(GamePhase lastPhase) {

/**
* @return an enumeration of all the entities in the game.
* @deprecated Use {@link #inGameTWEntities()} instead.
*/
@Deprecated
public Iterator<Entity> getEntities() {
Expand Down
1 change: 1 addition & 0 deletions megamek/src/megamek/common/options/GameOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public synchronized void initialize() {
addOption(allowed, OptionsConstants.ALLOWED_NO_CLAN_PHYSICAL, false);
addOption(allowed, OptionsConstants.ALLOWED_ALLOW_NUKES, false);
addOption(allowed, OptionsConstants.ALLOWED_REALLY_ALLOW_NUKES, false);
addOption(allowed, OptionsConstants.ALLOWED_GM_CAN_KILL_UNITS, false);

IBasicOptionGroup advancedRules = addGroup("advancedRules");
addOption(advancedRules, OptionsConstants.ADVANCED_MINEFIELDS, false);
Expand Down
1 change: 1 addition & 0 deletions megamek/src/megamek/common/options/OptionsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public class OptionsConstants {
public static final String ALLOWED_NO_CLAN_PHYSICAL = "no_clan_physical";
public static final String ALLOWED_ALLOW_NUKES = "allow_nukes";
public static final String ALLOWED_REALLY_ALLOW_NUKES = "really_allow_nukes";
public static final String ALLOWED_GM_CAN_KILL_UNITS = "gm_can_kill_units";
public static final String ADVANCED_MINEFIELDS = "minefields";
public static final String ADVANCED_HIDDEN_UNITS = "hidden_units";
public static final String ADVANCED_BLACK_ICE= "black_ice";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AllowGameMasterCommand extends ServerCommand {

public AllowGameMasterCommand(Server server, TWGameManager gameManager) {
super(server, "allowGM", "Allows a player become Game Master "
+ "Usage: /allowGameMaster used in respond to another " +
+ "Usage: /allowGM used in respond to another " +
"Player's request to become Game Master. All players assigned to" +
" a team must allow the change.");
this.gameManager = gameManager;
Expand Down
79 changes: 79 additions & 0 deletions megamek/src/megamek/server/commands/ChangeOwnershipCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.server.commands;

import megamek.common.Entity;
import megamek.common.Player;
import megamek.server.Server;
import megamek.server.totalwarfare.TWGameManager;

/**
* The Server Command "/changeOwner" that will switch an entity's owner to another player.
*
* @author Luana Scoppio
*/
public class ChangeOwnershipCommand extends ServerCommand implements IsGM {

private final TWGameManager gameManager;

public ChangeOwnershipCommand(Server server, TWGameManager gameManager) {
super(server,
"changeOwner",
"Switches ownership of a player's entity to another player during the end phase. "
+ "Usage: /changeOwner <Unit ID> <Player ID> "
+ "The following is an example of changing unit ID 7 to player ID 2: /changeOwner 7 2 ");
this.gameManager = gameManager;
}

/**
* Run this command with the arguments supplied
*
* @see ServerCommand#run(int, String[])
*/
@Override
public void run(int connId, String[] args) {
try {
if (!isGM(connId)) {
server.sendServerChat(connId, "You are not a Game Master.");
return;
}

int eid = Integer.parseInt(args[1]);
Entity ent = gameManager.getGame().getEntity(eid);
int pid = Integer.parseInt(args[2]);
Player player = server.getGame().getPlayer(pid);
if (null == ent) {
server.sendServerChat(connId, "No such entity.");
} else if (null == player) {
server.sendServerChat(connId, "No such player.");
} else if (player.getTeam() == Player.TEAM_UNASSIGNED) {
server.sendServerChat(connId, "Player must be assigned a team.");
} else {
server.sendServerChat(connId, ent.getDisplayName() + " will switch to " + player.getName() + "'s side at the end of this turn.");
ent.setTraitorId(pid);
}
} catch (NumberFormatException ignored) {
}
}

@Override
public TWGameManager getGameManager() {
return gameManager;
}
}
114 changes: 114 additions & 0 deletions megamek/src/megamek/server/commands/ChangeWeatherCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.server.commands;

import megamek.common.planetaryconditions.*;
import megamek.server.Server;
import megamek.server.totalwarfare.TWGameManager;


import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* @author Luana Scoppio
*/
public class ChangeWeatherCommand extends ServerCommand {

private final TWGameManager gameManager;

private static final String HELP_TEXT = "GM changes (weather) planetary conditions. The parameters are optional and unordered " +
"and the effects are applied at the beginning of the next turn. The square brackets means that argument is optional. " +
"Usage format: /weather [fog=0-2] [wind=0-6] [winddir=0-6] [light=0-6] [atmo=0-5] [blowsand=0-1] [weather=0-14] " +
"light= 0: daylight, 1: dusk, 2: full moon, 3: glare, 4: moonless night, 5: solar flare, 6: pitch black " +
"fog= 0: none, 1: light, 2: heavy " +
"wind= 0: calm, 1: light gale, 2: moderate gale, 3: strong gale, 4: storm, 5: tornado F1-F3, 6: tornado F4 " +
"winddir= 0: south, 1: southwest, 2: northwest, 3: north, 4: northeast, 5: southeast, 6: random " +
"atmo= 0: vacuum, 1: trace, 2: thin, 3: standard, 4: high, 5: very high " +
"blowsand= 0: no, 1: yes " +
"weather= 0: clear, 1: light rain, 2: moderate rain, 3: heavy rain, 4: gusting rain, 5: downpour, 6: light snow " +
"7: moderate snow, 8: snow flurries, 9: heavy snow, 10: sleet, 11: ice storm, 12: light hail, 13: heavy hail " +
"14: lightning storm";

/** Creates new ChangeWeatherCommand */
public ChangeWeatherCommand(Server server, TWGameManager gameManager) {
super(server, "weather", HELP_TEXT);
this.gameManager = gameManager;
}

private void updatePlanetaryCondition(String arg, String prefix, int connId, int maxLength, Consumer<Integer> setter,
Function<Integer, String> successMessage, Function<Integer, String> errorMessage) {
var value = Integer.parseInt(arg.substring(prefix.length()));

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
if (value >= 0 && value < maxLength) {
setter.accept(value);
server.sendServerChat(connId, successMessage.apply(value));
} else {
server.sendServerChat(connId, errorMessage.apply(maxLength));
}
}

private record Condition(int maxLength, Consumer<Integer> setter, Function<Integer, String> successMessage, Function<Integer, String> errorMessage) {}

/**
* Run this command with the arguments supplied
*/
@Override
public void run(int connId, String[] args) {
if (!server.getPlayer(connId).getGameMaster()) {
server.sendServerChat(connId, "You are not a Game Master.");
return;
}

var planetaryConditions = gameManager.getGame().getPlanetaryConditions();

if (args.length > 1) {

Map<String, Condition> conditions = Map.of(
"fog=", new Condition(Fog.values().length, value -> planetaryConditions.setFog(Fog.values()[value]),
value -> "The fog has changed.", maxLength -> "Invalid fog value. Must be between 0 and " + (maxLength - 1)),
"wind=", new Condition(Wind.values().length, value -> planetaryConditions.setWind(Wind.values()[value]),
value -> "The wind strength has changed.", maxLength -> "Invalid wind value. Must be between 0 and " + (maxLength - 1)),
"winddir=", new Condition(WindDirection.values().length, value -> planetaryConditions.setWindDirection(WindDirection.values()[value]),
value -> "The wind direction has changed.", maxLength -> "Invalid wind direction value. Must be between 0 and " + (maxLength - 1)),
"light=", new Condition(Light.values().length, value -> planetaryConditions.setLight(Light.values()[value]),
value -> "The light has changed.", maxLength -> "Invalid light value. Must be between 0 and " + (maxLength - 1)),
"atmo=", new Condition(Atmosphere.values().length, value -> planetaryConditions.setAtmosphere(Atmosphere.values()[value]),
value -> value == 0 ? "The air has vanished, put your vac suits!" : "The air is changing.", maxLength -> "Invalid atmosphere value. Must be between 0 and " + (maxLength - 1)),
"blowsand=", new Condition(BlowingSand.values().length, value -> planetaryConditions.setBlowingSand(BlowingSand.values()[value]),
value -> value == 1 ? "Sand started blowing." : "The sand has settled.", maxLength -> "Invalid blowsand value. Must be between 0 and " + (maxLength - 1)),
"weather=", new Condition(Weather.values().length, value -> planetaryConditions.setWeather(Weather.values()[value]),
value -> "The weather has changed.", maxLength -> "Invalid weather value. Must be between 0 and " + (maxLength - 1))
);

Stream.of(args)
.forEach(arg -> conditions.forEach((prefix, condition) -> {
if (arg.startsWith(prefix)) {
updatePlanetaryCondition(arg, prefix, connId, condition.maxLength, condition.setter, condition.successMessage, condition.errorMessage);
}
}));

gameManager.getGame().setPlanetaryConditions(planetaryConditions);
} else {
// Error out; it's not a valid call.
server.sendServerChat(connId, "weather command failed. " + HELP_TEXT);
}
}
}
62 changes: 62 additions & 0 deletions megamek/src/megamek/server/commands/DisasterCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.server.commands;

import megamek.server.Server;
import megamek.server.totalwarfare.TWGameManager;

/**
* @author Luana Scoppio
*/
public class DisasterCommand extends ServerCommand {

private final TWGameManager gameManager;

/** Creates new DisasterCommand */
public DisasterCommand(Server server, TWGameManager gameManager) {
super(server, "disaster", "GM calls a disaster at random, arguments in square brackets are optional. Usage: /disaster [type] " +
"if not type is passed, one is chosen at random. " +
"type= 0: hurricane, 1: lightning storm, 2: meteor shower, 3: orbital bombardment, 4: wildfire, 5: sandstorm, 6: hailstorm, " +
"7: heatwave");
this.gameManager = gameManager;
}

/**
* Run this command with the arguments supplied
*/
@Override
public void run(int connId, String[] args) {
if (!server.getPlayer(connId).getGameMaster()) {
server.sendServerChat(connId, "You are not a Game Master.");
return;
}

// Check argument integrity.
if (args.length == 1) {
// Check command
// NOT IMPLEMENTED
server.sendServerChat(connId, "Oh no...");
} else if (args.length == 2) {
// Error out; it's not a valid call.
server.sendServerChat(connId, "Oh no...");
} else {
server.sendServerChat(connId, "disaster command failed (1).");
}
}
}
13 changes: 13 additions & 0 deletions megamek/src/megamek/server/commands/IsGM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package megamek.server.commands;

import megamek.server.totalwarfare.TWGameManager;

public interface IsGM {

TWGameManager getGameManager();

default boolean isGM(int connId) {
return getGameManager().getGame().getPlayer(connId).getGameMaster();
}

}
Loading