Gacha without transactions

This commit is contained in:
raphaeIl 2024-06-05 03:22:29 +08:00
parent 3d4ba39bf4
commit 96f9c89f4d
2 changed files with 23 additions and 58 deletions

View File

@ -46,7 +46,17 @@
foreach (var item in items) foreach (var item in items)
{ {
item.AccountServerId = account.ServerId; item.AccountServerId = account.ServerId;
var existingItem = account.Items.FirstOrDefault(x => x.UniqueId == item.UniqueId);
if (existingItem != null)
{
existingItem.StackCount += item.StackCount;
item.ServerId = existingItem.ServerId;
} else
context.Items.Add(item); context.Items.Add(item);
context.SaveChanges();
} }
return [.. items]; return [.. items];

View File

@ -5,6 +5,7 @@ using SCHALE.Common.NetworkProtocol;
using SCHALE.Common.Utils; using SCHALE.Common.Utils;
using SCHALE.GameServer.Services; using SCHALE.GameServer.Services;
using SCHALE.GameServer.Utils; using SCHALE.GameServer.Utils;
using Serilog;
namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers
{ {
@ -242,72 +243,26 @@ namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers
} }
} }
using var transaction = _context.Database.BeginTransaction(); account.AddCharacters(_context, [.. gachaList.Where(x => x.Character != null).Select(x => x.Character)]);
try var acquiredItems = new List<ItemDB>();
{
// add characters
_context.Characters.AddRange(
gachaList.Where(x => x.Character != null)
.Select(x => x.Character)!);
// create if item does not exist acquiredItems = itemDict.Keys.Select(x => new ItemDB()
foreach (var id in itemDict.Keys)
{
var itemExists = _context.Items
.Any(x => x.AccountServerId == account.ServerId && x.UniqueId == id);
if (!itemExists)
{
_context.Items.Add(new ItemDB()
{ {
IsNew = true, IsNew = true,
UniqueId = id, UniqueId = x,
StackCount = 0, StackCount = itemDict[x],
AccountServerId = account.ServerId, }).ToList();
});
} account.AddItems(_context, [.. acquiredItems]);
}
_context.SaveChanges(); _context.SaveChanges();
// perform item count update
foreach (var (id, count) in itemDict)
{
_context.Items
.Where(x => x.AccountServerId == account.ServerId && x.UniqueId == id)
.ExecuteUpdate(setters => setters.SetProperty(
item => item.StackCount, item => item.StackCount + count));
}
_context.SaveChanges();
transaction.Commit();
_context.Entry(account).Collection(x => x.Items).Reload();
}
catch (Exception ex)
{
_logger.LogError("Transaction failed: {Message}", ex.Message);
throw;
}
var itemDbList = itemDict.Keys
.Select(id => _context.Items.AsNoTracking().First(x => x.AccountServerId == account.ServerId && x.UniqueId == id))
.ToList();
foreach (var gacha in gachaList)
{
if (gacha.Stone != null)
{
gacha.Stone.ServerId = itemDbList.First(x => x.UniqueId == gacha.Stone.UniqueId).ServerId;
}
}
return new ShopBuyGacha3Response() return new ShopBuyGacha3Response()
{ {
GachaResults = gachaList, GachaResults = gachaList,
UpdateTime = DateTime.UtcNow,
GemBonusRemain = int.MaxValue, GemBonusRemain = int.MaxValue,
ConsumedItems = [], ConsumedItems = [],
AcquiredItems = itemDbList, AcquiredItems = acquiredItems,
MissionProgressDBs = [], MissionProgressDBs = [],
}; };
} }