-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactorio.go
114 lines (95 loc) · 2.6 KB
/
factorio.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package utils
import (
"strconv"
"strings"
"time"
v1 "github.com/nekomeowww/factorio-rcon-api/v2/apis/factorioapi/v1"
v2 "github.com/nekomeowww/factorio-rcon-api/v2/apis/factorioapi/v2"
"github.com/nekomeowww/factorio-rcon-api/v2/pkg/apierrors"
"github.com/samber/lo"
)
func StringListToPlayers(list string) ([]*v1.Player, error) {
// output:
// NekoMeow (online)\n
// NekoMeow2 (offline)\n
split := strings.Split(strings.TrimSuffix(list, "\n"), "\n")
players := make([]*v1.Player, 0, len(split))
for _, line := range split {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
if len(parts) > 2 {
return nil, apierrors.NewErrBadRequest().WithDetailf("failed to parse admins: %s due to parts not equals 2", line).AsStatus()
}
player := &v1.Player{
Username: parts[0],
}
if len(parts) == 2 {
player.Online = parts[1] == "(online)"
}
players = append(players, player)
}
return players, nil
}
func MapV1PlayerToV2Player(v1Player *v1.Player) *v2.Player {
return &v2.Player{
Username: v1Player.Username,
Online: v1Player.Online,
}
}
func MapV1PlayersToV2Players(v1Players []*v1.Player) []*v2.Player {
return lo.Map(v1Players, func(item *v1.Player, _ int) *v2.Player { return MapV1PlayerToV2Player(item) })
}
func ParseDuration(input string) (time.Duration, error) {
// Split the input string into parts
parts := strings.Fields(input)
// Initialize the total duration
var totalDuration time.Duration
// Iterate over the parts and parse the time values
for i := 0; i < len(parts); i++ {
switch parts[i] {
case "days":
if i > 0 {
days, err := strconv.ParseInt(parts[i-1], 10, 64)
if err != nil {
return 0, err
}
totalDuration += time.Duration(days) * 24 * time.Hour
}
case "hours":
if i > 0 {
hours, err := time.ParseDuration(parts[i-1] + "h")
if err != nil {
return 0, err
}
totalDuration += hours
}
case "minutes":
if i > 0 {
minutes, err := time.ParseDuration(parts[i-1] + "m")
if err != nil {
return 0, err
}
totalDuration += minutes
}
case "seconds":
if i > 0 {
seconds, err := time.ParseDuration(parts[i-1] + "s")
if err != nil {
return 0, err
}
totalDuration += seconds
}
}
}
return totalDuration, nil
}
func ParseWhitelistedPlayers(input string) []string {
// Replace " and " with a comma to unify the delimiters
input = strings.ReplaceAll(input, " and ", ", ")
// Split the players by commas and trim spaces
players := strings.Split(input, ", ")
for i := range players {
players[i] = strings.TrimSpace(players[i])
}
return players
}