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

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

Fixes

File size: 3.0 KB
RevLine 
[93]1using System;
2using System.Collections.Generic;
3
[130]4namespace AllocsFixes
[93]5{
[130]6 public class PlayerDataStuff
[93]7 {
[130]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> ();
[93]12
[130]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 }
[98]23 }
[93]24 }
25
[130]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 }
[98]34 }
[93]35 }
36 }
[130]37 };
[93]38
[130]39 private static Dictionary<int, PlayerItems> itemsPerEntityId = new Dictionary<int, PlayerItems> ();
[93]40
[130]41 public static PlayerItems GetPlayerItems (int entityId)
42 {
43 if (itemsPerEntityId.ContainsKey (entityId))
44 return itemsPerEntityId [entityId];
45 else
46 return null;
47 }
[93]48
[130]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);
[93]55
[130]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);
[103]62 }
[93]63
[128]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// }
[93]75 }
76
[130]77 private static string getInvFieldName (InventoryField item)
78 {
[93]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 }
[130]85 return name;
[93]86 }
[130]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 }
[93]102 }
103}
Note: See TracBrowser for help on using the repository browser.