Gacha written in python-Rarity confirmed with bonus-

Contents

** "11 consecutive SR or more 1 confirmed gacha" ** In an actual social game, a gacha with a certain rarity or higher is a standard by using the cost of 10 times at a time. Based on the previous gacha logic, let's think about how to realize it. Simple gacha logic written in Python

#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}

Let's consider id_1 to 4 as the target of ** bonus rarity confirmation **.

Execute and merge in two steps

#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
#Number of lottery
times = 10

item_list = gacha(item_dic,times)

#Bonus item ID and weight dictionary for lottery
sp_item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30}
#Bonus lottery count
sp_times = 1

item_list.extend(gacha(sp_item_dic,sp_times))

This is a simple implementation method that uses the created gacha function as it is. However, as shown in the code **, redundant processing is described on the caller **. So, let's try the inclusion in the gacha function of the process.

Try modifying the gacha function

Consider what kind of information is needed as a parameter for the gacha function. As a premise, some of the items targeted for this gacha will be subject to the bonus lottery. Also, the bonus is fixed at one.

The information required for the bonus lottery is the list of target IDs and the number of lottery. First, let's simply add a parameter.

--sp_item_ids: List of IDs subject to bonus gacha

Also, so that you can execute gacha without extras Try setting the initial value for the added parameter.

def gacha(item_dic, times, sp_item_ids=[]):
    total_weight = 0
    sp_total_weight = 0
    sp_item_dic = {}
    for item_key,value in item_dic.items():
        total_weight += value
        if len(sp_item_ids) > 0:
            if item_key in sp_item_ids:
                sp_total_weight += value
                sp_item_dic[item_key] = value

    results = []
    for i in range(times):
        results.append(lottery(item_dic,total_weight))

    #Bonus gacha
    if len(sp_item_dic) > 0:
        results.append(lottery(sp_item_dic, sp_total_weight))
    return results

Execute the refurbished gacha

#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
#Number of lottery
times = 10

#List of item IDs subject to the bonus lottery
sp_item_list = ["id_1","id_2","id_3","id_4"]

item_list = gacha(item_dic, times, sp_item_list)

Similar results were obtained. However, the logic of the gacha function has become very complicated. In this case, it is simpler and cleaner to execute the original gacha separately.

** It can be said that the direction of repair was wrong **

Create a gacha function with a bonus

Undo the gacha function and Create a new function called gacha_omake.

#Gacha with bonus
def gacha_omake(item_dic, times, sp_item_ids=[]):
    item_list = gacha(item_dic, times)
    #sp_item_If ids exist, it will be a gacha with a bonus
    if len(sp_item_ids) > 0:
        sp_item_dic = {}
        for item_key,value in item_dic.items():
            if item_key in sp_item_ids:
                sp_item_dic[item_key] = value
        if len(sp_item_dic) > 0:
            item_list.extend(gacha(sp_item_dic, 1))

    return item_list

def gacha(item_dic, times):
    total_weight = 0
    for value in item_dic.values():
        total_weight += value

    results = []
    for i in range(times):
        results.append(lottery(item_dic,total_weight))

    return results
#Item ID and weight dictionary for lottery
item_dic = {"id_1":1,"id_2":5,"id_3":14,"id_4":30,"id_5":50}
#Number of lottery
times = 10

#List of item IDs subject to the bonus lottery
sp_item_list = ["id_1","id_2","id_3","id_4"]

item_list = gacha_omake(item_dic, times, sp_item_list)

Leave the gacha processing itself to the gacha function By cutting out and summarizing only the normal gacha and the process to execute the bonus gacha The function of the function has been clarified.

Recommended Posts

Gacha written in python-Rarity confirmed with bonus-
Gacha written in python-Practice 1-
Gacha written in Python-Data design-
Gacha written in Python -BOX gacha-
Simple gacha logic written in Python
Stress Test with Locust written in Python
Gacha written in python-Implementation in basic data structure-
Gacha written in python-Practice 2 ・ Basics of step-up gacha-
Gacha written in python-Practice 3 ・ Addition of step-up gacha functions-
Gacha written in python-Addition of period setting function-
Replace all flags written with short names in maya.cmds with long names
Morphological analysis using Igo + mecab-ipadic-neologd in Python (with Ruby bonus)