** "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 **.
#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.
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
#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 **
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