Skip to content

Commit

Permalink
Add Duels and Result's Strength and Advantage (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
janos authored Jan 21, 2024
1 parent d97d6cf commit c89ca02
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 107 deletions.
78 changes: 37 additions & 41 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Go
on: [push, pull_request]
jobs:

build:
name: Build
runs-on: ${{ matrix.os }}
Expand All @@ -10,43 +9,40 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20.0-rc.1
check-latest: true

- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Cache Go modules
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.OS }}-build-${{ env.cache-name }}-
${{ runner.OS }}-build-
${{ runner.OS }}-
# TODO: Enable linter when Go 1.20 is supported
# - name: Lint
# uses: golangci/golangci-lint-action@v2
# with:
# version: v1.50.1
# args: --timeout 10m

- name: Vet
if: matrix.os == 'ubuntu-latest'
run: go vet -v ./...

- name: Build
env:
CGO_ENABLED: 0
run: go build -ldflags "-s -w" ./...

- name: Test
run: go test -v -race ./...
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Cache Go modules
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.OS }}-build-${{ env.cache-name }}-
${{ runner.OS }}-build-
${{ runner.OS }}-
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.54.2
args: --timeout 10m

- name: Vet
if: matrix.os == 'ubuntu-latest'
run: go vet -v ./...

- name: Build
env:
CGO_ENABLED: 0
run: go build -ldflags "-s -w" ./...

- name: Test
run: go test -v -race ./...
95 changes: 48 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This client implements all Direct API features.
- Unvote
- Get submitted ballot
- Calculate results
- Get voting choices' duels (pairwise comparisons)

## Examples

Expand All @@ -52,48 +53,48 @@ import (
)

func main() {
client := directdecisions.NewClient("my-api-key", nil)
client := directdecisions.NewClient("my-api-key", nil)

ctx := context.Background()
ctx := context.Background()

v, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
log.Fatal(err)
}
v, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
log.Fatal(err)
}

log.Printf("Created voting with ID %s", v.ID)
log.Printf("Created voting with ID %s", v.ID)

if _, err := client.Votings.Vote(ctx, v.ID, "Leonardo", map[string]int{
"Pepperoni": 1,
"Margarita": 2,
}); err != nil {
log.Fatal(err)
}
if _, err := client.Votings.Vote(ctx, v.ID, "Leonardo", map[string]int{
"Pepperoni": 1,
"Margarita": 2,
}); err != nil {
log.Fatal(err)
}

log.Printf("Leonardo Voted for Pepperoni in voting with ID %s", v.ID)
log.Printf("Leonardo Voted for Pepperoni in voting with ID %s", v.ID)

if _, err := client.Votings.Vote(ctx, v.ID, "Michelangelo", map[string]int{
"Capricciosa": 1,
"Margarita": 2,
"Pepperoni": 2,
}); err != nil {
log.Fatal(err)
}
if _, err := client.Votings.Vote(ctx, v.ID, "Michelangelo", map[string]int{
"Capricciosa": 1,
"Margarita": 2,
"Pepperoni": 2,
}); err != nil {
log.Fatal(err)
}

log.Printf("Michelangelo Voted for Capricciosa in voting with ID %s", v.ID)
log.Printf("Michelangelo Voted for Capricciosa in voting with ID %s", v.ID)

results, tie, err := client.Votings.Results(ctx, v.ID)
if err != nil {
log.Fatal(err)
}
results, tie, err := client.Votings.Results(ctx, v.ID)
if err != nil {
log.Fatal(err)
}

if tie {
log.Printf("Voting with ID %s is tied", v.ID)
} else {
log.Printf("Voting with ID %s is not tied", v.ID)
}
if tie {
log.Printf("Voting with ID %s is tied", v.ID)
} else {
log.Printf("Voting with ID %s is not tied", v.ID)
}

log.Printf("Results for voting with ID %s: %v", v.ID, results)
log.Printf("Results for voting with ID %s: %v", v.ID, results)
}
```

Expand All @@ -110,21 +111,21 @@ import (
)

func main() {
client := directdecisions.NewClient("my-api-key", nil)

ctx := context.Background()

_, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
if errors.Is(err, directdecisions.ErrHTTPStatusUnauthorized) {
log.Fatal("Invalid API key")
}
if errors.Is(err, directdecisions.ErrChoiceTooLong) {
log.Fatal("Some of the choices are too long")
}
// ...
log.Fatal(err)
}
client := directdecisions.NewClient("my-api-key", nil)

ctx := context.Background()

_, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
if errors.Is(err, directdecisions.ErrHTTPStatusUnauthorized) {
log.Fatal("Invalid API key")
}
if errors.Is(err, directdecisions.ErrChoiceTooLong) {
log.Fatal("Some of the choices are too long")
}
// ...
log.Fatal(err)
}
}
```

Expand Down
44 changes: 28 additions & 16 deletions votings.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,43 @@ type Result struct {
Index int
Wins int
Percentage float64
Strength int
Advantage int
}

func (s *VotingsService) Results(ctx context.Context, votingID string) (results []Result, tie bool, err error) {

type votingResultAPIResponse struct {
Choice string `json:"choice"`
Index int `json:"index"`
Wins int `json:"wins"`
Percentage float64 `json:"percentage"`
}
type computeResultsAPIResponse struct {
Results []Result `json:"results"`
Tie bool `json:"tie"`
Duels []Duel `json:"duels"`
}

type computeResultsAPIResponse struct {
Results []votingResultAPIResponse `json:"results"`
Tie bool `json:"tie"`
}
func (s *VotingsService) Results(ctx context.Context, votingID string) (results []Result, tie bool, err error) {

var response *computeResultsAPIResponse
if err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID)+"/results", nil, &response); err != nil {
return nil, false, err
}

results = make([]Result, len(response.Results))
for i, r := range response.Results {
results[i] = Result(r)
return response.Results, response.Tie, nil
}

type Duel struct {
Left ChoiceStrength
Right ChoiceStrength
}

type ChoiceStrength struct {
Choice string
Index int
Strength int
}

func (s *VotingsService) Duels(ctx context.Context, votingID string) (results []Result, duels []Duel, tie bool, err error) {

var response *computeResultsAPIResponse
if err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID)+"/results/duels", nil, &response); err != nil {
return nil, nil, false, err
}

return results, response.Tie, nil
return response.Results, response.Duels, response.Tie, nil
}
Loading

0 comments on commit c89ca02

Please sign in to comment.