Skip to content

Instantly share code, notes, and snippets.

@k3kaimu
Last active September 22, 2015 10:29
Show Gist options
  • Save k3kaimu/90180d0a62cd056a91ec to your computer and use it in GitHub Desktop.
Save k3kaimu/90180d0a62cd056a91ec to your computer and use it in GitHub Desktop.
ツタンカーメンbot(TUTコンピュータクラブ2015年度夏合宿成果)
package main
import "github.com/ChimeraCoder/anaconda"
import "fmt"
import "net/url"
import "strings"
import "strconv"
import "math/rand"
import "time"
type AhiruyakiPattern struct {
Pred func(*anaconda.TwitterApi, *anaconda.Tweet) bool
Apply func(*anaconda.TwitterApi, *anaconda.Tweet)
}
type GameEnvironment struct {
Game *GameStatus
WinCNT int64
LoseCNT int64
}
type GameStatus struct {
Usya Player
Mao Player
}
type Player struct {
Tame int64
Life int64
}
var gameEnv = GameEnvironment{}
var gameCommands = []string{"こうげき", "ため", "ぼうぎょ", "ひっさつ"}
var ahirus [15]AhiruyakiPattern
func isReply(tweet *anaconda.Tweet) bool {
return strings.Contains(tweet.Text, "@")
}
func isRT(tweet *anaconda.Tweet) bool {
return strings.HasPrefix(tweet.Text, "RT")
}
func isContains(tweet *anaconda.Tweet, strs []string) bool {
for _, e := range strs {
if strings.Contains(tweet.Text, e) {
return true
}
}
return false
}
func postReply(api *anaconda.TwitterApi, tweet *anaconda.Tweet, str string) {
id := tweet.Id
v := url.Values{}
v.Add("in_reply_to_status_id", strconv.FormatInt(id, 10))
api.PostTweet("@"+tweet.User.ScreenName+" "+str, v)
fmt.Println("POST")
}
func getRandElement(strs []string) string {
return strs[rand.Intn(len(strs))]
}
func getRandTweet() string {
var randTweets = []string{
"なんやねん",
"なるほど",
"わかる",
"うける",
"神",
"ヨイ",
"D棟の最上階に行きませんか?",
"F棟の最上階に行きませんか?",
"You must go to the future",
"Go to the feature",
"技科大生って感じ悪いよね",
"デスノートオススメ",
// "るろうに剣心見ろよ",
"カイジから学ぶことは多い",
// "友利奈緒友利奈緒",
"D進不可避",
"D進待ったなし",
// "院死不可避",
"やったぜ",
"天伯臭がヒドい",
"サカキパークに埋まってるから発掘して",
"天伯の塩",
"食堂より纏ずしイイね",
"あのさぁ…",
"えぇぇ…",
"ゆるして",
"ファッ",
"やめてよぉ…",
"くぅ疲",
"発掘してくれ頼む",
"寝てください",
"ツイートしてる暇があるなら、論文読もう",
"ヒエログリフ忘れたから教えて",
}
return getRandElement(randTweets)
}
func gameEnvInit() {
gameEnv.Game = new(GameStatus)
gameEnv.Game.Mao.Life = 3
gameEnv.Game.Mao.Tame = 0
gameEnv.Game.Usya.Life = 3
gameEnv.Game.Usya.Tame = 0
}
func gameNextCommand(game *GameStatus) string {
if game.Mao.Tame >= 3 {
if game.Usya.Life <= 2 {
return "こうげき"
} else if game.Mao.Life <= 1 {
return "こうげき"
} else {
return "ぼうぎょ"
}
} else if game.Usya.Tame >= 3 {
return "ひっさつ"
} else if game.Mao.Life <= 1 {
return "こうげき"
} else if game.Usya.Life <= 1 {
return "こうげき"
} else {
if game.Usya.Tame == 2 && game.Mao.Life == 3 {
return "ため"
} else if game.Usya.Life < game.Mao.Life {
return "こうげき"
} else {
return getRandElement(gameCommands[0:2])
}
}
}
func ahiruyaki(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
for _, pattern := range ahirus {
if tweet.User.ScreenName != "k3_tut" {
if pattern.Pred(api, tweet) {
pattern.Apply(api, tweet)
return
}
}
}
}
func main() {
anaconda.SetConsumerKey("****************")
anaconda.SetConsumerSecret("****************")
api := anaconda.NewTwitterApi("****************", "****************")
ahiruyakiInit()
rand.Seed(time.Now().UnixNano())
api.SetLogger(anaconda.BasicLogger)
v := url.Values{}
v.Add("replies", "all")
stream := api.UserStream(v)
for {
select {
case item := <-stream.C:
switch status := item.(type) {
case anaconda.Tweet:
if !isRT(&status) {
ahiruyaki(api, &status)
}
default:
}
}
}
}
func ahiruyakiInit() {
idx := 0
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && strings.Contains(tweet.Text, "@k3_tut") && (gameEnv.Game == nil) && tweet.User.ScreenName == "golang_bot" && strings.Contains(tweet.Text, "ゲームを始めます")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
gameEnvInit()
postReply(api, tweet, gameNextCommand(gameEnv.Game))
}
idx += 1
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && strings.HasPrefix(tweet.Text, "@k3_tut") && (gameEnv.Game != nil) && tweet.User.ScreenName == "golang_bot" && (strings.Contains(tweet.Text, "勝ちです") || strings.Contains(tweet.Text, "負けです"))
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
gameEnv.Game = nil
if strings.Contains(tweet.Text, "あなたの勝ちです") {
postReply(api, tweet, getRandElement([]string{"余裕だった", "敗北を知りたい"}))
} else {
postReply(api, tweet, getRandElement([]string{"何がいけなかったんだ...", "キェェェェェエエエ"}))
}
}
idx += 1
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && strings.Contains(tweet.Text, "@k3_tut") && (gameEnv.Game != nil) && tweet.User.ScreenName == "golang_bot"
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
lines := strings.Split(tweet.Text, "\n")
var usyaAttack int64 = 0
var maoAttack int64 = 0
var usyaTame int64 = 0
var maoTame int64 = 0
var usyaAbsorb int64 = 0
var maoAbsorb int64 = 0
var attackP *int64
var tameP *int64
var absorbP *int64
for _, e := range lines {
if strings.Contains(e, "勇者は") {
attackP = &usyaAttack
tameP = &usyaTame
absorbP = &usyaAbsorb
} else {
attackP = &maoAttack
tameP = &maoTame
absorbP = &maoAbsorb
}
if strings.Contains(e, "こうげきをした") {
*attackP += 1
} else if strings.Contains(e, "ぼうぎょをした") {
*absorbP += 1
} else if strings.Contains(e, "ためをした") {
*tameP += 1
} else if strings.Contains(e, "ひっさつをした") {
*attackP += 3
}
}
gameEnv.Game.Usya.Life -= maoAttack - usyaAbsorb
gameEnv.Game.Mao.Life -= usyaAttack - maoAbsorb
gameEnv.Game.Usya.Tame += usyaTame
gameEnv.Game.Mao.Tame += maoTame
// return gameNextCommand(gameEnv.Game)
postReply(api, tweet, gameNextCommand(gameEnv.Game))
}
idx += 1
// "友利奈緒" -> "わかる"
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && strings.Contains(tweet.Text, "友利奈緒")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "わかる")
}
idx += 1
// GoToTheFuture -> "Go to the デスノート"
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
str := strings.ToLower(strings.Join(strings.Split(tweet.Text, " "), ""))
return !isRT(tweet) && strings.Contains(str, "gotothefuture") || strings.Contains(str, "gttf")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "GO TO THE デスノート")
}
idx += 1
// "天伯臭" -> "腐ってるからしかたない"
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && strings.Contains(tweet.Text, "天伯臭")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "腐ってるからしかたない")
}
idx += 1
// サカキパーク
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && isContains(tweet, []string{"榊", "サカキ", "サカキ", "SKK", "Skk", "skk"}) && isContains(tweet, []string{"パーク", "Park", "パーク", "park", "PARK"})
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "技科大生の墓地")
}
idx += 1
// SKK
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && isContains(tweet, []string{"SKK", "skk", "Skk"})
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "現学長は ジムリーダー サカキ")
}
idx += 1
// 呼んだ?
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && isContains(tweet, []string{"ツタンカーメン", "つたんかーめん", "ツタンカーメン"})
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "呼んだ?")
}
idx += 1
// return reply
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
if isRT(tweet) {
return false
}
if (tweet.User.ScreenName == "hibari_ch" || tweet.User.ScreenName == "hujiwara_sisiza") && (rand.Intn(4) == 0) {
return false
}
return strings.Contains(tweet.Text, "@k3_tut") && (tweet.User.ScreenName != "k3_tut")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, getRandTweet())
}
idx += 1
// ひばりチャンのリプライ以外の普通のツイートに対して返信
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && tweet.User.ScreenName == "hibari_ch" && !isReply(tweet) && rand.Intn(5) == 0
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, getRandTweet())
}
idx += 1
//
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && (!isReply(tweet)) && strings.Contains(tweet.Text, "論文")
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
postReply(api, tweet, "寝てください")
}
idx += 1
//
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return rand.Intn(20) == 0
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
v := url.Values{}
api.PostTweet(getRandTweet(), v)
}
idx += 1
//
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && !isReply(tweet) && rand.Intn(80) == 0 && gameEnv.Game == nil
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
v := url.Values{}
api.PostTweet("@golang_bot ゲームスタート", v)
}
idx += 1
//
ahirus[idx] = AhiruyakiPattern{}
ahirus[idx].Pred = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) bool {
return !isRT(tweet) && !isReply(tweet) && rand.Intn(80) == 0
}
ahirus[idx].Apply = func(api *anaconda.TwitterApi, tweet *anaconda.Tweet) {
v := url.Values{}
api.PostTweet("@hibari_ch "+getRandTweet(), v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment