mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
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:
@@ -28,9 +28,7 @@ import io.github.jwdeveloper.tiktok.data.models.battles.*;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.messages.enums.LinkMicBattleStatus;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.*;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Triggered every time a battle starts & ends
|
||||
@@ -44,23 +42,22 @@ public class TikTokLinkMicBattleEvent extends TikTokHeaderEvent
|
||||
true if battle is finished otherwise false
|
||||
*/
|
||||
private final boolean finished;
|
||||
private final List<Team> teams;
|
||||
private final Team team1, team2;
|
||||
|
||||
public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) {
|
||||
super(msg.getCommon());
|
||||
battleId = msg.getId();
|
||||
finished = msg.getBattleStatus() == LinkMicBattleStatus.BATTLE_FINISHED;
|
||||
teams = new ArrayList<>();
|
||||
if (msg.getHostTeamCount() == 2) { // 1v1 battle
|
||||
teams.add(new Team1v1(msg.getHostTeam(0), msg));
|
||||
teams.add(new Team1v1(msg.getHostTeam(1), msg));
|
||||
team1 = new Team1v1(msg.getHostTeam(0), msg);
|
||||
team2 = new Team1v1(msg.getHostTeam(1), msg);
|
||||
} else { // 2v2 battle
|
||||
if (isFinished()) {
|
||||
teams.add(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));
|
||||
team1 = new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 1).findFirst().orElse(null), msg);
|
||||
team2 = new Team2v2(msg.getHostData2V2List().stream().filter(data -> data.getTeamNumber() == 2).findFirst().orElse(null), msg);
|
||||
} else {
|
||||
teams.add(new Team2v2(msg.getHostTeam(0), msg.getHostTeam(1), msg));
|
||||
teams.add(new Team2v2(msg.getHostTeam(2), msg.getHostTeam(3), msg));
|
||||
team1 = new Team2v2(msg.getHostTeam(0), msg.getHostTeam(1), 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
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
if (!is1v1())
|
||||
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) {
|
||||
if (!is2v2())
|
||||
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
|
||||
return teams.stream().filter(team ->
|
||||
team.getAs2v2Team().getHosts().stream().anyMatch(user ->
|
||||
user.getName().equals(battleHostName))).findFirst().map(Team::getAs2v2Team).orElse(null);
|
||||
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
|
||||
return team1.getAs2v2Team();
|
||||
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
|
||||
return team2.getAs2v2Team();
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean is1v1() {
|
||||
return teams.get(0) instanceof Team1v1;
|
||||
return team1.is1v1Team() || team2.is1v1Team();
|
||||
}
|
||||
|
||||
public boolean is2v2() {
|
||||
return teams.get(0) instanceof Team2v2;
|
||||
return team1.is2v2Team() || team2.is2v2Team();
|
||||
}
|
||||
|
||||
public boolean isTie() {
|
||||
return isFinished() && teams.get(0).getTotalPoints() == teams.get(1).getTotalPoints();
|
||||
return isFinished() && team1.getTotalPoints() == team2.getTotalPoints();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user