modules/config+gacha: Make gacha rewards configurable

master
Luther Wen Xu 2020-01-04 20:23:37 +07:00
parent cc9b770cf6
commit c80fdc51cc
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
4 changed files with 63 additions and 55 deletions

@ -12,6 +12,7 @@ type config struct {
PrestigeRequirement int `json:"prestigeFactor"` PrestigeRequirement int `json:"prestigeFactor"`
StarChannel string `json:"starChannel"` StarChannel string `json:"starChannel"`
BotToken string `json:"token"` BotToken string `json:"token"`
Rewards []Reward `json:"rewards"`
LevelRoles []string `json:"roles"` LevelRoles []string `json:"roles"`
} }
@ -20,6 +21,13 @@ type activeNerf struct {
NotInGeneralNerf int `json:"nerfFactor"` NotInGeneralNerf int `json:"nerfFactor"`
} }
type Reward struct {
FriendlyName string `json:"friendlyName"`
Type string `json:"type"`
Weight int `json:"weight"`
Context map[string]interface{} `json:"context"`
}
func init() { func init() {
configFile, err := ioutil.ReadFile("config.json") configFile, err := ioutil.ReadFile("config.json")
if err != nil { if err != nil {
@ -40,6 +48,13 @@ func init() {
StarChannel = output.StarChannel StarChannel = output.StarChannel
BotToken = output.BotToken BotToken = output.BotToken
LevelRoles = output.LevelRoles LevelRoles = output.LevelRoles
Rewards = output.Rewards
if len(Rewards) == 0 {
panic("modules/config: Gacha rewards not provided")
}
for _, v := range output.Rewards {
TotalRewardWeight += v.Weight
}
} }
//Exposed Variables //Exposed Variables
@ -53,3 +68,7 @@ var PrestigeRequirement int
var StarChannel string var StarChannel string
var BotToken string var BotToken string
var LevelRoles []string var LevelRoles []string
var (
Rewards []Reward
TotalRewardWeight int
)

@ -0,0 +1,34 @@
package gacha
import (
"fmt"
"github.com/bwmarrin/discordgo"
"PermissionGacha/modules/config"
)
func rewardToUser(s *discordgo.Session, reward config.Reward, rewardee *discordgo.Member) (bool, error) {
switch reward.Type {
case "role":
return rewardRole(s, reward, rewardee)
}
return false, fmt.Errorf("gacha: rewardToUser: unknown reward type: %s", reward.Type)
}
func rewardRole(s *discordgo.Session, reward config.Reward, rewardee *discordgo.Member) (bool, error) {
roleID, found := reward.Context["id"].(string)
if !found {
return false, fmt.Errorf("gacha: rewardRole: `id` not passed to reward with type `role`")
}
for _, v := range rewardee.Roles {
if v == roleID {
return false, nil
}
}
err := s.GuildMemberRoleAdd(config.GuildID, rewardee.User.ID, roleID)
if err != nil {
return false, err
}
return true, nil
}

@ -1,50 +0,0 @@
package gacha
import (
"github.com/bwmarrin/discordgo"
"PermissionGacha/modules/config"
)
type reward struct {
FriendlyName string
Reward func(s *discordgo.Session, member *discordgo.Member) (bool, error)
}
var gachaRewards = []reward{
reward{
FriendlyName: "View Audit Log",
Reward: generateRoleReward("645856347258093568"),
},
reward{
FriendlyName: "Manage Webhook",
Reward: generateRoleReward("645856081750523914"),
},
reward{
FriendlyName: "Change Nickname",
Reward: generateRoleReward("645977231281356800"),
},
reward{
FriendlyName: "Use Voice Activity",
Reward: generateRoleReward("645880647474479115"),
},
reward{
FriendlyName: "Embed Link",
Reward: generateRoleReward("645977361254318080"),
},
}
func generateRoleReward(roleID string) func(s *discordgo.Session, member *discordgo.Member) (bool, error) {
return func(s *discordgo.Session, member *discordgo.Member) (bool, error) {
for _, v := range member.Roles {
if v == roleID {
return false, nil
}
}
err := s.GuildMemberRoleAdd(config.GuildID, member.User.ID, roleID)
if err != nil {
return false, err
}
return true, nil
}
}

@ -20,8 +20,13 @@ func RollReward(s *discordgo.Session, rewardeeID string, timesToReward int) erro
rewardCount := make(map[int]map[bool]int) rewardCount := make(map[int]map[bool]int)
shardCount := 0 shardCount := 0
for i := 0; i < timesToReward; i++ { for i := 0; i < timesToReward; i++ {
rewardID := rand.Intn(len(gachaRewards)) rewardWeightCount := rand.Intn(config.TotalRewardWeight)
success, err := gachaRewards[rewardID].Reward(s, rewardee) 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 { if err != nil {
return err return err
} }
@ -56,7 +61,7 @@ func RollReward(s *discordgo.Session, rewardeeID string, timesToReward int) erro
channel.ID, channel.ID,
fmt.Sprintf( fmt.Sprintf(
"**GACHA >** <@%s>, you just levelled up! You got __%s__.", "**GACHA >** <@%s>, you just levelled up! You got __%s__.",
rewardeeID, gachaRewards[k].FriendlyName, rewardeeID, config.Rewards[k].FriendlyName,
), ),
) )
} else { } else {
@ -65,7 +70,7 @@ func RollReward(s *discordgo.Session, rewardeeID string, timesToReward int) erro
channel.ID, channel.ID,
fmt.Sprintf( fmt.Sprintf(
"**GACHA >** <@%s>, you just levelled up! You originally got __%s__ but you got a shard instead.", "**GACHA >** <@%s>, you just levelled up! You originally got __%s__ but you got a shard instead.",
rewardeeID, gachaRewards[k].FriendlyName, rewardeeID, config.Rewards[k].FriendlyName,
), ),
) )
} }
@ -76,7 +81,7 @@ func RollReward(s *discordgo.Session, rewardeeID string, timesToReward int) erro
rewardeeID, timesToReward, rewardeeID, timesToReward,
) )
for k, v := range rewardCount { for k, v := range rewardCount {
msg += fmt.Sprintf("\n- __%s__", gachaRewards[k].FriendlyName) msg += fmt.Sprintf("\n- __%s__ x%d", config.Rewards[k].FriendlyName, v[true]+v[false])
if v[false] > 0 { if v[false] > 0 {
msg += fmt.Sprintf(" (%d converted to shards)", v[false]) msg += fmt.Sprintf(" (%d converted to shards)", v[false])
} }