private async Task <EFClientHitStatistic> GetOrAddClientHit(int clientId, long?serverId = null, int?hitLocationId = null, int?weaponId = null, int?attachmentComboId = null, int?meansOfDeathId = null) { var state = _clientHitStatistics[clientId]; await state.OnTransaction.WaitAsync(); var hitStat = state.Hits .FirstOrDefault(hit => hit.HitLocationId == hitLocationId && hit.WeaponId == weaponId && hit.WeaponAttachmentComboId == attachmentComboId && hit.MeansOfDeathId == meansOfDeathId && hit.ServerId == serverId); if (hitStat != null) { state.OnTransaction.Release(); return(hitStat); } hitStat = new EFClientHitStatistic() { ClientId = clientId, ServerId = serverId, WeaponId = weaponId, WeaponAttachmentComboId = attachmentComboId, HitLocationId = hitLocationId, MeansOfDeathId = meansOfDeathId }; try { /*if (state.UpdateCount > MaxUpdatesBeforePersist) * { * await UpdateClientStatistics(clientId); * state.UpdateCount = 0; * } * * state.UpdateCount++;*/ state.Hits.Add(hitStat); } catch (Exception ex) { _logger.LogError(ex, "Could not add {statsName} for {id}", nameof(EFClientHitStatistic), clientId); state.Hits.Remove(hitStat); } finally { if (state.OnTransaction.CurrentCount == 0) { state.OnTransaction.Release(); } } return(hitStat); }
private void RunCalculation(EFClientHitStatistic clientHit, HitInfo hitInfo, HitState hitState) { if (hitInfo.HitType == HitType.Kill || hitInfo.HitType == HitType.Damage) { if (clientHit.WeaponId != null) // we only want to calculate usage time for weapons { var timeElapsed = DateTime.Now - hitState.LastUsage; var isSameWeapon = clientHit.WeaponId == hitState.LastWeaponId; clientHit.UsageSeconds ??= 60; if (timeElapsed.HasValue && timeElapsed <= _maxActiveTime) { clientHit.UsageSeconds += // if it's the same weapon we can count the entire elapsed time // otherwise we split it to make a best guess (int)Math.Round(timeElapsed.Value.TotalSeconds / (isSameWeapon ? 1.0 : 2.0)); } hitState.LastUsage = DateTime.Now; } clientHit.DamageInflicted += hitInfo.Damage; clientHit.HitCount++; } if (hitInfo.HitType == HitType.Kill) { clientHit.KillCount++; } if (hitInfo.HitType == HitType.WasKilled || hitInfo.HitType == HitType.WasDamaged || hitInfo.HitType == HitType.Suicide) { clientHit.ReceivedHitCount++; clientHit.DamageReceived += hitInfo.Damage; } if (hitInfo.HitType == HitType.WasKilled) { clientHit.DeathCount++; } }
public static string RebuildWeaponName(this EFClientHitStatistic stat) => $"{stat.Weapon?.Name}{string.Join("_", stat.WeaponAttachmentCombo?.Attachment1?.Name, stat.WeaponAttachmentCombo?.Attachment2?.Name, stat.WeaponAttachmentCombo?.Attachment3?.Name)}";