Converted list of teams to just 2 teams as we know the size is always 2 and updated methods accordingly.

This commit is contained in:
kohlerpop1
2024-12-03 09:43:17 -05:00
parent ca741ed931
commit 8cd640f8eb

View File

@@ -28,9 +28,7 @@ import io.github.jwdeveloper.tiktok.data.models.battles.*;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.messages.enums.LinkMicBattleStatus; import io.github.jwdeveloper.tiktok.messages.enums.LinkMicBattleStatus;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle;
import lombok.*; import lombok.Getter;
import java.util.*;
/** /**
* Triggered every time a battle starts & ends * Triggered every time a battle starts & ends
@@ -44,23 +42,22 @@ public class TikTokLinkMicBattleEvent extends TikTokHeaderEvent
true if battle is finished otherwise false true if battle is finished otherwise false
*/ */
private final boolean finished; private final boolean finished;
private final List<Team> teams; private final Team team1, team2;
public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) { public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) {
super(msg.getCommon()); super(msg.getCommon());
battleId = msg.getId(); battleId = msg.getId();
finished = msg.getBattleStatus() == LinkMicBattleStatus.BATTLE_FINISHED; finished = msg.getBattleStatus() == LinkMicBattleStatus.BATTLE_FINISHED;
teams = new ArrayList<>();
if (msg.getHostTeamCount() == 2) { // 1v1 battle if (msg.getHostTeamCount() == 2) { // 1v1 battle
teams.add(new Team1v1(msg.getHostTeam(0), msg)); team1 = new Team1v1(msg.getHostTeam(0), msg);
teams.add(new Team1v1(msg.getHostTeam(1), msg)); team2 = new Team1v1(msg.getHostTeam(1), msg);
} else { // 2v2 battle } else { // 2v2 battle
if (isFinished()) { if (isFinished()) {
teams.add(new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 1).findFirst().orElse(null), msg)); team1 = new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 1).findFirst().orElse(null), msg);
teams.add(new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 2).findFirst().orElse(null), msg)); team2 = new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 2).findFirst().orElse(null), msg);
} else { } else {
teams.add(new Team2v2(msg.getHostTeam(0), msg.getHostTeam(1), msg)); team1 = new Team2v2(msg.getHostTeam(0), msg.getHostTeam(1), msg);
teams.add(new Team2v2(msg.getHostTeam(2), msg.getHostTeam(3), msg)); team2 = new Team2v2(msg.getHostTeam(2), msg.getHostTeam(3), msg);
} }
} }
@@ -70,29 +67,38 @@ public class TikTokLinkMicBattleEvent extends TikTokHeaderEvent
// - msg.getHostTeamCount() always is 2 for 1v1 or 4 for 2v2 // - msg.getHostTeamCount() always is 2 for 1v1 or 4 for 2v2
} }
/**
* @param battleHostName name of host to search
* @return Team1v1 instance containing name of host or null if no team found */
public Team1v1 get1v1Team(String battleHostName) { public Team1v1 get1v1Team(String battleHostName) {
if (!is1v1()) if (!is1v1())
throw new TikTokLiveException("Teams are not instance of 1v1 battle!"); throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
return teams.stream().filter(team -> team.getAs1v1Team().getHost().getName().equals(battleHostName)).findFirst().map(Team::getAs1v1Team).orElse(null); if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
return team1.getAs1v1Team();
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
return team2.getAs1v1Team();
return null;
} }
public Team2v2 get2v2Team(String battleHostName) { public Team2v2 get2v2Team(String battleHostName) {
if (!is2v2()) if (!is2v2())
throw new TikTokLiveException("Teams are not instance of 2v2 battle!"); throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
return teams.stream().filter(team -> if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
team.getAs2v2Team().getHosts().stream().anyMatch(user -> return team1.getAs2v2Team();
user.getName().equals(battleHostName))).findFirst().map(Team::getAs2v2Team).orElse(null); if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return team2.getAs2v2Team();
return null;
} }
public boolean is1v1() { public boolean is1v1() {
return teams.get(0) instanceof Team1v1; return team1.is1v1Team() || team2.is1v1Team();
} }
public boolean is2v2() { public boolean is2v2() {
return teams.get(0) instanceof Team2v2; return team1.is2v2Team() || team2.is2v2Team();
} }
public boolean isTie() { public boolean isTie() {
return isFinished() && teams.get(0).getTotalPoints() == teams.get(1).getTotalPoints(); return isFinished() && team1.getTotalPoints() == team2.getTotalPoints();
} }
} }