using System;
using System.IO;
using System.Linq;
using System.Text;
using K7host.Game;
using K7host.Client;
using K7host.Network;
using K7host.Database;
using System.Collections.Generic;
namespace K7host.Network.GamePackets
{
public class MsgMailList : Writer, Interfaces.IPacket
{
byte[] Packet;
ushort Offset = 17;
public MsgMailList()
{
Packet = new byte[20];
Ushort((ushort)(Packet.Length - 8), 0, Packet);
Ushort((ushort)MsgTypes.MsgMailList, 2, Packet);
}
public uint Count
{
get { return BitConverter.ReadUint(Packet, 5); }
set { WriteUint(value, 5, Packet); }
}
public uint Page
{
get { return BitConverter.ReadUint(Packet, 9); }
set { WriteUint(value, 9, Packet); }
}
public uint Unknown
{
get { return BitConverter.ReadUint(Packet, 13); }
set { WriteUint(value, 13, Packet); }
}
public class PrizeInfo
{
public void WriteItem(BinaryWriter writer)
{
writer.Write(ID);
writer.Write(Time);
writer.Write(Sender);
writer.Write(Subject);
writer.Write(Message);
writer.Write(goldprize);
writer.Write(cpsprize);
writer.Write(Attackment);
if (ItemPrize != null)
writer.Write(ItemPrize.UID);
else
writer.Write(0);
}
public PrizeInfo ReadItem(BinaryReader reader)
{
ID = reader.ReadUInt32();//4
Time = reader.ReadUInt32();//8
Sender = reader.ReadString();//10
Subject = reader.ReadString();//12
Message = reader.ReadString();//14
goldprize = reader.ReadUInt32();//18
cpsprize = reader.ReadUInt32();//22
Attackment = reader.ReadUInt32();//22
ItemPrize = ConquerItemTable.LoadItem(reader.ReadUInt32());
ItemPrize.Mode = Enums.ItemMode.ItemInbox;
ItemAddingTable.GetAddingsForItem(ItemPrize);
return this;
}
public ConquerItem ItemPrize;
public string
Subject, Sender, Message;
public uint
ID, Time, cpsprize,
Attackment, goldprize;
}
Random R = new Random();
List<PrizeInfo> List = new List<PrizeInfo>();
public void Check(GameClient client, uint page)
{
List<PrizeInfo> prizes = client.Prizes.Values.ToList();
List.Clear();
for (int i = (int)page; i < page + 7; i++)
{
if (i < prizes.Count)
{
List.Add(prizes[i]);
}
}
if (List.Count > 0)
{
Packet = new byte[24 + (134 * List.Count)];
Ushort((ushort)(Packet.Length - 8), 0, Packet);
Ushort((ushort)MsgTypes.MsgMailList, 2, Packet);
Count = (uint)List.Count;
Page = page;
Unknown = (uint)prizes.Count;
for (int i = 0; i < List.Count; i++)
Append(List[i], client);
}
}
public void Append(PrizeInfo prize, GameClient C)
{
Uint(prize.ID, Offset, Packet);//uid
Offset += 4;
WriteString(prize.Sender, Offset, Packet);//sender
Offset += 32;
WriteString(prize.Subject, Offset, Packet);//Subject
Offset += 64;
Uint(prize.goldprize, Offset, Packet);//attachment
Offset += 8;
Uint(prize.cpsprize, Offset, Packet);//attachment
Offset += 12;
Uint(prize.Time, Offset, Packet);//Time
Offset += 8;
Uint(prize.Attackment, Offset, Packet);
Offset += 4;
}
public static void SendMailbox(GameClient client, bool On)
{
byte[] test = new byte[20];
WriteUshort((ushort)(test.Length - 8), 0, test);
WriteUshort((ushort)MsgTypes.MsgMailNotify, 2, test);
if (On)
Byte(3, 4, test);
else
Byte(2, 4, test);
client.Send(test);
}
public static void AddPrize(GameClient client, string Sender = "Nana", string Subject = "Inbox Test", string Message = "Message...", uint money = 5000000, uint cps = 5000, uint Time = 600, uint attachment = 0, ConquerItem item = null)
{
int id = 0;
do
{
id = Kernel.Random.Next();
}
while (client.Prizes.ContainsKey((uint)id));
PrizeInfo prize = new PrizeInfo();
prize.ID = (uint)id;
prize.Sender = Sender;
prize.Subject = Subject;
prize.Message = Message;
prize.goldprize = money;
prize.cpsprize = cps;
prize.Time = Time;
prize.ItemPrize = item;
prize.Attackment = attachment;
client.Prizes.Add(prize.ID, prize);
if (client.Prizes.Count > 0)
SendMailbox(client, true);
}
public byte[] ToArray()
{
return Packet;
}
public void Send(GameClient client)
{
client.Send(ToArray());
}
public void Deserialize(byte[] Data)
{
Packet = Data;
}
public static void Process(byte[] Packet, GameClient client)
{
uint Action = BitConverter.ReadUint(Packet, 4);
uint ID = BitConverter.ReadUint(Packet, 6);
switch (Packet[4])
{
case 1://show
{
byte[] inbox = new byte[274];
Ushort((ushort)(inbox.Length - 8), 0, inbox);
Ushort((ushort)MsgTypes.MsgMailContent, 2, inbox);
Uint(ID, 4, inbox);
if (client.Prizes[ID] != null)
{
if (!string.IsNullOrEmpty(client.Prizes[ID].Message))
{
WriteString(client.Prizes[ID].Message, 8, inbox);
}
}
client.Send(inbox);
break;
}
case 2://delete
{
if (client.Prizes.ContainsKey(ID))
client.Prizes.Remove(ID);
if (client.Prizes.Count == 0)
SendMailbox(client, false);
break;
}
case 3://Money
{
if (client.Prizes[ID].goldprize > 0)
{
client.Entity.Money += client.Prizes[ID].goldprize;
client.Prizes[ID].goldprize = 0;
}
if (client.Prizes[ID].cpsprize > 0)
{
client.Entity.ConquerPoints += client.Prizes[ID].cpsprize;
////client.Entity.ConquerPointsFake += client.Prizes[ID].cpsprize;
client.Prizes[ID].cpsprize = 0;
}
break;
}
case 4://ItemPrize
{
client.Prizes[ID].ItemPrize.UID = ConquerItem.ItemUID.Next;
client.Prizes[ID].ItemPrize.Mode = Enums.ItemMode.Default;
client.Inventory.Add(client.Prizes[ID].ItemPrize, Enums.ItemUse.CreateAndAdd);
if (client.Prizes[ID].ItemPrize.Purification.PurificationItemID > 0)
{
client.Prizes[ID].ItemPrize.Purification.ItemUID = client.Prizes[ID].ItemPrize.UID;
ItemAddingTable.AddPurification(client.Prizes[ID].ItemPrize.Purification);
}
if (client.Prizes[ID].ItemPrize.ExtraEffect.EffectID > 0)
{
client.Prizes[ID].ItemPrize.ExtraEffect.ItemUID = client.Prizes[ID].ItemPrize.UID;
ItemAddingTable.AddExtraEffect(client.Prizes[ID].ItemPrize.ExtraEffect);
}
client.Prizes[ID].ItemPrize.SendExtras(client);
client.Prizes[ID].ItemPrize = null;
break;
}
case 5://Attackment
{
if (client.Prizes[ID].Attackment > 0)
{
client.Inventory.Add(client.Prizes[ID].Attackment, 0, 1);
client.Prizes[ID].Attackment = 0;
}
break;
}
default: Console.WriteLine("[Mailbox] Unknown Action:" + Action); break;
}
if (Action != 1)
MsgMailTable.Save(client);
client.Send(Packet);
}
}