Skip to content

Commit 01a06eb

Browse files
committed
fix: whitelist not properly parsed
Signed-off-by: Neko Ayaka <neko@ayaka.moe>
1 parent 853a7d1 commit 01a06eb

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

internal/grpc/services/factorioapi/v1/console/console.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/nekomeowww/factorio-rcon-api/pkg/apierrors"
1313
"github.com/nekomeowww/factorio-rcon-api/pkg/utils"
1414
"github.com/nekomeowww/xo/logger"
15+
"github.com/samber/lo"
1516
"go.uber.org/fx"
1617
"go.uber.org/zap"
1718
"google.golang.org/grpc/codes"
@@ -603,13 +604,34 @@ func (s *ConsoleService) CommandWhitelistGet(ctx context.Context, req *v1.Comman
603604
return nil, apierrors.NewErrInternal().WithDetail(err.Error()).WithError(err).WithCaller().AsStatus()
604605
}
605606

606-
whitelist, err := utils.StringListToPlayers(resp)
607+
resp = strings.TrimPrefix(resp, "Whitelisted players:")
608+
resp = strings.TrimSpace(resp)
609+
610+
playerNames := utils.ParseWhitelistedPlayers(resp)
611+
612+
players := lo.Map(playerNames, func(player string, _ int) *v1.Player {
613+
return &v1.Player{
614+
Username: player,
615+
}
616+
})
617+
618+
savePlayers, err := s.CommandPlayers(ctx, &v1.CommandPlayersRequest{})
607619
if err != nil {
608620
return nil, err
609621
}
610622

623+
mPlayers := lo.SliceToMap(savePlayers.Players, func(player *v1.Player) (string, *v1.Player) {
624+
return player.Username, player
625+
})
626+
627+
for _, player := range players {
628+
if p, ok := mPlayers[player.Username]; ok {
629+
player.Online = p.Online
630+
}
631+
}
632+
611633
return &v1.CommandWhitelistGetResponse{
612-
Whitelist: whitelist,
634+
Whitelist: players,
613635
}, nil
614636
}
615637

internal/grpc/services/factorioapi/v2/console/console.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/nekomeowww/factorio-rcon-api/pkg/apierrors"
1313
"github.com/nekomeowww/factorio-rcon-api/pkg/utils"
1414
"github.com/nekomeowww/xo/logger"
15+
"github.com/samber/lo"
1516
"go.uber.org/fx"
1617
"go.uber.org/zap"
1718
"google.golang.org/grpc/codes"
@@ -607,13 +608,34 @@ func (s *ConsoleService) CommandWhitelistGet(ctx context.Context, req *v2.Comman
607608
return nil, apierrors.NewErrInternal().WithDetail(err.Error()).WithError(err).WithCaller().AsStatus()
608609
}
609610

610-
whitelist, err := utils.StringListToPlayers(resp)
611+
resp = strings.TrimPrefix(resp, "Whitelisted players:")
612+
resp = strings.TrimSpace(resp)
613+
614+
playerNames := utils.ParseWhitelistedPlayers(resp)
615+
616+
players := lo.Map(playerNames, func(player string, _ int) *v2.Player {
617+
return &v2.Player{
618+
Username: player,
619+
}
620+
})
621+
622+
savePlayers, err := s.CommandPlayers(ctx, &v2.CommandPlayersRequest{})
611623
if err != nil {
612624
return nil, err
613625
}
614626

627+
mPlayers := lo.SliceToMap(savePlayers.Players, func(player *v2.Player) (string, *v2.Player) {
628+
return player.Username, player
629+
})
630+
631+
for _, player := range players {
632+
if p, ok := mPlayers[player.Username]; ok {
633+
player.Online = p.Online
634+
}
635+
}
636+
615637
return &v2.CommandWhitelistGetResponse{
616-
Whitelist: utils.MapV1PlayersToV2Players(whitelist),
638+
Whitelist: players,
617639
}, nil
618640
}
619641

pkg/utils/factorio.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,16 @@ func ParseDuration(input string) (time.Duration, error) {
9999

100100
return totalDuration, nil
101101
}
102+
103+
func ParseWhitelistedPlayers(input string) []string {
104+
// Replace " and " with a comma to unify the delimiters
105+
input = strings.ReplaceAll(input, " and ", ", ")
106+
107+
// Split the players by commas and trim spaces
108+
players := strings.Split(input, ", ")
109+
for i := range players {
110+
players[i] = strings.TrimSpace(players[i])
111+
}
112+
113+
return players
114+
}

pkg/utils/factorio_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"strconv"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -11,3 +12,23 @@ func TestStringListToPlayers(t *testing.T) {
1112
require.NoError(t, err)
1213
require.Len(t, players, 2)
1314
}
15+
16+
func TestParseWhitelistedPlayers(t *testing.T) {
17+
type testCase struct {
18+
input string
19+
expected []string
20+
}
21+
22+
testCases := []testCase{
23+
{input: "NekoMeow", expected: []string{"NekoMeow"}},
24+
{input: "LittleSound and NekoMeow", expected: []string{"LittleSound", "NekoMeow"}},
25+
{input: "LemonNeko, LittleSound and NekoMeow", expected: []string{"LemonNeko", "LittleSound", "NekoMeow"}},
26+
}
27+
28+
for index, tc := range testCases {
29+
t.Run(strconv.Itoa(index), func(t *testing.T) {
30+
players := ParseWhitelistedPlayers(tc.input)
31+
require.Equal(t, tc.expected, players)
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)