Skip to content

Commit

Permalink
Added chat messages when game is started and when invite is declined
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Oct 6, 2014
1 parent 0129526 commit 74c6964
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public void broadcast(ChatMessage message) {
clients.forEach(cl -> cl.sendToClient(message));
}

public void broadcast(String from, String message) {
broadcast(new ChatMessage(id, from, message));
}

public void add(ClientIO client) {
clients.add(client);
broadcast(new ChatMessage(id, "Server Chat " + id, client.getName() + " has joined the chat"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.cardshifter.api.both.ChatMessage;
import com.cardshifter.api.both.InviteRequest;
import com.cardshifter.server.main.FakeAIClientTCG;

Expand All @@ -20,11 +22,13 @@ public class GameInvite implements IdObject {
private final ServerGame game;
private final List<ClientIO> invited;
private final List<ClientIO> players;
private final ChatArea chatArea;

public GameInvite(Server server, int id, ClientIO host, ServerGame game) {
public GameInvite(Server server, int id, ChatArea chatlog, ClientIO host, ServerGame game) {
this.id = id;
this.host = host;
this.game = game;
this.chatArea = chatlog;
this.invited = Collections.synchronizedList(new ArrayList<>());
this.players = Collections.synchronizedList(new ArrayList<>());
players.add(host);
Expand Down Expand Up @@ -58,13 +62,15 @@ public boolean inviteAccept(ClientIO who) {

public boolean inviteDecline(ClientIO who) {
logger.info(this + " Invite Decline: " + who);
host.sendToClient(new ChatMessage(1, "Server", who.getName() + " declined invite"));
return invited.remove(who);
}

public boolean start() {
logger.info(this + " Game Start! " + players);
Collections.shuffle(players, random);
game.start(players);
chatArea.broadcast("Server", players.stream().map(io -> io.getName()).collect(Collectors.joining(", ")) + " are now playing game " + game.getId());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void play(StartGameRequest message, ClientIO client) {

ServerGame game = server.createGame(message.getGameType());
ServerHandler<GameInvite> invites = server.getInvites();
GameInvite invite = new GameInvite(server, invites.newId(), client, game);
GameInvite invite = new GameInvite(server, invites.newId(), server.getMainChat(), client, game);
invites.add(invite);
client.sendToClient(new WaitMessage());

Expand Down Expand Up @@ -99,7 +99,7 @@ private void playAny(StartGameRequest message, ClientIO client) {

ServerGame game = server.createGame(message.getGameType());
ServerHandler<GameInvite> invites = server.getInvites();
GameInvite invite = new GameInvite(server, invites.newId(), client, game);
GameInvite invite = new GameInvite(server, invites.newId(), server.getMainChat(), client, game);
invites.add(invite);
invite.addPlayer(opponent);
invite.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -132,62 +130,6 @@ public void addGameFactory(String gameType, GameFactory factory) {
this.gameFactories.put(gameType, factory);
}

@Deprecated
public boolean inviteRequest(Command cmd) {
final GameInvite invite;
switch (cmd.getCommand()) {
case "INVT":
String target = cmd.getParameter(2);
ServerGame game = createGame(cmd.getParameter(1));
// FindBugs tells this is redundant
// if (game == null) {
// cmd.getSender().sendToClient("FAIL Game creation failed");
// return false;
// }
invite = new GameInvite(this, invites.newId(), cmd.getSender(), game);
this.invites.add(invite);

Stream<ClientIO> targetStream = clients.values().stream().filter(cl -> cl.getName().equals(target));
Optional<ClientIO> result = targetStream.findFirst();
if (result.isPresent()) {
invite.sendInvite(result.get());
}
else {
cmd.getSender().sendToClient("FAIL No such user");
}
return result.isPresent();
case "INVY":
invite = invites.get(cmd.getParameterInt(1));
if (invite == null) {
cmd.getSender().sendToClient("FAIL Invalid invite id");
return false;
}
return invite.inviteAccept(cmd.getSender());
case "INVN":
invite = invites.get(cmd.getParameterInt(1));
if (invite == null) {
cmd.getSender().sendToClient("FAIL Invalid invite id");
return false;
}
return invite.inviteDecline(cmd.getSender());
default:
throw new AssertionError("Invalid command: " + cmd);
}
}

@Deprecated
public void incomingGameCommand(Command cmd) {
ServerGame game = games.get(cmd.getParameterInt(1));
if (game != null) {
if (!game.handleMove(cmd)) {
cmd.getSender().sendToClient("FAIL Invalid move");
}
}
else {
cmd.getSender().sendToClient("FAIL Invalid gameid");
}
}

public ServerGame createGame(String parameter) {
GameFactory suppl = gameFactories.get(parameter);
if (suppl == null) {
Expand Down

0 comments on commit 74c6964

Please sign in to comment.