source: binary-improvements/7dtd-server-fixes/src/PlayerDataStuff.cs@ 134

Last change on this file since 134 was 130, checked in by alloc, 10 years ago

Fixes

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace AllocsFixes
5{
6 public class PlayerDataStuff
7 {
8 public class PlayerItems
9 {
10 public SortedList<string, int> belt = new SortedList<string, int> ();
11 public SortedList<string, int> bag = new SortedList<string, int> ();
12
13 public PlayerItems (InventoryField[] _belt, InventoryField[] _bag)
14 {
15 foreach (InventoryField item in _belt) {
16 if (item.count > 0) {
17 string name = getInvFieldName (item);
18 if (belt.ContainsKey (name)) {
19 belt [name] += item.count;
20 } else {
21 belt.Add (name, item.count);
22 }
23 }
24 }
25
26 foreach (InventoryField item in _bag) {
27 if (item.count > 0) {
28 string name = getInvFieldName (item);
29 if (bag.ContainsKey (name)) {
30 bag [name] += item.count;
31 } else {
32 bag.Add (name, item.count);
33 }
34 }
35 }
36 }
37 };
38
39 private static Dictionary<int, PlayerItems> itemsPerEntityId = new Dictionary<int, PlayerItems> ();
40
41 public static PlayerItems GetPlayerItems (int entityId)
42 {
43 if (itemsPerEntityId.ContainsKey (entityId))
44 return itemsPerEntityId [entityId];
45 else
46 return null;
47 }
48
49 public static void GM_SavePlayerData (GameManager manager, int _clientId, PlayerDataFile _playerDataFile)
50 {
51 try {
52 int entityId = CommonMappingFunctions.GetEntityID (CommonMappingFunctions.GetClientInfoFromClientID (_clientId));
53 if (entityId >= 0) {
54 Log.Out ("Saving playerData for entity id: " + entityId);
55
56 if (itemsPerEntityId.ContainsKey (entityId))
57 itemsPerEntityId.Remove (entityId);
58 itemsPerEntityId.Add (entityId, new PlayerItems (_playerDataFile.inventory, _playerDataFile.bag));
59 }
60 } catch (Exception e) {
61 Log.Out ("Error in GM_SavePlayerData: " + e);
62 }
63
64// Log.Out ("Inventory of player:");
65// for (int i = 0; i < _playerDataFile.inventory.Length; i++) {
66// InventoryField item = _playerDataFile.inventory [i];
67// printItem (item, i);
68// }
69//
70// Log.Out ("Bag of player:");
71// for (int i = 0; i < _playerDataFile.bag.Length; i++) {
72// InventoryField item = _playerDataFile.bag [i];
73// printItem (item, i);
74// }
75 }
76
77 private static string getInvFieldName (InventoryField item)
78 {
79 ItemBase iBase = ItemBase.list [item.itemValue.type];
80 string name = iBase.name;
81 if (iBase.IsBlock ()) {
82 ItemBlock iBlock = (ItemBlock)iBase;
83 name = iBlock.GetItemName (item.itemValue);
84 }
85 return name;
86 }
87
88 private static void printItem (InventoryField item, int slot)
89 {
90 if (item.count > 0) {
91 ItemBase iBase = ItemBase.list [item.itemValue.type];
92 string name = iBase.name;
93 if (iBase.IsBlock ()) {
94 ItemBlock iBlock = (ItemBlock)iBase;
95 name = iBlock.GetItemName (item.itemValue);
96 }
97 Log.Out (string.Format ("Slot {0:00}: {1:00} * {2}, blockinst={3}, meta={4}, type={5}, usetimes={6}",
98 slot, item.count, name, item.itemValue.blockinst, item.itemValue.meta, item.itemValue.type, item.itemValue.usetimes)
99 );
100 }
101 }
102 }
103}
Note: See TracBrowser for help on using the repository browser.