-
Notifications
You must be signed in to change notification settings - Fork 3
/
Inventory.cs
506 lines (445 loc) · 19.5 KB
/
Inventory.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Eternal Lands Bot
// Copyright (C) 2006 Artem Makhutov
// artem@makhutov.org
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Timers;
namespace cs_elbot
{
/// <summary>
/// Description of Inventory.
/// </summary>
public class Inventory
{
// OnGotNewInventoryList
public delegate void GotNewInventoryListEventHandler(object sender, GotNewInventoryListEventArgs e);
public event GotNewInventoryListEventHandler GotNewInventoryList;
public class GotNewInventoryListEventArgs : EventArgs
{
public System.Collections.ArrayList TheInventory;
public GotNewInventoryListEventArgs(System.Collections.ArrayList TheInventory)
{
this.TheInventory = TheInventory;
}
}
protected void OnGotNewInventoryList(GotNewInventoryListEventArgs e)
{
inventoryRequested = false;
if (GotNewInventoryList != null)
GotNewInventoryList(this, e);
}
private System.Collections.Hashtable KnownItems = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
private System.Collections.ArrayList TheInventory = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList());
// public bool GettingInventoryItems = false;
//jg change here
public bool GettingInventoryItems = false;
public bool justGambled = false;
public bool Gambling = false;
public bool inventoryRequested = false;
private bool firstTime = true;
public static int sizeOfPacket = 0;
public struct inventory_item
{
public byte pos;
public string name;
public string description;
// public uint quantity;
public UInt32 quantity;
public int SqlID;
public int imageid;
public int weight;
public bool is_resource;
public bool is_reagent;
public bool is_stackable;
public bool use_with_inventory;
public UInt32 reservedQuantity;
}
private TCPWrapper TheTCPWrapper;
private Logger TheLogger;
private MySqlManager TheMySqlManager;
public Inventory(TCPWrapper MyTCPWrapper, Logger MyLogger, MySqlManager MyMySqlManager)
{
this.TheTCPWrapper = MyTCPWrapper;
this.TheTCPWrapper.GotCommand += new TCPWrapper.GotCommandEventHandler(OnGotCommand);
this.TheLogger = MyLogger;
this.TheMySqlManager = MyMySqlManager;
TheTCPWrapper.GotDisconnected += new TCPWrapper.GotDisconnectedEventHandler(GotServerDisconnected);
}
private void GotServerDisconnected(object Sender, EventArgs e)
{
GettingInventoryItems = false;
inventoryRequested = false;
// dirty hack: belongs into the TCPWrapper but we have no MySql access there
TheMySqlManager.ImStarted(Settings.botid);
}
public bool isStackable(int SQLID)
{
bool isStackable = false;
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.SqlID == SQLID)
{
isStackable = MyInventoryItem.is_stackable;
break;
}
}
return isStackable;
}
public int GetMoneySlotID()
{
int SlotID = -1;
int positionInArray = 0;
System.Collections.ArrayList InventoryPosList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.name == "Gold Coins")
{
SlotID = positionInArray;
break;
// return MyInventoryItem.pos;
}
positionInArray++;
}
return SlotID;
}
public uint GetMoneyAmount()
{
System.Collections.ArrayList InventoryPosList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.name == "Gold Coins")
{
return MyInventoryItem.quantity;
}
}
return 0;
}
public System.Collections.ArrayList GetInventoryListBySQLID(int SqlID, System.Collections.ArrayList InventoryList)
{
System.Collections.ArrayList InventorySQLIDList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in InventoryList)
{
if (MyInventoryItem.SqlID == SqlID)
{
InventorySQLIDList.Add(MyInventoryItem);
}
}
return (System.Collections.ArrayList)InventorySQLIDList.Clone();
}
public System.Collections.ArrayList GetInventoryListBySQLID(int SqlID)
{
System.Collections.ArrayList InventorySQLIDList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.SqlID == SqlID)
{
InventorySQLIDList.Add(MyInventoryItem);
}
}
return (System.Collections.ArrayList)InventorySQLIDList.Clone();
}
public System.Collections.ArrayList GetInventoryList()
{
return (System.Collections.ArrayList)TheInventory.Clone();
}
public void requestInventory()
{
Console.WriteLine("inventory requested: " + inventoryRequested);
if (!inventoryRequested)
{
inventoryRequested = true;
TheTCPWrapper.Send(CommandCreator.SEND_MY_INVENTORY());
}
}
private void HERE_YOUR_INVENTORY(byte[] data)
{
//ITEM_REAGENT = 1, /*!< can be used in magic */
//ITEM_RESOURCE = 2, /*!< can be used to manufacture */
//ITEM_STACKABLE = 4, /*!< the item is stackable */
//ITEM_INVENTORY_USABLE = 8, /*!< item can be used with inventory */
//ITEM_TILE_USABLE = 16,
//ITEM_PLAYER_USABLE = 32, /*!< item is usable by players */
//ITEM_OBJECT_USABLE = 64,
//ITEM_ON_OFF = 128,
if (GettingInventoryItems)
{
return;
}
TheLogger.Log("Getting inventory item names!");
TheInventory.Clear();
TheMySqlManager.ClearInventoryList(Settings.botid);
GettingInventoryItems = true;
int total_items = data[3];
if (total_items == 0)
{
OnGotNewInventoryList(new GotNewInventoryListEventArgs(TheInventory));
TheLogger.Log("Got all inventory item names!");
inventoryRequested = false;
GettingInventoryItems = false;
if (firstTime)
{
MainClass.gettingPerks = true;
TheTCPWrapper.Send(CommandCreator.RAW_TEXT("#list_perks"));
firstTime = false;
}
sizeOfPacket = 8;
//TheTCPWrapper.Send(CommandCreator.SIT_DOWN(false));
}
else
{
sizeOfPacket = (data.Length - 4) / total_items;
Console.WriteLine(data.Length + " " + total_items + ":size of packets: " + sizeOfPacket);
if (sizeOfPacket != 8)
{
for (int i = 0; i < total_items; i++)
{
inventory_item MyInventoryItem = new inventory_item();
byte pos = data[i * sizeOfPacket + 1 + 6 + 3];
MyInventoryItem.name = "";
MyInventoryItem.description = "";
MyInventoryItem.weight = 0;
MyInventoryItem.SqlID = 0;
MyInventoryItem.imageid = System.BitConverter.ToInt16(data, i * sizeOfPacket + 1 + 3);
MyInventoryItem.quantity = System.BitConverter.ToUInt32(data, i * sizeOfPacket + 1 + 2 + 3);
MyInventoryItem.pos = pos;
int flags = data[i * sizeOfPacket + 1 + 7 + 3];
MyInventoryItem.is_resource = ((flags & 2) > 0);
MyInventoryItem.is_reagent = ((flags & 1) > 0);
MyInventoryItem.use_with_inventory = ((flags & 8) > 0);
MyInventoryItem.is_stackable = ((flags & 4) > 0);
TheInventory.Add(MyInventoryItem);
TheTCPWrapper.Send(CommandCreator.LOOK_AT_INVENTORY_ITEM((byte)MyInventoryItem.pos));
}
}
else
{
TheTCPWrapper.Send(CommandCreator.RAW_TEXT("#item_uid"));
inventoryRequested = false;
GettingInventoryItems = false;
requestInventory();
}
}
//for (int i = 0; i < total_items; i++)
//{
// inventory_item MyInventoryItem = new inventory_item();
// byte pos = data[i * 8 + 1 + 6 + 3];
// MyInventoryItem.name = "";
// MyInventoryItem.description = "";
// MyInventoryItem.weight = 0;
// MyInventoryItem.SqlID = 0;
// MyInventoryItem.imageid = System.BitConverter.ToInt16(data, i * 8 + 1 + 3);
// MyInventoryItem.quantity = System.BitConverter.ToUInt32(data, i * 8 + 1 + 2 + 3);
// MyInventoryItem.pos = pos;
// int flags = data[i * 8 + 1 + 7 + 3];
// MyInventoryItem.is_resource = ((flags & 2) > 0);
// MyInventoryItem.is_reagent = ((flags & 1) > 0);
// MyInventoryItem.use_with_inventory = ((flags & 8) > 0);
// MyInventoryItem.is_stackable = ((flags & 4) > 0);
// TheInventory.Add(MyInventoryItem);
// TheTCPWrapper.Send(CommandCreator.LOOK_AT_INVENTORY_ITEM((byte)MyInventoryItem.pos));
// System.Threading.Thread.Sleep(10);
//}
}
private inventory_item SetSQLID(inventory_item MyInventoryItem)
{
MyInventoryItem.SqlID = TheMySqlManager.SetSQLID(MyInventoryItem);
return MyInventoryItem;
}
public int HaveItem(int MySQLID)
{
return HaveItem(MySQLID, "");
}
public int HaveItem(string MyItemName)
{
return HaveItem(0, MyItemName);
}
public int HaveItem(int MySQLID, string MyItemName)
{
int pos = -1;
System.Collections.ArrayList MyInventoryList = GetInventoryList();
foreach (Inventory.inventory_item MyInventoryItem in MyInventoryList)
{
if (MyInventoryItem.SqlID == MySQLID || MyItemName == MyInventoryItem.name)
{
if (MyInventoryItem.pos < 36)
{
pos = MyInventoryItem.pos;
}
}
}
return pos;
}
public int Quantity(string MyItemName)
{
return Quantity(0, MyItemName);
}
public int Quantity(int MySQLID)
{
return Quantity(MySQLID, "");
}
public int Quantity(int MySQLID, string MyItemName)
{
int ItemQuantity = 0;
System.Collections.ArrayList MyInventoryList = GetInventoryList();
foreach (Inventory.inventory_item MyInventoryItem in MyInventoryList)
{
if (MyInventoryItem.SqlID == MySQLID || MyInventoryItem.name == MyItemName)
{
if (MyInventoryItem.pos < 36)//draavell add to not count worn inventory in wanted items
{
ItemQuantity += (int)MyInventoryItem.quantity;
}
}
}
return ItemQuantity;
}
private void OnGotCommand(object sender, TCPWrapper.GotCommandEventArgs e)
{
if (e.CommandBuffer[0] == 19)
{
HERE_YOUR_INVENTORY(e.CommandBuffer);
return;
}
if (e.CommandBuffer[0] == 20)
{
INVENTORY_ITEM_TEXT(e.CommandBuffer);
return;
}
if (e.CommandBuffer[0] == 21)
{
GET_NEW_INVENTORY_ITEM(e.CommandBuffer);
return;
}
}
private void REMOVE_ITEM_FROM_INVENTORY(byte[] data)
{
byte pos = data[3];
string sql;
System.Collections.ArrayList InventoryPosList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.pos == pos)
{
sql = "DELETE FROM inventory WHERE `pos` = '" + pos.ToString() + "' AND `botid` = '" + Settings.botid + "' LIMIT 1;#" + MyInventoryItem.name;
TheMySqlManager.raw_sql(sql);
}
}
return;
}
private void GET_NEW_INVENTORY_ITEM(byte[] data)
{
byte pos = data[9];
byte flags = data[10];
uint quantity = System.BitConverter.ToUInt32(data, 5);
int image_id = System.BitConverter.ToInt16(data, 3);
string sql;
System.Collections.ArrayList InventoryPosList = new System.Collections.ArrayList();
foreach (inventory_item MyInventoryItem in GetInventoryList())
{
if (MyInventoryItem.pos == pos)
{
sql = "UPDATE inventory SET quantity = '" + quantity.ToString() + "' WHERE `pos` = '" + pos.ToString() + "' AND `botid` = '" + Settings.botid + "' LIMIT 1;";
TheMySqlManager.raw_sql(sql);
}
}
return;
}
private void INVENTORY_ITEM_TEXT(byte[] data)
{
if (GettingInventoryItems == false||Gambling)
{
return;
}
int i = 0;
string ItemDescription = "";
string TempItemDescription = "";
TempItemDescription = System.Text.ASCIIEncoding.ASCII.GetString(data, 4, data.Length - 4).Trim();
TempItemDescription = TempItemDescription.Replace((char)10, ' ');
// remove bad some chars (eg color tags)
for (i = 0; i < TempItemDescription.Length; i++)
{
if (!(TempItemDescription[i] < 32 || TempItemDescription[i] > 126))
{
ItemDescription = ItemDescription + TempItemDescription[i];
}
}
lock (TheInventory.SyncRoot)
{
for (i = 0; i < TheInventory.Count; i++)
{
inventory_item MyInventoryItem = (inventory_item)TheInventory[i];
if (MyInventoryItem.description == "")
{
ItemDescription = ItemDescription.Trim();
MyInventoryItem.description = ItemDescription;
if (MyInventoryItem.description.ToLower().Contains("extract"))
{
MyInventoryItem.name = ItemDescription.Substring(0, ItemDescription.LastIndexOf(" - ")).Trim();
}
else
{
MyInventoryItem.name = ItemDescription.Substring(0, ItemDescription.IndexOf(" - ")).Trim();
}
MyInventoryItem.weight = int.Parse(ItemDescription.Substring(ItemDescription.IndexOf("Weight:") + 8, ItemDescription.Length - (ItemDescription.IndexOf("Weight:") + 8 + 4)));
MyInventoryItem.SqlID = TheMySqlManager.SetSQLID(MyInventoryItem);
//get the reserved amount
MyInventoryItem.reservedQuantity = TheMySqlManager.ReservedAmount(MyInventoryItem.SqlID);
TheInventory[i] = MyInventoryItem;
// get the knownitems id (setsqlid)
// TheInventory[i] = SetSQLID((inventory_item)TheInventory[i]);
//update the known item info on the table mostly for weight and imageid
TheMySqlManager.updateknownitems(MyInventoryItem, MyInventoryItem.SqlID);
// insert the row may only need this for debugging
string dump = "INSERT INTO inventory (pos,quantity, knownitemsid, botid) VALUES ";
dump += "(" +
MyInventoryItem.pos + "," +
MyInventoryItem.quantity + "," +
MyInventoryItem.SqlID.ToString() + "," +
Settings.botid.ToString() + ")";
dump += ";";
TheMySqlManager.raw_sql(dump);
if (i + 1 < TheInventory.Count) //if this isn't the last one jump out of the loop
{
break;
}
// else
{
OnGotNewInventoryList(new GotNewInventoryListEventArgs(TheInventory));
TheLogger.Log("Got all inventory item names!");
inventoryRequested = false;
GettingInventoryItems = false;
if (firstTime)
{
MainClass.gettingPerks = true;
TheTCPWrapper.Send(CommandCreator.RAW_TEXT("#list_perks"));
firstTime = false;
}
//TheTCPWrapper.Send(CommandCreator.SIT_DOWN(false));
}
}
}
}
}
internal void updateCoins(uint priceOfTicket)
{
int slotID = GetMoneySlotID();
inventory_item myInventoryItem = (inventory_item)TheInventory[slotID];
myInventoryItem.quantity += priceOfTicket;
TheInventory[slotID] = myInventoryItem;
TheMySqlManager.updateCoins(priceOfTicket);
}
}
}