-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create single map to hold games
Every game is stored in a map and retrieved when a new message is fired, so the bot can only keep one handler and games are continued where they were started.
- Loading branch information
Théo Vidal
committed
Jan 6, 2021
1 parent
35874ab
commit 1ecf8ef
Showing
9 changed files
with
207 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package irregular_verbs | ||
|
||
import ( | ||
"fmt" | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/theovidal/105quiz/lib" | ||
"github.com/theovidal/onyxcord" | ||
) | ||
|
||
func HandleAnswer(bot *onyxcord.Bot, message *discordgo.Message, player *lib.Client) { | ||
if message.Author.Bot { | ||
return | ||
} | ||
trial := lib.TrimNonLetters(message.Content) | ||
|
||
if lib.Contains(stopSentences, trial) { | ||
player.Props["answers"] = player.Props["answers"].(int) - 1 | ||
bot.Client.ChannelMessageSend( | ||
message.ChannelID, | ||
fmt.Sprintf( | ||
":stop_sign: **Arrêt du quiz en cours!** Vous avez réussi %d questions sur %d (note de %.2f/20)", | ||
player.Props["successfulAnswers"].(int), | ||
player.Props["answers"].(int), | ||
(float64(player.Props["successfulAnswers"].(int))/float64(player.Props["answers"].(int)))*20.0, | ||
), | ||
) | ||
VerbsPlayers.RemovePlayer(message.ChannelID) | ||
return | ||
} | ||
|
||
if lib.Contains(skipSentences, trial) { | ||
bot.Client.ChannelMessageSend( | ||
message.ChannelID, | ||
fmt.Sprintf(":fast_forward: Le mot recherché était **%s**", player.Props["unknownVerb"]), | ||
) | ||
SendQuestion(bot, player) | ||
return | ||
} | ||
|
||
println(trial, lib.TrimNonLetters(player.Props["unknownVerb"].(string))) | ||
|
||
if trial == lib.TrimNonLetters(player.Props["unknownVerb"].(string)) { | ||
bot.Client.MessageReactionAdd(message.ChannelID, message.ID, "✅") | ||
if player.Props["succeeded"].(bool) { | ||
player.Props["successfulAnswers"] = player.Props["successfulAnswers"].(int) + 1 | ||
} | ||
SendQuestion(bot, player) | ||
} else { | ||
bot.Client.MessageReactionAdd(message.ChannelID, message.ID, "❌") | ||
player.Props["succeeded"] = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package irregular_verbs | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/theovidal/onyxcord" | ||
|
||
"github.com/theovidal/105quiz/lib" | ||
) | ||
|
||
var VerbsPlayers lib.Players | ||
|
||
func Command() *onyxcord.Command { | ||
return &onyxcord.Command{ | ||
Description: "Lancer un quiz sur les verbes irréguliers en anglais", | ||
Show: true, | ||
ListenInPublic: true, | ||
ListenInDM: true, | ||
Execute: func(arguments []string, bot onyxcord.Bot, message *discordgo.MessageCreate) (err error) { | ||
player := lib.NewClient(message) | ||
player.Props["answers"] = 0 | ||
player.Props["successfulAnswers"] = 0 | ||
VerbsPlayers.AddPlayer(&player) | ||
|
||
bot.Client.ChannelMessageSend(message.ChannelID, ":flag_gb: **Quiz sur les verbes irréguliers**") | ||
SendQuestion(&bot, &player) | ||
|
||
return | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package irregular_verbs | ||
|
||
import ( | ||
"fmt" | ||
"github.com/theovidal/onyxcord" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/theovidal/105quiz/lib" | ||
) | ||
|
||
func SendQuestion(bot *onyxcord.Bot, player *lib.Client) { | ||
rand.Seed(time.Now().UnixNano()) | ||
row := rand.Intn(len(verbs)) | ||
questionColumn := rand.Intn(4) | ||
verbColumn := questionColumn | ||
|
||
for verbColumn == questionColumn { | ||
verbColumn = rand.Intn(4) | ||
} | ||
|
||
question := verbs[row][questionColumn] | ||
verb := verbs[row][verbColumn] | ||
player.Props["verb"] = verb | ||
|
||
player.Props["answers"] = player.Props["answers"].(int) + 1 | ||
player.Props["succeeded"] = true | ||
bot.Client.ChannelMessageSend( | ||
player.Context.ChannelID, | ||
fmt.Sprintf("**#%d** `%s` - Indiquer %s", player.Props["answers"], question, categories[verbColumn]), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package irregular_verbs | ||
|
||
import ( | ||
"encoding/csv" | ||
"log" | ||
"os" | ||
) | ||
|
||
var categories = []string{"la **base verbale**", "le **prétérit**", "le **participe passé**", "la **traduction**"} | ||
var skipSentences = []string{"jepasse", "passe", "passer", "suivant", "skip", "jsp", "jesaispas", "jesaipa", "jesaispa", "jesaipas", "aucuneidee"} | ||
var stopSentences = []string{"stop", "arret", "arreter", "tg", "areter"} | ||
|
||
var verbs = OpenVerbs() | ||
|
||
func OpenVerbs() [][]string { | ||
file, err := os.Open("assets/irregular_verbs.csv") | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
reader := csv.NewReader(file) | ||
records, err := reader.ReadAll() | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
return records | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package lib | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
type Client struct { | ||
Context *discordgo.MessageCreate | ||
Props map[string]interface{} | ||
} | ||
|
||
func NewClient(context *discordgo.MessageCreate) Client { | ||
return Client{ | ||
Context: context, | ||
Props: make(map[string]interface{}), | ||
} | ||
} | ||
|
||
type Players struct { | ||
sync.RWMutex | ||
clients map[string]*Client | ||
} | ||
|
||
func (p *Players) Initialize() { | ||
p.clients = make(map[string]*Client) | ||
} | ||
|
||
func (p *Players) AddPlayer(client *Client) { | ||
p.Lock() | ||
p.clients[client.Context.ChannelID] = client | ||
p.Unlock() | ||
} | ||
|
||
func (p *Players) RemovePlayer(channel string) { | ||
p.Lock() | ||
delete(p.clients, channel) | ||
p.Unlock() | ||
} | ||
|
||
func (p *Players) GetPlayer(channel string) (player *Client, exists bool) { | ||
player, exists = p.clients[channel] | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters