0

I usually know how to replace a do..while loop with a for loop, but in this case variable check2 is used 2 times for different loops with got me confused on how to correctly replace it with for loop.

public static void RelayChat(ref int con, ref string Message)
{
    string temp_SubName = "relaychat";
    WriteSub(ref temp_SubName);
    int check2 = 0;

    if (Message.Length == 0) return;

    var check = UserConToCheck(con);

    if (CheckMute(get_UserName(check)) || get_UserChat(check) > 4) return;

    if (get_UserChat(check) < 8) set_UserChat(check, get_UserChat(check) + 1);

    if (Message.Length > 100) Message = Message.Substring(0, 99);

    if (Message[0] == '!')
    {
        if (UserCommand(con, Message.Substring(1).Split(" ".ToCharArray()))) return;
    }

    if (PlayerAdapter.get_Reference(check).get_IsAdmin(1))
    {
        if (Message.Substring(0, 1) == "@")
        {
            string[] temp_cmds = Message.Substring(1).Split(",".ToCharArray());
            admin.AdminCommand(PlayerAdapter.get_Reference(check), ref temp_cmds);
            return;
        }
    }

    if (Message.StartsWith(":g::", true, null))
    {
        do
        {
            check2++;
            var guild1 = get_UserGuild(check);
            var guild2 = get_UserGuild(check2);
            if (guild1 == null || guild2 == null || guild1.ToLower() != guild2.ToLower()) continue;
            var thisuser = get_UserName(check);
            var targetuser = get_UserName(check2);
            var found = targetuser != null && thisuser != null && 
                        IgnoreUserList.ContainsKey(targetuser) && 
                        IgnoreUserList[targetuser].Contains(thisuser);

            if (found == false)
            {
                Winsock.PrivMsg(get_UserCon(check2), "14,1,[ " + get_UserGuild(check2) + " ] Chat - " + get_UserName(check) + " : " + Message.Substring(4));
            }
        } while (check2 != UserMax);
        return;
    }

    if (check <= 0) return;

        do
        {
            check2++;

            bool found = false;

            var user = get_UserName(check2);
            if (user != null && IgnoreUserList.ContainsKey(user))
            {
                found = Convert.ToBoolean(IgnoreUserList[get_UserName(check2)].Contains(get_UserName(check)));
            }

            if (found) return;

            if (get_UserLanded(check2) != get_UserLanded(check)) return;

            if (get_UserLanded(check2))
            {
                if (get_UserInBar(check2) == false &&
                    get_UserInUniversalBar(check2) == false) return;

                if (get_UserInUniversalBar(check2) && get_UserInUniversalBar(check))
                {
                    Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
                }
                else if (get_UserInBar(check2) && get_UserInBar(check))
                {
                    if (get_UserLastPlanet(check2) !=
                        get_UserLastPlanet(check)) return;

                    Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
                }
            }
            else if (get_UserLanded(check2) == false)
            {
                if (get_UserSector(check2) != get_UserSector(check)) return;

                if (get_UserZone(check2) != get_UserZone(check)) return;

                Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
            }

        } while (check2 != UserMax);
}

I know that the function might be a lot complex, but please ignore that fact and focus on just the do..while loops. Thank you!

1 Answers1

0

Just declare the for loop to use the existing variable.

int check2 = 0;

if (Message.StartsWith(":g::", true, null))
{
    for (; check2 < UserMax; check2++)
    {
        // Do stuff
    }
    return;
}

if (check <= 0) return;

for (; check2 < 200; check2++)
{
    // Do stuff
}
eddiecjc
  • 222
  • 2
  • 5
  • Although looking at the code snippet you posted, seems like the counters can be independent of each other (the method exits after executing the first do...while), wouldn't it be better just to get rid of check2 declared at the top, and each for loop to declare their own counter? This probably would save confusion when glancing through the code. – eddiecjc Feb 09 '18 at 04:15
  • Yup that was exactly what I did. I used your code, I just declared new counter for each loop in there. All worked fine, Thank you. – Steve Jobless Feb 09 '18 at 04:44