// Removes a guild ID from a player's guild list
    IEnumerator RemoveGuildFromGuildList(string guildId, string accountId, LobbyPeer requester = null)
    {
        // Player online?
        if (LobbyPlayer.accountIdToLobbyPlayer.ContainsKey(accountId))
        {
            var playerKicked = LobbyPlayer.accountIdToLobbyPlayer[accountId];

            // Get guild ID list
            if (playerKicked.guildList == null)
            {
                yield return(GuildsDB.GetGuildList(accountId, data => {
                    if (data == null)
                    {
                        playerKicked.guildList = new GuildList();
                    }
                    else
                    {
                        playerKicked.guildList = data;
                    }
                }));
            }

            // Remove guild ID from the kicked player's guild ID list
            playerKicked.guildList.Remove(guildId);

            // Set guild ID list
            yield return(GuildsDB.SetGuildList(accountId, playerKicked.guildList));

            // Send the kicked player the new guild ID list
            SendGuildList(playerKicked);
            // Player offline
        }
        else
        {
            GuildList guildList = null;

            // Get guild ID list
            yield return(GuildsDB.GetGuildList(accountId, data => {
                guildList = data;
            }));

            if (guildList == null)
            {
                if (requester != null)
                {
                    Lobby.RPC("GuildKickError", requester, guildId, accountId);
                }
                yield break;
            }

            // Remove guild ID from the kicked player's guild ID list
            guildList.Remove(guildId);

            // Set guild ID list
            yield return(GuildsDB.SetGuildList(accountId, guildList));
        }
    }
    IEnumerator GuildRepresentRequest(string guildId, bool represent, LobbyMessageInfo info)
    {
        LobbyPlayer player    = LobbyServer.GetLobbyPlayer(info);
        string      accountId = player.accountId;

        // Get guild members from database
        if (!GameDB.guildIdToGuildMembers.ContainsKey(guildId))
        {
            yield return(GuildsDB.GetGuildMembers(guildId));
        }

        var guildMembers = GameDB.guildIdToGuildMembers[guildId];
        var index        = guildMembers.FindIndex(o => o.accountId == accountId);

        if (index == -1)
        {
            Lobby.RPC("GuildRepresentError", info.sender, guildId, represent);
            yield break;
        }

        // Get guild ID list
        if (player.guildList == null)
        {
            yield return(GuildsDB.GetGuildList(accountId, data => {
                if (data == null)
                {
                    player.guildList = new GuildList();
                }
                else
                {
                    player.guildList = data;
                }
            }));
        }

        // Set/unset main guild
        if (represent)
        {
            // Start representing
            player.guildList.mainGuildId = guildId;
        }
        else
        {
            // Stop representing
            if (player.guildList.mainGuildId == guildId)
            {
                player.guildList.mainGuildId = "";
            }
        }

        // Set guild ID list
        yield return(GuildsDB.SetGuildList(accountId, player.guildList));

        Lobby.RPC("GuildRepresentSuccess", info.sender, guildId, represent);
    }
    IEnumerator GuildCreationRequest(string name, string tag, LobbyMessageInfo info)
    {
        LobbyPlayer founder = LobbyServer.GetLobbyPlayer(info);

        // Protection against modified RPC packets
        if (!Validator.guildName.IsMatch(name))
        {
            yield break;
        }

        if (!Validator.guildTag.IsMatch(tag))
        {
            yield break;
        }

        // Check if guild name has already been registered
        bool guildNameExists = false;

        yield return(GuildsDB.GetGuildIdByGuildName(name, data => {
            if (data != null)
            {
                guildNameExists = true;
            }
        }));

        if (guildNameExists)
        {
            Lobby.RPC("GuildNameAlreadyExists", info.sender);
            yield break;
        }

        // Store new guild in database
        string guildId = null;
        var    guild   = new Guild(name, tag, founder.accountId);

        yield return(GuildsDB.PutGuild(
                         guild,
                         (key, data) => {
            if (key != null)
            {
                if (founder.guildList == null)
                {
                    founder.guildList = new GuildList();
                }

                guildId = key;
            }
        }
                         ));

        if (guildId == null)
        {
            Lobby.RPC("GuildCreationError", info.sender);
            yield break;
        }

        // Founder joins the guild automatically
        var memberList = new List <GuildMember>();        //GameDB.guildIdToGuildMembers[guildId];

        memberList.Add(new GuildMember(founder.accountId, (byte)GuildMember.Rank.Leader));
        yield return(GuildsDB.SetGuildMembers(guildId, memberList));

        founder.guildList.Add(guildId);

        // Store new guild membership in database
        yield return(GuildsDB.SetGuildList(founder.accountId, founder.guildList));

        // Let the player know that it worked
        Lobby.RPC("GuildCreationSuccess", info.sender);

        // Send him the new guild ID list
        SendGuildList(founder);

        LogManager.General.Log("Guild " + guild + " has been created.");
    }
    IEnumerator GuildInvitationResponse(string guildId, bool accepted, LobbyMessageInfo info)
    {
        LobbyPlayer player = LobbyServer.GetLobbyPlayer(info);

        // Get guild invitations
        if (player.guildInvitations == null)
        {
            yield return(GuildsDB.GetGuildInvitations(player.accountId, data => {
                if (data == null)
                {
                    player.guildInvitations = new List <string>();
                }
                else
                {
                    player.guildInvitations = data;
                }
            }));
        }

        if (player.guildInvitations == null)
        {
            Lobby.RPC("GuildInvitationResponseError", info.sender, guildId);
            yield break;
        }

        // Were you invited?
        if (!player.guildInvitations.Contains(guildId))
        {
            Lobby.RPC("GuildInvitationResponseError", info.sender, guildId);
            yield break;
        }

        // Did the player accept the invitation?
        if (accepted)
        {
            // Get guild members from database
            if (!GameDB.guildIdToGuildMembers.ContainsKey(guildId))
            {
                yield return(GuildsDB.GetGuildMembers(guildId));
            }

            var guildMembers = GameDB.guildIdToGuildMembers[guildId];
            guildMembers.Add(new GuildMember(player.accountId, player.name, (byte)GuildMember.Rank.Default));

            // Set guild members
            yield return(GuildsDB.SetGuildMembers(guildId, guildMembers));

            // Get guild ID list
            if (player.guildList == null)
            {
                yield return(GuildsDB.GetGuildList(player.accountId, data => {
                    if (data == null)
                    {
                        player.guildList = new GuildList();
                    }
                    else
                    {
                        player.guildList = data;
                    }
                }));
            }

            // Add to guild ID list
            player.guildList.Add(guildId);

            // Set guild ID list
            yield return(GuildsDB.SetGuildList(player.accountId, player.guildList));

            // Notify all guild members
            SendGuildMemberList(guildId, guildMembers);
        }

        // Remove guild from invitation list
        player.guildInvitations.Remove(guildId);

        // Set guild invitations
        yield return(GuildsDB.SetGuildInvitations(player.accountId, player.guildInvitations, data => {
            if (data == null)
            {
                Lobby.RPC("GuildInvitationResponseError", info.sender, guildId);
            }
            else
            {
                player.guildInvitations = data;
                Lobby.RPC("GuildInvitationResponseSuccess", info.sender, guildId, accepted);
                SendGuildList(player);
            }
        }));
    }