Merge pull request #115 from jwdeveloper/develop-1.8.13

Develop 1.8.13
This commit is contained in:
David Kohler
2024-12-23 22:41:01 -05:00
committed by GitHub
4 changed files with 60 additions and 27 deletions

View File

@@ -30,6 +30,8 @@ import io.github.jwdeveloper.tiktok.messages.enums.LinkMicBattleStatus;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle;
import lombok.Getter;
import java.util.List;
/**
* Triggered every time a battle starts & ends
*/
@@ -73,21 +75,52 @@ public class TikTokLinkMicBattleEvent extends TikTokHeaderEvent
public Team1v1 get1v1Team(String battleHostName) {
if (!is1v1())
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
return team1.getAs1v1Team();
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
return team2.getAs1v1Team();
return null;
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(0).getAs1v1Team();
}
public Team2v2 get2v2Team(String battleHostName) {
if (!is2v2())
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(0).getAs2v2Team();
}
/**
* @param battleHostName name of host to search
* @return Team1v1 instance not containing name of host */
public Team1v1 get1v1OpponentTeam(String battleHostName) {
if (!is1v1())
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(1).getAs1v1Team();
}
public Team2v2 get2x2OpponentTeam(String battleHostName) {
if (!is2v2())
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(1).getAs2v2Team();
}
/**
* @param battleHostName name of host to search
* @return {@link List<Team>} with host team first, then opponent team
* <p> Empty if host is in neither otherwise always 2 in length;
*/
public List<Team> getTeams(String battleHostName) {
if (is1v1()) {
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
return List.of(team1, team2);
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
return List.of(team2, team1);
} else {
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return team1.getAs2v2Team();
return List.of(team1, team2);
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return team2.getAs2v2Team();
return null;
return List.of(team2, team1);
}
return List.of();
}
public boolean is1v1() {

View File

@@ -33,7 +33,7 @@ import java.util.function.Consumer;
@Setter
public class ProxyClientSettings implements Iterator<ProxyData>, Iterable<ProxyData>
{
private boolean enabled, autoDiscard = true, fallback = true;
private boolean enabled, autoDiscard = true, fallback = true, allowWebsocket = true;
private Rotation rotation = Rotation.CONSECUTIVE;
private final List<ProxyData> proxyList = new ArrayList<>();
private int index;

View File

@@ -74,10 +74,10 @@ public class TikTokWebSocketClient implements LiveSocketClient {
tikTokEventHandler,
liveClient);
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
// if (proxyClientSettings.isEnabled())
// connectProxy(proxyClientSettings);
// else
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
if (proxyClientSettings.isEnabled() && proxyClientSettings.isAllowWebsocket())
connectProxy(proxyClientSettings);
else
connectDefault();
}
@@ -115,15 +115,14 @@ public class TikTokWebSocketClient implements LiveSocketClient {
}
while (proxySettings.hasNext()) {
ProxyData proxyData = proxySettings.next();
if (!tryProxyConnection(proxySettings, proxyData)) {
if (proxySettings.isAutoDiscard())
proxySettings.remove();
continue;
}
if (tryProxyConnection(proxySettings, proxyData)) {
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
isConnected = true;
break;
}
if (proxySettings.isAutoDiscard())
proxySettings.remove();
}
if (!isConnected)
throw new TikTokLiveException("Failed to connect to the websocket");
}

View File

@@ -24,13 +24,15 @@ package io.github.jwdeveloper.tiktok.websocket;
import org.java_websocket.WebSocket;
import java.util.Base64;
public class WebSocketHeartbeatTask
{
private Thread thread;
private boolean isRunning = false;
private final int MAX_TIMEOUT = 250;
private final int SLEEP_TIME = 500;
private final byte[] heartbeatBytes = {58, 2, 104, 98}; // Byte Array of "3A026862" which is TikTok's custom heartbeat value
private final byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.
public void run(WebSocket webSocket, long pingTaskTime) {
stop();
@@ -58,6 +60,5 @@ public class WebSocketHeartbeatTask
isRunning = false;
}
}
}
}