Skip to content

Commit

Permalink
fix: whitelist not properly parsed
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <neko@ayaka.moe>
  • Loading branch information
nekomeowww committed Oct 31, 2024
1 parent 853a7d1 commit 01a06eb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
26 changes: 24 additions & 2 deletions internal/grpc/services/factorioapi/v1/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/nekomeowww/factorio-rcon-api/pkg/apierrors"
"github.com/nekomeowww/factorio-rcon-api/pkg/utils"
"github.com/nekomeowww/xo/logger"
"github.com/samber/lo"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -603,13 +604,34 @@ func (s *ConsoleService) CommandWhitelistGet(ctx context.Context, req *v1.Comman
return nil, apierrors.NewErrInternal().WithDetail(err.Error()).WithError(err).WithCaller().AsStatus()
}

whitelist, err := utils.StringListToPlayers(resp)
resp = strings.TrimPrefix(resp, "Whitelisted players:")
resp = strings.TrimSpace(resp)

playerNames := utils.ParseWhitelistedPlayers(resp)

players := lo.Map(playerNames, func(player string, _ int) *v1.Player {
return &v1.Player{
Username: player,
}
})

savePlayers, err := s.CommandPlayers(ctx, &v1.CommandPlayersRequest{})
if err != nil {
return nil, err
}

mPlayers := lo.SliceToMap(savePlayers.Players, func(player *v1.Player) (string, *v1.Player) {
return player.Username, player
})

for _, player := range players {
if p, ok := mPlayers[player.Username]; ok {
player.Online = p.Online
}
}

return &v1.CommandWhitelistGetResponse{
Whitelist: whitelist,
Whitelist: players,
}, nil
}

Expand Down
26 changes: 24 additions & 2 deletions internal/grpc/services/factorioapi/v2/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/nekomeowww/factorio-rcon-api/pkg/apierrors"
"github.com/nekomeowww/factorio-rcon-api/pkg/utils"
"github.com/nekomeowww/xo/logger"
"github.com/samber/lo"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -607,13 +608,34 @@ func (s *ConsoleService) CommandWhitelistGet(ctx context.Context, req *v2.Comman
return nil, apierrors.NewErrInternal().WithDetail(err.Error()).WithError(err).WithCaller().AsStatus()
}

whitelist, err := utils.StringListToPlayers(resp)
resp = strings.TrimPrefix(resp, "Whitelisted players:")
resp = strings.TrimSpace(resp)

playerNames := utils.ParseWhitelistedPlayers(resp)

players := lo.Map(playerNames, func(player string, _ int) *v2.Player {
return &v2.Player{
Username: player,
}
})

savePlayers, err := s.CommandPlayers(ctx, &v2.CommandPlayersRequest{})
if err != nil {
return nil, err
}

mPlayers := lo.SliceToMap(savePlayers.Players, func(player *v2.Player) (string, *v2.Player) {
return player.Username, player
})

for _, player := range players {
if p, ok := mPlayers[player.Username]; ok {
player.Online = p.Online
}
}

return &v2.CommandWhitelistGetResponse{
Whitelist: utils.MapV1PlayersToV2Players(whitelist),
Whitelist: players,
}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/factorio.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,16 @@ func ParseDuration(input string) (time.Duration, error) {

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
}
21 changes: 21 additions & 0 deletions pkg/utils/factorio_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,3 +12,23 @@ func TestStringListToPlayers(t *testing.T) {
require.NoError(t, err)
require.Len(t, players, 2)
}

func TestParseWhitelistedPlayers(t *testing.T) {
type testCase struct {
input string
expected []string
}

testCases := []testCase{
{input: "NekoMeow", expected: []string{"NekoMeow"}},
{input: "LittleSound and NekoMeow", expected: []string{"LittleSound", "NekoMeow"}},
{input: "LemonNeko, LittleSound and NekoMeow", expected: []string{"LemonNeko", "LittleSound", "NekoMeow"}},
}

for index, tc := range testCases {
t.Run(strconv.Itoa(index), func(t *testing.T) {
players := ParseWhitelistedPlayers(tc.input)
require.Equal(t, tc.expected, players)
})
}
}

0 comments on commit 01a06eb

Please sign in to comment.