From 7f775913fc7cf9b9934e5d75fe86623c6671befb Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 10 Jun 2024 15:55:07 +0200 Subject: [PATCH 1/2] fix(#232): Fixed boosterpack opening seeding --- .../keeper/msg_server_open_booster_pack.go | 115 +++++++++--------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/x/cardchain/keeper/msg_server_open_booster_pack.go b/x/cardchain/keeper/msg_server_open_booster_pack.go index a27a9e41..b06407df 100644 --- a/x/cardchain/keeper/msg_server_open_booster_pack.go +++ b/x/cardchain/keeper/msg_server_open_booster_pack.go @@ -1,74 +1,75 @@ package keeper import ( - "context" - "math/rand" - "slices" + "context" + "math/rand" + "slices" - sdkerrors "cosmossdk.io/errors" - "github.com/DecentralCardGame/Cardchain/x/cardchain/types" - sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "cosmossdk.io/errors" + "github.com/DecentralCardGame/Cardchain/x/cardchain/types" + sdk "github.com/cosmos/cosmos-sdk/types" ) func (k msgServer) OpenBoosterPack(goCtx context.Context, msg *types.MsgOpenBoosterPack) (*types.MsgOpenBoosterPackResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) + ctx := sdk.UnwrapSDKContext(goCtx) - rand.Seed(ctx.BlockTime().Unix()) + rand.Seed(ctx.BlockTime().Unix()) - creator, err := k.GetUserFromString(ctx, msg.Creator) - if err != nil { - return nil, sdkerrors.Wrap(types.ErrUserDoesNotExist, err.Error()) - } + creator, err := k.GetUserFromString(ctx, msg.Creator) + if err != nil { + return nil, sdkerrors.Wrap(types.ErrUserDoesNotExist, err.Error()) + } - if uint64(len(creator.BoosterPacks)) <= msg.BoosterPackId { - return nil, sdkerrors.Wrapf( - types.ErrBoosterPack, - "BoosterPackId %d is not in list length %d", - msg.BoosterPackId, - len(creator.BoosterPacks), - ) - } + if uint64(len(creator.BoosterPacks)) <= msg.BoosterPackId { + return nil, sdkerrors.Wrapf( + types.ErrBoosterPack, + "BoosterPackId %d is not in list length %d", + msg.BoosterPackId, + len(creator.BoosterPacks), + ) + } + rand.Seed(ctx.BlockTime().Unix() + int64(len(creator.BoosterPacks))) - var ( - cardsList []uint64 - cleanedRatios [3]uint64 - ) - pack := creator.BoosterPacks[msg.BoosterPackId] - set := k.Sets.Get(ctx, pack.SetId) + var ( + cardsList []uint64 + cleanedRatios [3]uint64 + ) + pack := creator.BoosterPacks[msg.BoosterPackId] + set := k.Sets.Get(ctx, pack.SetId) - for idx, ratio := range pack.DropRatiosPerPack { - if len(set.Rarities[idx+2].R) == 0 { - cleanedRatios[idx] = 0 - } else { - cleanedRatios[idx] = ratio - } - } + for idx, ratio := range pack.DropRatiosPerPack { + if len(set.Rarities[idx+2].R) == 0 { + cleanedRatios[idx] = 0 + } else { + cleanedRatios[idx] = ratio + } + } - for idx, num := range pack.RaritiesPerPack { - for i := 0; i < int(num); i++ { - if idx != 2 { - cardsList = append(cardsList, set.Rarities[idx].R[rand.Intn(len(set.Rarities[idx].R))]) - } else { - res := uint64(rand.Intn(int(cleanedRatios[0] + cleanedRatios[1] + cleanedRatios[2]))) - j := 4 - if res < cleanedRatios[0] { - j = 2 - } else if res < cleanedRatios[0]+cleanedRatios[1] { - j = 3 - } - cardsList = append(cardsList, set.Rarities[j].R[rand.Intn(len(set.Rarities[j].R))]) - } - } - } + for idx, num := range pack.RaritiesPerPack { + for i := 0; i < int(num); i++ { + if idx != 2 { + cardsList = append(cardsList, set.Rarities[idx].R[rand.Intn(len(set.Rarities[idx].R))]) + } else { + res := uint64(rand.Intn(int(cleanedRatios[0] + cleanedRatios[1] + cleanedRatios[2]))) + j := 4 + if res < cleanedRatios[0] { + j = 2 + } else if res < cleanedRatios[0]+cleanedRatios[1] { + j = 3 + } + cardsList = append(cardsList, set.Rarities[j].R[rand.Intn(len(set.Rarities[j].R))]) + } + } + } - creator.Cards = append(creator.Cards, cardsList...) - creator.BoosterPacks = slices.Delete( - creator.BoosterPacks, - int(msg.BoosterPackId), - int(msg.BoosterPackId+1), - ) + creator.Cards = append(creator.Cards, cardsList...) + creator.BoosterPacks = slices.Delete( + creator.BoosterPacks, + int(msg.BoosterPackId), + int(msg.BoosterPackId+1), + ) - k.SetUserFromUser(ctx, creator) + k.SetUserFromUser(ctx, creator) - return &types.MsgOpenBoosterPackResponse{CardIds: cardsList}, nil + return &types.MsgOpenBoosterPackResponse{CardIds: cardsList}, nil } From 91bb56eb823280fd56b9b0edf513923fafa5345d Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 10 Jun 2024 15:57:54 +0200 Subject: [PATCH 2/2] fix(#232): Removed old rand.Seed --- x/cardchain/keeper/msg_server_open_booster_pack.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/cardchain/keeper/msg_server_open_booster_pack.go b/x/cardchain/keeper/msg_server_open_booster_pack.go index b06407df..b9055102 100644 --- a/x/cardchain/keeper/msg_server_open_booster_pack.go +++ b/x/cardchain/keeper/msg_server_open_booster_pack.go @@ -13,8 +13,6 @@ import ( func (k msgServer) OpenBoosterPack(goCtx context.Context, msg *types.MsgOpenBoosterPack) (*types.MsgOpenBoosterPackResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - rand.Seed(ctx.BlockTime().Unix()) - creator, err := k.GetUserFromString(ctx, msg.Creator) if err != nil { return nil, sdkerrors.Wrap(types.ErrUserDoesNotExist, err.Error())