forked from jo3bingham/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketDetail.cs
More file actions
102 lines (87 loc) · 3.91 KB
/
MarketDetail.cs
File metadata and controls
102 lines (87 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using OXGaming.TibiaAPI.Constants;
namespace OXGaming.TibiaAPI.Network.ServerPackets
{
public class MarketDetail : ServerPacket
{
public Dictionary<MarketDetailField, string> Details { get; } = new Dictionary<MarketDetailField, string>();
public List<(uint TotalTransactions, uint TotalPrice, uint MaximumPrice, uint MinimumPrice)> BuyStatistics { get; } =
new List<(uint TotalTransactions, uint TotalPrice, uint MaximumPrice, uint MinimumPrice)>();
public List<(uint TotalTransactions, uint TotalPrice, uint MaximumPrice, uint MinimumPrice)> SellStatistics { get; } =
new List<(uint TotalTransactions, uint TotalPrice, uint MaximumPrice, uint MinimumPrice)>();
public ushort TypeId { get; set; }
public MarketDetail(Client client)
{
Client = client;
PacketType = ServerPacketType.MarketDetail;
}
public override void ParseFromNetworkMessage(NetworkMessage message)
{
TypeId = message.ReadUInt16();
foreach (MarketDetailField value in Enum.GetValues(typeof(MarketDetailField)))
{
Details.Add(value, message.ReadString());
}
var timestamp = (uint)DateTimeOffset.UtcNow.ToUnixTimeSeconds() * 86400;
var tempTimestamp = timestamp;
BuyStatistics.Capacity = message.ReadByte();
for (var i = 0; i < BuyStatistics.Capacity; ++i)
{
tempTimestamp -= 86400;
var totalTransactions = message.ReadUInt32();
var totalPrice = message.ReadUInt32();
var maximumPrice = message.ReadUInt32();
var minimumPrice = message.ReadUInt32();
BuyStatistics.Add((totalTransactions, totalPrice, maximumPrice, minimumPrice));
}
tempTimestamp = timestamp;
SellStatistics.Capacity = message.ReadByte();
for (var i = 0; i < SellStatistics.Capacity; ++i)
{
tempTimestamp -= 86400;
var totalTransactions = message.ReadUInt32();
var totalPrice = message.ReadUInt32();
var maximumPrice = message.ReadUInt32();
var minimumPrice = message.ReadUInt32();
SellStatistics.Add((totalTransactions, totalPrice, maximumPrice, minimumPrice));
}
}
public override void AppendToNetworkMessage(NetworkMessage message)
{
message.Write((byte)ServerPacketType.MarketDetail);
message.Write(TypeId);
foreach (MarketDetailField value in Enum.GetValues(typeof(MarketDetailField)))
{
if (Details.ContainsKey(value))
{
message.Write(Details[value]);
}
else
{
message.Write(string.Empty);
}
}
var count = Math.Min(BuyStatistics.Count, byte.MaxValue);
message.Write((byte)count);
for (var i = 0; i < BuyStatistics.Count; ++i)
{
var (TotalTransactions, TotalPrice, MaximumPrice, MinimumPrice) = BuyStatistics[i];
message.Write(TotalTransactions);
message.Write(TotalPrice);
message.Write(MaximumPrice);
message.Write(MinimumPrice);
}
count = Math.Min(SellStatistics.Count, byte.MaxValue);
message.Write((byte)count);
for (var i = 0; i < SellStatistics.Count; ++i)
{
var (TotalTransactions, TotalPrice, MaximumPrice, MinimumPrice) = SellStatistics[i];
message.Write(TotalTransactions);
message.Write(TotalPrice);
message.Write(MaximumPrice);
message.Write(MinimumPrice);
}
}
}
}