Example #1
0
def select(current_part, current_slot):
    filename = "affix_" + current_part + ".yml"
    parts = yaml.load(open(filename))
    part_choices = []
    for part in parts:
        if part['slot'] == current_slot:
            part_choices.append(part)
    part_group = helpers.weighted_choice(part_choices)
    specific = helpers.weighted_choice(part_group['tiers'])
    roll = random.uniform(specific['min'], specific['max'])

    return dict(name=specific['name'],
                part=current_part,
                slot=current_slot,
                effect_name=part_group['effect_name'],
                effect_text=part_group['effect_text'],
                roll=roll)
Example #2
0
    def double(self, player):
        r = self.runners
        outs_to_add = 0
        runners_scored = []  # list of players who scored

        # Runner on third scores
        if r[3]:
            runners_scored.append(r[3])
            r[3] = None

        # Runner on second scores (98.4% certain Levitt 1999)
        if r[2]:
            runners_scored.append(r[2])
            r[2] = None

        # Runner on first has three possible outcomes based on Levitt (1999)
        # 1) 1-3 536
        # 2) 1-h 433
        # 4) 1-x 31
        if r[1]:
            prob_dic = {"1-3": 536, "1-h": 433, "1-x": 31}
            event = weighted_choice(prob_dic)
            if event == "1-3":
                r[3] = r[1]
                r[1] = None
            elif event == "1-h":
                runners_scored.append(r[1])
                r[1] = None
            elif event == "1-x":
                r[1] = None
                outs_to_add += 1
            else:
                assert False, "double: Shouldn't get here"

        # Hitter now on second
        r[2] = player

        # Updates to game state
        score_to_add = len(runners_scored)
        self.score += score_to_add
        self.outs += outs_to_add
        self.total_hits += 1

        # Updates to player stats
        player.incr_stats_obj("AB", 1)
        player.incr_stats_obj("H", 1)
        player.incr_stats_obj("2B", 1)
        player.incr_stats_obj("RBI", score_to_add)

        # Updates to runner stats
        for runner in runners_scored:
            runner.incr_stats_obj("R", 1)
Example #3
0
    def double(self, player):
        r = self.runners
        outs_to_add = 0
        runners_scored = []  # list of players who scored

        # Runner on third scores
        if r[3]:
            runners_scored.append(r[3])
            r[3] = None

        # Runner on second scores (98.4% certain Levitt 1999)
        if r[2]:
            runners_scored.append(r[2])
            r[2] = None

        # Runner on first has three possible outcomes based on Levitt (1999)
        # 1) 1-3 536
        # 2) 1-h 433
        # 4) 1-x 31
        if r[1]:
            prob_dic = {"1-3": 536, "1-h": 433, "1-x": 31}
            event = weighted_choice(prob_dic)
            if event == "1-3":
                r[3] = r[1]
                r[1] = None
            elif event == "1-h":
                runners_scored.append(r[1])
                r[1] = None
            elif event == "1-x":
                r[1] = None
                outs_to_add += 1
            else:
                assert False, "double: Shouldn't get here"

        # Hitter now on second
        r[2] = player

        # Updates to game state
        score_to_add = len(runners_scored)
        self.score += score_to_add
        self.outs += outs_to_add
        self.total_hits += 1

        # Updates to player stats
        player.incr_stats_obj("AB", 1)
        player.incr_stats_obj("H", 1)
        player.incr_stats_obj("2B", 1)
        player.incr_stats_obj("RBI", score_to_add)

        # Updates to runner stats
        for runner in runners_scored:
            runner.incr_stats_obj("R", 1)
Example #4
0
    def play_inning(self):
        """
        Plays (or finishes) current inning of game i.e. until max_outs is reached
        """
        while (self.outs < self.max_outs):
            current_hitter = self.lineup.pop(0)
            self.lineup.append(current_hitter)

            event = weighted_choice(current_hitter.get_attr_obj().get_attr_dic())
            self.event_handler(event, current_hitter)

            if self.live_update:
                self.play_by_play(event, current_hitter)

        self.inning += 1
        self.outs = 0
        self.runners = {1: None, 2: None, 3: None}
Example #5
0
    def play_inning(self):
        """
        Plays (or finishes) current inning of game i.e. until max_outs is reached
        """
        while (self.outs < self.max_outs):
            current_hitter = self.lineup.pop(0)
            self.lineup.append(current_hitter)

            event = weighted_choice(
                current_hitter.get_attr_obj().get_attr_dic())
            self.event_handler(event, current_hitter)

            if self.live_update:
                self.play_by_play(event, current_hitter)

        self.inning += 1
        self.outs = 0
        self.runners = {1: None, 2: None, 3: None}
Example #6
0
def best(s, regenf, canRegen, scoref):
    niches = {}
    verb = h.getV(s)
    root = Node(s, Settings(regenf, canRegen))
    root.score = scoref([s])[0]
    ni = Niche(verb, root)
    niches[verb] = ni
    while True:
        #print "--------------------------------"
        children = []
        allDead = True
        for k in niches:
            n = niches[k]
            if not n.isDead:
                allDead = False
                children += n.step()
        if allDead and not children:
            break
        if not children:
            continue
        raw = [" ".join(c.words) for c in children]
        #speed up by preventing generation of stories with verbs that match stale > strikes or intrans niches!
        scores = scoref(raw)
        for i, child in enumerate(children):
            child.score = scores[i]
            v = h.getV(child.s)
            if v not in niches:
                ni2 = Niche(v, child)
                niches[v] = ni2
            else:
                niches[v].push(child)
    choices = []
    for v in niches:
        n = niches[v]
        if not n.intrans:
            continue
        print n.bestch.s, n.bestsc
        choices.append((n.bestch, n.bestsc))
    m = min([c[1] for c in choices])
    if m >= 0:
        m = 0
    i = h.weighted_choice(choices, -m)
    best = choices[i][0]
    return best.s, best.score
Example #7
0
    def single(
            self,
            player):  # DANGER, small bug can potentially result in 2 outs...
        r = self.runners
        outs_to_add = 0
        runners_scored = []  # list of players who scored

        # Runner on third scores (99% certain Levitt 1999)
        if r[3]:
            runners_scored.append(r[3])
            r[3] = None

        # Runner on second
        if r[2]:
            prob_dic = {"2-2": 12, "2-3": 299, "2-h": 653, "2-x": 36}
            event = weighted_choice(prob_dic)
            if event == "2-2":
                r[2] = r[2]
            elif event == "2-3":
                r[3] = r[2]
                r[2] = None
            elif event == "2-h":
                runners_scored.append(r[2])
                r[2] = None
            elif event == "2-x":
                r[2] = None
                outs_to_add += 1
            else:
                assert False, "single: Shouldn't get here"

        # Runner on first
        if r[1]:
            prob_dic = {"1-2": 652, "1-3": 313, "1-h": 14, "1-x": 21}
            event = weighted_choice(prob_dic)
            if event == "1-2":
                r[2] = r[1]
                r[1] = None
            elif event == "1-3":
                r[3] = r[1]
                r[1] = None
            elif event == "1-h":
                runners_scored.append(r[1])
                r[1] = None
            elif event == "1-x":
                r[1] = None
                outs_to_add += 1
            else:
                assert False, "single: Shouldn't get here"

        # Hitter now on first
        r[1] = player

        # Updates to game state
        score_to_add = len(runners_scored)
        self.score += score_to_add
        self.outs += outs_to_add
        self.total_hits += 1

        # Updates to player stats
        player.incr_stats_obj("AB", 1)
        player.incr_stats_obj("H", 1)
        player.incr_stats_obj("1B", 1)
        player.incr_stats_obj("RBI", score_to_add)

        # Updates to runner stats
        for runner in runners_scored:
            runner.incr_stats_obj("R", 1)
Example #8
0
def pickOne(l):
    i = h.weighted_choice(l)
    return l.pop(i)[0]
Example #9
0
def best(stories, regenf, canRegen, scoref, fraw, norm=False):
    if type(stories) != list:
        stories = [stories]
    species = {}
    seedi = fraw['root']['index']

    bad = True
    for s in stories:
        if not s:
            continue
        seed = getIndex(s, seedi)
        root = Node(s, Settings(regenf, canRegen))
        if root.isbad:
            break
        root.score = scoref([s])[0]
        species[seed] = Species(seed, root)
        bad = False
    if bad:
        print "Refiner got no stories!"
        return None

    while True:
        #print "--------------------------------"
        children = []
        allDead = True
        for k in sorted(species.keys(),
                        key=lambda x: species[x].bestsc,
                        reverse=True)[:maxSpecies]:
            p = species[k]
            if not p.isDead:
                allDead = False
                children += p.step()
        if allDead and not children:
            break
        if not children:
            continue
        #print "Num species, children:",len(species.keys()),",",len(children)
        #raw = [h.strip(c.s) for c in children]
        scores = scoref([c.s for c in children])
        for i, child in enumerate(children):
            child.score = scores[i]
            k = getIndex(child.s, seedi)
            if k not in species:
                ni2 = Species(k, child)
                species[k] = ni2
            else:
                species[k].push(child)
        #print len(species)

    lowest = 1000
    highest = -1000
    choices = []
    for k in species:
        p = species[k]
        if p.lowsc < lowest:
            lowest = p.lowsc
        if p.bestsc > highest:
            highest = p.bestsc
        #print p.bestch.s,p.bestsc
        assert p.bestch.score == p.bestsc
        choices.append((p.bestch.s, p.bestsc))
        if p.secondch:  #balance between not letting good stories getting buried under slightly better stories and minimizing species score bias
            choices.append((p.secondch.s, p.secondch.score))

    if norm:
        choices = [(s, h.rangify(c, lowest, highest, 0, 1))
                   for s, c in choices]

    choices = sorted(choices, key=lambda x: x[1], reverse=True)[:maxSpecies]
    if False:
        for s, c in choices:
            print s, c
    m = min([c[1] for c in choices])
    if m >= 0:
        m = 0
    i = h.weighted_choice(choices, -m)
    return choices[i] + (choices, )
Example #10
0
from gun import Gun
import random
import yaml
import helpers

makers = yaml.load(open("makers.yml"))
guntypes = yaml.load(open("guntypes.yml"))
qualities = yaml.load(open("qualities.yml"))

manufacturer = random.choice(makers)
guntype = random.choice(guntypes)
quality = helpers.weighted_choice(qualities)

chome = Gun(guntype, manufacturer, quality)

nice_damage = chome.damage + (chome.damage * chome.nice_multiplier)
expected_damage = (chome.damage *
                   (1 - chome.nice_chance)) + (nice_damage * chome.nice_chance)
time_to_magdump = chome.magazine_size / chome.fire_rate
damage_per_mag = expected_damage * chome.magazine_size
DPS_per_mag = damage_per_mag / time_to_magdump

percent_affix_format = "{0:.2f}% {1}"
raw_affix_format = "{0:.2f} {1}"

print("")
print(chome.display_name)
print("Manufacturer: ", chome.manufacturer['name'])
print("Damage per bullet: ", chome.damage)
print("Bullets per magazine: ", chome.magazine_size)
print("Bullets fired per second: ", chome.fire_rate)
Example #11
0
def pickOne(l):
	i = h.weighted_choice(l)
	return l[i][0]
Example #12
0
    def single(self, player):  # DANGER, small bug can potentially result in 2 outs...
        r = self.runners
        outs_to_add = 0
        runners_scored = []  # list of players who scored

        # Runner on third scores (99% certain Levitt 1999)
        if r[3]:
            runners_scored.append(r[3])
            r[3] = None

        # Runner on second
        if r[2]:
            prob_dic = {"2-2": 12, "2-3": 299, "2-h": 653, "2-x": 36}
            event = weighted_choice(prob_dic)
            if event == "2-2":
                r[2] = r[2]
            elif event == "2-3":
                r[3] = r[2]
                r[2] = None
            elif event == "2-h":
                runners_scored.append(r[2])
                r[2] = None
            elif event == "2-x":
                r[2] = None
                outs_to_add += 1
            else:
                assert False, "single: Shouldn't get here"

        # Runner on first
        if r[1]:
            prob_dic = {"1-2": 652, "1-3": 313, "1-h": 14, "1-x": 21}
            event = weighted_choice(prob_dic)
            if event == "1-2":
                r[2] = r[1]
                r[1] = None
            elif event == "1-3":
                r[3] = r[1]
                r[1] = None
            elif event == "1-h":
                runners_scored.append(r[1])
                r[1] = None
            elif event == "1-x":
                r[1] = None
                outs_to_add += 1
            else:
                assert False, "single: Shouldn't get here"

        # Hitter now on first
        r[1] = player

        # Updates to game state
        score_to_add = len(runners_scored)
        self.score += score_to_add
        self.outs += outs_to_add
        self.total_hits += 1

        # Updates to player stats
        player.incr_stats_obj("AB", 1)
        player.incr_stats_obj("H", 1)
        player.incr_stats_obj("1B", 1)
        player.incr_stats_obj("RBI", score_to_add)

        # Updates to runner stats
        for runner in runners_scored:
            runner.incr_stats_obj("R", 1)