Skip to content

Commit

Permalink
optimize(cybercat): nt image
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 30, 2024
1 parent 0f38846 commit b15ff7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
12 changes: 6 additions & 6 deletions plugin/cybercat/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func init() {
ctx.SendChain(message.At(winer.User), message.Text("你家的喵喵为你赢得了", money))
}
winer.ArenaTime = now
err = catdata.insert(gidStr, winer)
err = catdata.insert(gidStr, &winer)
if err == nil {
loser.ArenaTime = now
err = catdata.insert(gidStr, loser)
err = catdata.insert(gidStr, &loser)
}
if err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
Expand All @@ -124,10 +124,10 @@ func init() {
if fatLine {
ctx.SendChain(message.Reply(id), message.Text(duelInfo.Name, "和", userInfo.Name, "之间并没有PK的意愿呢\nPK结束"))
userInfo.ArenaTime = now
err = catdata.insert(gidStr, userInfo)
err = catdata.insert(gidStr, &userInfo)
if err == nil {
duelInfo.ArenaTime = now
err = catdata.insert(gidStr, duelInfo)
err = catdata.insert(gidStr, &duelInfo)
}
if err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
Expand Down Expand Up @@ -156,10 +156,10 @@ func init() {
message.Text("\n", loser.Name, "在PK中受伤了\n在医疗中心治愈过程中体重降低至", strconv.FormatFloat(loser.Weight, 'f', 2, 64)))
}
userInfo.ArenaTime = time.Now().Unix()
err = catdata.insert(gidStr, winer)
err = catdata.insert(gidStr, &winer)
if err == nil {
duelInfo.ArenaTime = time.Now().Unix()
err = catdata.insert(gidStr, loser)
err = catdata.insert(gidStr, &loser)
}
if err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
Expand Down
23 changes: 16 additions & 7 deletions plugin/cybercat/catdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/pool"
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)

const apiURL = "https://api.thecatapi.com/v1/images/"

var catType = map[string]string{
var typeEN2ZH = map[string]string{
"Abyssinian": "阿比西尼亚猫", "Aegean": "爱琴猫", "American Bobtail": "美国短尾猫", "American Curl": "美国卷耳猫", "American Shorthairs": "美洲短毛猫", "American Wirehair": "美国硬毛猫",
"Arabian Mau": "美英猫", "Australian Mist": "澳大利亚雾猫", "Balinese": "巴厘岛猫", "Bambino": "班比诺猫", "Bengal": "孟加拉虎", "Birman": "比尔曼猫", "Bombay": "孟买猫", "British Longhair": "英国长毛猫",
"British Shorthair": "英国短毛猫", "Burmese": "缅甸猫", "Burmilla": "博美拉猫", "California Spangled": "加州闪亮猫", "Chantilly-Tiffany": "查达利/蒂法尼猫", "Chartreux": "夏特鲁斯猫", "Chausie": "非洲狮子猫",
Expand All @@ -30,7 +31,7 @@ var catType = map[string]string{
"Somali": "索马里猫", "Sphynx": "斯芬克斯猫", "Tonkinese": "东京猫", "Toyger": "玩具虎猫", "Turkish Angora": "土耳其安哥拉猫",
"Turkish Van": "土耳其梵猫", "York Chocolate": "约克巧克力猫", "Cymic": "金力克长毛猫"}

var catBreeds = map[string]string{
var typeZH2Breeds = map[string]string{
"阿比西尼亚猫": "abys", "爱琴猫": "aege", "美国短尾猫": "abob", "美国卷耳猫": "acur", "美洲短毛猫": "asho", "美国硬毛猫": "awir", "美英猫": "amau", "澳大利亚雾猫": "amis", "巴厘岛猫": "bali",
"班比诺猫": "bamb", "孟加拉虎": "beng", "比尔曼猫": "birm", "孟买猫": "bomb", "英国长毛猫": "bslo", "英国短毛猫": "bsho", "缅甸猫": "bure", "博美拉猫": "buri", "加州闪亮猫": "cspa",
"查达利/蒂法尼猫": "ctif", "夏特鲁斯猫": "char", "非洲狮子猫": "chau", "奇多猫": "chee", "重点色短毛猫": "csho", "康沃尔-雷克斯猫": "crex", "威尔士猫": "cymr", "塞浦路斯猫": "cypr",
Expand Down Expand Up @@ -59,6 +60,14 @@ type catInfo struct {
Picurl string // 猫猫图片
}

func (inf *catInfo) Avatar() string {
nti, err := pool.NewNTImage(inf.Picurl)
if err != nil {
return inf.Picurl
}
return nti.String()
}

var (
catdata = &catdb{
db: &sql.Sqlite{},
Expand Down Expand Up @@ -95,7 +104,7 @@ func init() {
engine.OnRegex(`^吸(.*猫)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
typeOfcat := ctx.State["regex_matched"].([]string)[1]
if typeOfcat == "猫" {
typeName, temperament, description, url, err := getCatAPI()
typeName, temperament, description, url, err := suineko()
if err != nil {
ctx.SendChain(message.Text("[ERROR]: ", err))
return
Expand All @@ -104,7 +113,7 @@ func init() {
"\n气质:\n", temperament, "\n描述:\n", description))
return
}
breeds, ok := catBreeds[typeOfcat]
breeds, ok := typeZH2Breeds[typeOfcat]
if !ok {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("没有相关该品种的猫图"))
return
Expand All @@ -118,7 +127,7 @@ func init() {
})
}

func getCatAPI() (typeName, temperament, description, url string, err error) {
func suineko() (typeName, temperament, description, url string, err error) {
data, err := web.GetData(apiURL + "search?has_breeds=1")
if err != nil {
return
Expand All @@ -129,7 +138,7 @@ func getCatAPI() (typeName, temperament, description, url string, err error) {
return
}
name := gjson.ParseBytes(picdata).Get("breeds.0.name").String()
return catType[name], gjson.ParseBytes(picdata).Get("breeds.0.temperament").String(), gjson.ParseBytes(picdata).Get("breeds.0.description").String(), gjson.ParseBytes(picdata).Get("url").String(), nil
return typeEN2ZH[name], gjson.ParseBytes(picdata).Get("breeds.0.temperament").String(), gjson.ParseBytes(picdata).Get("breeds.0.description").String(), gjson.ParseBytes(picdata).Get("url").String(), nil
}

func getPicByBreed(catBreed string) (url string, err error) {
Expand All @@ -140,7 +149,7 @@ func getPicByBreed(catBreed string) (url string, err error) {
return gjson.ParseBytes(data).Get("0.url").String(), nil
}

func (sql *catdb) insert(gid string, dbInfo catInfo) error {
func (sql *catdb) insert(gid string, dbInfo *catInfo) error {
sql.Lock()
defer sql.Unlock()
err := sql.db.Create(gid, &catInfo{})
Expand Down
12 changes: 6 additions & 6 deletions plugin/cybercat/keepcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func init() {
userInfo.Mood = 0
}
if rand.Intn(10) < 6 && subtime < 2 && userInfo.Satiety > 90 {
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -157,15 +157,15 @@ func init() {
userInfo.LastTime = time.Now().Unix()
userInfo.Mood += int(userInfo.Satiety)/5 - int(userInfo.Weight)/10
userInfo = userInfo.settleOfData()
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
if !cmd && userInfo.Satiety < 80 {
stauts = "完全没有饱"
}
ctx.SendChain(message.Reply(id), message.Text(userInfo.Name, "当前信息如下:\n"),
message.Image(userInfo.Picurl),
message.Image(userInfo.Avatar()),
message.Text("品种: "+userInfo.Type,
"\n饱食度: ", strconv.FormatFloat(userInfo.Satiety, 'f', 0, 64),
"\n心情: ", userInfo.Mood,
Expand Down Expand Up @@ -226,7 +226,7 @@ func init() {
workTime, _ = strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
}
userInfo.Work = time.Now().Unix()*10 + int64(workTime)
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func init() {
userInfo.Mood += rand.Intn(100)
}
userInfo = userInfo.settleOfData()
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -359,7 +359,7 @@ func (data *catInfo) settleOfWork(gid string) (int, bool) {
data.Mood += mood
data.Work = time.Now().Unix() * 10
data.LastTime = time.Unix(data.LastTime, 0).Add(time.Duration(workTime) * time.Hour).Unix()
if catdata.insert(gid, *data) != nil {
if catdata.insert(gid, data) != nil {
return 0, true
}
getmoney := 10 + rand.Intn(10*int(workTime))
Expand Down
16 changes: 8 additions & 8 deletions plugin/cybercat/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func init() {
} else {
userInfo.Work++
}
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand All @@ -85,8 +85,8 @@ func init() {
}
/*******************************************************/
if typeOfcat == "猫" {
nameMap := make([]string, 0, len(catBreeds))
for zhName := range catBreeds {
nameMap := make([]string, 0, len(typeZH2Breeds))
for zhName := range typeZH2Breeds {
nameMap = append(nameMap, zhName)
}
if rand.Intn(100) >= 90 {
Expand Down Expand Up @@ -131,7 +131,7 @@ func init() {
}
}
/*******************************************************/
picurl, _ := getPicByBreed(catBreeds[typeOfcat])
picurl, _ := getPicByBreed(typeZH2Breeds[typeOfcat])
satiety := 90 * rand.Float64() // 饱食度
mood := 50 + rand.Intn(50) // 心情
weight := 2 + 8*rand.Float64() // 体重
Expand Down Expand Up @@ -194,7 +194,7 @@ func init() {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func init() {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func init() {
default:
userInfo.Name = newName
}
if err = catdata.insert(gidStr, userInfo); err != nil {
if err = catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func init() {
uidStr := strconv.FormatInt(ctx.Event.UserID, 10)
userInfo, _ := catdata.find(gidStr, uidStr)
userInfo.Picurl = ctx.State["image_url"].([]string)[0]
if err := catdata.insert(gidStr, userInfo); err != nil {
if err := catdata.insert(gidStr, &userInfo); err != nil {
ctx.SendChain(message.Text("[ERROR]:", err))
return
}
Expand Down

0 comments on commit b15ff7c

Please sign in to comment.