source: binary-improvements2/AllocsCommands/Commands/ShowInventory.cs@ 379

Last change on this file since 379 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 6.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.CustomCommands {
6 public class ShowInventory : ConsoleCmdAbstract {
7 public override string GetDescription () {
8 return "list inventory of a given player";
9 }
10
11 public override string GetHelp () {
12 return "Usage:\n" +
13 " showinventory <user id / player name / entity id> [tag]\n" +
14 "Show the inventory of the player given by his UserID, player name or\n" +
15 "entity id (as given by e.g. \"lpi\").\n" +
16 "Optionally specify a tag that is included in each line of the output. In\n" +
17 "this case output is designed to be easily parseable by tools.\n" +
18 "Note: This only shows the player's inventory after it was first sent to\n" +
19 "the server which happens at least every 30 seconds.";
20 }
21
22 public override string[] GetCommands () {
23 return new[] {"showinventory", "si"};
24 }
25
26 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
27 if (_params.Count < 1) {
28 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
29 return;
30 }
31
32 PlatformUserIdentifierAbs steamid = PersistentContainer.Instance.Players.GetSteamID (_params [0], true);
33 if (steamid == null) {
34 SdtdConsole.Instance.Output (
35 "Playername or entity/steamid id not found or no inventory saved (first saved after a player has been online for 30s).");
36 return;
37 }
38
39 string tag = null;
40 if (_params.Count > 1 && _params [1].Length > 0) {
41 tag = _params [1];
42 }
43
44 Player p = PersistentContainer.Instance.Players [steamid, false];
45 PersistentData.Inventory inv = p.Inventory;
46
47 if (tag == null) {
48 SdtdConsole.Instance.Output ("Belt of player " + p.Name + ":");
49 }
50
51 PrintInv (inv.belt, p.EntityID, "belt", tag);
52 if (tag == null) {
53 SdtdConsole.Instance.Output (string.Empty);
54 }
55
56 if (tag == null) {
57 SdtdConsole.Instance.Output ("Bagpack of player " + p.Name + ":");
58 }
59
60 PrintInv (inv.bag, p.EntityID, "backpack", tag);
61 if (tag == null) {
62 SdtdConsole.Instance.Output (string.Empty);
63 }
64
65 if (tag == null) {
66 SdtdConsole.Instance.Output ("Equipment of player " + p.Name + ":");
67 }
68
69 PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
70
71 if (tag != null) {
72 SdtdConsole.Instance.Output ("tracker_item id=" + p.EntityID + ", tag=" + tag +
73 ", SHOWINVENTORY DONE");
74 }
75 }
76
77 private void PrintInv (List<InvItem> _inv, int _entityId, string _location, string _tag) {
78 for (int i = 0; i < _inv.Count; i++) {
79 if (_inv [i] != null) {
80 if (_tag == null) {
81 // no Tag defined -> readable output
82 if (_inv [i].quality < 0) {
83 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2}", i,
84 _inv [i].count, _inv [i].itemName));
85 } else {
86 SdtdConsole.Instance.Output (string.Format (" Slot {0}: {1:000} * {2} - quality: {3}", i,
87 _inv [i].count, _inv [i].itemName, _inv [i].quality));
88 }
89
90 DoParts (_inv [i].parts, 1, null);
91 } else {
92 // Tag defined -> parseable output
93 string partsMsg = DoParts (_inv [i].parts, 1, "");
94 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
95 ", slot=" + i + ", item=" + _inv [i].itemName + ", qnty=" + _inv [i].count +
96 ", quality=" + _inv [i].quality + ", parts=(" + partsMsg + ")";
97 SdtdConsole.Instance.Output (msg);
98 }
99 }
100 }
101 }
102
103 private void PrintEquipment (InvItem[] _equipment, int _entityId, string _location, string _tag) {
104 AddEquipment ("head", _equipment, EquipmentSlots.Headgear, _entityId, _location, _tag);
105 AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
106 AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
107
108 AddEquipment ("armor", _equipment, EquipmentSlots.ChestArmor, _entityId, _location, _tag);
109 AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
110 AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
111
112 AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
113 AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
114 AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
115
116 AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
117 }
118
119 private void AddEquipment (string _slotname, InvItem[] _items, EquipmentSlots _slot, int _entityId,
120 string _location, string _tag) {
121 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
122
123 for (int i = 0; i < slotindices.Length; i++) {
124 if (_items != null && _items [slotindices [i]] != null) {
125 InvItem item = _items [slotindices [i]];
126 if (_tag == null) {
127 // no Tag defined -> readable output
128 if (item.quality < 0) {
129 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000}", _slotname,
130 item.itemName));
131 } else {
132 SdtdConsole.Instance.Output (string.Format (" Slot {0:8}: {1:000} - quality: {2}",
133 _slotname, item.itemName, item.quality));
134 }
135
136 DoParts (_items [slotindices [i]].parts, 1, null);
137 } else {
138 // Tag defined -> parseable output
139 string partsMsg = DoParts (_items [slotindices [i]].parts, 1, "");
140 string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
141 ", slot=" + _slotname + ", item=" + item.itemName + ", qnty=1, quality=" +
142 item.quality + ", parts=(" + partsMsg + ")";
143 SdtdConsole.Instance.Output (msg);
144 }
145
146 return;
147 }
148 }
149 }
150
151 private string DoParts (InvItem[] _parts, int _indent, string _currentMessage) {
152 if (_parts != null && _parts.Length > 0) {
153 string indenter = new string (' ', _indent * 4);
154 for (int i = 0; i < _parts.Length; i++) {
155 if (_parts [i] != null) {
156 if (_currentMessage == null) {
157 // no currentMessage given -> readable output
158 if (_parts [i].quality < 0) {
159 SdtdConsole.Instance.Output (string.Format ("{0} - {1}", indenter,
160 _parts [i].itemName));
161 } else {
162 SdtdConsole.Instance.Output (string.Format ("{0} - {1} - quality: {2}",
163 indenter, _parts [i].itemName, _parts [i].quality));
164 }
165
166 DoParts (_parts [i].parts, _indent + 1, _currentMessage);
167 } else {
168 // currentMessage given -> parseable output
169 if (_currentMessage.Length > 0) {
170 _currentMessage += ",";
171 }
172
173 _currentMessage += _parts [i].itemName + "@" + _parts [i].quality;
174 _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
175 }
176 }
177 }
178 }
179
180 return _currentMessage;
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.