+
Skip to content

Fixes optional and required parameters from scripted effects #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
18 changes: 17 additions & 1 deletion CWTools/Game/Compute.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ module STL =
open CWTools.Common.STLConstants
open CWTools.Validation.Stellaris

let getScriptedEffectParams (node : Node) =
let getDollarText (s : string) (acc) =
s.Split('$') |> Array.mapi (fun i s -> i, s) |> Array.fold (fun acc (i, s) ->
if i % 2 = 1 then (s.Split('|').[0], s.Contains("|"))::acc else acc
) acc
let fNode = (fun (x:Node) acc ->
let nodeRes = getDollarText x.Key acc
x.Values |> List.fold (fun a n -> getDollarText n.Key (getDollarText (n.Value.ToRawString()) a)) nodeRes
)
node |> (foldNode7 fNode) |> List.ofSeq

let getScriptedEffectParamsEntity (e : Entity) =
if (e.logicalpath.StartsWith("common/scripted_effects", StringComparison.OrdinalIgnoreCase)
|| e.logicalpath.StartsWith("common/scripted_triggers", StringComparison.OrdinalIgnoreCase))
then getScriptedEffectParams (e.entity) else []

let getAllTechPrereqs (e : Entity) =
let fNode = (fun (x : Node) acc ->
match x with
Expand Down Expand Up @@ -180,7 +196,7 @@ module STL =
// let definedvariable = (if infoService().IsSome then Some ((infoService().Value.GetDefinedVariables )(e)) else None)
// let effectBlocks, triggersBlocks = (if infoService().IsSome then let (e, t) = ((infoService().Value.GetEffectBlocks )(e)) in Some e, Some t else None, None)
let hastechs = getAllTechPrereqs e
let scriptedeffectparams = Some (EU4.getScriptedEffectParamsEntity e)
let scriptedeffectparams = Some (getScriptedEffectParamsEntity e)
let referencedtypes = referencedtypes |> Option.map (fun r -> r |> List.ofSeq |> List.fold (fun acc (kv) -> acc |> (Map.add kv.Key (kv.Value))) Map.empty )
let referencedtypes = referencedtypes |> Option.map (fun r -> r |> Map.map (fun k v -> (v.ToArray() |> List.ofSeq)))
STLComputedData(eventIds, setvariables, setflags, savedeventtargets, referencedtypes, hastechs, definedvariable, withRulesData, effectBlocks, triggersBlocks, scriptedeffectparams, savedEventTargets)
Expand Down
4 changes: 2 additions & 2 deletions CWTools/Game/Stellaris/STLGame.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ module STLGameFunctions =
rulesWithEmbeddedScopes

let refreshConfigBeforeFirstTypesHook (lookup : STLLookup) (resources : IResourceAPI<STLComputedData>) (embeddedSettings : EmbeddedSettings) =
lookup.STLScriptedEffectKeys <- "scaled_skill" :: (resources.AllEntities() |> PSeq.map (fun struct(e, l) -> (l.Force().ScriptedEffectParams |> (Option.defaultWith (fun () -> CWTools.Games.Compute.EU4.getScriptedEffectParamsEntity e))))
|> List.ofSeq |> List.collect id)
lookup.STLScriptedEffectKeys <- "scaled_skill" :: (resources.AllEntities() |> PSeq.map (fun struct(e, l) -> (l.Force().ScriptedEffectParams |> (Option.defaultWith (fun () -> CWTools.Games.Compute.STL.getScriptedEffectParamsEntity e))))
|> List.ofSeq |> List.collect (List.map fst))
let scriptedEffectParmas = { key = "scripted_effect_params"; description = "Scripted effect parameter"; values = lookup.STLScriptedEffectKeys }
let scriptedEffectParmasD = { key = "scripted_effect_params_dollar"; description = "Scripted effect parameter"; values = lookup.STLScriptedEffectKeys |> List.map (fun k -> sprintf "$%s$" k)}
lookup.enumDefs <-
Expand Down
2 changes: 1 addition & 1 deletion CWTools/Game/Stellaris/STLLookup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module STLLookup =
member __.Setflags : (FlagType * string) list = setflags
member __.Savedeventtargets : string list = savedeventtargets
member __.Hastechs : string list = hastechs
member __.ScriptedEffectParams : string list option = scriptedeffectparams
member __.ScriptedEffectParams : (string * bool) list option = scriptedeffectparams

let getChildrenWithComments (root : Node) =
let findComment t s (a : Child) =
Expand Down
6 changes: 5 additions & 1 deletion CWTools/Validation/Common/CommonValidation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ module CommonValidation =
location = callSite
message = sprintf "This call of scripted effect %s results in an error" name
}
res |> (function
let requiredParams = CWTools.Games.Compute.STL.getScriptedEffectParams rootNode |> List.filter (fun (_, optional) -> not optional) |> List.map fst
let usedParams = seParams |> Option.defaultValue [] |> List.map fst
let missingParams = List.except usedParams requiredParams
let newError = if missingParams.Length > 0 then Invalid (System.Guid.NewGuid(), [inv (ErrorCodes.CustomError "Some non optional parameters are not supplied" Severity.Error) node]) else OK
(res <&&> newError) |> (function
| OK -> OK
| Invalid (_, inv) -> Invalid (System.Guid.NewGuid(), inv |> List.map (fun e -> { e with relatedErrors = Some message })))
let memoizeValidation =
Expand Down
3 changes: 3 additions & 0 deletions CWToolsTests/testfiles/configtests/config/test.cwt
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,6 @@ alias[effect:<scripted_effect>] = {
## cardinality = 1..inf
enum[scripted_effect_params] = scope_field
}

###Scripted effects called by "scripted_effect_name = yes" shouldn't throw an error
alias[effect:<scripted_effect>] = yes
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,43 @@ test_scripted_effect_params = {
#error
#error
set_ship_flag = $test_rhs$
}

test_scripted_effect_optional_params_supplied = {
set_name = $optional_param|test_flag$
}

test_scripted_effect_optional_params_not_supplied = {
set_name = $optional_param|test_flag$
}

test_scripted_effect_not_optional_params_supplied = {
set_name = $not_optional_param$
}

#error
test_scripted_effect_not_optional_params_not_supplied = {
set_name = $not_optional_param$
}

test_scripted_effect_several_params_all_supplied = {
set_name = $not_optional_param$
set_name = $optional_param|test_flag$
}

#error
test_scripted_effect_several_params_optional_supplied = {
set_name = $not_optional_param$
set_name = $optional_param|test_flag$
}

test_scripted_effect_several_params_non_optional_supplied = {
set_name = $not_optional_param$
set_name = $optional_param|test_flag$
}

#error
test_scripted_effect_several_params_none_supplied = {
set_name = $not_optional_param$
set_name = $optional_param|test_flag$
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ country_event = {
test_lhs = blah
test_rhs = blah
}
test_scripted_effect_optional_params_supplied = {
optional_param = param_flag
}
test_scripted_effect_optional_params_not_supplied = yes
test_scripted_effect_not_optional_params_supplied = {
not_optional_param = param_flag
}
#error
test_scripted_effect_not_optional_params_not_supplied = yes
test_scripted_effect_several_params_all_supplied = {
optional_param = param_flag
not_optional_param = param_flag
}
test_scripted_effect_several_params_non_optional_supplied = {
not_optional_param = param_flag
}
#error
test_scripted_effect_several_params_optional_supplied = {
optional_param = param_flag
}
#error
test_scripted_effect_several_params_none_supplied = yes


}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载