using ProtoBuf;
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections.Concurrent;
using System.Text;
using LordsRoad;
using LordsRoad.Database;
using System.Threading.Tasks;
namespace LordsRoad.Network.GamePackets
{
public class MsgGlobalLottery
{
public MsgGlobalLottery() { }
public MsgGlobalLotteryProtoBlossom Info;
[ProtoContract]
public class MsgGlobalLotteryProtoBlossom
{
[ProtoMember(1, IsRequired = true)]
public uint Type;
[ProtoMember(2, IsRequired = true)]
public uint PrizesType;
[ProtoMember(3, IsRequired = true)]
///0 = Not Opened, 1 = Ready, 2 = Opened, 3 = Ranking
public uint GUI;
[ProtoMember(4, IsRequired = true)]
public uint Time;
[ProtoMember(5, IsRequired = true)]
public uint Remain;
[ProtoMember(6, IsRequired = true)]
public uint TotalCount;
}
[ProtoContract]
public class MsgGlobalLotteryProtoPick
{
[ProtoMember(1, IsRequired = true)]
public uint Type;
[ProtoMember(2, IsRequired = true)]
public uint UID;
[ProtoMember(3, IsRequired = true)]
public uint GUI;
[ProtoMember(4, IsRequired = true)]
public uint PickItemID;
[ProtoMember(5, IsRequired = true)]
public string Name;
[ProtoMember(6, IsRequired = true)]
public uint Remain;
[ProtoMember(7, IsRequired = true)]
public uint TotalCount;
}
public bool Read(byte[] packet)
{
using (var memoryStream = new MemoryStream(packet))
{
Info = Serializer.DeserializeWithLengthPrefix<MsgGlobalLotteryProtoBlossom>(memoryStream, PrefixStyle.Fixed32);
}
return true;
}
public static Dictionary<string, uint> Ranking;
public const double GoldenTreeHours = 2;//120 min
public const uint Max = 2500;
public void Handle(Client.GameClient client)
{
if (Kernel.GoldenTree == null) return;
if (Ranking == null) Ranking = new Dictionary<string, uint>();
switch (Info.Type)
{
case 1:
{
if (client.Inventory.Remove(Kernel.GoldenTree.CostItemID, 1))
{
Kernel.GoldenTreeTimes++;
var proto = new MsgGlobalLotteryProtoPick();
proto.UID = client.Entity.UID;
proto.Name = client.Entity.Name;
proto.Remain = (uint)(Max - Kernel.GoldenTreeTimes);
proto.TotalCount = Max;
proto.Type = 1;
proto.PickItemID = Kernel.RandomTree(LordsRoad.Database.GoldenTreeTable.Pools.Values.Where(i => i.Type == Kernel.GoldenTree.Type).ToArray());
client.Inventory.AddBound(LordsRoad.Database.GoldenTreeTable.Pools[proto.PickItemID].ItemID, 0, 1);
if (LordsRoad.Database.GoldenTreeTable.Pools[proto.PickItemID].PerfectLevel == 1)
{
if (!Ranking.ContainsKey(client.Entity.Name)) Ranking.Add(client.Entity.Name, LordsRoad.Database.GoldenTreeTable.Pools[proto.PickItemID].ID);
else
{
Ranking[client.Entity.Name] = LordsRoad.Database.GoldenTreeTable.Pools[proto.PickItemID].ID;
}
}
foreach (var player in Kernel.GamePool.Values)
player.Send(Kernel.FinalizeProtoBuf(proto, 3282));
}
break;
}
case 5:
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.Type = 5;
Info.GUI = 2;
Info.PrizesType = Kernel.GoldenTree.ID;
client.Send(Kernel.FinalizeProtoBuf(Info, 3282));
break;
}
default: Console.WriteLine("MsgGlobalLottery UNKNOWN ACTION: " + Info.Type.ToString(), ConsoleColor.DarkRed); break;
}
}
public void Ready(Client.GameClient client)
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 1;
Info.Time = 5;
client.Send(Kernel.FinalizeProtoBuf(Info, 3282));
}
public void Login(Client.GameClient client)
{
if ((DateTime.Now > Kernel.GoldenTree.StartTime.AddHours(GoldenTreeHours) || DateTime.Now < Kernel.GoldenTree.StartTime))
{
if (Ranking == null)
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 0;
}
else
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 3;
}
client.Send(Kernel.FinalizeProtoBuf(Info, 3282));
return;
}
Blossom(client);
}
public void Blossom(Client.GameClient client)
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 2;
TimeSpan span = Kernel.GoldenTree.StartTime.AddHours(GoldenTreeHours) - DateTime.Now;
Info.Time = (uint)(span.TotalSeconds);
Info.Remain = (uint)(Max - Kernel.GoldenTreeTimes);
Info.TotalCount = Max;
client.Send(Kernel.FinalizeProtoBuf(Info, 3282));
}
public void End(Client.GameClient client)
{
if (Ranking == null)
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 0;
}
else
{
Info = new MsgGlobalLotteryProtoBlossom();
Info.PrizesType = Kernel.GoldenTree.ID;
Info.GUI = 3;
}
client.Send(Kernel.FinalizeProtoBuf(Info, 3282));
return;
}
}
}