Skip to content

Commit

Permalink
Major code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
asenci committed Dec 27, 2018
1 parent f3da0c9 commit ecd5eb2
Show file tree
Hide file tree
Showing 26 changed files with 1,124 additions and 360 deletions.
95 changes: 95 additions & 0 deletions cmd/draw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"log"
"strings"

"github.com/Necroforger/dgrouter/exrouter"
"github.com/asenci/pickerbot/draws"
)

func RunDraw(ctx *exrouter.Context) {
defer func(channel string) {
err := draws.All.Delete(channel)
if err != nil {
log.Println("error removing draw,", err)
}
}(ctx.Msg.ChannelID)

draw, err := draws.All.Get(ctx.Msg.ChannelID)
if err == draws.DrawNotFound {
ctx.Reply("No draws currently in place, let's start a new one?")
return
}
if err != nil {
ctx.Reply("An error has occurred while processing your request. Please try again.")
return
}

teams, err := draw.Run()
if err == draws.DrawNotEnough {
ctx.Reply("Not enough players for drawing a team")
return
}
if err != nil {
ctx.Reply("An error has occurred while processing your request. Please try again.")
return
}

channel, err := ctx.Ses.Channel(ctx.Msg.ChannelID)
if err != nil {
log.Printf("failed to retrieve channel, %s", err)
}

guild := channel.GuildID

for _, team := range teams {
var members []string
for p, _ := range team.Players {
members = append(members, p)
}

ctx.Reply(team.Name, ": <@", strings.Join(members, ">, <@"), ">")

teamChan, err := ctx.Ses.GuildChannelCreate(guild, team.Name, "voice")
if err != nil {
log.Printf("error creating team channel %q, %s", team.Name, err)

ctx.Reply("Error creating channel for team ", team.Name, ", please create it manually")
}

for _, player := range members {
err = ctx.Ses.GuildMemberMove(guild, player, teamChan.ID)
if err != nil {
log.Printf("error moving user to channel %q, %s", team.Name, err)

ctx.Reply("Error moving <@", player, "> to channel ", team.Name, ", please join manually")
}
}

// TODO: cleanup idle channels (use Guild.VoiceStates?)
// go func(channel, name string) {
// timer := time.NewTicker(5 * time.Minute)
// for range timer.C {
// c, err := ctx.Ses.Channel(channel)
// if err != nil {
// log.Printf("failed to find channel %q", name)
//
// ctx.Reply("Error deleting channel ", name, ", please delete it manually")
// }
//
// if len(c.Recipients) == 0 {
// ctx.Reply("Deleting idle channel ", name)
//
// _, err := ctx.Ses.ChannelDelete(channel)
// if err != nil {
// log.Printf("error deleting channel %q, %s", name, err)
//
// ctx.Reply("Error deleting channel ", name, ", please delete it manually")
// }
// return
// }
// }
// }(teamChan.ID, team.Name)
}
}
20 changes: 20 additions & 0 deletions cmd/games.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"
"strings"

"github.com/asenci/pickerbot/games"

"github.com/Necroforger/dgrouter/exrouter"
)

func Games(ctx *exrouter.Context) {
var s strings.Builder
fmt.Fprintln(&s, "Known games:")
for _, game := range games.All {
fmt.Fprintf(&s, " :small_blue_diamond: %s (%d players per team)\n", game.Name, game.PlayersPerTeam)
}

ctx.Reply(s.String())
}
20 changes: 20 additions & 0 deletions cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"
"strings"

"github.com/Necroforger/dgrouter/exrouter"
)

func Help(ctx *exrouter.Context) {
s := &strings.Builder{}

fmt.Fprint(s, "```")
for _, v := range ctx.Route.Parent.Routes {
fmt.Fprintf(s, "%s: %s\n", v.Name, v.Description)
}
fmt.Fprint(s, "```")

ctx.Reply("Hi mate, here is what I can do:", s)
}
43 changes: 43 additions & 0 deletions cmd/maps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"log"
"strings"

"github.com/asenci/pickerbot/games"

"github.com/Necroforger/dgrouter/exrouter"
)

func Maps(ctx *exrouter.Context) {
gameName := ctx.Args.Get(1)

if gameName == "" {
ctx.Reply("Which game? Pick one from \"<@", ctx.Ses.State.User.ID, "> games\"")
return
}

game, err := games.All.Get(gameName)
if err != nil {
if err == games.GameNotFound {
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, I don't know ", gameName)
return
}

log.Println(err)
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request: ", err)
}

var s strings.Builder
fmt.Fprintf(&s, "Known %q maps:\n", game)
for _, m := range game.Maps {
if m.Name == "" {
fmt.Fprintf(&s, " :small_blue_diamond: %s[Default map]\n", m)
} else {
fmt.Fprintf(&s, " :small_blue_diamond: %s\n", m)
}
}

ctx.Reply(s.String())
}
32 changes: 32 additions & 0 deletions cmd/me.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/Necroforger/dgrouter/exrouter"
"github.com/asenci/pickerbot/draws"
)

func JoinDraw(ctx *exrouter.Context) {
draw, err := draws.All.Get(ctx.Msg.ChannelID)
if err == draws.DrawNotFound {
ctx.Reply("No draws currently in place, let's start a new one?")
return
}
if err != nil {
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request. Please try again.")
return
}

player := ctx.Msg.Author.ID

err = draw.Join(player)
if err == draws.DrawAlreadyJoined {
ctx.Reply("<@", ctx.Msg.Author.ID, "> you are already on the draw.")
return
}
if err != nil {
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request. Please try again.")
return
}

ctx.Reply("<@", player, "> joined the draw")
}
5 changes: 2 additions & 3 deletions ping.go → cmd/ping.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main
package cmd

import "github.com/Necroforger/dgrouter/exrouter"

// ToDo: chill bro
func Ping(ctx *exrouter.Context) error {
func Ping(ctx *exrouter.Context) {
ctx.Reply("<@", ctx.Msg.Author.ID, "> pong")
return nil
}
71 changes: 71 additions & 0 deletions cmd/play.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"log"
"strconv"
"time"

"github.com/asenci/pickerbot/draws"
"github.com/asenci/pickerbot/games"

"github.com/Necroforger/dgrouter/exrouter"
)

func NewDraw(ctx *exrouter.Context) {
var game *games.Game
var err error

gameName := ctx.Args.Get(1)
numPlayersStr := ctx.Args.Get(2)

if gameName == "" {
ctx.Reply("Which game? Pick one from \"<@", ctx.Ses.State.User.ID, "> games\" or specify a new one:\n<@", ctx.Ses.State.User.ID, "> play <game name> <number of players per team>")
return
}

if numPlayersStr == "" {
game, err = games.All.Get(gameName)
if err != nil {
if err == games.GameNotFound {
ctx.Reply("I don't know ", gameName, ", how many players can play on the same team?\nUse: <@", ctx.Ses.State.User.ID, "> play ", gameName, " <number of players per team>")
return
}
}

} else {
numPlayers, err := strconv.Atoi(numPlayersStr)
if err != nil || numPlayers == 0 {
log.Printf("invalid number: \"%s\", %s\n", numPlayersStr, err)

ctx.Reply("Invalid number of players: ", numPlayersStr)
return
}

game = &games.Game{
Name: gameName,
PlayersPerTeam: numPlayers,
Maps: nil,
}
}

_, err = draws.All.New(ctx.Msg.ChannelID, game)
if err == draws.DrawAlreadyExists {
ctx.Reply("Draw in progress, please wait for it to finish before starting a new one")
return
}
if err != nil {
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request. Please try again.")
return
}

ctx.Reply("Sweet! Who is up for some ", game, "? (reply with \"<@", ctx.Ses.State.User.ID, "> me\")")

time.AfterFunc(time.Minute, func() {
_, err := draws.All.Get(ctx.Msg.ChannelID)
if err == draws.DrawNotFound {
return
}

RunDraw(ctx)
})
}
60 changes: 60 additions & 0 deletions cmd/whereto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"log"
"math/rand"
"strings"

"github.com/asenci/pickerbot/maps"

"github.com/asenci/pickerbot/games"

"github.com/Necroforger/dgrouter/exrouter"
)

func WhereTo(ctx *exrouter.Context) {
gameName := ctx.Args.Get(1)
mapName := ctx.Args.Get(2)
locations := ctx.Args.After(3)

if locations != "" {
ls := strings.Split(locations, " ")
i := rand.Intn(len(ls))
ctx.Reply("Let's go ", ls[i], "!")
return
}

if gameName == "" {
ctx.Reply("Which game? Pick one from \"<@", ctx.Ses.State.User.ID, "> games\" or specify a custom one:\n<@", ctx.Ses.State.User.ID, "> whereto <game name> <map name> <location 1> [<location 2> ...]")
return
}

game, err := games.All.Get(gameName)
if err != nil {
if err == games.GameNotFound {
ctx.Reply("I don't know ", gameName, ", give me some locations to draw")
return
}

log.Println(err)
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request: ", err)
}

if mapName == "" {
ctx.Reply("Which map? Pick one from \"<@", ctx.Ses.State.User.ID, "> maps\" or specify a custom one:\n<@", ctx.Ses.State.User.ID, "> whereto <game name> <map name> <location 1> [<location 2> ...]")
return
}

gameMap, err := game.Maps.Get(mapName)
if err != nil {
if err == maps.MapNotFound {
ctx.Reply("I don't know ", mapName, ", give me some locations to draw")
return
}

log.Println(err)
ctx.Reply("Sorry <@", ctx.Msg.Author.ID, ">, an error has occurred while processing your request: ", err)
}

ctx.Reply("Let's go ", gameMap.Locations.Random(), "!")
}
Loading

0 comments on commit ecd5eb2

Please sign in to comment.