Skip to content

Commit

Permalink
Filter out duplicate servers
Browse files Browse the repository at this point in the history
  • Loading branch information
ResamVi committed Aug 25, 2023
1 parent 16f53c6 commit c680b99
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
83 changes: 83 additions & 0 deletions backend/cmd/fetch/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/ResamVi/dontstarve-datavis/fetch/fetcher"
"github.com/ResamVi/dontstarve-datavis/model"

"github.com/olekukonko/tablewriter"
)

func main() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Africa", "Empty", "Europe", "ERROR", "South America", "Asia", "North America", "Oceania"})
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
go func() {
<-sc
table.Render()
os.Exit(0)
}()

for {

// B Processing
// data, err := fetcher.GetServers()
// if err != nil {
// panic(err)
// }
// for _, entry := range data {
// table.Append([]string{entry.Addr, entry.Name})
// }
// table.Render()

// A Processing
// table.SetHeader([]string{"Name", "Sign", "Rating"}) // A
f := fetcher.New()
data, err := f.Fetch()
if err != nil {
panic(err)
}
count := make(map[string]int)

for _, entry := range data {
for _, player := range entry.Players {
count[player.Continent]++
}
}

// C Processing
table.Append(conv([]int{count["Africa"], count[""], count["ERROR"], count["South America"], count["Asia"], count["North America"], count["Oceania"]}))

// for k, v := range count {
// fmt.Printf("%v: %v\n", k, v)
// }

fmt.Println("----------------------------------")
}
}

func playerStr(players []model.Player) string {
var str []string

for _, player := range players {
line := fmt.Sprintf("%v (%v)", player.Name, player.Country)
str = append(str, line)
}

return strings.Join(str, ", ")
}

func conv(nums []int) []string {
result := make([]string, len(nums))
for i := range nums {
result[i] = strconv.Itoa(nums[i])
}
return result
}
16 changes: 13 additions & 3 deletions backend/fetch/fetcher/fetcher.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fetcher

import (
"cmp"
_ "embed"
"fmt"
"regexp"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -38,7 +40,7 @@ func New() *Fetcher {
// If it cannot do any of those things it will fail silently: this
// behavior is intended to keep running but simply skip one cycle.
func (f *Fetcher) Fetch() ([]model.Server, error) {
servers, err := getServers()
servers, err := GetServers()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -74,8 +76,8 @@ func (f *Fetcher) Fetch() ([]model.Server, error) {
return result, nil
}

// getServers reads from all of klei's endpoints.
func getServers() ([]klei.Server, error) {
// GetServers reads from all of klei's endpoints.
func GetServers() ([]klei.Server, error) {
regions, err := klei.Regions()
if err != nil {
return nil, fmt.Errorf("could not get regions: %w", err)
Expand All @@ -102,6 +104,14 @@ func getServers() ([]klei.Server, error) {
log.Infof("Parsed in %.2fs with %v/%v failing", time.Since(start).Seconds(), len(lobby)-len(servers), len(lobby))
}

// Remove duplicates
slices.SortFunc(result, func(a, b klei.Server) int {
return cmp.Compare(a.Addr, b.Addr)
})
result = slices.CompactFunc(result, func(a, b klei.Server) bool {
return a.Addr == b.Addr
})

return result, nil
}

Expand Down
2 changes: 1 addition & 1 deletion backend/fetch/fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestToken(t *testing.T) {
os.Setenv("TOKEN", "YOUR TOKEN HERE")

servers, err := getServers()
servers, err := GetServers()
if len(servers) == 0 || err != nil {
t.Errorf("Token test failed: " + err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ResamVi/dontstarve-datavis

go 1.20
go 1.21

require (
github.com/ecnepsnai/discord v1.2.0
Expand Down Expand Up @@ -35,8 +35,10 @@ require (
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand All @@ -279,6 +281,8 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down

0 comments on commit c680b99

Please sign in to comment.