Skip to content

Commit

Permalink
One key per user (#26)
Browse files Browse the repository at this point in the history
* First steps at adding a userdb of taken games, hopefully created db and added a listing function just to get some ideas down. Still need to get testenv set back up but I believe this should work. Then just need code for GrabKey to check a userlist, grant/deny, add game if grant

* Added user limit checking, verification, adding to a users game list. Last thing needed is to actually scan a users list for comparison to see if they have taken a game.

Finally need to actually ya know, run the code

* Appears to work, tested with varying cases

* Removed some debug
  • Loading branch information
ezelkow1 authored Oct 19, 2018
1 parent b6aa51d commit ecab7d2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
113 changes: 100 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"os/signal"
"regexp"
"sort"
"strconv"
"strings"
"syscall"

"github.com/bwmarrin/discordgo"
)

//GameKey struct
Expand All @@ -29,26 +30,32 @@ type Configuration struct {
BroadcastChannel string
DbFile string
KeyRole string
UserFile string
}

// Variables used for command line parameters or global
var (
config = Configuration{}
re = regexp.MustCompile(`.*\s`)
gog = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
steamOne = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
steamTwo = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
ps3 = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
uplayOne = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
uplayTwo = regexp.MustCompile(`^[a-z,A-Z,0-9]{3}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
origin = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
url = regexp.MustCompile(`^http`)
x = make(map[string][]GameKey)
config = Configuration{}
re = regexp.MustCompile(`.*\s`)
gog = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
steamOne = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
steamTwo = regexp.MustCompile(`^[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}-[a-z,A-Z,0-9]{5}$`)
ps3 = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
uplayOne = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
uplayTwo = regexp.MustCompile(`^[a-z,A-Z,0-9]{3}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
origin = regexp.MustCompile(`^[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}-[a-z,A-Z,0-9]{4}$`)
url = regexp.MustCompile(`^http`)
// Game key Database
x = make(map[string][]GameKey)

// Optional user database for tracking
userList = make(map[string][]string)
configfile string
embedColor = 0x00ff00
initialized = false
guildID string
roleID string
limitUsers = false
)

func init() {
Expand Down Expand Up @@ -106,6 +113,19 @@ func main() {
return
}

if config.UserFile != "" {
limitUsers = true
if _, err := os.Stat(config.UserFile); os.IsNotExist(err) {
fmt.Println("User File does not exist, creating")
newFile, _ := os.Create(config.UserFile)
newFile.Close()
}

Load(config.UserFile, &userList)
} else {
fmt.Println("No user db specified, not limiting users")
}

// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
Expand Down Expand Up @@ -206,6 +226,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, "!help") == true {
PrintHelp(s, m)
}

if m.Content == "!mygames" {
if limitUsers {
PrintMyGames(s, m)
}
}
}

//PrintHelp will print out the help dialog
Expand All @@ -214,6 +240,9 @@ func PrintHelp(s *discordgo.Session, m *discordgo.MessageCreate) {
buffer.WriteString("!add game name key - this will add a new key to the database. This should be done in a DM with the bot\n")
buffer.WriteString("!listkeys - PLEASE USE THIS IN A PRIVATE MESSAGE WITH THE BOT. Lists current games and the number of available keys\n")
buffer.WriteString("!take game name - Will give you one of the keys for the game in a DM\n")
if limitUsers {
buffer.WriteString("!mygames - Will give a list of games you have taken\n")
}
buffer.WriteString("!search search-string - Will search the database for matching games")
SendEmbed(s, m.ChannelID, "", "Available Commands", buffer.String())
}
Expand Down Expand Up @@ -264,6 +293,27 @@ func SearchGame(s *discordgo.Session, m *discordgo.MessageCreate) {
SendEmbed(s, m.ChannelID, "", "Search Results", output)
}

//CheckUserLimitAllowed to see if a user has this normalized game name in their taken list
func CheckUserLimitAllowed(s *discordgo.Session, m *discordgo.MessageCreate, gamename string) bool {

if limitUsers == false {
// We aren't doing user limiting, always allow
return true
}

Load(config.UserFile, &userList)
normalized := NormalizeGame(gamename)
//keys := make([]string, 0, len(userList[m.Author.Username]))
for key := range userList[m.Author.Username] {
//keys = append(keys, key)
if strings.Compare(NormalizeGame(userList[m.Author.Username][key]), normalized) == 0 {
return false
}
}

return true
}

//GrabKey will grab one of the keys for the current game, pop it, and save
func GrabKey(s *discordgo.Session, m *discordgo.MessageCreate) {
// Clean up content
Expand All @@ -272,8 +322,22 @@ func GrabKey(s *discordgo.Session, m *discordgo.MessageCreate) {
normalized := NormalizeGame(gamename)

Load(config.DbFile, &x)

allowed := CheckUserLimitAllowed(s, m, gamename)

// User already took this game
if !allowed {
dmchan, _ := s.UserChannelCreate(m.Author.ID)
SendEmbed(s, dmchan.ID, "", "Access Denied", "You have already received a copy of "+gamename)
return
} else if limitUsers {
//User limiting enabled and user allowed, add game
userList[m.Author.Username] = append(userList[m.Author.Username], gamename)
Save(config.UserFile, &userList)
}

//We need to pop off a game, if it exists
if len(x[normalized]) > 0 {
if len(x[normalized]) > 0 && allowed {
var userkey GameKey
userkey, x[normalized] = x[normalized][0], x[normalized][1:]
dmchan, _ := s.UserChannelCreate(m.Author.ID)
Expand Down Expand Up @@ -303,6 +367,29 @@ func GrabKey(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}

//PrintMyGames will print out the list of games a user has taken
func PrintMyGames(s *discordgo.Session, m *discordgo.MessageCreate) {
Load(config.UserFile, &userList)

if len(userList) == 0 {
SendEmbed(s, m.ChannelID, "", "Empty User List", "No users present in list")
return
}

list := userList[m.Author.Username]
var buffer bytes.Buffer

sort.Strings(list)

for i := range list {
buffer.WriteString(userList[m.Author.Username][i])
buffer.WriteString("\n")
}

SendEmbed(s, m.ChannelID, "", "Game List", buffer.String())
return
}

//ListKeys lists what games and how many keys for each
func ListKeys(s *discordgo.Session, m *discordgo.MessageCreate) {
Load(config.DbFile, &x)
Expand Down
6 changes: 4 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"strings"

"github.com/bwmarrin/discordgo"
)

//isGogMatch
Expand Down Expand Up @@ -158,7 +159,8 @@ func checkPrefix(msg string) bool {
(strings.HasPrefix(msg, "!add ") == true) ||
(strings.HasPrefix(msg, "!take ") == true) ||
(strings.HasPrefix(msg, "!search ") == true) ||
(strings.HasPrefix(msg, "!help") == true) {
(strings.HasPrefix(msg, "!help") == true) ||
(msg == "!mygames") {
return true
}

Expand Down

0 comments on commit ecab7d2

Please sign in to comment.