Skip to content

Commit

Permalink
introduce JoinBattle & LeaveBattle command-processors
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed Jan 18, 2011
1 parent 8965ff2 commit 3e95a13
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 63 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/springrts/tasserver/Battles.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* @author Betalord
*/
public class Battles implements ContextReceiver {

private static final Log s_log = LogFactory.getLog(Battle.class);

private List<Battle> battles;
private Context context = null;

Expand All @@ -31,6 +36,14 @@ public void receiveContext(Context context) {
}
}

public void verify(Battle battle) {

if (battle == null) {
s_log.fatal("Invalid battle ID. Server will now exit!");
context.getServer().closeServerAndExit();
}
}

public int getBattlesSize() {
return battles.size();
}
Expand Down
74 changes: 11 additions & 63 deletions src/main/java/com/springrts/tasserver/TASServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,6 @@ private void readIncomingMessages() {
}
}

private void verifyBattle(Battle battle) {

if (battle == null) {
s_log.fatal("Invalid battle ID. Server will now exit!");
context.getServer().closeServerAndExit();
}
}

public boolean redirectAndKill(Socket socket) {
if (!context.getServer().isRedirectActive()) {
return false;
Expand Down Expand Up @@ -508,50 +500,6 @@ public boolean tryToExecCommand(String command, Client client) {
if (response.substring(0, 12).toUpperCase().equals("SERVERMSGBOX")) {
context.getClients().killClient(client);
}
} else if (commands[0].equals("JOINBATTLEDENY")) {
if (commands.length < 2) {
return false;
}
if (client.getAccount().getAccess().compareTo(Account.Access.NORMAL) < 0) {
return false;
}
if (client.getBattleID() == Battle.NO_BATTLE_ID) {
return false;
}
Battle bat = context.getBattles().getBattleByID(client.getBattleID());
if (bat == null) {
return false;
}
if (bat.getFounder() != client) {
return false;
} // only founder can deny battle join
Client joiningClient = context.getClients().getClient(commands[1]);
if (joiningClient == null) {
return false;
}
if (joiningClient.getRequestedBattleID() != client.getBattleID()) {
return false;
}
joiningClient.setRequestedBattleID(Battle.NO_BATTLE_ID);
if(commands.length > 2) {
joiningClient.sendLine("JOINBATTLEFAILED Denied by battle founder - " + Misc.makeSentence(commands, 2));
} else {
joiningClient.sendLine("JOINBATTLEFAILED Denied by battle founder");
}
} else if (commands[0].equals("LEAVEBATTLE")) {
if (commands.length != 1) {
return false;
}
if (client.getAccount().getAccess().compareTo(Account.Access.NORMAL) < 0) {
return false;
}

if (client.getBattleID() == Battle.NO_BATTLE_ID) {
return false; // this may happen when client sent LEAVEBATTLE command right after he was kicked from the battle, for example.
}
Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().leaveBattle(client, bat); // automatically checks if client is a founder and closes battle
} else if (commands[0].equals("MYBATTLESTATUS")) {
if (commands.length != 3) {
return false;
Expand Down Expand Up @@ -943,7 +891,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

int value;
try {
Expand Down Expand Up @@ -993,7 +941,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

Bot bot = bat.getBot(commands[1]);
if (bot == null) {
Expand All @@ -1018,7 +966,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

Bot bot = bat.getBot(commands[1]);
if (bot == null) {
Expand Down Expand Up @@ -1147,7 +1095,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

if (!bat.isClientInBattle(commands[1])) {
client.sendLine("SERVERMSG RING command failed: You don't have permission to ring players other than those participating in your battle!");
Expand Down Expand Up @@ -1182,7 +1130,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

if (bat.getFounder() != client) {
return false;
Expand Down Expand Up @@ -1235,7 +1183,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

if (bat.getFounder() != client) {
return false;
Expand Down Expand Up @@ -1275,7 +1223,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

bat.getTempReplayScript().clear();
} else if (commands[0].equals("SCRIPT")) {
Expand All @@ -1288,7 +1236,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

bat.getTempReplayScript().add(Misc.makeSentence(commands, 1));
} else if (commands[0].equals("SCRIPTEND")) {
Expand All @@ -1301,7 +1249,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

// copy temp script to active script:
bat.ratifyTempScript();
Expand All @@ -1317,7 +1265,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

if (bat.getFounder() != client) {
return false;
Expand Down Expand Up @@ -1425,7 +1373,7 @@ public boolean tryToExecCommand(String command, Client client) {
}

Battle bat = context.getBattles().getBattleByID(client.getBattleID());
verifyBattle(bat);
context.getBattles().verify(bat);

if (bat.getFounder() != client) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@
import com.springrts.tasserver.commands.impl.Ip2CountryCommandProcessor;
import com.springrts.tasserver.commands.impl.JoinBattleAcceptCommandProcessor;
import com.springrts.tasserver.commands.impl.JoinBattleCommandProcessor;
import com.springrts.tasserver.commands.impl.JoinBattleDenyCommandProcessor;
import com.springrts.tasserver.commands.impl.JoinCommandProcessor;
import com.springrts.tasserver.commands.impl.KickUserCommandProcessor;
import com.springrts.tasserver.commands.impl.KillAllCommandProcessor;
import com.springrts.tasserver.commands.impl.KillCommandProcessor;
import com.springrts.tasserver.commands.impl.KillIpCommandProcessor;
import com.springrts.tasserver.commands.impl.LeaveBattleCommandProcessor;
import com.springrts.tasserver.commands.impl.LeaveCommandProcessor;
import com.springrts.tasserver.commands.impl.LoginCommandProcessor;
import com.springrts.tasserver.commands.impl.LongTimeToDateCommandProcessor;
Expand Down Expand Up @@ -230,6 +232,8 @@ public void init() {
commandProcessorClasses.add(SayBattleExCommandProcessor.class);
commandProcessorClasses.add(JoinBattleCommandProcessor.class);
commandProcessorClasses.add(JoinBattleAcceptCommandProcessor.class);
commandProcessorClasses.add(JoinBattleDenyCommandProcessor.class);
commandProcessorClasses.add(LeaveBattleCommandProcessor.class);

try {
load(commandProcessorClasses);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright (c) 2010 Robin Vobruba <robin.vobruba@derisk.ch>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.springrts.tasserver.commands.impl;


import com.springrts.tasserver.Account;
import com.springrts.tasserver.Battle;
import com.springrts.tasserver.Client;
import com.springrts.tasserver.Misc;
import com.springrts.tasserver.commands.AbstractCommandProcessor;
import com.springrts.tasserver.commands.CommandProcessingException;
import com.springrts.tasserver.commands.SupportedCommand;
import java.util.List;

/**
* Sent by client in response to a JOINBATTLEREQUEST command in order to prevent
* the user from joining the battle.
* @author hoijui
*/
@SupportedCommand("JOINBATTLEDENY")
public class JoinBattleDenyCommandProcessor extends AbstractCommandProcessor {

public JoinBattleDenyCommandProcessor() {
super(1, ARGS_MAX_NOCHECK, Account.Access.NORMAL);
}

@Override
public boolean process(Client client, List<String> args)
throws CommandProcessingException
{
boolean checksOk = super.process(client, args);
if (!checksOk) {
return false;
}

if (client.getBattleID() == Battle.NO_BATTLE_ID) {
return false;
}

Battle bat = getContext().getBattles().getBattleByID(client.getBattleID());
if (bat == null) {
return false;
}

// only the founder can deny a battle join
if (bat.getFounder() != client) {
return false;
}

String username = args.get(0);
Client joiningClient = getContext().getClients().getClient(username);
if (joiningClient == null) {
return false;
}
if (joiningClient.getRequestedBattleID() != client.getBattleID()) {
return false;
}
joiningClient.setRequestedBattleID(Battle.NO_BATTLE_ID);
if(args.size() > 1) {
String reason = Misc.makeSentence(args, 1);
joiningClient.sendLine("JOINBATTLEFAILED Denied by battle founder - " + reason);
} else {
joiningClient.sendLine("JOINBATTLEFAILED Denied by battle founder");
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright (c) 2010 Robin Vobruba <robin.vobruba@derisk.ch>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.springrts.tasserver.commands.impl;


import com.springrts.tasserver.Account;
import com.springrts.tasserver.Battle;
import com.springrts.tasserver.Client;
import com.springrts.tasserver.commands.AbstractCommandProcessor;
import com.springrts.tasserver.commands.CommandProcessingException;
import com.springrts.tasserver.commands.SupportedCommand;
import java.util.List;

/**
* Sent by the client when he leaves a battle.
* Also sent by a founder of the battle when he closes the battle.
* @author hoijui
*/
@SupportedCommand("LEAVEBATTLE")
public class LeaveBattleCommandProcessor extends AbstractCommandProcessor {

public LeaveBattleCommandProcessor() {
super(0, 0, Account.Access.NORMAL);
}

@Override
public boolean process(Client client, List<String> args)
throws CommandProcessingException
{
boolean checksOk = super.process(client, args);
if (!checksOk) {
return false;
}

if (client.getBattleID() == Battle.NO_BATTLE_ID) {
// this may happen when client sent the LEAVEBATTLE command
// right after he was kicked from the battle, for example.
return false;
}
Battle bat = getContext().getBattles().getBattleByID(client.getBattleID());
getContext().getBattles().verify(bat);

// automatically checks if the client is a founder and closes the battle
getContext().getBattles().leaveBattle(client, bat);

return true;
}
}

0 comments on commit 3e95a13

Please sign in to comment.