{veyron,javascript}/examples/rockpaperscissors: Use unresolved names
When a player sends a challenge, it needs to include the address of the
judge that will host the game. Before this change, the judge's address
was resolved by the player initiating the game, which is fine.
However, I'm working on another change that will make it possible for different
players to resolve the same name differently depending on whether they are on
the same local network or not. So, instead of passing a resolved name, we're
now passing an unresolved one.
Change-Id: I733a9552bf67a5ff929e8e46685dbebc042995a0
diff --git a/examples/rockpaperscissors/common/common.go b/examples/rockpaperscissors/common/common.go
index 796d8c3..fa4cc0d 100644
--- a/examples/rockpaperscissors/common/common.go
+++ b/examples/rockpaperscissors/common/common.go
@@ -10,7 +10,6 @@
rps "veyron/examples/rockpaperscissors"
- "veyron2/naming"
"veyron2/rt"
"veyron2/vlog"
)
@@ -78,27 +77,12 @@
}
var servers []string
for e := range c {
- if len(e.Servers) > 0 {
- servers = append(servers, pickBestServer(e.Servers))
- }
+ servers = append(servers, e.Name)
}
vlog.VI(1).Infof("findAll(%q) elapsed: %s", t, time.Now().Sub(start))
return servers, nil
}
-// pickBestServer returns the server with the highest TTL.
-func pickBestServer(servers []naming.MountedServer) string {
- var server string
- var max time.Duration
- for _, x := range servers {
- if x.TTL > max {
- max = x.TTL
- server = x.Server
- }
- }
- return server
-}
-
func FormatScoreCard(score rps.ScoreCard) string {
buf := bytes.NewBufferString("")
var gameType string
diff --git a/examples/rockpaperscissors/rpsplayercli/main.go b/examples/rockpaperscissors/rpsplayercli/main.go
index 092dc3f..1c8d847 100644
--- a/examples/rockpaperscissors/rpsplayercli/main.go
+++ b/examples/rockpaperscissors/rpsplayercli/main.go
@@ -131,8 +131,8 @@
// and asking the user to select one of each, to select the game options, what
// to play, etc.
func initiateGame() error {
- jChan := make(chan findResult)
- oChan := make(chan findResult)
+ jChan := make(chan []string)
+ oChan := make(chan []string)
go findAll("judge", jChan)
go findAll("player", oChan)
@@ -140,36 +140,36 @@
judges := <-jChan
opponents := <-oChan
fmt.Println()
- if len(judges.names) == 0 || len(opponents.names) == 0 {
+ if len(judges) == 0 || len(opponents) == 0 {
return errors.New("no one to play with")
}
fmt.Println("Choose a judge:")
- j := selectOne(judges.names)
+ j := selectOne(judges)
fmt.Println("Choose an opponent:")
- o := selectOne(opponents.names)
+ o := selectOne(opponents)
fmt.Println("Choose the type of rock-paper-scissors game would you like to play:")
gameType := selectOne([]string{"Classic", "LizardSpock"})
fmt.Println("Choose the number of rounds required to win:")
numRounds := selectOne([]string{"1", "2", "3", "4", "5", "6"}) + 1
gameOpts := rps.GameOptions{NumRounds: int32(numRounds), GameType: rps.GameTypeTag(gameType)}
- gameID, err := createGame(judges.servers[j], gameOpts)
+ gameID, err := createGame(judges[j], gameOpts)
if err != nil {
vlog.Infof("createGame: %v", err)
return err
}
for {
- err := sendChallenge(opponents.servers[o], judges.servers[j], gameID, gameOpts)
+ err := sendChallenge(opponents[o], judges[j], gameID, gameOpts)
if err == nil {
break
}
- fmt.Printf("Challenge was declined by %s (%v)\n", opponents.names[o], err)
+ fmt.Printf("Challenge was declined by %s (%v)\n", opponents[o], err)
fmt.Println("Choose another opponent:")
- o = selectOne(opponents.names)
+ o = selectOne(opponents)
}
fmt.Println("Joining the game...")
- if _, err = playGame(judges.servers[j], gameID); err != nil {
+ if _, err = playGame(judges[j], gameID); err != nil {
vlog.Infof("playGame: %v", err)
return err
}
@@ -285,28 +285,9 @@
return
}
-type findResult struct {
- names []string
- servers []string
-}
-
-// byName implements sort.Interface for findResult.
-type byName findResult
-
-func (n byName) Len() int {
- return len(n.names)
-}
-func (n byName) Swap(i, j int) {
- n.names[i], n.names[j] = n.names[j], n.names[i]
- n.servers[i], n.servers[j] = n.servers[j], n.servers[i]
-}
-func (n byName) Less(i, j int) bool {
- return n.names[i] < n.names[j]
-}
-
-func findAll(t string, out chan findResult) {
+func findAll(t string, out chan []string) {
ns := rt.R().Namespace()
- var result findResult
+ var result []string
c, err := ns.Glob(rt.R().TODOContext(), "rps/"+t+"/*")
if err != nil {
vlog.Infof("ns.Glob failed: %v", err)
@@ -315,11 +296,8 @@
}
for e := range c {
fmt.Print(".")
- if len(e.Servers) > 0 {
- result.names = append(result.names, e.Name)
- result.servers = append(result.servers, e.Servers[0].Server)
- }
+ result = append(result, e.Name)
}
- sort.Sort(byName(result))
+ sort.Strings(result)
out <- result
}