Gacha written in Python-Data design-

Contents

Consider the data design of gacha.

Data elements required for gacha

So far, we have practiced only ** ID ** and ** weight value ** as gacha data.

-Simple gacha logic written in Python -Gacha written in python-Rarity confirmed with bonus-

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

I will list the elements necessary for actually making a gacha.

The table is as follows gacha_items

id weight rarity item_name
1 1 5 UR_HOGE
2 1 5 UR_FUGA
3 9 4 SSR_HOGE
4 9 4 SSR_FUGA
5 20 3 SR_HOGE
6 20 3 SR_FUGA
7 30 2 R_HOGE
8 30 2 R_FUGA
9 40 1 N_HOGE
10 40 1 N_FUGA

--The smaller the weight, the lower the winning probability --The higher the rarity, the higher the rarity.

The correspondence table between the rarity and its name is as follows. ratiry_names

id rarity_name
5 UR
4 SSR
3 SR
2 R
1 N
gacha_items = {
    1: {"weight":1, "rarity":5, "item_name":"UR_HOGE"},
    2: {"weight":1, "rarity":5, "item_name":"UR_FUGA"},
    3: {"weight":9, "rarity":4, "item_name":"SSR_HOGE"},
    4: {"weight":9, "rarity":4, "item_name":"SSR_FUGA"},
    5: {"weight":20,"rarity":3, "item_name":"SR_HOGE"},
    6: {"weight":20,"rarity":3, "item_name":"SR_FUGA"},
    7: {"weight":30,"rarity":2, "item_name":"R_HOGE"},
    8: {"weight":30,"rarity":2, "item_name":"R_FUGA"},
    9: {"weight":40,"rarity":1, "item_name":"N_HOGE"},
    10:{"weight":40,"rarity":1, "item_name":"N_FUGA"}
}

rarity_names = {5: "UR", 4: "SSR", 3: "SR", 2: "R", 1: "N"}

Description of each dictionary

--gacha_items holds information such as weights using id as a key --rarity_names holds rare names corresponding to rarity (id)

Gacha with an extra rarity

As a bonus, I will create a gacha that allows you to specify more than rarity. (I will use the source of @shiracamus)

gacha.py


import random

def gacha(gacha_items: dict, times: int=1) -> list:
    #Create a dictionary of IDs and weights required for lottery
    lots = {key: info["weight"] for key, info in gacha_items.items()}
    return random.choices(tuple(lots), weights=lots.values(), k=times)

def gacha_omake(gacha_items: dict, times: int, omake_rarity: int) -> list:
    #Create a dictionary with rarity restrictions for the bonus target
    omake = {key: info for key, info in gacha_items.items() if info["rarity"] >= omake_rarity}
    ids = gacha(gacha_items, times)
    omake and ids.extend(gacha(omake))
    return ids

def main():
    #Gacha item information
    gacha_items = {
        1: {"weight": 1, "rarity": 5, "item_name": "UR_HOGE"},
        2: {"weight": 1, "rarity": 5, "item_name": "UR_FUGA"},
        3: {"weight": 9, "rarity": 4, "item_name": "SSR_HOGE"},
        4: {"weight": 9, "rarity": 4, "item_name": "SSR_FUGA"},
        5: {"weight": 20, "rarity": 3, "item_name": "SR_HOGE"},
        6: {"weight": 20, "rarity": 3, "item_name": "SR_FUGA"},
        7: {"weight": 30, "rarity": 2, "item_name": "R_HOGE"},
        8: {"weight": 30, "rarity": 2, "item_name": "R_FUGA"},
        9: {"weight": 40, "rarity": 1, "item_name": "N_HOGE"},
        10: {"weight": 40, "rarity": 1, "item_name": "N_FUGA"}
    }

    #Rarity name
    rarity_names = {5: "UR", 4: "SSR", 3: "SR", 2: "R", 1: "N"}

    #Number of lottery
    times = 10

    #Bonus rarity (or more)
    omake_rarity = 3

    #Get a list of ids by lottery
    ids = gacha_omake(gacha_items, times, omake_rarity)

    #Result output
    for id in ids:
        print("ID:%d, %s, %s" % (id, rarity_names[gacha_items[id]["rarity"]], gacha_items[id]["item_name"]))

if __name__ == '__main__':
    main()

Execution result

ID:8, R, R_FUGA
ID:7, R, R_HOGE
ID:5, SR, SR_HOGE
ID:5, SR, SR_HOGE
ID:5, SR, SR_HOGE
ID:3, SSR, SSR_HOGE
ID:5, SR, SR_HOGE
ID:10, N, N_FUGA
ID:5, SR, SR_HOGE
ID:9, N, N_HOGE
ID:5, SR, SR_HOGE

Consideration

Let's consider the difference from the last time. (Gacha written in python-Rarity confirmed with bonus-)

--Information is added for displaying the results --The bonus target is controlled by ** rarity value **

The minimum information required for gacha is ID and probability (weight), In addition, by adding this information, we were able to obtain ** behavior ** and ** results ** that are close to the actual gacha.

The important thing is that ** data definition (data design) ** is performed and the logic corresponding to it is described in order to realize the processing.

The gacha itself is a simple logic. However, in actual services, data design that can handle gacha operation is required, and it is no exaggeration to say that data design is everything. (Actually, these data will be stored in the DB for operation)

Recommended Posts

Gacha written in Python-Data design-
Gacha written in python-Practice 1-
Gacha written in Python -BOX gacha-
Simple gacha logic written in Python
Gacha written in python-Rarity confirmed with bonus-
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-
Design Patterns in Python: Introduction
Squid Lisp written in Python: Hy
Compatibility diagnosis program written in python