コード例 #1
0
def createPlayer(g, id_in_group,role):
    print("BEGIN createPlayer (g, id_in_group,role):",g, id_in_group,role)# DEM
    # create a player in group #g (object), with #number id_in_group (an integer) and
    # with role #role (#role=="PR" for PRoducer or #role=="RE" for REtailer)
    # Values for the #vouchers are assigned (they are the cost of producing for Producers
    # and the earnings for Retailers).
    # The marginal values are random with, for Retailers, average #r_average and standard deviation
    # r_sd and, for Producers, average #p_average and standard deviation
    # p_sd


    r_average=10
    r_sd=20
    r_start=1000
    r_max=60
    r_min=0

    p_average=.01
    p_sd=.5
    p_max=30
    p_min=0

    p = Player.objects.get_or_create(group=g,id_in_group=id_in_group)[0]
        # creates a player or retrieves him
    #print("CREATEPLAYER!!! for:",p)
    p.codename = ''.join([random.choice(string.ascii_uppercase) for n in range(1)] +[random.choice(string.digits) for n in range(1)])
    p.codeurl=Constants.baseurl+p.codename
        # creates the codename and the url for the player to use

    p.role=role
    print ("role",role)
        # assigns the role for the player
    p.money=10000
        # assigns some starting money to a player - NOT USED YET

    if role=="RE":
        p.name="value"
        # else it is by default "cost" - this is used for printing on the page in the templates
    p.save()
    value2=[]
    value2_cumm=[]
    valPR=0
    valRE=r_start
    random.seed()
    if role=="PR":
        for i in range(35):
            valPR+=abs(myround(random.gauss(i*i*i*p_average, (i*i*p_sd))))
            valPR=min(valPR,1200)
            value2.append(valPR)
                # creates a list with marginal values. The values are strictly increasing and convex
                # with a maximum of 1200. This construction avoids needing to sort.
            value2_cumm=cummulator(value2)
                # #value2_cumm has the cummulative valuesof value2
    elif role=="RE":
        for i in range(35):
            #value2.append(int(max(0,round(random.gauss(r_average, r_sd),-1))))  #is good, but slow
            #valRE=max(0,valRE - round(random.randint(r_min, r_max),-1))
            valRE=max(0,valRE - abs(myround(random.gauss(r_average, r_sd))))
            value2.append(valRE)
                # creates a list with marginal values. The values are strictly increasing and convex
                # with a maximum of 1200. This construction avoids needing to sort.
            value2_cumm=cummulator(value2)
                # #value2_cumm has the cummulative valuesof value2

    for i in range(35):
        v = Voucher(idd=i+1,value_cum=value2_cumm[i],value=value2[i],player=p,group=p.group,role=role)
        v.save()
        # only here all the vouchers are created, using the values put in the variables #value2 and #value2_cumm
    print("END createPlayer (g, id_in_group,role):",g, id_in_group,role)# DEM
    return p