204 lines
6.3 KiB
C#
204 lines
6.3 KiB
C#
using UnityEngine;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using EEventBuildState = EventPartnerGatherRequests.EEventBuildState;
|
|
using EventPartnerGather;
|
|
using cfg;
|
|
using game;
|
|
using System.Collections.Generic;
|
|
using EventPartnerGatherAcceptResponse = EventPartnerGatherRequests.EventPartnerGatherAcceptResponse;
|
|
|
|
public class EventPartnerGatherRobotInvitationSystem
|
|
{
|
|
private bool _isInitiated = false;
|
|
private EventPartnerService _eventPartnerService;
|
|
private DataCenter.EventPartnerGatherManager _manager;
|
|
private List<BotInvitationInfo> _botsToAdd;
|
|
|
|
public void Init()
|
|
{
|
|
if (_isInitiated)
|
|
return;
|
|
_eventPartnerService = GContext.container.Resolve<EventPartnerService>();
|
|
_manager = GContext.container.Resolve<DataCenter.EventPartnerGatherManager>();
|
|
if (_eventPartnerService == null || _manager == null)
|
|
return;
|
|
_botsToAdd = new List<BotInvitationInfo>();
|
|
_isInitiated = true;
|
|
}
|
|
|
|
public void OnInviteRobot(EventPartnerScrollViewItem.ScrollViewItemInfo botInfo)
|
|
{
|
|
if (!_isInitiated)
|
|
{
|
|
LogError("RobotInvitationSystemNotInitiated");
|
|
return;
|
|
}
|
|
_botsToAdd ??= new List<BotInvitationInfo>();
|
|
if (_botsToAdd.Count >= PartnerInfoManager.BuilderCapacity * 2)
|
|
return;
|
|
_botsToAdd.Add(new BotInvitationInfo(botInfo));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public async void CheckAndFire()
|
|
{
|
|
if (!_isInitiated)
|
|
{
|
|
LogError("RobotInvitationSystemNotInitiated");
|
|
return;
|
|
}
|
|
|
|
if (_manager.PartnerInfoManager.IsFullyMatched)
|
|
{
|
|
// Debug.Log("Full BuildPartners");
|
|
_botsToAdd.Clear();
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
foreach (var botInfo in _botsToAdd)
|
|
{
|
|
if (botInfo.InviteTime < ZZTimeHelper.UtcNow())
|
|
await TryAddRobot(botInfo);
|
|
}
|
|
_botsToAdd.RemoveAll(bot => bot.IsProcessed);
|
|
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
LogError($"CheckAndFireException error={e.Message}");
|
|
}
|
|
}
|
|
|
|
private async Task TryAddRobot(BotInvitationInfo botInvitationInfo)
|
|
{
|
|
ELog($"TryAddRobot botId={botInvitationInfo?.BotInfo?.PlayfabId}");
|
|
|
|
var botInfo = botInvitationInfo.BotInfo;
|
|
var partnerId = botInfo.PlayfabId;
|
|
if (!_isInitiated)
|
|
{
|
|
LogError("RobotInvitationSystemNotInitiated");
|
|
return;
|
|
}
|
|
|
|
if (!partnerId.EndsWith(EventPartnerBot.RobotIdentifier))
|
|
{
|
|
LogWarning($"AddRobotSkipped reason=NotRobot partnerId={partnerId}");
|
|
// _botsToAdd.Remove(botInvitationInfo);
|
|
botInvitationInfo.IsProcessed = true;
|
|
return;
|
|
}
|
|
|
|
var response = await _eventPartnerService.AcceptPartner(partnerId, _manager.EventId);
|
|
if (response.State != EEventBuildState.Success)
|
|
{
|
|
LogWarning($"AddRobotFailed partnerId={partnerId} state={response.State} reason={response.Message}");
|
|
return;
|
|
}
|
|
botInvitationInfo.IsProcessed = true;
|
|
|
|
var positionId = -1;
|
|
for(var i =0;i < response.PartnerList.Count; ++i)
|
|
{
|
|
AddRobotPartner(i, botInfo);
|
|
if (partnerId == response.PartnerList[i].PlayFabId)
|
|
{
|
|
positionId = i;
|
|
}
|
|
}
|
|
|
|
#if AGG
|
|
var userService = GContext.container.Resolve<IUserService> ();
|
|
using (var e = GEvent.GameEvent("event_partners_gather_invite"))
|
|
{
|
|
e.AddContent("teammate_id", "robot")
|
|
.AddContent("position_id", positionId)
|
|
.AddContent("inviter_id", userService.UserId);
|
|
}
|
|
#endif
|
|
|
|
}
|
|
|
|
private void AddRobotPartner(int slotId, EventPartnerScrollViewItem.ScrollViewItemInfo botInfo)
|
|
{
|
|
if (slotId == -1)
|
|
{
|
|
LogWarning($"AddRobotFailed reason=InvalidSlot botId={botInfo.PlayfabId}");
|
|
return;
|
|
}
|
|
|
|
if (slotId >= PartnerInfoManager.BuilderCapacity)
|
|
{
|
|
LogError("AddRobotFailed reason=SlotOutOfRange");
|
|
return;
|
|
}
|
|
|
|
var botResult = EventPartnerGatherBot.AddBot(botInfo, slotId);
|
|
var builder = new BuildPartner
|
|
{
|
|
SlotId = slotId,
|
|
PartnerId = botInfo.PlayfabId,
|
|
AvatarUrl = botInfo.AvatarUrl,
|
|
DisplayName = botInfo.DisplayName,
|
|
Cache = _manager.PartnerInfoManager.Cache,
|
|
};
|
|
|
|
var bRet = _manager.PartnerInfoManager.AddBuildPartner(builder);
|
|
if (bRet)
|
|
{
|
|
builder.UpdateRobotInfo(botResult);
|
|
builder.ScheduleBotScoreAction();
|
|
GContext.Publish(new EventPartnerGatherAddPartner
|
|
{
|
|
SlotId = builder.SlotId
|
|
});
|
|
}
|
|
}
|
|
|
|
public class BotInvitationInfo
|
|
{
|
|
public EventPartnerScrollViewItem.ScrollViewItemInfo BotInfo { get; set; }
|
|
public System.DateTime InviteTime { get; set; }
|
|
public bool IsProcessed { get; set; } = false;
|
|
|
|
public BotInvitationInfo(EventPartnerScrollViewItem.ScrollViewItemInfo botInfo)
|
|
{
|
|
BotInfo = botInfo;
|
|
InviteTime = ZZTimeHelper.UtcNow() + GetRandomRobotAcceptDelay();
|
|
}
|
|
private System.TimeSpan GetRandomRobotAcceptDelay()
|
|
{
|
|
Tables _tables = GContext.container.Resolve<Tables>();
|
|
var delayTime = Random.Range(_tables.TbEventPartnerConfig.RobotAgreeTime[0], _tables.TbEventPartnerConfig.RobotAgreeTime[1]);
|
|
return System.TimeSpan.FromSeconds(delayTime);
|
|
}
|
|
}
|
|
|
|
private static void Log(string message)
|
|
{
|
|
Debug.Log($"<color=cyan>EventPartnerGatherRobotInvitationSystem -> {message}</color>");
|
|
}
|
|
|
|
private static void ELog(string message)
|
|
{
|
|
#if UNITY_EDITOR
|
|
Debug.Log($"<color=cyan>EventPartnerGatherRobotInvitationSystem -> {message}</color>");
|
|
#endif
|
|
}
|
|
|
|
private static void LogWarning(string message)
|
|
{
|
|
Debug.LogWarning($"<color=yellow>EventPartnerGatherRobotInvitationSystem -> {message}</color>");
|
|
}
|
|
|
|
private static void LogError(string message)
|
|
{
|
|
Debug.LogError($"<color=red>EventPartnerGatherRobotInvitationSystem -> {message}</color>");
|
|
}
|
|
}
|