I haven't tested the code yet, would this code work?
Nope, will not.
The problem is here:
PHP:
for iLoopPlayer in range(gc.getMAX_CIV_PLAYERS()):
LoopPlayerTeam = gc.getTeam(iLoopPlayer)
You're looping here over the players. So, with the max players, the loop goes normally from 0 to 17.
But then you're getting team instances. So first, you try to get team #0, then team #1, till team #17. But again, what if you have an alliance? Then there's no team #17.
You're looping over players, not over the teams. You can't just drag teams out of this loop, this doesn't make that much sense

Just imagine: Player 1 and player 2 are in an alliance.
You loop over the players, you have the ID 1, and you say gc.getTeam(1), okay, that's fine. Player #1 is in team #1. But then, next round, with the ID 2, you say gc.getTeam(2)...wait...no...that is still team #1, but you have player #2. Player #2 is not in team #2, so using this command is logically wrong.
Same for your assignments of playerTeam and rivalTeam: You have player IDs, they are not necessarily the same as team IDs.
Right would be:
PHP:
for iLoopPlayer in range(gc.getMAX_CIV_PLAYERS()):
LoopPlayer = gc.getPlayer(iLoopPlayer)
if LoopPlayer.getTeam() == iTeam or LoopPlayer.getTeam() == iRivalTeam:
This loops over the players, gets first the right player instances, and then compares their team ID to the given IDs.
That should work (hopefully, because i can't test it right now).