private void SaveAudit(SocketGuildUser user, SocketUserMessage message, MathSession session, MathCalcResult result) { using var scope = ServiceProvider.CreateScope(); using var auditService = scope.ServiceProvider.GetRequiredService <MathAuditService>(); auditService.SaveItem(user, message.Channel, session, result); }
private static void ReleaseSession(MathSession session, MathCalcResult result) { if (session == null) { return; } lock (Locker) { session.Release(result); } }
private void ReleaseSession(MathSession session) { if (session == null) { return; } lock (Locker) { session.Release(); } }
public void SaveItem(SocketGuildUser user, IChannel channel, MathSession session, MathCalcResult result) { var unitInfo = new MathUnitInfo() { ComputeLimit = session.ComputingTime, ForBooster = session.ForBooster, SessionID = session.ID }; var dbUser = UsersRepository.GetOrCreateUser(user.Guild.Id, user.Id, false, false, true, false); dbUser.MathAudit.Add(new MathAuditLogItem() { ChannelIDSnowflake = channel.Id, DateTime = DateTime.Now, Expression = session.Expression, Result = JsonConvert.SerializeObject(result), UnitInfo = JsonConvert.SerializeObject(unitInfo) }); UsersRepository.SaveChanges(); }
public MathCalcResult Solve(string input, SocketUserMessage message) { MathSession session = null; MathCalcResult result = null; var user = (SocketGuildUser)message.Author; var boosterRoleId = ConfigurationService.GetValue(Enums.GlobalConfigItems.ServerBoosterRoleId); try { bool booster = !string.IsNullOrEmpty(boosterRoleId) && user.Roles.Any(o => o.Id == Convert.ToUInt64(boosterRoleId)); session = LockAndGetSession(input, booster); input = ("" + input).Trim(); // treatment against null values. var parser = new ExpressionParser(input); if (parser.Empty) { result = new MathCalcResult() { ErrorMessage = "Nelze spočítat prázdný výraz." }; return(result); } if (!parser.IsValid) { result = new MathCalcResult() { ErrorMessage = string.Join(Environment.NewLine, parser.Errors) }; return(result); } try { var task = Task.Run(() => { return(new MathCalcResult() { IsValid = true, Result = parser.Expression.calculate(), ComputingTime = parser.Expression.getComputingTime() * 1000 }); }); if (!task.Wait(session.ComputingTime)) { try { task.Dispose(); } #pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception. catch (Exception) { /* This exception we can ignore. */ } #pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception. result = new MathCalcResult() { IsTimeout = true, AssingedComputingTime = session.ComputingTime }; return(result); } else { result = task.Result; return(result); } } catch (Exception ex) { Logger.LogError(ex, ""); result = new MathCalcResult() { ErrorMessage = ex.Message }; return(result); } } finally { ReleaseSession(session, result); } }
public MathCalcResult Solve(string input, SocketUserMessage message) { MathSession session = null; MathCalcResult result = null; var user = (SocketGuildUser)message.Author; try { bool booster = Config.Discord.IsBooster(user.Roles); session = LockAndGetSession(input, booster); input = ("" + input).Trim(); // treatment against null values. if (string.IsNullOrEmpty(input)) { result = new MathCalcResult() { ErrorMessage = "Nelze spočítat prázdný výraz." }; return(result); } if (input.Contains("nan", StringComparison.InvariantCultureIgnoreCase)) { result = new MathCalcResult() { ErrorMessage = "NaN není platný vstup." }; return(result); } var appPath = GetExecutablePath(user.Guild); using var process = new Process(); process.StartInfo.FileName = appPath; process.StartInfo.Arguments = $"\"{input}\""; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); if (!process.WaitForExit(session.ComputingTime)) { process.Kill(); result = new MathCalcResult() { IsTimeout = true, AssingedComputingTime = session.ComputingTime }; return(result); } else { var output = process.StandardOutput.ReadToEnd(); result = JsonConvert.DeserializeObject <MathCalcResult>(output); if (result == null) { result = new MathCalcResult { IsValid = false, ErrorMessage = "Výpočetní jednotka nevrátila žádná data." }; } const string exceptionPrefix = "|EXCEPTION|"; if (!result.IsValid && result.ErrorMessage.StartsWith(exceptionPrefix)) { var exception = result.ErrorMessage.Substring(exceptionPrefix.Length).Trim(); Logger.LogError(exception); var lines = exception.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); result.ErrorMessage = lines[0]; } return(result); } } finally { SaveAudit(user, message, session, result); ReleaseSession(session, result); } }
public MathCalcResult Solve(string input, SocketUserMessage message) { MathSession session = null; try { var user = (SocketGuildUser)message.Author; bool booster = Config.Discord.IsBooster(user.Roles); session = LockAndGetSession(input, booster); input = ("" + input).Trim(); // treatment against null values. if (string.IsNullOrEmpty(input)) { return(new MathCalcResult() { ErrorMessage = "Nelze spočítat prázdný výraz.", }); } if (input.Contains("nan", StringComparison.InvariantCultureIgnoreCase)) { return(new MathCalcResult() { ErrorMessage = "NaN není platný vstup." }); } using (var repository = new GrillBotRepository(Config)) { var config = repository.Config.FindConfig(user.Guild.Id, "", "solve"); var configData = config.GetData <MathConfig>(); var appPath = configData.ProcessPath; using (var process = new Process()) { process.StartInfo.FileName = "dotnet"; process.StartInfo.Arguments = $"{appPath} \"{input}\""; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); if (!process.WaitForExit(session.ComputingTime)) { process.Kill(); return(new MathCalcResult() { IsTimeout = true, AssingedComputingTime = session.ComputingTime }); } var output = process.StandardOutput.ReadToEnd(); return(JsonConvert.DeserializeObject <MathCalcResult>(output)); } } } finally { ReleaseSession(session); } }