Skip to content

Commit d939b18

Browse files
author
nashqueue
committed
added submit wordle keeper
1 parent f299ffe commit d939b18

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

x/wordle/keeper/msg_server_submit_wordle.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,58 @@ package keeper
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/hex"
7+
"time"
8+
"unicode"
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"
813
)
914

1015
func (k msgServer) SubmitWordle(goCtx context.Context, msg *types.MsgSubmitWordle) (*types.MsgSubmitWordleResponse, error) {
1116
ctx := sdk.UnwrapSDKContext(goCtx)
17+
// Check to See the Wordle is 5 letters
18+
if len(msg.Word) != 5 {
19+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Wordle Must Be A 5 Letter Word")
20+
}
21+
// Check to See Only Alphabets Are Passed for the Wordle
22+
if !(IsLetter(msg.Word)) {
23+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Wordle Must Only Consist Of Letters In The Alphabet")
24+
}
1225

13-
// TODO: Handling the message
14-
_ = ctx
26+
// Use Current Day to Create The Index of the Newly-Submitted Wordle of the Day
27+
currentTime := time.Now().Local()
28+
var currentTimeBytes = []byte(currentTime.Format("2006-01-02"))
29+
var currentTimeHash = sha256.Sum256(currentTimeBytes)
30+
var currentTimeHashString = hex.EncodeToString(currentTimeHash[:])
31+
// Hash The Newly-Submitted Wordle of the Day
32+
var submittedSolutionHash = sha256.Sum256([]byte(msg.Word))
33+
var submittedSolutionHashString = hex.EncodeToString(submittedSolutionHash[:])
1534

35+
var wordle = types.Wordle{
36+
Index: currentTimeHashString,
37+
Word: submittedSolutionHashString,
38+
Submitter: msg.Creator,
39+
}
40+
41+
// Try to Get Wordle From KV Store Using Current Day as Key
42+
// This Helps ensure only one Wordle is submitted per day
43+
_, isFound := k.GetWordle(ctx, currentTimeHashString)
44+
if isFound {
45+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Wordle of the Day is Already Submitted")
46+
}
47+
// Write Wordle to KV Store
48+
k.SetWordle(ctx, wordle)
1649
return &types.MsgSubmitWordleResponse{}, nil
1750
}
51+
52+
func IsLetter(s string) bool {
53+
for _, r := range s {
54+
if !unicode.IsLetter(r) {
55+
return false
56+
}
57+
}
58+
return true
59+
}

0 commit comments

Comments
 (0)