Skip to content

Commit

Permalink
feat: 抖音增加礼物数量计算时机配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
1962247851 committed May 16, 2024
1 parent 8902258 commit 77a6b2d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package tech.ordinaryroad.live.chat.client.codec.douyin.api;

import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReUtil;
Expand All @@ -32,6 +33,7 @@
import cn.hutool.http.HttpStatus;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import tech.ordinaryroad.live.chat.client.codec.douyin.constant.DouyinGiftCountCalculationTimeEnum;
import tech.ordinaryroad.live.chat.client.codec.douyin.constant.DouyinRoomStatusEnum;
import tech.ordinaryroad.live.chat.client.codec.douyin.msg.DouyinGiftMsg;
import tech.ordinaryroad.live.chat.client.codec.douyin.protobuf.DouyinWebcastGiftMessageMsgOuterClass;
Expand All @@ -40,6 +42,7 @@
import tech.ordinaryroad.live.chat.client.commons.util.OrLiveChatHttpUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author mjz
Expand All @@ -57,6 +60,10 @@ public class DouyinApis {
public static final String PATTERN_USER_UNIQUE_ID = "\\\\\"user_unique_id\\\\\":\\\\\"(\\d+)\\\\\"";
public static final String PATTERN_ROOM_ID = "\\\\\"roomId\\\\\":\\\\\"(\\d+)\\\\\"";
public static final String PATTERN_ROOM_STATUS = "\\\\\"roomInfo\\\\\".+\\\\\"status\\\\\":(\\d+)";
/**
* 礼物连击缓存
*/
private static final TimedCache<String, DouyinWebcastGiftMessageMsgOuterClass.DouyinWebcastGiftMessageMsg> DOUYIN_GIFT_MSG_CACHE = new TimedCache<>(300 * 1000L, new ConcurrentHashMap<>());

public static RoomInitResult roomInit(Object roomId, String cookie) {
Map<String, String> cookieMap = OrLiveChatCookieUtil.parseCookieString(cookie);
Expand Down Expand Up @@ -111,21 +118,40 @@ public static RoomInitResult roomInit(Object roomId) {
* @param msg DouyinGiftMsg
* @return 礼物个数
*/
public static int calculateGiftCount(DouyinGiftMsg msg) {
public static int calculateGiftCount(DouyinGiftMsg msg, DouyinGiftCountCalculationTimeEnum calculationTimeEnum) {
if (msg == null || msg.getMsg() == null) {
return 0;
}

long giftCount;
DouyinWebcastGiftMessageMsgOuterClass.DouyinWebcastGiftMessageMsg douyinWebcastGiftMessageMsg = msg.getMsg();
if (douyinWebcastGiftMessageMsg.getGift().getCombo() && douyinWebcastGiftMessageMsg.getRepeatEnd() != 1) {// 连击中
return 0;
long giftCount = 0;
if (calculationTimeEnum == DouyinGiftCountCalculationTimeEnum.COMBO_END) {
if (!douyinWebcastGiftMessageMsg.getGift().getCombo() || douyinWebcastGiftMessageMsg.getRepeatEnd() == 1) {// 非连击中
long comboCount = douyinWebcastGiftMessageMsg.getComboCount();
if (douyinWebcastGiftMessageMsg.getGroupCount() != 1L) {// 每点击一次送礼的数量不是1时
comboCount = douyinWebcastGiftMessageMsg.getGroupCount() * comboCount;
}
giftCount = comboCount;
}
} else {
// DouyinGiftCountCalculationTimeEnum.IMMEDIATELY
long groupId = douyinWebcastGiftMessageMsg.getGroupId();
long giftId = douyinWebcastGiftMessageMsg.getLongGiftId();
// groupId有时会重复
String key = groupId + "-" + msg.getUid() + "-" + giftId;
if (DOUYIN_GIFT_MSG_CACHE.containsKey(key)) {
DouyinWebcastGiftMessageMsgOuterClass.DouyinWebcastGiftMessageMsg douyinWebcastGiftMessageMsgByGroupId = DOUYIN_GIFT_MSG_CACHE.get(key);
long repeatCountByGroupId = douyinWebcastGiftMessageMsgByGroupId.getRepeatCount();
giftCount = douyinWebcastGiftMessageMsg.getRepeatCount() - repeatCountByGroupId;
} else {
giftCount = douyinWebcastGiftMessageMsg.getRepeatCount();
}
// 存在顺序错误的情况,后收到的消息礼物个数反而减少了,跳过缓存该消息,但仍保存计算后的小于0的礼物个数
if (giftCount > 0) {
DOUYIN_GIFT_MSG_CACHE.put(key, douyinWebcastGiftMessageMsg);
}
}
long comboCount = douyinWebcastGiftMessageMsg.getComboCount();
if (douyinWebcastGiftMessageMsg.getGroupCount() != 1L) {// 每点击一次送礼的数量不是1时
comboCount = douyinWebcastGiftMessageMsg.getGroupCount() * comboCount;
}
giftCount = comboCount;

msg.setCalculatedGiftCount((int) giftCount);
return (int) giftCount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* MIT License
*
* Copyright (c) 2023 OrdinaryRoad
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package tech.ordinaryroad.live.chat.client.codec.douyin.constant;

/**
* @author mjz
* @date 2024/5/16
*/
public enum DouyinGiftCountCalculationTimeEnum {

/**
* 收到礼物消息后立即计算礼物个数
*/
IMMEDIATELY,

/**
* 等待礼物连击结束后再计算礼物个数
*/
COMBO_END

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import tech.ordinaryroad.live.chat.client.codec.douyin.constant.DouyinGiftCountCalculationTimeEnum;
import tech.ordinaryroad.live.chat.client.servers.netty.client.config.BaseNettyClientConfig;

import java.util.List;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class DouyinLiveChatClientConfig extends BaseNettyClientConfig {

private String updateVersionCode = "1.0.12";

@Builder.Default
private DouyinGiftCountCalculationTimeEnum giftCountCalculationTime = DouyinGiftCountCalculationTimeEnum.IMMEDIATELY;

/**
* 示例
* wss://webcast5-ws-web-lf.douyin.com/webcast/im/push/v2/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void onCmdMsg(DouyinCmdEnum cmd, ICmdMsg<DouyinCmdEnum> cmdMsg) {
DouyinWebcastGiftMessageMsgOuterClass.DouyinWebcastGiftMessageMsg douyinWebcastGiftMessageMsg = DouyinWebcastGiftMessageMsgOuterClass.DouyinWebcastGiftMessageMsg.parseFrom(payload);
DouyinGiftMsg msg = new DouyinGiftMsg(douyinWebcastGiftMessageMsg);
// 计算礼物个数
DouyinApis.calculateGiftCount(msg);
DouyinApis.calculateGiftCount(msg, getClient().getConfig().getGiftCountCalculationTime());
iteratorMsgListeners(msgListener -> msgListener.onGiftMsg(DouyinBinaryFrameHandler.this, msg));
} catch (InvalidProtocolBufferException e) {
throw new BaseException(e);
Expand Down

0 comments on commit 77a6b2d

Please sign in to comment.