Skip to content

Commit

Permalink
Changed MongoId
Browse files Browse the repository at this point in the history
  • Loading branch information
nexus4880 committed Jan 19, 2025
1 parent 183c8e8 commit ce03796
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 68 deletions.
12 changes: 6 additions & 6 deletions Fuyu.Backend.BSG/Services/InventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ public void RegenerateIds(InventoryInfo inventory)
var mapping = new Dictionary<string, string>();

// regenerate inventory equipment
mapping.Add(inventory.Equipment, new MongoId(true));
mapping.Add(inventory.Equipment, MongoId.Generate());
inventory.Equipment = mapping[inventory.Equipment];

// regenerate inventory stash
if (inventory.Stash != null)
{
mapping.Add(inventory.Stash.Value, new MongoId(true));
mapping.Add(inventory.Stash.Value, MongoId.Generate());
inventory.Stash = mapping[inventory.Stash.Value];
}

// regenerate inventory quest raid items
if (inventory.QuestRaidItems != null)
{
mapping.Add(inventory.QuestRaidItems.Value, new MongoId(true));
mapping.Add(inventory.QuestRaidItems.Value, MongoId.Generate());
inventory.QuestRaidItems = mapping[inventory.QuestRaidItems.Value];
}

// regenerate inventory quest stash items
if (inventory.QuestStashItems != null)
{
mapping.Add(inventory.QuestStashItems.Value, new MongoId(true));
mapping.Add(inventory.QuestStashItems.Value, MongoId.Generate());
inventory.QuestStashItems = mapping[inventory.QuestStashItems.Value];
}

// regenerate inventory sorting table
if (inventory.SortingTable != null)
{
mapping.Add(inventory.SortingTable.Value, new MongoId(true));
mapping.Add(inventory.SortingTable.Value, MongoId.Generate());
inventory.SortingTable = mapping[inventory.SortingTable.Value];
}

Expand All @@ -67,7 +67,7 @@ public void RegenerateIds(InventoryInfo inventory)
{
if (!mapping.ContainsKey(id))
{
mapping.Add(id, new MongoId(true));
mapping.Add(id, MongoId.Generate());
}
}

Expand Down
4 changes: 2 additions & 2 deletions Fuyu.Backend.BSG/Services/ItemFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public T GetItemProperties<T>(MongoId templateId) where T : ItemProperties
string slotId = null)
{
var items = new List<ItemInstance>();
var itemId = id.GetValueOrDefault(new MongoId(true));
var itemId = id.GetValueOrDefault(MongoId.Generate());
var upd = CreateItemUpdatable(template);
var item = new ItemInstance
Expand Down Expand Up @@ -92,7 +92,7 @@ public List<ItemInstance> CreateItem(ItemTemplate template, int? count = null, M

for (var i = 0; i < itemCount; i++)
{
var itemId = i == 0 && id.HasValue ? id.Value : new MongoId(true);
var itemId = i == 0 && id.HasValue ? id.Value : MongoId.Generate();
var upd = CreateItemUpdatable(template);

var item = new ItemInstance
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.BSG/Services/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void RegenerateItemIds(List<ItemInstance> items)
var mapping = new Dictionary<string, string>();
foreach (var item in items)
{
mapping.TryAdd(item.Id, new MongoId(true));
mapping.TryAdd(item.Id, MongoId.Generate());
}

RegenerateItemIds(items, mapping);
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.EFTMain/Services/BotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private Profile GenerateBot(EWildSpawnType role, EBotDifficulty difficulty)
var profile = Json.Parse<Profile>(_profiles[role]);

// regenerate all ids
profile._id = new MongoId(true);
profile._id = MongoId.Generate();
_inventoryService.RegenerateIds(profile.Inventory);

// set difficulty
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.EFTMain/Services/HandbookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HandbookService
public HandbookService()
{
_eftOrm = EftOrm.Instance;
_generatedCategoryId = new MongoId(true);
_generatedCategoryId = MongoId.Generate();
}

public HashSet<HandbookCategory> GetHandbookTree(List<HandbookCategory> categories, MongoId rootId)
Expand Down
6 changes: 4 additions & 2 deletions Fuyu.Backend.EFTMain/Services/ProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public string CreateProfile(int accountId)
};

// generate new ids
var pmcId = new MongoId(true).ToString();
var savageId = new MongoId(pmcId, 1, false).ToString();
var mongoId = MongoId.Generate();
var pmcId = mongoId.ToString();
mongoId = mongoId.Next();
var savageId = mongoId.ToString();

// set profile info
profile.Pmc._id = pmcId;
Expand Down
2 changes: 1 addition & 1 deletion Fuyu.Backend.EFTMain/Services/RagfairService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Offer CreateAndAddOffer(IRagfairUser user, List<ItemInstance> items, bool

var offer = new Offer()
{
Id = new MongoId(true),
Id = MongoId.Generate(),
IntId = Offers.Count,
User = user,
RootItemId = items[0].Id,
Expand Down
81 changes: 30 additions & 51 deletions Fuyu.Common/Hashing/MongoId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Fuyu.Common.Hashing;
public readonly struct MongoId : IComparable<MongoId>, IEquatable<MongoId>
{
private static readonly Random _random = new Random();
private static readonly ulong _processId = Hash;
private static uint _newIdCounter;
private readonly uint _timeStamp;
private readonly ulong _counter;

Expand All @@ -33,35 +31,45 @@ public static ulong Hash
}
}

[JsonConstructor]
public MongoId([JsonProperty("$value")] string id)
public static bool TryParse(string str, out MongoId id)
{
if (id == null || id.Length != 24)
if (str == null || str.Length != 24)
{
throw new ArgumentOutOfRangeException($"Critical MongoId error: incorrect length. Id: {id}");
id = default;
return false;
}

_timeStamp = GetTimestamp(id);
_counter = GetCounter(id);
var timeStamp = GetTimestamp(str);
var counter = GetCounter(str);
id = new MongoId(timeStamp, counter);

GenerateNew();
return true;
}

public MongoId(bool newProcessId)
public MongoId(uint timeStamp, ulong counter)
{
_timeStamp = 0U;
_timeStamp = timeStamp;
_counter = counter;
}

if (newProcessId)
{
_counter = Hash << 24;
}
else
public static MongoId Parse(string str)
{
if (str == null || str.Length != 24)
{
_newIdCounter += 1U;
_counter = (_processId << 24) + (ulong)_newIdCounter;
throw new Exception();
}

_timeStamp = UnixTimestamp;
var timeStamp = GetTimestamp(str);
var counter = GetCounter(str);

return new MongoId(timeStamp, counter);
}

public static MongoId Generate()
{
var counter = Hash << 24;

return new MongoId(UnixTimestamp, counter);
}

public MongoId(int accountId)
Expand All @@ -76,27 +84,6 @@ public MongoId(int accountId)
_counter |= (ulong)num2;
}

public MongoId(MongoId source, int increment, bool newTimestamp = true)
{
if (newTimestamp)
{
_timeStamp = UnixTimestamp;
}
else
{
_timeStamp = source._timeStamp;
}

if (increment > 0)
{
_counter = source._counter + (ulong)Convert.ToUInt32(increment);
}
else
{
_counter = source._counter - (ulong)Convert.ToUInt32(Math.Abs(increment));
}
}

// smethod_1
public static uint GetTimestamp(string id)
{
Expand All @@ -110,17 +97,9 @@ public static ulong GetCounter(string id)
}

// method_0
public void GenerateNew()
public MongoId Next()
{
var num = Convert.ToUInt64(_counter >> 24);

if (_processId != num)
{
return;
}

var num2 = Convert.ToUInt32(_counter << 40 >> 40);
_newIdCounter = Math.Max(_newIdCounter, num2);
return new MongoId(_timeStamp, _counter + 1);
}

public bool Equals(MongoId other)
Expand Down Expand Up @@ -186,7 +165,7 @@ public static implicit operator string(MongoId mongoId)

public static implicit operator MongoId(string id)
{
return new MongoId(id);
return Parse(id);
}

public static bool operator ==(MongoId a, MongoId b)
Expand Down
5 changes: 2 additions & 3 deletions Fuyu.Common/Serialization/MongoIdConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ public class MongoIdConverter : JsonConverter<MongoId?>
{
if (reader.Value != null)
{
var str = (string)reader.Value;
if (str.Length == 24)
if (MongoId.TryParse((string)reader.Value, out MongoId mongoId))
{
return new MongoId(str);
return mongoId;
}
}

Expand Down

0 comments on commit ce03796

Please sign in to comment.