Skip to content

Player

Player

Bases: Deck, Beast

Source code in automation/simulator/player.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
class Player(Deck, Beast):
    def __init__(self, use_TC=True, **kwargs):
        Deck.__init__(self, use_TC)
        Beast.__init__(self, **kwargs)
        self._valid_attribs = [f.name for f in fields(self.Attribs)] + ["None"]
        self._valid_skills = [f.name for f in fields(self.Skills)] + ["None"]
        self._valid_mods = self._valid_attribs + [f.name for f in fields(self.Skills)]
        self._fatigue = 0
        self._PP_mult = 1
        self._statuses = {
            "upper_lower_save": 0,
            "upper_lower_check": 0,
            "upper_lower_next_save": 0,
            "upper_lower_next_check": 0,
            "Entangled": 0,
            "Knocked Down": 0,
            "Knocked Out": 0,
        }
        self._not_simulated = ["Blinded", "Deafened", "Enthralled", "Charmed"]
        self._CSV_LOGGING = False  # Toggled at Encounter level

    def __repr__(self):
        """Result of print(Player)"""
        output = self.Name + "\n"
        output += "TC       :%02s | pc.HP : %d/%d\n" % (
            self.TC,
            self.HP,
            self.HP_Max,
        )
        output += "Hand     :  %02d | pc.PP : %d/%d\n" % (
            len(self.hand),
            self.PP,
            self.PP_Max,
        )
        output += "Deck     :  %02d | pc.AP : %d/%d\n" % (
            len(self.cards),
            self.AP,
            self.AP_Max,
        )
        output += "Discards :  %02d | RestC : %d/%d\n" % (
            len(self.discards),
            self.RestCards,
            self.RestCards_Max,
        )
        return output

    def _apply_upper_lower(self, save_check: str, kwarg_dict: dict, skill="") -> dict:
        # TODO: change save_check to enum: save, check
        all_type = "upper_lower_" + save_check
        next_type = "upper_lower_next_" + save_check
        extra = 0
        if self._statuses.get("Entangled", False) and skill == "AGL":
            extra -= 1
        if self._statuses.get("Knocked Down", False) and skill in ["AGL", "STR"]:
            extra -= 1
        if self._statuses.get("Frozen", False) and save_check == "check":
            extra -= 1
        if self._fatigue > 0 and save_check == "save":
            extra -= 1
        elif self._fatigue > 1:
            extra -= 1
        kwarg_dict["upper_lower_int"] = (
            0 + kwarg_dict.get("upper_lower_int", 0)
            if kwarg_dict.get("upper_lower_int", False)
            else 0
            + self._statuses.get(all_type, 0)
            + self._statuses.get(next_type, 0)
            + extra
        )
        self._statuses[next_type] = 0
        return kwarg_dict

    def modify_fatigue(self, change=1, shuffle=True):
        self._fatigue += change
        if shuffle:
            self.shuffle()
        if self._fatigue >= 3 and not self._statuses.get("_fatigue3"):
            self._statuses["_fatigue3"] = True
            self.Speed = self.Speed / 2
        if self._fatigue >= 4 and not self._statuses.get("_fatigue4"):
            self._statuses["_fatigue4"] = True
            self._PP_mult = 2
        if self._fatigue >= 5 and not self._statuses.get("_fatigue5"):
            self._statuses["_fatigue5"] = True
            self._statuses["Knocked Out"] = self._statuses.get("Knocked Out", 0) + 1

    def _find_highest_stat(self, options: list) -> int:
        if not isinstance(options, list):
            return options, getattr(self.Attribs, options, 0) or getattr(
                self.Skills, options, 0
            )
        vals = {}
        for option in options:
            vals.update(
                {
                    option: getattr(self.Attribs, option, 0)
                    or getattr(self.Skills, option, 0)
                }
            )
        skill = max(vals, key=vals.get)
        return skill, vals[skill]

    def check_by_skill(self, TC: Card = None, DR: int = 3, skill: str = None, **kwargs):
        """Accepts any Skill or Attrib. Accepts any valid args of Deck.check"""
        if not TC:
            TC = Card("random")

        if not skill:
            skill = "None"
        if self._CSV_LOGGING:
            kwargs["return_val"] = True

        skill, mod = self._find_highest_stat(skill)
        new_kwargs = self._apply_upper_lower("check", kwargs, skill=skill)

        n_deck = len(self.cards)
        if abs(new_kwargs.get("upper_lower_int", 1)) > n_deck or n_deck == 0:
            # does not offer option to use fate cards to avoid fatigue
            self.modify_fatigue()
            logger.debug("Fatigue modified from check_by_skill")

        # Need to account for return_string for bot and result for checking result
        if new_kwargs.get("return_string"):
            result_string, result = self.check(TC, DR, mod=mod, **new_kwargs)
        else:
            result = self.check(TC, DR, mod=mod, **new_kwargs)

        # NOTE: draw_log doesn't know if had options
        draw_log.info(
            [
                self.id,
                "check",
                result,
                self.result_types.get(result, None),
                DR,
                skill,
                mod,
                kwargs.get("upper_lower", "n"),
                kwargs.get("draw_n", 1),
            ]
        )
        if new_kwargs.get("return_string"):
            return result_string
        return result

    def save(self, DR: int = 3, attrib="None", **kwargs):
        """Accepts any Attrib. Accepts any valid args of Deck.check"""
        if self._CSV_LOGGING:
            kwargs["return_val"] = True

        attrib, mod = self._find_highest_stat(attrib)

        assert (
            attrib in self._valid_attribs
        ), f"Could not find {attrib} in {self._valid_attribs}"
        new_kwargs = self._apply_upper_lower("save", kwargs, skill=attrib)

        n_deck = len(self.cards)
        if abs(new_kwargs.get("upper_lower_int", 1)) > n_deck or n_deck == 0:
            # does not offer option to use fate cards to avoid fatigue
            self.modify_fatigue(1)
            logger.debug("Modified fatigue from Player.save")

        # Need to account for return_string for bot and result for checking result
        if new_kwargs.get("return_string"):
            result_string, result = self.check(
                TC=self.TC, DR=DR, mod=getattr(self.Attribs, attrib, 0), **new_kwargs
            )
        else:
            result = self.check(
                TC=self.TC, DR=DR, mod=getattr(self.Attribs, attrib, 0), **new_kwargs
            )

        # NOTE: draw_log doesn't know if had options
        draw_log.info(
            [
                self.id,
                "save",
                result,
                self.result_types.get(result),
                DR,
                attrib,
                mod,
                kwargs.get("upper_lower", "n"),
                kwargs.get("draw_n", 1),
            ]
        )

        if new_kwargs.get("return_string"):
            return result_string
        if new_kwargs.get("return_val"):
            return result

    def full_rest(self, return_string=False, **_):
        rest_log.info(
            [
                self.id,
                "before",
                "full",
                len(self.discards),
                len(self.hand),
                self.HP,
                self.AP,
                self.PP,
                self.RestCards,
            ]
        )
        self.shuffle()
        for i in ["HP", "PP", "AP", "RestCards", "Speed"]:
            setattr(self, i, getattr(self, i + "_Max"))
        self._statuses = {}
        self._fatigue = 0  # TODO: Check rules. Fatigue 0 on full rest?
        rest_log.info(
            [
                self.id,
                "after",
                "full",
                len(self.discards),
                len(self.hand),
                self.HP,
                self.AP,
                self.PP,
                self.RestCards,
            ]
        )
        if return_string:
            return f"{self.Name} fully rested."

    def quick_rest(self, return_string=False, **kwargs):
        # Never uses Fate cards here
        rest_log.info(
            [
                self.id,
                "before",
                "quick",
                len(self.discards),
                len(self.hand),
                self.HP,
                self.AP,
                self.PP,
                self.RestCards,
            ]
        )
        point_total = 0  # Recover HP/PP
        while self.RestCards > 0 and (
            (self.HP < self.HP_Max) or (self.PP < self.PP_Max)
        ):  # Will always fully recover with available rest cards
            draw = self.save(return_val=True, **kwargs)
            if not draw:
                break
            points = 2 if draw > 0 else 1
            point_total += points
            logger.debug(f"Recovering {points} with cards")
            for _ in range(points):  # Prioritizes 'where am I missing more?'
                attr_diffs = {
                    "HP": abs(self.HP - self.HP_Max),
                    "PP": abs(self.PP - self.PP_Max),
                }
                increment_this = max(attr_diffs, key=attr_diffs.get)
                setattr(self, increment_this, getattr(self, increment_this) + 1)
                logger.debug(
                    f"   1 {increment_this} to {getattr(self,increment_this,'?')}"
                )
            self.RestCards -= 1
        rest_log.info(
            [
                self.id,
                "after",
                "quick",
                len(self.discards),
                len(self.hand),
                self.HP,
                self.AP,
                self.PP,
                self.RestCards,
            ]
        )

        AP_check_mod = max([self.Skills.Knowledge, self.Skills.Craft])
        down_AP = self.AP_Max - self.AP
        while down_AP > 0:
            draw = self.check(
                TC=self.TC,
                DR=max(0, 7 - down_AP),  # of below 7, 0
                mod=AP_check_mod,
                return_val=True,
                **kwargs,
            )
            if not draw:
                break
            elif draw < 0:
                down_AP -= 1  # Try again with recovering one less
            else:
                self.AP += down_AP
                logger.debug(f"Recovering {down_AP} AP to {self.AP}")
                point_total += down_AP
                break

        result = f"{self.Name} recovered {point_total} HP/PP/AP during Quick Rest"
        self.shuffle(limit=(10 + self.Attribs.VIT * 2))
        if return_string:
            return result
        logger.info(result)

    def wound(self, wound_val, bypass_HP=False):
        for _ in range(wound_val):
            if self.AP > 0 and not bypass_HP:
                self.AP -= 1
            else:
                self.HP -= 1
        if self.HP <= 0:
            if self._statuses.get("Knocked Out"):
                logger.info(
                    f"{self.Name} attacked again while KO. "
                    + "Epic Event not simulated."
                )
            else:
                self._statuses["Knocked Out"] = 1
        self._shake_status(["Charmed", "Enthralled"])

    def take_action(self, type="Major") -> Power:
        if self.HP <= 0:
            return
        if self._statuses.get("Stunned") and type == "Minor":
            logger.info(f"{self.Name} stunned, skipping Minor Action.")
            return
        if self._statuses.get("Burned") and type == "Minor":
            self._shake_status(["Burned"])
            return
        if self._statuses.get("Entangled") and type == "Major":
            self._shake_status(["Entangled"])
            return
        options = [
            p
            for p in self.Powers.values()
            if p is not None
            and p.Type == type
            and ensure_list(p.PP)[0] <= self.PP * self._PP_mult
        ]
        choice = random.choice(options) if options else None
        if choice:
            logger.info(
                f"{self.Name} used {ensure_list(choice.PP)[0]}/{self.PP} PP "
                + f"with {choice.Name}"
            )
        else:
            logger.info(f"{self.Name} no {type} choices")
        self.PP -= ensure_list(getattr(choice, "PP", 0))[0] * self._PP_mult
        return choice

    def start_turn(self):
        if self._statuses.get("Knocked Down"):
            logger.info(f"{self.Name} gets up")
            self._statuses["Knocked Down"] = 0
        self._shake_status(["Stunned", "Poisoned"])

    def end_turn(self):
        self._shake_status(["Frozen", "Suffocating"])

    def _pass(self, *args, **kwargs):
        pass  # Intentionally empty for statuses with no consequence

    def _shake_status(self, statuses: list):
        status_dict = {
            "Stunned": {"attrib": "CON", "fail": self._pass, "succeed": self._pass},
            "Poisoned": {"attrib": "VIT", "fail": self.discard, "succeed": self._pass},
            "Frozen": {"attrib": "STR", "fail": self._pass, "succeed": self._pass},
            "Suffocating": {"attrib": "VIT", "fail": self.wound, "succeed": self.wound},
            "Burned": {"attrib": "GUT", "fail": self.wound, "succeed": self._pass},
            "Entangled": {"attrib": "STR", "fail": self._pass, "succeed": self._pass},
        }
        for status in statuses:
            if self._statuses.get(status):
                if self.save(attrib=status_dict[status]["attrib"], return_val=True) > 0:
                    logger.info(f"{self.Name} shakes off {status}")
                    self._statuses[status] = 0
                    status_dict[status]["succeed"](
                        self._statuses[status], bypass_HP=True
                    )
                else:
                    logger.info(f"{self.Name} remains {status}")
                    status_dict[status]["fail"](
                        self._statuses[status] + 1, bypass_HP=True
                    )

__repr__()

Result of print(Player)

Source code in automation/simulator/player.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __repr__(self):
    """Result of print(Player)"""
    output = self.Name + "\n"
    output += "TC       :%02s | pc.HP : %d/%d\n" % (
        self.TC,
        self.HP,
        self.HP_Max,
    )
    output += "Hand     :  %02d | pc.PP : %d/%d\n" % (
        len(self.hand),
        self.PP,
        self.PP_Max,
    )
    output += "Deck     :  %02d | pc.AP : %d/%d\n" % (
        len(self.cards),
        self.AP,
        self.AP_Max,
    )
    output += "Discards :  %02d | RestC : %d/%d\n" % (
        len(self.discards),
        self.RestCards,
        self.RestCards_Max,
    )
    return output

check_by_skill(TC=None, DR=3, skill=None, **kwargs)

Accepts any Skill or Attrib. Accepts any valid args of Deck.check

Source code in automation/simulator/player.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def check_by_skill(self, TC: Card = None, DR: int = 3, skill: str = None, **kwargs):
    """Accepts any Skill or Attrib. Accepts any valid args of Deck.check"""
    if not TC:
        TC = Card("random")

    if not skill:
        skill = "None"
    if self._CSV_LOGGING:
        kwargs["return_val"] = True

    skill, mod = self._find_highest_stat(skill)
    new_kwargs = self._apply_upper_lower("check", kwargs, skill=skill)

    n_deck = len(self.cards)
    if abs(new_kwargs.get("upper_lower_int", 1)) > n_deck or n_deck == 0:
        # does not offer option to use fate cards to avoid fatigue
        self.modify_fatigue()
        logger.debug("Fatigue modified from check_by_skill")

    # Need to account for return_string for bot and result for checking result
    if new_kwargs.get("return_string"):
        result_string, result = self.check(TC, DR, mod=mod, **new_kwargs)
    else:
        result = self.check(TC, DR, mod=mod, **new_kwargs)

    # NOTE: draw_log doesn't know if had options
    draw_log.info(
        [
            self.id,
            "check",
            result,
            self.result_types.get(result, None),
            DR,
            skill,
            mod,
            kwargs.get("upper_lower", "n"),
            kwargs.get("draw_n", 1),
        ]
    )
    if new_kwargs.get("return_string"):
        return result_string
    return result

save(DR=3, attrib='None', **kwargs)

Accepts any Attrib. Accepts any valid args of Deck.check

Source code in automation/simulator/player.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def save(self, DR: int = 3, attrib="None", **kwargs):
    """Accepts any Attrib. Accepts any valid args of Deck.check"""
    if self._CSV_LOGGING:
        kwargs["return_val"] = True

    attrib, mod = self._find_highest_stat(attrib)

    assert (
        attrib in self._valid_attribs
    ), f"Could not find {attrib} in {self._valid_attribs}"
    new_kwargs = self._apply_upper_lower("save", kwargs, skill=attrib)

    n_deck = len(self.cards)
    if abs(new_kwargs.get("upper_lower_int", 1)) > n_deck or n_deck == 0:
        # does not offer option to use fate cards to avoid fatigue
        self.modify_fatigue(1)
        logger.debug("Modified fatigue from Player.save")

    # Need to account for return_string for bot and result for checking result
    if new_kwargs.get("return_string"):
        result_string, result = self.check(
            TC=self.TC, DR=DR, mod=getattr(self.Attribs, attrib, 0), **new_kwargs
        )
    else:
        result = self.check(
            TC=self.TC, DR=DR, mod=getattr(self.Attribs, attrib, 0), **new_kwargs
        )

    # NOTE: draw_log doesn't know if had options
    draw_log.info(
        [
            self.id,
            "save",
            result,
            self.result_types.get(result),
            DR,
            attrib,
            mod,
            kwargs.get("upper_lower", "n"),
            kwargs.get("draw_n", 1),
        ]
    )

    if new_kwargs.get("return_string"):
        return result_string
    if new_kwargs.get("return_val"):
        return result