Skip to content

Commit

Permalink
feat(import): choose from existed player if name is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
crispgm committed Jan 15, 2024
1 parent 6c0d070 commit a35f42b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ kicker-cli event open 351e00bf-025c-4243-b381-2f5a135c3070
kicker-cli event delete 351e00bf-025c-4243-b381-2f5a135c3070
```

Event analysis:
```shell
# analyze with "double_player_rank" operator for "byp" event order by ELO
kicker-cli events analyze -m double_player_rank -t byp -o ELO

# analyze with "single_player_rank" operator for "single" event order by win rate
kicker-cli events analyze -m single_player_rank -t single --sort-by elo
````

Player management:
```shell
# list players
Expand Down
46 changes: 36 additions & 10 deletions cmd/kicker-cli/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var importCmd = &cobra.Command{
return
}

var players []string
for _, p := range instance.Conf.Players {
players = append(players, p.Name)
}
eventsAdded := 0
for _, importPath := range args {
if strings.HasPrefix(importPath, "~") {
Expand All @@ -58,17 +62,39 @@ var importCmd = &cobra.Command{
}
}
if !found {
var needCreate bool
if !importEventCreateUnknownPlayers {
needCreate, _ = pterm.DefaultInteractiveConfirm.
WithDefaultText(fmt.Sprintf("Create a new player with name `%s`", p.Name)).
WithDefaultValue(true).
Show()
} else {
needCreate = true
}
if needCreate {
instance.AddPlayer(*entity.NewPlayer(p.Name))
text := fmt.Sprint("Unknown player: ", p.Name)
selectedOption, _ := pterm.
DefaultInteractiveSelect.
WithOptions([]string{
"Create a new player",
"Attach to an existed player",
"Cancel",
}).
Show(text)

if selectedOption == "Create a new player" {
instance.AddPlayer(*entity.NewPlayer(p.Name))
pterm.Printfln("Creating player %s", p.Name)
} else if selectedOption == "Attach to an existed player" {
selectedPlayer, _ := pterm.
DefaultInteractiveSelect.
WithOptions(players).
Show("Choose a player")
sp := instance.FindPlayer(selectedPlayer)
if sp == nil {
errorMessageAndExit("Find player failed:", selectedPlayer)
}
sp.AddAlias(p.Name)
res := instance.SetPlayer(sp)
if res == nil {
errorMessageAndExit("Attach player failed", selectedPlayer)
} else {
pterm.Printfln("Attaching alias %s to player %s", p.Name, sp.Name)
}
} else {
errorMessageAndExit("Cancelled")
}
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion internal/app/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ const DefaultName = ".kicker.yaml"
type Conf struct {
ManifestVersion string `yaml:"manifest_version"`

// Settings
// NoColors bool `yaml:"no_colors"`

// Organization
Organization entity.Organization `yaml:"organization"`

Events []entity.Event `yaml:"events"`
// Events
Events []entity.Event `yaml:"events"`

// Players
Players []entity.Player `yaml:"players"`
}

Expand Down Expand Up @@ -94,6 +101,29 @@ func (app App) GetPlayer(id string) *entity.Player {
return nil
}

// SetPlayer sets player data with the given id.
func (app App) SetPlayer(player *entity.Player) *entity.Player {
for idx, p := range app.Conf.Players {
if player.ID == p.ID {
app.Conf.Players[idx] = *player
return &p
}
}

return nil
}

// FindPlayer returns player with the given name
func (app App) FindPlayer(name string) *entity.Player {
for _, p := range app.Conf.Players {
if name == p.Name {
return &p
}
}

return nil
}

// DeletePlayer delete a player
func (app *App) DeletePlayer(id string) error {
s := -1
Expand Down
2 changes: 1 addition & 1 deletion internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *Converter) playerRank(
// remove third from fourth level
fourthPos := lastPos - 1
for i, p := range c.record.Ranks[fourthPos] {
if p.ID == third[0].ID {
if len(third) > 0 && p.ID == third[0].ID {
ranks := c.record.Ranks[fourthPos]
ranks = append(ranks[:i], ranks[i+1:]...)
c.record.Ranks[fourthPos] = ranks
Expand Down

0 comments on commit a35f42b

Please sign in to comment.