Skip to content

Commit cab64a4

Browse files
micsor-norlysMichael Thorsager
andauthored
Fixing #24 tiebreaker (#25)
Co-authored-by: Michael Thorsager <mit@stofa.dk>
1 parent 314fe7f commit cab64a4

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ as described above, ex `echo $p1 >> the_file.log`
5050

5151
# Environment variables in conversations
5252
Any environment variable prefixed with `MOCKDEV_` will be available in conversations, when generating response body.
53-
ex. `MOCDEV_FOO` will be available using `{{ .ENV.FOO }}`. The current bind address, and the bind port is available as:
54-
`{{ .CFG.Address }}` and `{{ .CFG.Port }}` (_note that the `Address` will most likely be `""` meaning that mockdev is
53+
ex. `MOCDEV_FOO` will be available using `{{ .env.FOO }}`. The current bind address, and the bind port is available as:
54+
`{{ .cfg.Address }}` and `{{ .cfg.Port }}` (_note that the `Address` will most likely be `""` meaning that mockdev is
5555
bound to all addresses_)
5656

5757
# Current Time in conversations

mockhttp/context.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,26 @@ func (s *conversationScores) tieBreak(candidates []Conversation) (Conversation,
8282
}
8383
var ss []kv
8484
for k, v := range s.values {
85-
ss = append(ss, kv{k, v})
85+
if c, found := lookupByName(candidates, k); found && !c.IsBreaking() {
86+
ss = append(ss, kv{k, v - (c.Order * 100)})
87+
}
8688
}
8789
sort.Slice(ss, func(i, j int) bool {
8890
return ss[i].v > ss[j].v
8991
})
9092

91-
for _, s := range ss {
92-
for _, c := range candidates {
93-
if s.k == c.Name {
94-
return c, nil
95-
}
93+
if theOne, found := lookupByName(candidates, ss[0].k); found {
94+
return theOne, nil
95+
} else {
96+
return Conversation{}, fmt.Errorf("that is wierd, no candidates found in score")
97+
}
98+
}
99+
100+
func lookupByName(haystack []Conversation, needle string) (Conversation, bool) {
101+
for _, c := range haystack {
102+
if c.Name == needle {
103+
return c, true
96104
}
97105
}
98-
return Conversation{}, fmt.Errorf("that is wierd, no candidates found in score")
106+
return Conversation{}, false
99107
}

0 commit comments

Comments
 (0)