Skip to content

Commit

Permalink
function passed using references
Browse files Browse the repository at this point in the history
  • Loading branch information
iamananya committed Jun 1, 2023
1 parent ac0fe17 commit 977f886
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions pkg/controllers/gacha-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
}

0 comments on commit 977f886

Please sign in to comment.