Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace array with cache to store games
- Loading branch information
Théo Vidal
committed
Jan 6, 2021
1 parent
1ecf8ef
commit 5f45464
Showing
5 changed files
with
93 additions
and
33 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 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 |
---|---|---|
@@ -1,52 +1,55 @@ | ||
package irregular_verbs | ||
|
||
import ( | ||
"context" | ||
"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) { | ||
func HandleAnswer(bot *onyxcord.Bot, message *discordgo.Message, cacheID string) { | ||
if message.Author.Bot { | ||
return | ||
} | ||
trial := lib.TrimNonLetters(message.Content) | ||
verb := bot.Cache.HGet(context.Background(), cacheID, "verb").Val() | ||
|
||
if lib.Contains(stopSentences, trial) { | ||
player.Props["answers"] = player.Props["answers"].(int) - 1 | ||
bot.Cache.HIncrBy(context.Background(), cacheID, "answers", -1) | ||
successfulAnswers, _ := bot.Cache.HGet(context.Background(), cacheID, "successfulAnswers").Int() | ||
answers, _ := bot.Cache.HGet(context.Background(), cacheID, "answers").Int() | ||
|
||
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, | ||
successfulAnswers, | ||
answers, | ||
(float64(successfulAnswers)/float64(answers))*20.0, | ||
), | ||
) | ||
VerbsPlayers.RemovePlayer(message.ChannelID) | ||
bot.Cache.Del(context.Background(), cacheID) | ||
return | ||
} | ||
|
||
if lib.Contains(skipSentences, trial) { | ||
bot.Client.ChannelMessageSend( | ||
message.ChannelID, | ||
fmt.Sprintf(":fast_forward: Le mot recherché était **%s**", player.Props["unknownVerb"]), | ||
fmt.Sprintf(":fast_forward: Le mot recherché était **%s**", verb), | ||
) | ||
SendQuestion(bot, player) | ||
SendQuestion(bot, message.ChannelID) | ||
return | ||
} | ||
|
||
println(trial, lib.TrimNonLetters(player.Props["unknownVerb"].(string))) | ||
|
||
if trial == lib.TrimNonLetters(player.Props["unknownVerb"].(string)) { | ||
if trial == lib.TrimNonLetters(verb) { | ||
bot.Client.MessageReactionAdd(message.ChannelID, message.ID, "✅") | ||
if player.Props["succeeded"].(bool) { | ||
player.Props["successfulAnswers"] = player.Props["successfulAnswers"].(int) + 1 | ||
if bot.Cache.HGet(context.Background(), cacheID, "succeeded").Val() == "true" { | ||
bot.Cache.HIncrBy(context.Background(), cacheID, "successfulAnswers", 1) | ||
} | ||
SendQuestion(bot, player) | ||
SendQuestion(bot, message.ChannelID) | ||
} else { | ||
bot.Client.MessageReactionAdd(message.ChannelID, message.ID, "❌") | ||
player.Props["succeeded"] = false | ||
bot.Cache.HSet(context.Background(), cacheID, "suceeded", "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
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