@@ -2,16 +2,87 @@ package keeper
22
33import (
44 "context"
5+ "crypto/sha256"
6+ "encoding/hex"
7+ "strconv"
8+ "time"
59
610 "github.com/YazzyYaz/wordle/x/wordle/types"
711 sdk "github.com/cosmos/cosmos-sdk/types"
12+ sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
13+ "github.com/tendermint/tendermint/crypto"
814)
915
1016func (k msgServer ) SubmitGuess (goCtx context.Context , msg * types.MsgSubmitGuess ) (* types.MsgSubmitGuessResponse , error ) {
1117 ctx := sdk .UnwrapSDKContext (goCtx )
18+ // Check Word is 5 Characters Long
19+ if len (msg .Word ) != 5 {
20+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "Guess Must Be A 5 Letter Word!" )
21+ }
1222
13- // TODO: Handling the message
14- _ = ctx
23+ // Check String Contains Alphabet Letters Only
24+ if ! (IsLetter (msg .Word )) {
25+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "Guess Must Only Consist of Alphabet Letters!" )
26+ }
1527
16- return & types.MsgSubmitGuessResponse {}, nil
28+ // Get Current Day to Pull Up Wordle of That Day As A Hash
29+ currentTime := time .Now ().Local ()
30+ var currentTimeBytes = []byte (currentTime .Format ("2006-01-02" ))
31+ var currentTimeHash = sha256 .Sum256 (currentTimeBytes )
32+ var currentTimeHashString = hex .EncodeToString (currentTimeHash [:])
33+ wordle , isFound := k .GetWordle (ctx , currentTimeHashString )
34+ if ! isFound {
35+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "Wordle of The Day Hasn't Been Submitted Yet. Feel Free to Submit One!" )
36+ }
37+
38+ // We Convert Current Day and Guesser to A Hash To Use As An Index For Today's Guesses For That Guesser
39+ // That Way, A Person Can Guess 6 Times A Day For Each New Wordle Created
40+ var currentTimeGuesserBytes = []byte (currentTime .Format ("2006-01-02" ) + msg .Creator )
41+ var currentTimeGuesserHash = sha256 .Sum256 (currentTimeGuesserBytes )
42+ var currentTimeGuesserHashString = hex .EncodeToString (currentTimeGuesserHash [:])
43+ // Hash The Guess To The Wordle
44+ var submittedSolutionHash = sha256 .Sum256 ([]byte (msg .Word ))
45+ var submittedSolutionHashString = hex .EncodeToString (submittedSolutionHash [:])
46+
47+ // Get the Latest Guess entry for this Submitter for the current Wordle of the Day
48+ var count int
49+ guess , isFound := k .GetGuess (ctx , currentTimeGuesserHashString )
50+ if isFound {
51+ // Check if Submitter Reached 6 Tries
52+ if guess .Count == strconv .Itoa (6 ) {
53+ return nil , sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "You Have Guessed The Maximum Amount of Times for The Day! Try Again Tomorrow With A New Wordle." )
54+ }
55+ currentCount , err := strconv .Atoi (guess .Count )
56+ if err != nil {
57+ panic (err )
58+ }
59+ count = currentCount
60+ } else {
61+ // Initialize Count Value If No Entry Exists for this Submitter for Today's Wordle
62+ count = 0
63+ }
64+ // Increment Guess Count
65+ count += 1
66+ var newGuess = types.Guess {
67+ Index : currentTimeGuesserHashString ,
68+ Submitter : msg .Creator ,
69+ Word : submittedSolutionHashString ,
70+ Count : strconv .Itoa (count ),
71+ }
72+ // Remove Current Guess Entry to be Updated With New Entry
73+ k .RemoveGuess (ctx , currentTimeGuesserHashString )
74+ // Add New Guess Entry
75+ k .SetGuess (ctx , newGuess )
76+ // Setup Reward
77+ reward := sdk.Coins {sdk .NewInt64Coin ("WORDLE" , 100 )}
78+ if ! (wordle .Word == submittedSolutionHashString ) {
79+ return & types.MsgSubmitGuessResponse {Title : "Wrong Answer" , Body : "Your Guess Was Wrong. Try Again" }, nil
80+ } else {
81+ // If Submitter Guesses Correctly
82+ guesserAddress , _ := sdk .AccAddressFromBech32 (msg .Creator )
83+ moduleAcct := sdk .AccAddress (crypto .AddressHash ([]byte (types .ModuleName )))
84+ // Send Reward
85+ k .bankKeeper .SendCoins (ctx , guesserAddress , moduleAcct , reward )
86+ return & types.MsgSubmitGuessResponse {Title : "Correct" , Body : "You Guessed The Wordle Correctly!" }, nil
87+ }
1788}
0 commit comments