From 977f886b5155a485be856c33c90eb65cf69592dc Mon Sep 17 00:00:00 2001 From: iamananya Date: Thu, 1 Jun 2023 16:46:43 +0530 Subject: [PATCH] function passed using references --- pkg/controllers/gacha-controller.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/controllers/gacha-controller.go b/pkg/controllers/gacha-controller.go index 6e82b49..321ba7a 100644 --- a/pkg/controllers/gacha-controller.go +++ b/pkg/controllers/gacha-controller.go @@ -143,13 +143,13 @@ func HandleGachaDraw(w http.ResponseWriter, r *http.Request) { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } - characterPool := generateCharacterPool(characters) + characterPool := GenerateCharacterPool(characters) response := models.GachaDrawResponse{ Results: make([]models.CharacterResponse, reqBody.Times), } for i := 0; i < reqBody.Times; i++ { - character, err := drawCharacter(characterPool) + character, err := DrawCharacter(&characterPool) if err != nil { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return @@ -174,8 +174,8 @@ func HandleGachaDraw(w http.ResponseWriter, r *http.Request) { w.Write(respBody) } -func generateCharacterPool(characters []models.Character) []models.Character { - characterPool := make([]models.Character, 0) +func GenerateCharacterPool(characters []models.Character) []*models.Character { + characterPool := make([]*models.Character, 0) probabilityMap := make(map[uint]int) for _, character := range characters { @@ -195,7 +195,7 @@ func generateCharacterPool(characters []models.Character) []models.Character { } for characterID, probability := range probabilityMap { - character := findCharacterByID(characters, characterID) + character := FindCharacterByID(characters, characterID) for i := 0; i < probability; i++ { characterPool = append(characterPool, character) } @@ -204,31 +204,31 @@ func generateCharacterPool(characters []models.Character) []models.Character { return characterPool } -func findCharacterByID(characters []models.Character, characterID uint) models.Character { +func FindCharacterByID(characters []models.Character, characterID uint) *models.Character { for _, character := range characters { if character.ID == characterID { - return character + return &character } } - return models.Character{} + return nil } -func drawCharacter(characterPool []models.Character) (models.Character, error) { - poolSize := len(characterPool) +func DrawCharacter(characterPool *[]*models.Character) (*models.Character, error) { + poolSize := len(*characterPool) if poolSize == 0 { - return models.Character{}, errors.New("empty character pool") + return nil, errors.New("empty character pool") } // Randomly select a character from the pool randIndex, err := rand.Int(rand.Reader, big.NewInt(int64(poolSize))) if err != nil { - return models.Character{}, errors.New("failed to generate random number") + return nil, errors.New("failed to generate random number") } index := int(randIndex.Int64()) - character := characterPool[index] + character := (*characterPool)[index] // Remove the selected character from the pool - characterPool = append(characterPool[:index], characterPool[index+1:]...) + *characterPool = append((*characterPool)[:index], (*characterPool)[index+1:]...) return character, nil }