Began rework to dynamic gifts. Did not fetch from url yet.

This commit is contained in:
kohlerpop1
2024-02-21 17:27:02 -05:00
parent 0252b9a42f
commit a68eaba5a1
14 changed files with 789 additions and 817 deletions

1
.gitignore vendored
View File

@@ -6,7 +6,6 @@ backend-infrastructure/.aws-sam
*.db *.db
### Linux ### ### Linux ###
*~ *~
.db
# temporary files which can be created if a process still has a handle open of a deleted file # temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden* .fuse_hidden*

View File

@@ -22,10 +22,8 @@
*/ */
package io.github.jwdeveloper.tiktok.data.events.gift; package io.github.jwdeveloper.tiktok.data.events.gift;
import io.github.jwdeveloper.tiktok.annotations.EventMeta; import io.github.jwdeveloper.tiktok.annotations.*;
import io.github.jwdeveloper.tiktok.annotations.EventType; import io.github.jwdeveloper.tiktok.data.models.gifts.*;
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.users.User; import io.github.jwdeveloper.tiktok.data.models.users.User;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
import lombok.Getter; import lombok.Getter;
@@ -49,7 +47,7 @@ import lombok.Getter;
public class TikTokGiftComboEvent extends TikTokGiftEvent { public class TikTokGiftComboEvent extends TikTokGiftEvent {
private final GiftSendType comboState; 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); super(gift, host, msg);
this.comboState = comboState; this.comboState = comboState;
} }

View File

@@ -23,10 +23,9 @@
package io.github.jwdeveloper.tiktok.data.events.gift; package io.github.jwdeveloper.tiktok.data.events.gift;
import io.github.jwdeveloper.tiktok.annotations.EventMeta; import io.github.jwdeveloper.tiktok.annotations.*;
import io.github.jwdeveloper.tiktok.annotations.EventType;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokHeaderEvent; 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.data.models.users.User;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage; import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
import lombok.Getter; import lombok.Getter;
@@ -40,12 +39,12 @@ import lombok.Getter;
@EventMeta(eventType = EventType.Message) @EventMeta(eventType = EventType.Message)
@Getter @Getter
public class TikTokGiftEvent extends TikTokHeaderEvent { public class TikTokGiftEvent extends TikTokHeaderEvent {
private final GiftOld gift; private final Gift gift;
private final User user; private final User user;
private final User toUser; private final User toUser;
private final int combo; private final int combo;
public TikTokGiftEvent(GiftOld gift, User liveHost, WebcastGiftMessage msg) { public TikTokGiftEvent(Gift gift, User liveHost, WebcastGiftMessage msg) {
super(msg.getCommon()); super(msg.getCommon());
this.gift = gift; this.gift = gift;
user = User.map(msg.getUser(), msg.getUserIdentity()); user = User.map(msg.getUser(), msg.getUserIdentity());

View File

@@ -2,12 +2,17 @@ package io.github.jwdeveloper.tiktok.data.models.gifts;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.github.jwdeveloper.tiktok.data.models.Picture; import io.github.jwdeveloper.tiktok.data.models.Picture;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data;
import java.util.*;
@Data @Data
@AllArgsConstructor @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 int id;
private final String name; private final String name;
@@ -17,4 +22,20 @@ public class Gift {
private final Picture picture; private final Picture picture;
private final JsonObject properties; 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;
}
} }

View File

@@ -24,7 +24,6 @@
package io.github.jwdeveloper.tiktok.data.models.gifts; package io.github.jwdeveloper.tiktok.data.models.gifts;
import io.github.jwdeveloper.tiktok.data.models.Picture; import io.github.jwdeveloper.tiktok.data.models.Picture;
import java.lang.String;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
@@ -1401,7 +1400,7 @@ public enum GiftOld {
private final Picture picture; private final Picture picture;
private GiftOld(int id, String name, int diamondCost, String pictureLink) { GiftOld(int id, String name, int diamondCost, String pictureLink) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.diamondCost = diamondCost; this.diamondCost = diamondCost;

View File

@@ -28,11 +28,9 @@ public enum GiftSendType
Begin, Begin,
Active; Active;
public static GiftSendType fromNumber(long number) public static GiftSendType fromNumber(long number)
{ {
return switch ((int) number) { return switch ((int) number) {
case 0 -> GiftSendType.Finished;
case 1, 2, 4 -> GiftSendType.Active; case 1, 2, 4 -> GiftSendType.Active;
default -> GiftSendType.Finished; default -> GiftSendType.Finished;
}; };

View File

@@ -22,8 +22,9 @@
*/ */
package io.github.jwdeveloper.tiktok.live; 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.Picture;
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
import java.util.List; import java.util.List;
@@ -38,26 +39,29 @@ public interface GiftManager {
* @param diamondCost diamond cost * @param diamondCost diamond cost
* @return * @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 * @param giftId
* @return * @return
*/ */
GiftOld findById(int giftId); Gift findById(int giftId);
/** /**
* *
* @param giftName * @param giftName
* @return * @return
*/ */
GiftOld findByName(String giftName); Gift findByName(String giftName);
/** /**
* *
* @return all gifts * @return all gifts
*/ */
List<GiftOld> getGifts(); List<Gift> getGifts();
} }

View File

@@ -22,19 +22,18 @@
*/ */
package io.github.jwdeveloper.tiktok.gifts; 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.Picture;
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftOld; import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.live.GiftManager; import io.github.jwdeveloper.tiktok.live.GiftManager;
import sun.misc.Unsafe;
import java.util.*; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
public class TikTokGiftManager implements GiftManager { public class TikTokGiftManager implements GiftManager {
private final Map<Integer, GiftOld> indexById; private final Map<Integer, Gift> indexById;
private final Map<String, GiftOld> indexByName; private final Map<String, Gift> indexByName;
private final Logger logger; private final Logger logger;
public TikTokGiftManager(Logger logger) public TikTokGiftManager(Logger logger)
@@ -46,58 +45,29 @@ public class TikTokGiftManager implements GiftManager {
} }
protected void init() { protected void init() {
for (var gift : GiftOld.values()) { for (var gift : Gift.getGifts()) {
indexById.put(gift.getId(), gift); indexById.put(gift.getId(), gift);
indexByName.put(gift.getName(), gift); indexByName.put(gift.getName(), gift);
} }
} }
public GiftOld registerGift(int id, String name, int diamondCost, Picture picture) { public Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties) {
try { Gift gift = new Gift(id, name, diamondCost, picture, properties);
var constructor = Unsafe.class.getDeclaredConstructors()[0]; indexById.put(gift.getId(), gift);
constructor.setAccessible(true); indexByName.put(gift.getName(), gift);
var unsafe = (Unsafe) constructor.newInstance(); return gift;
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 GiftOld findById(int giftId) { public Gift findById(int giftId) {
GiftOld gift = indexById.get(giftId); return indexById.getOrDefault(giftId, Gift.UNDEFINED);
return gift == null ? GiftOld.UNDEFINED : gift;
} }
public GiftOld findByName(String giftName) { public Gift findByName(String giftName) {
GiftOld gift = indexByName.get(giftName); return indexByName.getOrDefault(giftName, Gift.UNDEFINED);
return gift == null ? GiftOld.UNDEFINED : gift;
} }
@Override @Override
public List<GiftOld> getGifts() { public List<Gift> getGifts() {
return indexById.values().stream().toList(); return indexById.values().stream().toList();
} }
} }

View File

@@ -24,11 +24,9 @@ package io.github.jwdeveloper.tiktok.mappers.handlers;
import io.github.jwdeveloper.tiktok.TikTokRoomInfo; import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent; 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.*;
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
import io.github.jwdeveloper.tiktok.data.models.Picture; 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.*;
import io.github.jwdeveloper.tiktok.data.models.gifts.GiftSendType;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.live.GiftManager; import io.github.jwdeveloper.tiktok.live.GiftManager;
import io.github.jwdeveloper.tiktok.mappers.TikTokMapperHelper; import io.github.jwdeveloper.tiktok.mappers.TikTokMapperHelper;
@@ -37,9 +35,7 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastGiftMessage;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import sun.misc.Unsafe; import sun.misc.Unsafe;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
public class TikTokGiftEventHandler { public class TikTokGiftEventHandler {
private final GiftManager giftManager; private final GiftManager giftManager;
@@ -112,13 +108,12 @@ public class TikTokGiftEventHandler {
return new TikTokGiftComboEvent(gift, tikTokRoomInfo.getHost(), message, state); return new TikTokGiftComboEvent(gift, tikTokRoomInfo.getHost(), message, state);
} }
private GiftOld getGiftObject(WebcastGiftMessage giftMessage) { private Gift getGiftObject(WebcastGiftMessage giftMessage) {
var giftId = (int) giftMessage.getGiftId(); var giftId = (int) giftMessage.getGiftId();
var gift = giftManager.findById(giftId); var gift = giftManager.findById(giftId);
if (gift == GiftOld.UNDEFINED) { if (gift == Gift.UNDEFINED)
gift = giftManager.findByName(giftMessage.getGift().getName()); gift = giftManager.findByName(giftMessage.getGift().getName());
} if (gift == Gift.UNDEFINED) {
if (gift == GiftOld.UNDEFINED) {
gift = giftManager.registerGift( gift = giftManager.registerGift(
giftId, giftId,
giftMessage.getGift().getName(), giftMessage.getGift().getName(),
@@ -126,19 +121,18 @@ public class TikTokGiftEventHandler {
Picture.map(giftMessage.getGift().getImage())); Picture.map(giftMessage.getGift().getImage()));
} }
if (gift.getPicture().getLink().endsWith(".webp")) { if (gift.getPicture().getLink().endsWith(".webp"))
updatePicture(gift, giftMessage); updatePicture(gift, giftMessage);
}
return gift; return gift;
} }
// TODO-kohlerpop1: I do not think this method is needed for any reason?
private void updatePicture(GiftOld gift, WebcastGiftMessage webcastGiftMessage) { private void updatePicture(Gift gift, WebcastGiftMessage webcastGiftMessage) {
try { try {
var picture = Picture.map(webcastGiftMessage.getGift().getImage()); var picture = Picture.map(webcastGiftMessage.getGift().getImage());
var constructor = Unsafe.class.getDeclaredConstructors()[0]; var constructor = Unsafe.class.getDeclaredConstructors()[0];
constructor.setAccessible(true); constructor.setAccessible(true);
var field = GiftOld.class.getDeclaredField("picture"); var field = Gift.class.getDeclaredField("picture");
field.setAccessible(true); field.setAccessible(true);
field.set(gift, picture); field.set(gift, picture);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -23,18 +23,18 @@
package io.github.jwdeveloper.tiktok; package io.github.jwdeveloper.tiktok;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent; 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; import lombok.AllArgsConstructor;
public class CustomEventExample { public class CustomEventExample {
@AllArgsConstructor @AllArgsConstructor
public static class CheapGiftEvent extends TikTokEvent { public static class CheapGiftEvent extends TikTokEvent {
GiftOld gift; Gift gift;
} }
@AllArgsConstructor @AllArgsConstructor
public static class ExpensiveGiftEvent extends TikTokEvent { public static class ExpensiveGiftEvent extends TikTokEvent {
GiftOld gift; Gift gift;
} }
public static void main(String[] args) public static void main(String[] args)

View File

@@ -84,12 +84,12 @@ public class ListenerExample
@TikTokEventObserver @TikTokEventObserver
public void onGift(LiveClient liveClient, TikTokGiftEvent event) { public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
var message = switch (event.getGift()) { var message = switch (event.getGift().getName()) {
case ROSE -> "Thanks :)"; case "ROSE" -> "Thanks :)";
case APPETIZERS -> ":OO"; case "APPETIZERS" -> ":OO";
case APRIL -> ":D"; case "APRIL" -> ":D";
case TIKTOK -> ":P"; case "TIKTOK" -> ":P";
case CAP -> ":F"; case "CAP" -> ":F";
default -> ":I"; default -> ":I";
}; };
liveClient.getLogger().info(message); liveClient.getLogger().info(message);

View File

@@ -84,10 +84,10 @@ public class SimpleExample {
}) })
.onGift((liveClient, event) -> .onGift((liveClient, event) ->
{ {
switch (event.getGift()) { switch (event.getGift().getName()) {
case ROSE -> print(ConsoleColors.RED, "Rose!"); case "ROSE" -> print(ConsoleColors.RED, "Rose!");
case GG -> print(ConsoleColors.YELLOW, " GOOD GAME!"); case "GG" -> print(ConsoleColors.YELLOW, " GOOD GAME!");
case TIKTOK -> print(ConsoleColors.CYAN, "Thanks for TikTok"); case "TIKTOK" -> print(ConsoleColors.CYAN, "Thanks for TikTok");
default -> default ->
print(ConsoleColors.GREEN, "[Thanks for gift] ", ConsoleColors.YELLOW, event.getGift().getName(), "x", event.getCombo()); print(ConsoleColors.GREEN, "[Thanks for gift] ", ConsoleColors.YELLOW, event.getGift().getName(), "x", event.getCombo());
} }

View File

@@ -83,7 +83,7 @@ Are you willing to help or improve TikTokLiveJava?
#### Tools #### Tools
Project that contains code generators. Project that contains code generators.
The most useful one is class `GenerateGiftsEnum` that download gifts json from TikTok 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 #### Tools-EventsCollector
Tool that can be used to store all `protocol-buffer` and `events` from live to `sqlLite` database or `Json` file Tool that can be used to store all `protocol-buffer` and `events` from live to `sqlLite` database or `Json` file

View File

@@ -52,16 +52,6 @@ public class RecorderListener implements LiveRecorder {
this.consumer = consumer; this.consumer = consumer;
} }
@Override
public void pause() {
}
@Override
public void unpause() {
}
@TikTokEventObserver @TikTokEventObserver
private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event) { private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event) {
settings = RecorderSettings.DEFAULT(); settings = RecorderSettings.DEFAULT();