|
30 | 30 | import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle; |
31 | 31 | import lombok.Getter; |
32 | 32 |
|
| 33 | +import java.util.List; |
| 34 | + |
33 | 35 | /** |
34 | 36 | * Triggered every time a battle starts & ends |
35 | 37 | */ |
@@ -73,21 +75,52 @@ public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) { |
73 | 75 | public Team1v1 get1v1Team(String battleHostName) { |
74 | 76 | if (!is1v1()) |
75 | 77 | throw new TikTokLiveException("Teams are not instance of 1v1 battle!"); |
76 | | - if (team1.getAs1v1Team().getHost().getName().equals(battleHostName)) |
77 | | - return team1.getAs1v1Team(); |
78 | | - if (team2.getAs1v1Team().getHost().getName().equals(battleHostName)) |
79 | | - return team2.getAs1v1Team(); |
80 | | - return null; |
| 78 | + List<Team> list = getTeams(battleHostName); |
| 79 | + return list.isEmpty() ? null : list.get(0).getAs1v1Team(); |
81 | 80 | } |
82 | 81 |
|
83 | 82 | public Team2v2 get2v2Team(String battleHostName) { |
84 | 83 | if (!is2v2()) |
85 | 84 | throw new TikTokLiveException("Teams are not instance of 2v2 battle!"); |
86 | | - if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName))) |
87 | | - return team1.getAs2v2Team(); |
88 | | - if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName))) |
89 | | - return team2.getAs2v2Team(); |
90 | | - return null; |
| 85 | + List<Team> list = getTeams(battleHostName); |
| 86 | + return list.isEmpty() ? null : list.get(0).getAs2v2Team(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param battleHostName name of host to search |
| 91 | + * @return Team1v1 instance not containing name of host */ |
| 92 | + public Team1v1 get1v1OpponentTeam(String battleHostName) { |
| 93 | + if (!is1v1()) |
| 94 | + throw new TikTokLiveException("Teams are not instance of 1v1 battle!"); |
| 95 | + List<Team> list = getTeams(battleHostName); |
| 96 | + return list.isEmpty() ? null : list.get(1).getAs1v1Team(); |
| 97 | + } |
| 98 | + |
| 99 | + public Team2v2 get2x2OpponentTeam(String battleHostName) { |
| 100 | + if (!is2v2()) |
| 101 | + throw new TikTokLiveException("Teams are not instance of 2v2 battle!"); |
| 102 | + List<Team> list = getTeams(battleHostName); |
| 103 | + return list.isEmpty() ? null : list.get(1).getAs2v2Team(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param battleHostName name of host to search |
| 108 | + * @return {@link List<Team>} with host team first, then opponent team |
| 109 | + * <p> Empty if host is in neither otherwise always 2 in length; |
| 110 | + */ |
| 111 | + public List<Team> getTeams(String battleHostName) { |
| 112 | + if (is1v1()) { |
| 113 | + if (team1.getAs1v1Team().getHost().getName().equals(battleHostName)) |
| 114 | + return List.of(team1, team2); |
| 115 | + if (team2.getAs1v1Team().getHost().getName().equals(battleHostName)) |
| 116 | + return List.of(team2, team1); |
| 117 | + } else { |
| 118 | + if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName))) |
| 119 | + return List.of(team1, team2); |
| 120 | + if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName))) |
| 121 | + return List.of(team2, team1); |
| 122 | + } |
| 123 | + return List.of(); |
91 | 124 | } |
92 | 125 |
|
93 | 126 | public boolean is1v1() { |
|
0 commit comments