2019-11-27 09:07:48 +07:00
|
|
|
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++ {
|
2020-01-04 12:23:37 +07:00
|
|
|
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)
|
2019-11-27 09:07:48 +07:00
|
|
|
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__.",
|
2020-01-04 12:23:37 +07:00
|
|
|
rewardeeID, config.Rewards[k].FriendlyName,
|
2019-11-27 09:07:48 +07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
} 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.",
|
2020-01-04 12:23:37 +07:00
|
|
|
rewardeeID, config.Rewards[k].FriendlyName,
|
2019-11-27 09:07:48 +07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg := fmt.Sprintf(
|
|
|
|
"**GACHA >** <@%s>, you just levelled up %d times. Here are your rewards:",
|
|
|
|
rewardeeID, timesToReward,
|
|
|
|
)
|
|
|
|
for k, v := range rewardCount {
|
2020-01-04 12:23:37 +07:00
|
|
|
msg += fmt.Sprintf("\n- __%s__ x%d", config.Rewards[k].FriendlyName, v[true]+v[false])
|
2019-11-27 09:07:48 +07:00
|
|
|
if v[false] > 0 {
|
|
|
|
msg += fmt.Sprintf(" (%d converted to shards)", v[false])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.ChannelMessageSend(channel.ID, msg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|