forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapsule.cs
More file actions
103 lines (88 loc) · 3.18 KB
/
Copy pathCapsule.cs
File metadata and controls
103 lines (88 loc) · 3.18 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
103
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using L2dotNET.Logging.Abstraction;
using L2dotNET.Models.Player;
namespace L2dotNET.Models.Items
{
public class Capsule
{
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
private static volatile Capsule _instance;
private static readonly object SyncRoot = new object();
public static Capsule Instance
{
get
{
if (_instance != null)
return _instance;
lock (SyncRoot)
{
if (_instance == null)
_instance = new Capsule();
}
return _instance;
}
}
public void Initialize()
{
LoadXml();
Log.Info($"Loaded {Items.Count} items.");
}
public SortedList<int, CapsuleItem> Items = new SortedList<int, CapsuleItem>();
public void Process(L2Character character, L2Item item)
{
if (!(character is L2Player))
return;
if (!Items.ContainsKey(item.Template.ItemId))
return;
CapsuleItem caps = Items[item.Template.ItemId];
Random rn = new Random();
((L2Player)character).DestroyItem(item, 1);
foreach (CapsuleItemReward rew in caps.Rewards.Where(rew => rn.Next(100) <= rew.Rate))
((L2Player)character).AddItem(rew.Id, rn.Next(rew.Min, rew.Max));
}
public void LoadXml()
{
XElement xml = XElement.Parse(File.ReadAllText(@"scripts\extractable.xml"));
XElement ex = xml.Element("list");
if (ex == null)
return;
foreach (XElement m in ex.Elements())
{
if (m.Name != "capsule")
continue;
CapsuleItem caps = new CapsuleItem
{
Id = Convert.ToInt32(m.Attribute("id").Value)
};
foreach (XElement stp in m.Elements())
{
switch (stp.Name.LocalName)
{
case "item":
try
{
CapsuleItemReward rew = new CapsuleItemReward
{
Id = int.Parse(stp.Attribute("id").Value),
Min = int.Parse(stp.Attribute("min").Value),
Max = int.Parse(stp.Attribute("max").Value),
Rate = int.Parse(stp.Attribute("rate").Value)
};
caps.Rewards.Add(rew);
}
catch (Exception)
{
Log.Error($"cant parse capsule {caps.Id}");
}
break;
}
}
Items.Add(caps.Id, caps);
}
}
}
}