This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/modules/gacha/roll.go

93 lines
2.2 KiB
Go

package gacha
import (
"fmt"
"github.com/bwmarrin/discordgo"
"PermissionGacha/db"
"PermissionGacha/modules/config"
)
func RollReward(s *discordgo.Session, rewardeeID string, timesToReward int) error {
if timesToReward <= 0 {
return nil
}
rewardee, err := s.GuildMember(config.GuildID, rewardeeID)
if err != nil {
return err
}
rewardCount := make(map[int]map[bool]int)
shardCount := 0
for i := 0; i < timesToReward; i++ {
rewardWeightCount := rand.Intn(config.TotalRewardWeight)
var rewardID int
for rewardWeightCount > config.Rewards[rewardID].Weight {
rewardWeightCount -= config.Rewards[rewardID].Weight
rewardID++
}
success, err := rewardToUser(s, config.Rewards[rewardID], rewardee)
if err != nil {
return err
}
if _, ok := rewardCount[rewardID]; !ok {
rewardCount[rewardID] = make(map[bool]int)
}
if success {
rewardee, err = s.GuildMember(config.GuildID, rewardeeID)
if err != nil {
return err
}
} else {
shardCount++
}
rewardCount[rewardID][success]++
}
err = db.RewardShard(rewardeeID, shardCount)
if err != nil {
return err
}
channel, err := s.UserChannelCreate(rewardeeID)
if err != nil {
return err
}
if timesToReward == 1 {
for k, v := range rewardCount {
if v[true] > 0 {
//They were rewarded that exact thing.
s.ChannelMessageSend(
channel.ID,
fmt.Sprintf(
"**GACHA >** <@%s>, you just levelled up! You got __%s__.",
rewardeeID, config.Rewards[k].FriendlyName,
),
)
} else {
//They were rewarded that thing but got a shard instead.
s.ChannelMessageSend(
channel.ID,
fmt.Sprintf(
"**GACHA >** <@%s>, you just levelled up! You originally got __%s__ but you got a shard instead.",
rewardeeID, config.Rewards[k].FriendlyName,
),
)
}
}
} else {
msg := fmt.Sprintf(
"**GACHA >** <@%s>, you just levelled up %d times. Here are your rewards:",
rewardeeID, timesToReward,
)
for k, v := range rewardCount {
msg += fmt.Sprintf("\n- __%s__ x%d", config.Rewards[k].FriendlyName, v[true]+v[false])
if v[false] > 0 {
msg += fmt.Sprintf(" (%d converted to shards)", v[false])
}
}
s.ChannelMessageSend(channel.ID, msg)
}
return nil
}