mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Began rework to dynamic gifts. Did not fetch from url yet.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,7 +6,6 @@ backend-infrastructure/.aws-sam
|
||||
*.db
|
||||
### Linux ###
|
||||
*~
|
||||
.db
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
|
||||
@@ -22,10 +22,8 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.data.events.gift;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
|
||||
import io.github.jwdeveloper.tiktok.annotations.EventType;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftSendType;
|
||||
import io.github.jwdeveloper.tiktok.annotations.*;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
import io.github.jwdeveloper.tiktok.data.models.users.User;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import lombok.Getter;
|
||||
@@ -49,8 +47,8 @@ import lombok.Getter;
|
||||
public class TikTokGiftComboEvent extends TikTokGiftEvent {
|
||||
private final GiftSendType comboState;
|
||||
|
||||
public TikTokGiftComboEvent(GiftOld gift, User host, WebcastGiftMessage msg, GiftSendType comboState) {
|
||||
public TikTokGiftComboEvent(Gift gift, User host, WebcastGiftMessage msg, GiftSendType comboState) {
|
||||
super(gift, host, msg);
|
||||
this.comboState = comboState;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,9 @@
|
||||
package io.github.jwdeveloper.tiktok.data.events.gift;
|
||||
|
||||
|
||||
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
|
||||
import io.github.jwdeveloper.tiktok.annotations.EventType;
|
||||
import io.github.jwdeveloper.tiktok.annotations.*;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokHeaderEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
import io.github.jwdeveloper.tiktok.data.models.users.User;
|
||||
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import lombok.Getter;
|
||||
@@ -40,12 +39,12 @@ import lombok.Getter;
|
||||
@EventMeta(eventType = EventType.Message)
|
||||
@Getter
|
||||
public class TikTokGiftEvent extends TikTokHeaderEvent {
|
||||
private final GiftOld gift;
|
||||
private final Gift gift;
|
||||
private final User user;
|
||||
private final User toUser;
|
||||
private final int combo;
|
||||
|
||||
public TikTokGiftEvent(GiftOld gift, User liveHost, WebcastGiftMessage msg) {
|
||||
public TikTokGiftEvent(Gift gift, User liveHost, WebcastGiftMessage msg) {
|
||||
super(msg.getCommon());
|
||||
this.gift = gift;
|
||||
user = User.map(msg.getUser(), msg.getUserIdentity());
|
||||
|
||||
@@ -2,12 +2,17 @@ package io.github.jwdeveloper.tiktok.data.models.gifts;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Gift {
|
||||
public class Gift
|
||||
{
|
||||
@Getter private static final Set<Gift> gifts = new HashSet<>();
|
||||
public static final Gift UNDEFINED = new Gift(-1, "undefined", -1, "", null);
|
||||
|
||||
private final int id;
|
||||
|
||||
private final String name;
|
||||
@@ -17,4 +22,20 @@ public class Gift {
|
||||
private final Picture picture;
|
||||
|
||||
private final JsonObject properties;
|
||||
}
|
||||
|
||||
public Gift(int id, String name, int diamondCost, String pictureLink, JsonObject properties) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.diamondCost = diamondCost;
|
||||
this.picture = new Picture(pictureLink);
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public boolean hasDiamondCostRange(int minimalCost, int maximalCost) {
|
||||
return diamondCost >= minimalCost && diamondCost <= maximalCost;
|
||||
}
|
||||
|
||||
public boolean hasDiamondCost(int cost) {
|
||||
return diamondCost == cost;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -28,13 +28,11 @@ public enum GiftSendType
|
||||
Begin,
|
||||
Active;
|
||||
|
||||
|
||||
public static GiftSendType fromNumber(long number)
|
||||
{
|
||||
return switch ((int) number) {
|
||||
case 0 -> GiftSendType.Finished;
|
||||
case 1, 2, 4 -> GiftSendType.Active;
|
||||
default -> GiftSendType.Finished;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,9 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.live;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -38,26 +39,29 @@ public interface GiftManager {
|
||||
* @param diamondCost diamond cost
|
||||
* @return
|
||||
*/
|
||||
GiftOld registerGift(int id, String name, int diamondCost, Picture picture);
|
||||
default Gift registerGift(int id, String name, int diamondCost, Picture picture) {
|
||||
return registerGift(id, name, diamondCost, picture, null);
|
||||
}
|
||||
|
||||
Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param giftId
|
||||
* @return
|
||||
*/
|
||||
GiftOld findById(int giftId);
|
||||
Gift findById(int giftId);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param giftName
|
||||
* @return
|
||||
*/
|
||||
GiftOld findByName(String giftName);
|
||||
Gift findByName(String giftName);
|
||||
|
||||
/**
|
||||
*
|
||||
* @return all gifts
|
||||
*/
|
||||
List<GiftOld> getGifts();
|
||||
}
|
||||
List<Gift> getGifts();
|
||||
}
|
||||
@@ -22,19 +22,18 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.gifts;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class TikTokGiftManager implements GiftManager {
|
||||
|
||||
private final Map<Integer, GiftOld> indexById;
|
||||
private final Map<String, GiftOld> indexByName;
|
||||
private final Map<Integer, Gift> indexById;
|
||||
private final Map<String, Gift> indexByName;
|
||||
private final Logger logger;
|
||||
|
||||
public TikTokGiftManager(Logger logger)
|
||||
@@ -46,58 +45,29 @@ public class TikTokGiftManager implements GiftManager {
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
for (var gift : GiftOld.values()) {
|
||||
for (var gift : Gift.getGifts()) {
|
||||
indexById.put(gift.getId(), gift);
|
||||
indexByName.put(gift.getName(), gift);
|
||||
}
|
||||
}
|
||||
|
||||
public GiftOld registerGift(int id, String name, int diamondCost, Picture picture) {
|
||||
try {
|
||||
var constructor = Unsafe.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
var unsafe = (Unsafe) constructor.newInstance();
|
||||
GiftOld enumInstance = (GiftOld) unsafe.allocateInstance(GiftOld.class);
|
||||
|
||||
var field = GiftOld.class.getDeclaredField("id");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, id);
|
||||
|
||||
field = GiftOld.class.getDeclaredField("name");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, name);
|
||||
|
||||
|
||||
// EnumSet
|
||||
field = GiftOld.class.getDeclaredField("diamondCost");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, diamondCost);
|
||||
|
||||
field = GiftOld.class.getDeclaredField("picture");
|
||||
field.setAccessible(true);
|
||||
field.set(enumInstance, picture);
|
||||
|
||||
indexById.put(enumInstance.getId(), enumInstance);
|
||||
indexByName.put(enumInstance.getName(), enumInstance);
|
||||
|
||||
return enumInstance;
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unable to register gift: " + name + ": " + id);
|
||||
}
|
||||
public Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties) {
|
||||
Gift gift = new Gift(id, name, diamondCost, picture, properties);
|
||||
indexById.put(gift.getId(), gift);
|
||||
indexByName.put(gift.getName(), gift);
|
||||
return gift;
|
||||
}
|
||||
|
||||
public GiftOld findById(int giftId) {
|
||||
GiftOld gift = indexById.get(giftId);
|
||||
return gift == null ? GiftOld.UNDEFINED : gift;
|
||||
public Gift findById(int giftId) {
|
||||
return indexById.getOrDefault(giftId, Gift.UNDEFINED);
|
||||
}
|
||||
|
||||
public GiftOld findByName(String giftName) {
|
||||
GiftOld gift = indexByName.get(giftName);
|
||||
return gift == null ? GiftOld.UNDEFINED : gift;
|
||||
public Gift findByName(String giftName) {
|
||||
return indexByName.getOrDefault(giftName, Gift.UNDEFINED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GiftOld> getGifts() {
|
||||
public List<Gift> getGifts() {
|
||||
return indexById.values().stream().toList();
|
||||
}
|
||||
}
|
||||
@@ -24,11 +24,9 @@ package io.github.jwdeveloper.tiktok.mappers.handlers;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.*;
|
||||
import io.github.jwdeveloper.tiktok.data.models.Picture;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftSendType;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
||||
import io.github.jwdeveloper.tiktok.mappers.TikTokMapperHelper;
|
||||
@@ -37,9 +35,7 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
|
||||
import lombok.SneakyThrows;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class TikTokGiftEventHandler {
|
||||
private final GiftManager giftManager;
|
||||
@@ -112,37 +108,35 @@ public class TikTokGiftEventHandler {
|
||||
return new TikTokGiftComboEvent(gift, tikTokRoomInfo.getHost(), message, state);
|
||||
}
|
||||
|
||||
private GiftOld getGiftObject(WebcastGiftMessage giftMessage) {
|
||||
private Gift getGiftObject(WebcastGiftMessage giftMessage) {
|
||||
var giftId = (int) giftMessage.getGiftId();
|
||||
var gift = giftManager.findById(giftId);
|
||||
if (gift == GiftOld.UNDEFINED) {
|
||||
if (gift == Gift.UNDEFINED)
|
||||
gift = giftManager.findByName(giftMessage.getGift().getName());
|
||||
}
|
||||
if (gift == GiftOld.UNDEFINED) {
|
||||
if (gift == Gift.UNDEFINED) {
|
||||
gift = giftManager.registerGift(
|
||||
giftId,
|
||||
giftMessage.getGift().getName(),
|
||||
giftMessage.getGift().getDiamondCount(),
|
||||
Picture.map(giftMessage.getGift().getImage()));
|
||||
giftId,
|
||||
giftMessage.getGift().getName(),
|
||||
giftMessage.getGift().getDiamondCount(),
|
||||
Picture.map(giftMessage.getGift().getImage()));
|
||||
}
|
||||
|
||||
if (gift.getPicture().getLink().endsWith(".webp")) {
|
||||
if (gift.getPicture().getLink().endsWith(".webp"))
|
||||
updatePicture(gift, giftMessage);
|
||||
}
|
||||
return gift;
|
||||
}
|
||||
|
||||
|
||||
private void updatePicture(GiftOld gift, WebcastGiftMessage webcastGiftMessage) {
|
||||
// TODO-kohlerpop1: I do not think this method is needed for any reason?
|
||||
private void updatePicture(Gift gift, WebcastGiftMessage webcastGiftMessage) {
|
||||
try {
|
||||
var picture = Picture.map(webcastGiftMessage.getGift().getImage());
|
||||
var constructor = Unsafe.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
var field = GiftOld.class.getDeclaredField("picture");
|
||||
var field = Gift.class.getDeclaredField("picture");
|
||||
field.setAccessible(true);
|
||||
field.set(gift, picture);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unable to update picture in gift: " + gift.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,18 +23,18 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
public class CustomEventExample {
|
||||
@AllArgsConstructor
|
||||
public static class CheapGiftEvent extends TikTokEvent {
|
||||
GiftOld gift;
|
||||
Gift gift;
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class ExpensiveGiftEvent extends TikTokEvent {
|
||||
GiftOld gift;
|
||||
Gift gift;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
@@ -62,4 +62,4 @@ public class CustomEventExample {
|
||||
})
|
||||
.buildAndConnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,12 +84,12 @@ public class ListenerExample
|
||||
|
||||
@TikTokEventObserver
|
||||
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
|
||||
var message = switch (event.getGift()) {
|
||||
case ROSE -> "Thanks :)";
|
||||
case APPETIZERS -> ":OO";
|
||||
case APRIL -> ":D";
|
||||
case TIKTOK -> ":P";
|
||||
case CAP -> ":F";
|
||||
var message = switch (event.getGift().getName()) {
|
||||
case "ROSE" -> "Thanks :)";
|
||||
case "APPETIZERS" -> ":OO";
|
||||
case "APRIL" -> ":D";
|
||||
case "TIKTOK" -> ":P";
|
||||
case "CAP" -> ":F";
|
||||
default -> ":I";
|
||||
};
|
||||
liveClient.getLogger().info(message);
|
||||
@@ -115,4 +115,4 @@ public class ListenerExample
|
||||
""");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,10 +84,10 @@ public class SimpleExample {
|
||||
})
|
||||
.onGift((liveClient, event) ->
|
||||
{
|
||||
switch (event.getGift()) {
|
||||
case ROSE -> print(ConsoleColors.RED, "Rose!");
|
||||
case GG -> print(ConsoleColors.YELLOW, " GOOD GAME!");
|
||||
case TIKTOK -> print(ConsoleColors.CYAN, "Thanks for TikTok");
|
||||
switch (event.getGift().getName()) {
|
||||
case "ROSE" -> print(ConsoleColors.RED, "Rose!");
|
||||
case "GG" -> print(ConsoleColors.YELLOW, " GOOD GAME!");
|
||||
case "TIKTOK" -> print(ConsoleColors.CYAN, "Thanks for TikTok");
|
||||
default ->
|
||||
print(ConsoleColors.GREEN, "[Thanks for gift] ", ConsoleColors.YELLOW, event.getGift().getName(), "x", event.getCombo());
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ Are you willing to help or improve TikTokLiveJava?
|
||||
#### Tools
|
||||
Project that contains code generators.
|
||||
The most useful one is class `GenerateGiftsEnum` that download gifts json from TikTok
|
||||
and generates code for `Gift` enum that is later added to `API` module at path `io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld`
|
||||
and generates code for `Gift` enum that is later added to `API` module at path `io.github.jwdeveloper.tiktok.data.models.gifts.Gift`
|
||||
|
||||
#### Tools-EventsCollector
|
||||
Tool that can be used to store all `protocol-buffer` and `events` from live to `sqlLite` database or `Json` file
|
||||
|
||||
@@ -52,16 +52,6 @@ public class RecorderListener implements LiveRecorder {
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unpause() {
|
||||
|
||||
}
|
||||
|
||||
@TikTokEventObserver
|
||||
private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event) {
|
||||
settings = RecorderSettings.DEFAULT();
|
||||
|
||||
Reference in New Issue
Block a user