المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : اضافة Golden Tree


محمد ياسر
2019-04-19, 09:43 PM
السلام عليكم ورحمة الله وبركاته

اضافة Golden tree

والي مش عارفه

ده حدث كان في كونكر 2d وده الاضافات

قبل ما نبداء حقوق الاضافه

Copyright Zahran Joe Lotfy



اول كده اعمل كلاس جديد باسم
MsgGlobalLottery

وحط دول فيه

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.CostIte mID, 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.GoldenTreeTab le.Pools.Values.Where(i => i.Type == Kernel.GoldenTree.Type).ToArray());
client.Inventory.AddBound(LordsRoad.Database.Golde nTreeTable.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(GoldenTreeHou rs) || 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(GoldenTreeHou rs) - 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;

}
}
}

واعمل كلاس تاني باسم

MsgGlobalLotteryRankList

وحط دول فيه



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 MsgGlobalLotteryRankList
{
public MsgGlobalLotteryRankList() { }
public MsgGlobalLotteryRankListProto Info;
[ProtoContract]
public class MsgGlobalLotteryRankListProto
{
[ProtoMember(1, IsRequired = true)]
public uint Page;
[ProtoMember(2, IsRequired = true)]
public uint CountinPage;
[ProtoMember(3, IsRequired = true)]
public uint AllCount;
[ProtoMember(4, IsRequired = true)]
public Picker[] Pickers;
}
[ProtoContract]
public class Picker
{
[ProtoMember(1, IsRequired = true)]
public uint PickItem;
[ProtoMember(2, IsRequired = true)]
public string Name;
}
public bool Read(byte[] packet)
{
using (var memoryStream = new MemoryStream(packet))
{
Info = Serializer.DeserializeWithLengthPrefix<MsgGlobalLotteryRankListProto>(memoryStream, PrefixStyle.Fixed32);
}
return true;
}
public void Handle(Client.GameClient client)
{
switch (Info.Page)
{
default:// Page
{
var proto = new MsgGlobalLotteryRankListProto();
proto.CountinPage = Math.Min((uint)MsgGlobalLottery.Ranking.Count, (uint)10);
proto.AllCount = Math.Min((uint)MsgGlobalLottery.Ranking.Count, (uint)2500);
proto.Pickers = new Picker[Math.Min((uint)MsgGlobalLottery.Ranking.Count, (uint)2500)];
var array = MsgGlobalLottery.Ranking.ToArray();
for (int i = 0; i < proto.Pickers.Length; i++)
{
proto.Pickers[i] = new Picker();
proto.Pickers[i].Name = array[i].Key;
proto.Pickers[i].PickItem = array[i].Value;
}
client.Send(Kernel.FinalizeProtoBuf(proto, 3283));
break;
}
}
}
}
}


بعدين خش ملف

PacketHanlder حط دول فيه


#region MsgGlobalLottery
case 3282:
{
var pkt = new MsgGlobalLottery();
var myPacket = new byte[packet.Length - 8];
for (int i = 0; i < myPacket.Length; i++)
{
myPacket[i] = packet[i];
}
if (pkt.Read(myPacket))
pkt.Handle(client);
break;
}
#endregion
#region MsgGlobalLotteryRankList
case 3283:
{
var pkt = new MsgGlobalLotteryRankList();
var myPacket = new byte[packet.Length - 8];
for (int i = 0; i < myPacket.Length; i++)
{
myPacket[i] = packet[i];
}
if (pkt.Read(myPacket))
pkt.Handle(client);
break;
}
#endregion


بعدين في نفس كلاس هتروح LoginMessage او لو انت سورس اسلام MsgGenralData
تحط دي فيه


new MsgGlobalLottery().Login(client);


روح علي ملف World او Thread علي حسب سورسك

وحط دي في فويد

CharactersCallback

#region GoldenTree
if (Kernel.GoldenTree != null)
{
var gTime = Kernel.GoldenTree.StartTime;
var blossomTime = gTime.AddSeconds(-5);
if (blossomTime.Day == DateTime.Now.Day && blossomTime.Hour == DateTime.Now.Hour && blossomTime.Minute == DateTime.Now.Minute && blossomTime.Second == DateTime.Now.Second)
{
new MsgGlobalLottery().Ready(client);
}
else if (gTime.Day == DateTime.Now.Day && gTime.Hour == DateTime.Now.Hour && gTime.Minute == DateTime.Now.Minute && gTime.Second == DateTime.Now.Second)
{
new MsgGlobalLottery().Blossom(client);
}
else if (gTime.Day == DateTime.Now.Day && (gTime.Hour + 2) == DateTime.Now.Hour && gTime.Minute == DateTime.Now.Minute && gTime.Second == DateTime.Now.Second)
{
new MsgGlobalLottery().End(client);
}
}
#endregion

وانت في نفس كلاس روح علي فويد
private void ServerFunctions(int time)

وحط دي فيه
if (Kernel.GoldenTree == null)
{
Kernel.GoldenTree = Database.GoldenTreeTable.Conditions.Values.OrderBy Descending(i => DateTime.Now <= i.StartTime).FirstOrDefault();
}

بعدين روح علي ملف Kernel
وحط دول فيه
public static Database.GoldenTreeTable.GoldenTreeCondition GoldenTree;
public static uint GoldenTreeTimes = 0;
public static uint RandomTree(Database.GoldenTreeTable.GoldenTreePool[] nums)
{
if (nums == null || nums.Length == 0) return 0;
return nums[Random.Next(0, nums.Length)].ID;
}

بعدين اعمل ملف جديد في Databass باسم GoldenTreeTable
وحط دي فيه
using System;
using System.Collections.Generic;

namespace LordsRoad.Database
{
public class GoldenTreeTable
{
public class GoldenTreeCondition
{
public uint ID;
public uint Type;
public byte CostType;
public uint CostItemID;
public byte Monopoly;
public byte CD;
public byte CDCost;
public DateTime StartTime;
}

public class GoldenTreePool
{
public uint ID;
public uint Type;
public byte PerfectLevel;
public string Name;
public uint ItemID;
}

public static Dictionary<uint, GoldenTreeCondition> Conditions = new Dictionary<uint, GoldenTreeCondition>();
public static Dictionary<uint, GoldenTreePool> Pools = new Dictionary<uint, GoldenTreePool>();

public static void Load()
{
#region GoldenLotteryCondition

Conditions = new Dictionary<uint, GoldenTreeCondition>();
var r = new IniFile(Constants.DataHolderPath + "globallotterycondition.ini");
int count = r.ReadInt32("ConditionAmount", "Amount", 0);
for (uint i = 0; i < count; i++)
{
GoldenTreeCondition prize = new GoldenTreeCondition();
prize.ID = r.ReadUInt32(i.ToString(), "id");
prize.Type = r.ReadUInt32(i.ToString(), "Type");
prize.CostType = r.ReadByte(i.ToString(), "CostType", 0);
prize.CostItemID = r.ReadUInt32(i.ToString(), "CostValue");
prize.Monopoly = r.ReadByte(i.ToString(), "Monopoly", 0);
prize.CD = r.ReadByte(i.ToString(), "CD", 0);
prize.CDCost = r.ReadByte(i.ToString(), "CDCost", 0);
prize.StartTime = Kernel.FromDateTimeInt(ulong.Parse(r.ReadString(i. ToString(), "StartTime")));
Conditions.Add(prize.ID, prize);
}

#endregion GoldenLotteryCondition

#region GoldenLotteryPool

var file = new DatCryption().Decrypt(Constants.DataHolderPath + "global_lottery_pool.dat");
foreach (var line1 in file)
{
var spilit = line1.Split(new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries);
GoldenTreePool pool = new GoldenTreePool();
pool.ID = uint.Parse(spilit[0]);
pool.Type = uint.Parse(spilit[1]);
pool.PerfectLevel = byte.Parse(spilit[2]);
pool.Name = spilit[3];
pool.ItemID = uint.Parse(spilit[4]);
Pools.Add(pool.ID, pool);
}

#endregion GoldenLotteryPool
}
}
}

بعدين روح علي ملف Program او Server علي حسب سورسك وحط دول مع اخواته
GoldenTreeTable.Load();

وهيجيلو ايرور في FinalizeProtoBuf
يحط دي في Kernel
لو جيه ايرور انا واضح
public static byte[] FinalizeProtoBuf(object proto, ushort packetid)
{
byte[] protobuff;
using (var ms = new System.IO.MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, proto);
protobuff = ms.ToArray();
byte[] buffer;
buffer = new byte[12 + protobuff.Length];
System.Buffer.BlockCopy(protobuff, 0, buffer, 4, protobuff.Length);
Writer.Write(buffer.Length - 8, 0, buffer);
Writer.Write(packetid, 2, buffer);
return buffer;
}
}

بس كده لو في ايرور او مشكله ارامي في رد وانا هتابع برضه

karem1q
2023-10-13, 08:59 PM
تسلم