lucky-tgbot/plugin/reborn/main.go
sunyz b04e1fc72d
Some checks failed
Build and Push Docker Images / docker (push) Failing after 31s
[+] improve random function
Signed-off-by: sunyz <i@sunyz.net>
2025-01-24 16:17:44 +08:00

74 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package reborn
import (
"encoding/json"
"fmt"
"math/rand"
"os"
tele "gopkg.in/telebot.v3"
)
type Country struct {
Name string `json:"country"`
Pop int64 `json:"population"`
}
type RebornData struct {
List []Country
TotalPop int64
}
func InitRebornList(filePath string) (*RebornData, error) {
rawJson, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var countries []Country
if err = json.Unmarshal(rawJson, &countries); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
}
totalPop := int64(0)
for _, country := range countries {
totalPop += country.Pop
}
return &RebornData{
List: countries,
TotalPop: totalPop,
}, nil
}
func (data *RebornData) randCountry(r *rand.Rand) (Country, error) {
randNum := r.Int63n(data.TotalPop)
for _, country := range data.List {
if randNum < country.Pop {
return country, nil
}
randNum -= country.Pop
}
return Country{}, fmt.Errorf("failed to select a country")
}
func randGender(r *rand.Rand) string {
genders := []string{"男孩子", "女孩子", " MtF", " FtM", "萝莉", "正太", "武装直升机", "沃尔玛购物袋", "狗狗", "猫猫"}
return genders[r.Intn(len(genders))]
}
func Execute(c tele.Context, r *rand.Rand, data *RebornData) error {
country, err := data.randCountry(r)
if err != nil {
return nil
}
gender := randGender(r)
outputText := fmt.Sprintf("投胎成功!\n你出生在%s是%s。", country.Name, gender)
return c.Reply(outputText)
}