Mercurial > hg > Members > anatofuz > logroulette
changeset 0:02ea3a34fe2c
write cmd
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 26 May 2020 17:03:59 +0900 |
parents | |
children | 4fb2b1cac732 |
files | .hgignore cmd/logroulette.go go.mod go.sum members.json members.pl run.go user.go |
diffstat | 8 files changed, 180 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,26 @@ +syntax:glob +# Created by https://www.gitignore.io/api/go +# Edit at https://www.gitignore.io/?templates=go + +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +### Go Patch ### +/vendor/ +/Godeps/ + +# End of https://www.gitignore.io/api/go
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd/logroulette.go Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,10 @@ +package main + +import ( + "firefly/hg/Members/anatofuz/logroulette" + "os" +) + +func main() { + os.Exit(logroulette.Run(os.Args[1])) +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/go.mod Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,5 @@ +module firefly/hg/Members/anatofuz/logroulette + +go 1.14 + +require github.com/int128/slack v1.5.0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/go.sum Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,4 @@ +github.com/int128/slack v1.5.0 h1:4r3ucP66TnCMsvjUN+QHWsaaIMxxkSisX0Otcegh9xM= +github.com/int128/slack v1.5.0/go.mod h1:ic8nm8JW0R3PRxFXDu4U5JYFGxOpDXR0tiQO3wzZB5o= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/members.json Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,1 @@ +[{"id":"@e175701","done":false},{"id":"@e175706","done":true},{"id":"@e175733","done":false},{"id":"@e175748","done":false},{"id":"@k198576","done":false},{"id":"@k198584","done":false},{"id":"@k198592","done":false},{"id":"@k208573","done":false},{"id":"@riono","done":false}] \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/members.pl Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,26 @@ +use strict; +use warnings; +use JSON; + +my @users; + +while (my $line = <DATA>) { + if ($line =~/(@\w+)/) { + push(@users, {id => $1, done => 0}); + } +} + +print encode_json(\@users); + + +__DATA__ +@e175701 - 奥田 光希 +@e175706 - 上地 悠斗 (soto) +@e175733 - mk (mk) +@e175748 - 大蔵 海斗 (Tobiuo) +@k198576 - 城後 明慈 (akishige-jogo) +@k198584 - 清水 隆博 (AnaTofuZ) +@k198592 - 東恩納 琢偉 +@k208573 - 一木 貴裕 (Itsuki) +@riono - 安田 亮 (riono) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/run.go Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,77 @@ +package logroulette + +import ( + "fmt" + "math/rand" + "os" + "strings" + "time" + + "github.com/int128/slack" +) + +var iconURL string = "https://upload.wikimedia.org/wikipedia/commons/a/a4/13-02-27-spielbank-wiesbaden-by-RalfR-093.jpg" +var botName string = "ログルーレット" +var webhook string = os.Getenv("MATTERMOST_WEBHOOK") + +func Run(jsonp string) int { + + users, err := convertFromJSonToUserInfo(jsonp) + if err != nil { + fmt.Println(err) + return 1 + } + targetUser, newUsers, err := perform(users) + if err != nil { + fmt.Println(err) + return 1 + } + err = converUserInfoToJSON(newUsers, jsonp) + if err != nil { + fmt.Println(err) + return 1 + } + + var strBuilder strings.Builder + strBuilder.WriteString("今日の議事録担当は ") + strBuilder.WriteString(targetUser) + strBuilder.WriteString("さんです!! オナシャス!!") + + slack.Send(webhook, &slack.Message{ + Username: botName, + Text: strBuilder.String(), + IconURL: iconURL, + }) + return 0 +} + +func perform(users []UserInfo) (string, []UserInfo, error) { + var entryUsers []UserInfo + for _, user := range users { + if user.Done { + continue + } + entryUsers = append(entryUsers, user) + } + + if len(entryUsers) == 0 { + for _, user := range users { + user.Done = false + } + entryUsers = users + } + + targetUser, newUsers := loulette(users) + + return targetUser.Id, newUsers, nil +} + +func loulette(users []UserInfo) (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 +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/user.go Tue May 26 17:03:59 2020 +0900 @@ -0,0 +1,31 @@ +package logroulette + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +type UserInfo struct { + Id string `json:"id"` + Done bool `json:"done"` +} + +func convertFromJSonToUserInfo(filep string) ([]UserInfo, error) { + raw, err := ioutil.ReadFile(filep) + if err != nil { + return nil, err + } + var users []UserInfo + json.Unmarshal(raw, &users) + fmt.Println(users) + return users, nil +} + +func converUserInfoToJSON(users []UserInfo, filep string) error { + b, err := json.Marshal(users) + if err != nil { + return err + } + return ioutil.WriteFile(filep, b, 0755) +}