aboutsummaryrefslogtreecommitdiff
path: root/api/ruleset.py
blob: b42b4e93a415a36fe6d1689593aeb74d6c17510f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from mergedeep import merge
import json

# predefined rulesets
rulesets = {
    "default": {
        "timelimit": {
            "enabled": False,
            "minutes": 0,
            "seconds": 0,
            "addmove": 0,
            "shared": False,
        },
        "ranked": True,
    }
}

# resolve ruleset from ruleset name or dict
def resolve_ruleset(ruleset):
    # create return variable
    export = {}
    try:
        # try to parse the ruleset as json
        export = json.loads(ruleset)
        merged = dict(rulesets["default"])

        # fill missing keys in dict
        merge(merged, export)
        export = merged
    except ValueError as e:
        # if the ruleset is a name like 'default' or 'columns+2', read it from the predefined rulesets
        if ruleset in rulesets:
            export = rulesets[ruleset]
    if not export:
        export = rulesets["default"]
    return export