Skip to content

Commit

Permalink
Added character alias chracter limit
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Jan 23, 2024
1 parent c00470a commit 63eeaa7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ genesis:
validators:
- name: alice
bonded: 5000000ubpf
home: "~/.cardchaind"
app:
api:
address: 0.0.0.0:1317
Expand Down
5 changes: 5 additions & 0 deletions x/cardchain/keeper/msg_server_change_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func (k msgServer) ChangeAlias(goCtx context.Context, msg *types.MsgChangeAlias)
return nil, err
}

err = checkAliasLimit(msg.Alias)
if err != nil {
return nil, err
}

user.Alias = msg.Alias

k.SetUserFromUser(ctx, user)
Expand Down
18 changes: 16 additions & 2 deletions x/cardchain/keeper/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const MAX_ALIAS_LEN = 25

// User Combines types.User and it's account address for better usability
type User struct {
types.User
Expand Down Expand Up @@ -33,9 +35,13 @@ func (k Keeper) InitUser(ctx sdk.Context, address sdk.AccAddress, alias string)
alias = "newbie"
}
newUser := types.NewUser()
err := checkAliasLimit(alias)
if err != nil {
return err
}
newUser.Alias = alias
newUser.Cards = k.GetAllStarterCards(ctx)
err := k.MintCoinsToAddr(ctx, address, sdk.Coins{sdk.NewInt64Coin("ucredits", 10000000000)})
err = k.MintCoinsToAddr(ctx, address, sdk.Coins{sdk.NewInt64Coin("ucredits", 10000000000)})
if err != nil {
return err
}
Expand Down Expand Up @@ -107,7 +113,15 @@ func (k Keeper) GetAllUsers(ctx sdk.Context) (allUsers []*types.User, allAddress
k.cdc.MustUnmarshal(iterator.Value(), &gottenUser)

allUsers = append(allUsers, &gottenUser)
allAddresses = append(allAddresses, sdk.AccAddress(iterator.Key()))
allAddresses = append(allAddresses, iterator.Key())
}
return
}

func checkAliasLimit(alias string) error {
if len(alias) > MAX_ALIAS_LEN {
return sdkerrors.Wrapf(types.InvalidAlias, "alias is too long (%d) maximum is %d", len(alias), MAX_ALIAS_LEN)
}

return nil
}
1 change: 1 addition & 0 deletions x/cardchain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ var (
ErrWaitingForPlayers = sdkerrors.Register(ModuleName, 23, "Waiting for players")
ErrUninitializedType = sdkerrors.Register(ModuleName, 24, "Type not yet initialized")
ErrFinalizeSet = sdkerrors.Register(ModuleName, 25, "Set can't be finalized")
InvalidAlias = sdkerrors.Register(ModuleName, 26, "Invalid alias")
)

0 comments on commit 63eeaa7

Please sign in to comment.