Skip to content

Commit

Permalink
Download lists in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Feb 23, 2023
1 parent 824105c commit 64d1b13
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
16 changes: 0 additions & 16 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,22 +679,6 @@ func (bd *BD) Shutdown() {
if bd.settings.DiscordPresenceEnabled {
client.Logout()
}
// Ensure we save on exit
playerListFile, playerListFileErr := os.Create(bd.settings.LocalPlayerListPath())
if playerListFileErr != nil {
log.Panicf("Failed to open player list for writing: %v\n", playerListFileErr)
}
if errWrite := bd.rules.ExportPlayers(rules.LocalRuleName, playerListFile); errWrite != nil {
log.Panicf("Failed to export player list: %v\n", playerListFileErr)
}

rulesFile, rulesFileErr := os.Create(bd.settings.LocalRulesListPath())
if rulesFileErr != nil {
log.Panicf("Failed to open player list for writing: %v\n", rulesFileErr)
}
if errWrite := bd.rules.ExportRules(rules.LocalRuleName, rulesFile); errWrite != nil {
log.Panicf("Failed to export rules list: %v\n", rulesFileErr)
}
logClose(bd.store)
}

Expand Down
35 changes: 24 additions & 11 deletions lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"net/http"
"sync"
)

func fetchURL(ctx context.Context, client http.Client, url string) ([]byte, error) {
Expand All @@ -31,35 +32,47 @@ func fetchURL(ctx context.Context, client http.Client, url string) ([]byte, erro
func downloadLists(ctx context.Context, lists []model.ListConfig) ([]rules.PlayerListSchema, []rules.RuleSchema) {
var playerLists []rules.PlayerListSchema
var rulesLists []rules.RuleSchema
mu := &sync.RWMutex{}
client := http.Client{}
for _, u := range lists {
if !u.Enabled {
continue
}
downloadFn := func(u model.ListConfig) error {
body, errFetch := fetchURL(ctx, client, u.URL)
if errFetch != nil {
log.Printf("Failed to fetch player list: %v", u.URL)
continue
return errors.Wrapf(errFetch, "Failed to fetch player list: %s", u.URL)
}
switch u.ListType {
case model.ListTypeTF2BDPlayerList:
var result rules.PlayerListSchema
if errParse := rules.ParsePlayerSchema(bytes.NewReader(body), &result); errParse != nil {
log.Printf("Failed to parse request: %v\n", errParse)
continue
return errors.Wrap(errParse, "Failed to parse request")
}
mu.Lock()
playerLists = append(playerLists, result)
mu.Unlock()
log.Printf("Downloaded playerlist successfully: %s\n", result.FileInfo.Title)
case model.ListTypeTF2BDRules:
var result rules.RuleSchema
if errParse := rules.ParseRulesList(bytes.NewReader(body), &result); errParse != nil {
log.Printf("Failed to parse request: %v\n", errParse)
continue
return errors.Wrap(errParse, "Failed to parse request")
}
mu.Lock()
rulesLists = append(rulesLists, result)
mu.Unlock()
log.Printf("Downloaded rules successfully: %s\n", result.FileInfo.Title)
}

return nil
}
wg := &sync.WaitGroup{}
for _, listConfig := range lists {
if !listConfig.Enabled {
continue
}
wg.Add(1)
go func(lc model.ListConfig) {
defer wg.Done()
if errDL := downloadFn(lc); errDL != nil {
log.Printf("Failed to download list: %v", errDL)
}
}(listConfig)
}
return playerLists, rulesLists
}
8 changes: 2 additions & 6 deletions ui/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (chatList *userMessageList) Reload(rr []model.UserMessage) error {
if errReload := chatList.boundList.Reload(); errReload != nil {
return errReload
}
chatList.list.Refresh()
chatList.list.ScrollToBottom()
return nil
}

Expand Down Expand Up @@ -60,16 +60,12 @@ func (ui *Ui) createGameChatMessageList() *userMessageList {
userMessageListWidget := widget.NewListWithData(
boundList,
func() fyne.CanvasObject {
rootContainer := container.NewBorder(
return container.NewBorder(
nil,
nil,
container.NewHBox(widget.NewLabel(""), newContextMenuRichText(nil)),
nil,
widget.NewRichTextWithText(""))

rootContainer.Refresh()

return rootContainer
},
func(i binding.DataItem, o fyne.CanvasObject) {
value := i.(binding.Untyped)
Expand Down
2 changes: 1 addition & 1 deletion ui/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (nameList *userNameList) Reload(rr []model.UserNameHistory) error {
if errReload := nameList.boundList.Reload(); errReload != nil {
return errReload
}
nameList.list.Refresh()
nameList.list.ScrollToBottom()
return nil
}

Expand Down
15 changes: 10 additions & 5 deletions ui/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ func (ui *Ui) generateUserMenu(steamId steamid.SID64, userId int64) *fyne.Menu {
// │ K: 10 A: 66 │
// └─────────────────────────────────────────────────────────┘
func (ui *Ui) createPlayerList() *PlayerList {
const (
symbolOk = "✓"
symbolBad = "✗"
)

//iconSize := fyne.NewSize(64, 64)
pl := &PlayerList{}
boundList := binding.BindUntypedList(&[]interface{}{})
Expand Down Expand Up @@ -316,20 +321,20 @@ func (ui *Ui) createPlayerList() *PlayerList {
profileLabel.Refresh()
var vacState []string
if ps.NumberOfVACBans > 0 {
vacState = append(vacState, fmt.Sprintf("VB: %d", ps.NumberOfGameBans))
vacState = append(vacState, fmt.Sprintf("VB: %s", strings.Repeat(symbolBad, ps.NumberOfVACBans)))
}
if ps.NumberOfGameBans > 0 {
vacState = append(vacState, fmt.Sprintf("GB: %d", ps.NumberOfVACBans))
vacState = append(vacState, fmt.Sprintf("GB: %s", strings.Repeat(symbolBad, ps.NumberOfGameBans)))
}
if ps.CommunityBanned {
vacState = append(vacState, "CB: ✓")
vacState = append(vacState, fmt.Sprintf("CB: %s", symbolBad))
}
if ps.EconomyBan {
vacState = append(vacState, "EB: ✓")
vacState = append(vacState, fmt.Sprintf("EB: %s", symbolBad))
}
vacStyle := stlBad
if len(vacState) == 0 {
vacState = append(vacState, "✓")
vacState = append(vacState, symbolOk)
vacStyle = stlOk

}
Expand Down

0 comments on commit 64d1b13

Please sign in to comment.