summaryrefslogtreecommitdiff
path: root/lib/utils.php
blob: 639eed4c2c5b63b12ad2caea876e35c7badb0d61 (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
83
84
85
86
87
88
89
90
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php';
use Raudius\Luar\Luar;

function format_timestamp($timestamp)
{
    $days = (int) floor($timestamp / (60.0 * 60.0 * 24.0));
    $years = (int) floor($days / 365);
    $hours = (int) floor(round($timestamp / (60 * 60)) % 24);
    $minutes = (int) floor(round($timestamp % (60 * 60)) / 60);
    $seconds = (int) floor($timestamp % 60);

    if ($years == 0 && $days == 0 && $hours == 0 && $minutes == 0) {
        return "$seconds second" . ($seconds > 1 ? "s" : "");
    } else if ($years == 0 && $days == 0 && $hours == 0) {
        return "$minutes minute" . ($minutes > 1 ? "s" : "");
    } else if ($years == 0 && $days == 0) {
        return "$hours hour" . ($hours > 1 ? "s" : "");
    } else if ($years == 0) {
        return "$days day" . ($days > 1 ? "s" : "");
    } else {
        return "$years year" . ($years > 1 ? "s" : "");
    }
}

function parse_lua_script($value)
{
    if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) {
        return $value->getValue();
    } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) {
        return parse_lua_script($value->getValue());
    }

    if (is_array($value)) {
        $out = [];
        foreach ($value as $k => $v) {
            if ($k == 'handle') {
                continue;
            }
            $out[$k] = parse_lua_script($v);
        }
        return $out;
    }

    return $value;
}

function build_script_metadata(string $folder_path)
{
    $file_path = $_SERVER["DOCUMENT_ROOT"] . "/../scriptmetadata.json";
    $should_create = is_dir($folder_path);
    $files = glob("$folder_path/*.lua");

    if (file_exists($file_path)) {
        $file_last_change = filemtime($file_path);
        $script_last_change = 0;
        foreach ($files as $file) {
            $l = filemtime($file);
            if ($l > $script_last_change) {
                $l = $script_last_change;
            }
        }
        $should_create = $script_last_change > $file_last_change;
    }

    if ($should_create) {
        $scripts = [];

        $luar = new Luar();
        $parsedown = new Parsedown();

        foreach ($files as $file) {
            $contents = file_get_contents($file);

            $result = $luar->eval($contents);
            $script = parse_lua_script($result);
            if (!isset($script["description"])) {
                $script["description"] = "_No description provided._";
            }
            $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']);
            $script['description_formatted'] = $parsedown->text($script['description']);

            array_push($scripts, $script);
        }

        file_put_contents($file_path, json_encode($scripts, JSON_UNESCAPED_SLASHES));
    }

    return true;
}