aboutsummaryrefslogtreecommitdiff
path: root/south_browser/rpc.py
blob: db87d3552cc5d799c49f4227308a3d323b1e8558 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import json

from requests import post
from time import time, sleep

_PAYLOAD_ID = 0
LOCAL_RPC = "http://localhost:26503/rpc"

def cmd(method: str, params = None):
    global _PAYLOAD_ID
    _PAYLOAD_ID += 1

    payload = {
        "jsonrpc": "2.0",
        "id": int(time()),
        "method": method
    }

    if params is not None:
        payload["params"] = params

    r = post(LOCAL_RPC, data=json.dumps(payload))

    if r.status_code != 200:
        raise Exception(r.text)

    data = r.json()
    return data

def NSRequestServerList():
    return cmd("execute_squirrel", {
        "code": "return NSRequestServerList()"
    })["result"]

def NSGetServerCount(repeat: bool = True):
    data = cmd("execute_squirrel", {
        "code": "return NSGetServerCount()"
    })

    c = data["result"]

    if not c and repeat:
        NSRequestServerList()
        sleep(1) # give it a second to popualte
        return NSGetServerCount(False)

    return c

def NSGetGameServers():
    # work around Squirrel closure bullshit
    server_count = NSGetServerCount()
    if not server_count:
        return []

    primret = []
    for c in range(server_count):
        primret.append(f"[s[{c}].index, s[{c}].id, s[{c}].name, s[{c}].description, s[{c}].map, s[{c}].playlist, s[{c}].playerCount, s[{c}].maxPlayerCount, s[{c}].requiresPassword, s[{c}].region]")

    ret = "[" + ",".join(primret) + "]"

    return cmd("execute_squirrel", {
        "code": f"array<ServerInfo> s = NSGetGameServers()\nreturn {ret}"
    })["result"]


def NSTryAuthWithServer(index: int, password: str = None):
    if password is None:
        password = ""

    return cmd("execute_squirrel", {
        "code": f"return NSTryAuthWithServer({index}, {json.dumps(password)})"
    })["result"]

def NSWasAuthSuccessful():
    return cmd("execute_squirrel", {
        "code": "return NSWasAuthSuccessful()"
    })["result"]

def NSConnectToAuthedServer():
    return cmd("execute_squirrel", {
        "code": "return NSConnectToAuthedServer()"
    })["result"]