source: binary-improvements/7dtd-server-fixes/src/PersistentData/Inventory.cs@ 477

Last change on this file since 477 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 2.6 KB
RevLine 
[253]1using System;
2using System.Collections.Generic;
3
[324]4namespace AllocsFixes.PersistentData {
[325]5 [Serializable]
6 public class Inventory {
7 public List<InvItem> bag;
8 public List<InvItem> belt;
9 public InvItem[] equipment;
[253]10
[325]11 public Inventory () {
12 bag = new List<InvItem> ();
13 belt = new List<InvItem> ();
14 equipment = null;
15 }
[253]16
[351]17 public void Update (PlayerDataFile _pdf) {
[325]18 lock (this) {
19 //Log.Out ("Updating player inventory - player id: " + pdf.id);
[351]20 ProcessInv (bag, _pdf.bag, _pdf.id);
21 ProcessInv (belt, _pdf.inventory, _pdf.id);
22 ProcessEqu (_pdf.equipment, _pdf.id);
[325]23 }
24 }
[253]25
[455]26 private void ProcessInv (ICollection<InvItem> _target, IReadOnlyList<ItemStack> _sourceFields, int _id) {
[351]27 _target.Clear ();
[455]28 for (int i = 0; i < _sourceFields.Count; i++) {
[351]29 InvItem item = CreateInvItem (_sourceFields [i].itemValue, _sourceFields [i].count, _id);
30 if (item != null && _sourceFields [i].itemValue.Modifications != null) {
31 ProcessParts (_sourceFields [i].itemValue.Modifications, item, _id);
[325]32 }
[253]33
[351]34 _target.Add (item);
[325]35 }
36 }
[253]37
[351]38 private void ProcessEqu (Equipment _sourceEquipment, int _playerId) {
39 equipment = new InvItem[_sourceEquipment.GetSlotCount ()];
40 for (int i = 0; i < _sourceEquipment.GetSlotCount (); i++) {
41 equipment [i] = CreateInvItem (_sourceEquipment.GetSlotItem (i), 1, _playerId);
[325]42 }
43 }
[253]44
[455]45 private void ProcessParts (IReadOnlyList<ItemValue> _parts, InvItem _item, int _playerId) {
46 InvItem[] itemParts = new InvItem[_parts.Count];
47 for (int i = 0; i < _parts.Count; i++) {
[325]48 InvItem partItem = CreateInvItem (_parts [i], 1, _playerId);
49 if (partItem != null && _parts [i].Modifications != null) {
50 ProcessParts (_parts [i].Modifications, partItem, _playerId);
51 }
[253]52
[325]53 itemParts [i] = partItem;
54 }
[253]55
[325]56 _item.parts = itemParts;
57 }
[253]58
[325]59 private InvItem CreateInvItem (ItemValue _itemValue, int _count, int _playerId) {
[326]60 if (_count <= 0 || _itemValue == null || _itemValue.Equals (ItemValue.None)) {
61 return null;
62 }
[253]63
[326]64 ItemClass itemClass = ItemClass.list [_itemValue.type];
65 int maxAllowed = itemClass.Stacknumber.Value;
66 string name = itemClass.GetItemName ();
[324]67
[326]68 if (_count > maxAllowed) {
[455]69 Log.Out ($"Player with ID {_playerId} has stack for \"{name}\" greater than allowed ({_count} > {maxAllowed})");
[326]70 }
[324]71
[326]72 InvItem item;
73 if (_itemValue.HasQuality) {
74 item = new InvItem (name, _count, _itemValue.Quality, _itemValue.MaxUseTimes, _itemValue.UseTimes);
75 } else {
76 item = new InvItem (name, _count, -1, _itemValue.MaxUseTimes, _itemValue.UseTimes);
77 }
[324]78
[326]79 item.icon = itemClass.GetIconName ();
[324]80
[326]81 item.iconcolor = AllocsUtils.ColorToHex (itemClass.GetIconTint ());
[325]82
[326]83 return item;
[325]84 }
85 }
[324]86}
Note: See TracBrowser for help on using the repository browser.