191 lines
7.1 KiB
C#
191 lines
7.1 KiB
C#
using Cal.DataTable;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace ET
|
|
{
|
|
public class QuestComponentAwakeSystem : AwakeSystem<QuestComponent>
|
|
{
|
|
public override void Awake(QuestComponent self)
|
|
{
|
|
self.Awake();
|
|
}
|
|
}
|
|
public class QuestComponentLoadSystem : LoadSystem<QuestComponent>
|
|
{
|
|
public override void Load(QuestComponent self)
|
|
{
|
|
self.Load();
|
|
}
|
|
}
|
|
public class QuestComponentDestroySystem : DestroySystem<QuestComponent>
|
|
{
|
|
public override void Destroy(QuestComponent self)
|
|
{
|
|
self.scordList.Clear();
|
|
self.configDic.Clear();
|
|
}
|
|
}
|
|
public static class QuestComponentSystem
|
|
{
|
|
|
|
public static void Awake(this QuestComponent self)
|
|
{
|
|
QuestComponent.instance = self;
|
|
self.Load();
|
|
}
|
|
public static void Load(this QuestComponent self)
|
|
{
|
|
AwakeAsync(self).Coroutine();
|
|
self.configs = DataTableHelper.GetAll<QuestConfig>().ToArray();
|
|
}
|
|
private static async ETVoid AwakeAsync(QuestComponent self)
|
|
{
|
|
List<QuestComponent> list = await DBComponent.Instance.QueryJson<QuestComponent>("{}");
|
|
if (list.Count <= 0)
|
|
{
|
|
self.scordList = new LinkedList<KeyValuePair<long, int>>();
|
|
return;
|
|
}
|
|
QuestComponent component = list[0];
|
|
self.scordList = component.scordList;
|
|
}
|
|
public static async ETVoid Save(this QuestComponent self)
|
|
{
|
|
await DBComponent.Instance.Save(self);
|
|
}
|
|
public static async ETTask GetScordInfo(this QuestComponent self, System.Collections.Generic.List<QuestScordInfo> infoList)
|
|
{
|
|
LinkedList<KeyValuePair<long, int>> list = self.scordList;
|
|
for (LinkedListNode<KeyValuePair<long, int>>? node = list.First; node !=null;node =node.Next)
|
|
{
|
|
(long id, int scord) = node.Value;
|
|
User user = await UserComponent.Instance.Query(id);
|
|
infoList.Add(new QuestScordInfo
|
|
{
|
|
Id = id,
|
|
Name = user.NickName,
|
|
Level = user.Level,
|
|
Scord = scord
|
|
});
|
|
}
|
|
}
|
|
public static void ModifyScrodInfo(this QuestComponent self, long id, int scord)
|
|
{
|
|
LinkedList<KeyValuePair<long, int>> list = self.scordList;
|
|
int oldScord = 0;
|
|
for (LinkedListNode<KeyValuePair<long, int>>? item = list.First; item != null; item = item.Next)
|
|
{
|
|
if (item.Value.Key == id)
|
|
{
|
|
oldScord = item.Value.Value;
|
|
list.Remove(item);
|
|
break;
|
|
}
|
|
}
|
|
scord += oldScord;
|
|
LinkedListHelper.InsertToLinkedListAndSort(list, KeyValuePair.Create(id, scord), (p1, p2) => p1.Value > p2.Value);
|
|
self.Save().Coroutine();
|
|
}
|
|
public static void InsertScrodInfo(this QuestComponent self, long id, int scord)
|
|
{
|
|
LinkedList<KeyValuePair<long, int>> list = self.scordList;
|
|
LinkedListHelper.InsertToLinkedListAndSort(list, KeyValuePair.Create(id, scord), (p1, p2) => p1.Value > p2.Value);
|
|
self.Save().Coroutine();
|
|
}
|
|
public static void ClearScordList(this QuestComponent self)
|
|
{
|
|
self.scordList.Clear();
|
|
}
|
|
/// <summary>
|
|
/// 检查答题卡数量
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public static int CheckAnswerCard(this QuestComponent self, long id)
|
|
{
|
|
if (self.configDic.ContainsKey(id))
|
|
{
|
|
Log.Error($"错误逻辑,字典中包括{id}时不会检查答题卡数量!");
|
|
return 0;
|
|
}
|
|
Unit unit = MapUnitComponent.Instance.Get(id);
|
|
PlayerData data = unit.GetComponent<PlayerData>();
|
|
return data.questAnswerCard;
|
|
}
|
|
public static int StartAnswer(this QuestComponent self, Unit unit)
|
|
{
|
|
long id = unit.Id;
|
|
if (self.configDic.ContainsKey(id))
|
|
{
|
|
Log.Error($"错误逻辑,字典中包括{id}时不会检查答题卡数量!");
|
|
self.configDic.Remove(id);
|
|
return 0;
|
|
}
|
|
PlayerData data = unit.GetComponent<PlayerData>();
|
|
if (data.questAnswerCard <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
data.questAnswerCard--;
|
|
self.configDic[id] = new QuestAnswerInfo { questAnswerCount = new QuestAnswerCount { totalCount = 10 } };
|
|
Game.EventSystem.Publish(new ET.EventType.UpdateTaskState
|
|
{
|
|
unit=unit,
|
|
type = TaskTargetType.CompleteTask,
|
|
value = 1
|
|
}).Coroutine();
|
|
return self.GetQuestion(id);
|
|
}
|
|
public static int GetQuestion(this QuestComponent self, long id)
|
|
{
|
|
QuestConfig config = self.configs[RandomHelper.RandomNumber(0, self.configs.Length)];
|
|
if (!self.configDic.TryGetValue(id, out QuestAnswerInfo answerInfo))
|
|
{
|
|
return 0;
|
|
}
|
|
answerInfo.answer = config.Answer;
|
|
answerInfo.startTime = TimeHelper.ClientNow();
|
|
self.configDic[id] = answerInfo;
|
|
return (int)config.Id;
|
|
}
|
|
public static string CheckQuestAnswer(this QuestComponent self, Unit unit, int answer, out int time, out int continueCorrectCount,out int scord)
|
|
{
|
|
long id = unit.Id;
|
|
time = 0;
|
|
continueCorrectCount = 0;
|
|
scord = 0;
|
|
if (!self.configDic.TryGetValue(id, out QuestAnswerInfo answerInfo))
|
|
{
|
|
Log.Error($"dic hs not the key = {id}");
|
|
return "系统错误";
|
|
}
|
|
time = (int)(TimeHelper.ClientNow() - answerInfo.startTime);
|
|
if (answerInfo.answer == answer)
|
|
{
|
|
answerInfo.AddContinueCount();
|
|
int remainTime = 15 * 1000 - time;
|
|
scord = remainTime >= 0 ? remainTime : 0;
|
|
answerInfo.scord += scord;
|
|
}
|
|
else
|
|
answerInfo.ReSetContinueCount();
|
|
answerInfo.questAnswerCount.AddCount(out bool isFinish);
|
|
if (isFinish)
|
|
{
|
|
self.configDic.Remove(id);
|
|
self.ModifyScrodInfo(id, answerInfo.scord);
|
|
BagHelper.AddItem(unit, BagHelper.VoucherId, answerInfo.rewardCount * 15, false,getSource:"答题奖励");
|
|
//增加等级
|
|
CharacterHelper.AddLevel(unit,answerInfo.rewardCount* 30);
|
|
}
|
|
else
|
|
self.configDic[id] = answerInfo;
|
|
continueCorrectCount = answerInfo.continueCorrectCount;
|
|
return null;
|
|
}
|
|
}
|
|
}
|