Mercurial > hg > Members > anatofuz > logroulette
changeset 2:a92a8de9d190
...
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 27 May 2020 10:45:45 +0900 |
parents | 4fb2b1cac732 |
children | 1a10e3641292 269c2bf2cfce |
files | run.go |
diffstat | 1 files changed, 12 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/run.go Wed May 27 10:31:13 2020 +0900 +++ b/run.go Wed May 27 10:45:45 2020 +0900 @@ -47,7 +47,10 @@ func perform(users []UserInfo) (string, []UserInfo, error) { var entryUsers []UserInfo - for _, user := range users { + name2Index := make(map[string]int, len(entryUsers)) + + for i, user := range users { + name2Index[user.Id] = i if user.Done { continue } @@ -56,22 +59,25 @@ if len(entryUsers) == 0 { entryUsers = users - for _, user := range entryUsers { - user.Done = false + for i := range entryUsers { + entryUsers[i].Done = false + users[i].Done = false } } - targetUser, newUsers := loulette(entryUsers) + targetUser := loulette(entryUsers) + newUsers := users + newUsers[name2Index[targetUser.Id]].Done = true return targetUser.Id, newUsers, nil } -func loulette(users []UserInfo) (UserInfo, []UserInfo) { +func loulette(users []UserInfo) UserInfo { rand.Seed(time.Now().UnixNano()) index := rand.Intn(len(users)) users[index].Done = true for _, user := range users { fmt.Println(user) } - return users[index], users + return users[index] }