完成随机PK模块

This commit is contained in:
2022-12-07 15:36:23 +08:00
parent e134ef8d54
commit 28b4a36800
17 changed files with 824 additions and 372 deletions

View File

@@ -598,7 +598,7 @@ public class LiveNetManager {
@Override
public void accept(ResponseModel<String> responseModel) throws Exception {
if (callback != null) {
callback.onSuccess("36956");
callback.onSuccess("98196");
// callback.onSuccess(responseModel.getData().getInfo());
}
}

View File

@@ -1,5 +1,7 @@
package com.yunbao.common.manager;
import com.yunbao.common.utils.ToastUtil;
import cn.rongcloud.rtc.api.RCRTCRoom;
import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback;
import cn.rongcloud.rtc.base.RTCErrorCode;
@@ -71,7 +73,17 @@ public class IMRTCManager {
2:无论为true或false双方都可以使用RCRTCLiveInfo.setMixConfig(RCRTCMixConfig, IRCRTCResultCallback) 方法主动设置合流布局。一旦主动设置过合流布局,后续音视频直播过程中设置的自动合流参数将失效。
extra - 扩展字段,默认为空
*/
rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid,liveUid,inviterAutoMix,extra,callback);
rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid, liveUid, inviterAutoMix, extra, new IRCRTCResultCallback() {
@Override
public void onSuccess() {
callback.onSuccess();
}
@Override
public void onFailed(RTCErrorCode errorCode) {
callback.onFailed(errorCode);
}
});
}else{
callback.onFailed(RTCErrorCode.RongRTCCodeIMError);
}

View File

@@ -0,0 +1,272 @@
package com.yunbao.common.manager;
import android.os.Handler;
import android.os.Looper;
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.http.base.HttpCallback;
import com.yunbao.common.http.live.LiveNetManager;
import com.yunbao.common.utils.ToastUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class RandomPkManager {
public final static int PK_STATUS_DEFAULT = -1;
public final static int PK_STATUS_REQUEST = 0;
public final static int PK_STATUS_START = 1;
public final static int PK_STATUS_CLOSE = 2;
public final static int PK_STATUS_REFUSE = 3;
public final static int PK_STATUS_EXIT_ING = 4;
private static RandomPkManager manager;
private int min = -1;
private int sec = -1;
private int status = PK_STATUS_DEFAULT;
private Timer timer;
private TimerTask task;
private List<OnRandomPkTimer> randomPkTimer = new ArrayList<>();
Handler handler = new Handler(Looper.getMainLooper());
private RandomPkManager() {
initTask();
}
public static RandomPkManager getInstance() {
if (manager == null) {
manager = new RandomPkManager();
}
return manager;
}
public void addOnRandomPkTimer(OnRandomPkTimer randomPkTimer) {
if (randomPkTimer != null) {
this.randomPkTimer.add(randomPkTimer);
}
}
public void unregisterOnRandomPkTimer(OnRandomPkTimer randomPkTimer) {
if (randomPkTimer != null) {
this.randomPkTimer.remove(randomPkTimer);
}
}
private void initVal() {
min = -1;
sec = -1;
}
private void initTask() {
if (task != null) {
return;
}
task = new TimerTask() {
@Override
public void run() {
status = PK_STATUS_REQUEST;
sec++;
if (sec % 60 == 0) {
min++;
sec = 0;
}
handler.post(() -> {
for (OnRandomPkTimer onRandomPkTimer : randomPkTimer) {
onRandomPkTimer.onTimer(String.format(Locale.CHINA, "%02d:%02d", min, sec));
}
if (sec == 8) {
nextPk();
}
});
}
};
}
public String getTimer() {
return String.format(Locale.CHINA, "%02d:%02d", min, sec);
}
private void nextPk() {
LiveNetManager.get(CommonAppContext.getTopActivity())
.randomPK(new HttpCallback<String>() {
@Override
public void onSuccess(String data) {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onStartPK(data);
}
}
@Override
public void onError(String error) {
}
});
}
public boolean start() {
if (status == PK_STATUS_START) {
ToastUtil.show("PK中");
return false;
}
if (status == PK_STATUS_EXIT_ING) {
return false;
}
if (task != null) {
task.cancel();
}
task = null;
initTask();
initVal();
timer = new Timer();
timer.schedule(task, 0, 1000);
return true;
}
private void callEndPkStart() {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onPkEndStart();
}
}
private void callEndPkTimer(String time) {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onPkEndTimer(time);
}
}
private void callEndPkSuccess() {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onPkEndSuccess();
}
}
private int exitTimer;
public void exitPk() {
if (status == PK_STATUS_EXIT_ING) {
return;
}
callEndPkStart();
status = PK_STATUS_EXIT_ING;
exitTimer = 11;
new Timer().schedule(new TimerTask() {
Handler handler = new Handler(Looper.getMainLooper());
@Override
public void run() {
handler.post(() -> {
exitTimer--;
callEndPkTimer(exitTimer + "");
if (exitTimer == 0) {
end();
callEndPkSuccess();
cancel();
status = PK_STATUS_DEFAULT;
}
});
}
}, 0, 1000);
}
public void end() {
if (task != null) {
task.cancel();
}
task = null;
status = PK_STATUS_DEFAULT;
callEnd();
}
public boolean isPking() {
return status == PK_STATUS_REQUEST;
}
private void callEnd() {
if (task != null) {
task.cancel();
task = null;
}
handler.post(() -> {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onPkEnd();
}
});
}
private void callStart() {
if (task != null) {
task.cancel();
task = null;
}
handler.post(() -> {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onPking();
}
});
}
private void callRefuse() {
handler.post(() -> {
for (OnRandomPkTimer pkTimer : randomPkTimer) {
pkTimer.onRefuse();
}
});
}
public void setPkStatus(int status) {
this.status = status;
switch (status) {
case PK_STATUS_START:
callStart();
break;
case PK_STATUS_CLOSE:
callEnd();
break;
case PK_STATUS_REFUSE:
callRefuse();
//nextPk();
break;
}
}
public boolean isExiting() {
return status == PK_STATUS_EXIT_ING;
}
public String getExitTimer() {
return exitTimer + "";
}
public static abstract class OnRandomPkTimer {
public void onTimer(String time) {
}
public void onStartPK(String pkUid) {
}
public void onPking() {
}
public void onPkEnd() {
}
public void onRefuse() {
}
public void onPkEndTimer(String time) {
}
public void onPkEndStart() {
}
public void onPkEndSuccess() {
}
}
}