Browse Source

Moves CalculateScore function to outer scope.

tags/3.0.0
roridev 4 years ago
parent
commit
574b503e9e
1 changed files with 18 additions and 19 deletions
  1. +18
    -19
      src/Discord.Net.Commands/CommandService.cs

+ 18
- 19
src/Discord.Net.Commands/CommandService.cs View File

@@ -511,7 +511,6 @@ namespace Discord.Commands
await _commandExecutedEvent.InvokeAsync(Optional.Create<CommandInfo>(), context, searchResult).ConfigureAwait(false); await _commandExecutedEvent.InvokeAsync(Optional.Create<CommandInfo>(), context, searchResult).ConfigureAwait(false);
return searchResult; return searchResult;
} }


var commands = searchResult.Commands; var commands = searchResult.Commands;
var preconditionResults = new Dictionary<CommandMatch, PreconditionResult>(); var preconditionResults = new Dictionary<CommandMatch, PreconditionResult>();
@@ -559,24 +558,6 @@ namespace Discord.Commands
parseResultsDict[pair.Key] = parseResult; parseResultsDict[pair.Key] = parseResult;
} }


// Calculates the 'score' of a command given a parse result
float CalculateScore(CommandMatch match, ParseResult parseResult)
{
float argValuesScore = 0, paramValuesScore = 0;

if (match.Command.Parameters.Count > 0)
{
var argValuesSum = parseResult.ArgValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;
var paramValuesSum = parseResult.ParamValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;

argValuesScore = argValuesSum / match.Command.Parameters.Count;
paramValuesScore = paramValuesSum / match.Command.Parameters.Count;
}

var totalArgsScore = (argValuesScore + paramValuesScore) / 2;
return match.Command.Priority + totalArgsScore * 0.99f;
}

//Order the parse results by their score so that we choose the most likely result to execute //Order the parse results by their score so that we choose the most likely result to execute
var parseResults = parseResultsDict var parseResults = parseResultsDict
.OrderByDescending(x => CalculateScore(x.Key, x.Value)); .OrderByDescending(x => CalculateScore(x.Key, x.Value));
@@ -603,6 +584,24 @@ namespace Discord.Commands
return result; return result;
} }


// Calculates the 'score' of a command given a parse result
float CalculateScore(CommandMatch match, ParseResult parseResult)
{
float argValuesScore = 0, paramValuesScore = 0;

if (match.Command.Parameters.Count > 0)
{
var argValuesSum = parseResult.ArgValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;
var paramValuesSum = parseResult.ParamValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;

argValuesScore = argValuesSum / match.Command.Parameters.Count;
paramValuesScore = paramValuesSum / match.Command.Parameters.Count;
}

var totalArgsScore = (argValuesScore + paramValuesScore) / 2;
return match.Command.Priority + totalArgsScore * 0.99f;
}

protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!_isDisposed) if (!_isDisposed)


Loading…
Cancel
Save