zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/Event/UpdatePer1MinuteEvent.cs

104 lines
3.8 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal.DataTable;
using ET.EventType;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ET
{
public class UnitOnlineTimeEvent : AEvent<EventType.UpdatePer1MinuteOfDay>
{
public override async ETTask Run(UpdatePer1MinuteOfDay args)
{
2021-04-11 19:50:39 +08:00
long now = args.now;
foreach (Unit unit in MapUnitComponent.Instance.GetAll())
2021-04-08 20:09:59 +08:00
{
try
{
if (!unit) continue;
if (unit.UnitType != UnitType.Player)
continue;
if (unit.IsOffline)
continue;
PlayerData data = unit.GetComponent<PlayerData>();
if (!data)
{
Log.Error($"data is invalid isNull = {data == null}");
continue;
}
data.UpdateOnlineTime(now);
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null) continue;
if (unitScene.MapId/100 == Sys_SceneId.Scene_MainCity)
{
int maxSceneId = data.MainStoryRecordId;
2021-04-11 19:50:39 +08:00
MainStoryExp mainStoryExp = MainStoryExpCategory.Instance.Get(maxSceneId);
2021-04-08 20:09:59 +08:00
CharacterHelper.AddExp(unit, MathHelper.RoundToLong(mainStoryExp.Exp/30f));
}
2021-04-11 19:50:39 +08:00
TimeSpan time = TimeSpan.FromMilliseconds(data.OnlineTime);
2021-04-08 20:09:59 +08:00
if(AppConfig.inst.isTest)
Log.Debug($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】在线时间:{time}");
}
catch (Exception e)
{
Log.Error(e);
}
}
await ETTask.CompletedTask;
}
}
public class UnitSaveEvent : AEvent<EventType.UpdatePer1MinuteOfDay>
{
public override async ETTask Run(UpdatePer1MinuteOfDay args)
{
2021-04-11 19:50:39 +08:00
long now = args.now;
2021-04-08 20:09:59 +08:00
//!dTime =59s
//long dTime = TimeSpan.TicksPerMinute / 10000 - 1000;
2021-04-11 19:50:39 +08:00
Unit[] arr = MapUnitComponent.Instance.GetAll().ToArray();
2021-04-08 20:09:59 +08:00
for (int i = 0; i < arr.Length; i++)
{
try
{
2021-04-11 19:50:39 +08:00
Unit unit = arr[i];
2021-04-08 20:09:59 +08:00
if (!unit) continue;
if (unit.UnitType != UnitType.Player)
continue;
Team team = TeamComponent.Instance.Get(unit.Id);
if (team?.TeamState != TeamState.Fight)
{
await UnitHelper.Save<PlayerData>(unit);
await UnitHelper.Save<NumericComponent>(unit);
await UnitHelper.Save<Character>(unit);
await UnitHelper.Save<Bag>(unit);
await UnitHelper.Save<UnitTask>(unit);
await UnitHelper.Save<UnitScene>(unit);
await UnitHelper.Save<UserSetting>(unit);
await UnitHelper.Save<UnitSkillComponent>(unit);
await UnitHelper.Save<Pet>(unit);
}
}
catch (Exception e)
{
Log.Error(e);
}
}
2021-04-11 19:50:39 +08:00
User[] userArr = UserComponent.Instance.GetAll().ToArray();
2021-04-08 20:09:59 +08:00
for (int i = 0; i < userArr.Length; i++)
{
try
{
2021-04-11 19:50:39 +08:00
User user = userArr[i];
2021-04-08 20:09:59 +08:00
await DBComponent.Instance.Save(user);
}
catch (Exception e)
{
Log.Error(e);
}
}
await ETTask.CompletedTask;
}
}
}