package com.cardshifter.server.model;

import com.cardshifter.api.*;
import com.cardshifter.core.game.*;

import java.util.*;
import java.util.concurrent.*;

public class InviteManager {

    private final Server server;
    private final ServerHandler<GameInvite> inviteHandler = new ServerHandler<>();
    private final Set<ClientIO> currentInvitors = Collections.newSetFromMap(new ConcurrentHashMap<>());

    public InviteManager(Server server) {
        this.server = server;
    }

    private GameInvite createInvite(ClientIO sender, String gameType) {
        // Can only have one open invite at a time, see https://github.com/Cardshifter/Cardshifter/issues/373
        if (!currentInvitors.add(sender)) {
            return null;
        }

        ServerGame game = server.createGame(gameType);
        GameInvite invite = new GameInvite(this, server.getMainChat(), sender, game, gameType, inviteHandler.newId());
        inviteHandler.add(invite);

        return invite;
    }

    public void createAndSend(ClientIO sender, ClientIO receiver, String gameType) {
        GameInvite invite = createInvite(sender, gameType);
        if (invite != null) {
            invite.sendInvite(receiver);
        }
    }

    public void createAndAdd(ClientIO sender, ClientIO receiver, String gameType) {
        GameInvite invite = createInvite(sender, gameType);
        Objects.requireNonNull(invite);
        invite.addPlayer(receiver);
    }

    public void remove(GameInvite invite) {
        inviteHandler.remove(invite);
        currentInvitors.remove(invite.getHost());
    }

    public GameInvite getInvite(int id) {
        return inviteHandler.get(id);
    }

    public Collection<GameInvite> getInvites() {
        return inviteHandler.all().values();
    }

}

@@ -97,16 +97,43 @@
 .game.*;
+%0Aimport org.apache.log4j.*;
 %0A%0Aimport
@@ -208,16 +208,101 @@
 ager %7B%0A%0A
+    private static final Logger logger = LogManager.getLogger(InviteManager.class);%0A%0A
     priv
@@ -815,24 +815,100 @@
 (sender)) %7B%0A
+            logger.info(%22%5B%22 + sender + %22%5D already has an open invitation%22);%0A
             
@@ -1137,24 +1137,74 @@
 d(invite);%0A%0A
+        logger.info(%22Created %5B%22 + invite + %22%5D%22);%0A%0A
         retu
@@ -1392,24 +1392,99 @@
  != null) %7B%0A
+            logger.info(%22Sending %5B%22 + invite + %22%5D to %5B%22 + receiver + %22%5D%22);%0A
             
@@ -1748,32 +1748,32 @@
 ceiver);%0A    %7D%0A%0A
-
     public void 
@@ -1796,24 +1796,74 @@
 e invite) %7B%0A
+        logger.info(%22Removing %5B%22 + invite + %22%5D%22);%0A
         invi

Add logging to InviteManager