PokéLLMon 源码解析(五)(3)https://developer.aliyun.com/article/1483728
.\PokeLLMon\poke_env\ps_client\server_configuration.py
# 该模块包含与服务器配置相关的对象 from typing import NamedTuple # 定义一个名为ServerConfiguration的命名元组,表示服务器配置对象,包含两个条目:服务器URL和认证端点URL class ServerConfiguration(NamedTuple): server_url: str # 服务器URL authentication_url: str # 认证端点URL # 使用本地主机和smogon的认证端点创建一个名为LocalhostServerConfiguration的ServerConfiguration对象 LocalhostServerConfiguration = ServerConfiguration( "localhost:8000", "https://play.pokemonshowdown.com/action.php?" ) # 使用smogon的服务器和认证端点创建一个名为ShowdownServerConfiguration的ServerConfiguration对象 ShowdownServerConfiguration = ServerConfiguration( "sim.smogon.com:8000", "https://play.pokemonshowdown.com/action.php?" )
.\PokeLLMon\poke_env\ps_client\__init__.py
# 导入所需的模块和类 from poke_env.ps_client.account_configuration import AccountConfiguration from poke_env.ps_client.ps_client import PSClient from poke_env.ps_client.server_configuration import ( LocalhostServerConfiguration, ServerConfiguration, ShowdownServerConfiguration, ) # 定义 __all__ 列表,包含需要导出的模块和类 __all__ = [ "AccountConfiguration", "LocalhostServerConfiguration", "PSClient", "ServerConfiguration", "ShowdownServerConfiguration", ]
.\PokeLLMon\poke_env\stats.py
# 该模块包含与统计相关的实用函数和对象 import math from typing import List from poke_env.data import GenData # 定义将统计名称映射到索引的字典 STATS_TO_IDX = { "hp": 0, "atk": 1, "def": 2, "spa": 3, "spd": 4, "spe": 5, "satk": 3, "sdef": 4, } # 计算原始统计值的函数 def _raw_stat(base: int, ev: int, iv: int, level: int, nature_multiplier: float) -> int: """Converts to raw stat :param base: the base stat :param ev: Stat Effort Value (EV) :param iv: Stat Individual Values (IV) :param level: pokemon level :param nature_multiplier: stat multiplier of the nature (either 0.9, 1 or 1.1) :return: the raw stat """ s = math.floor( (5 + math.floor((math.floor(ev / 4) + iv + 2 * base) * level / 100)) * nature_multiplier ) return int(s) # 计算原始 HP 值的函数 def _raw_hp(base: int, ev: int, iv: int, level: int) -> int: """Converts to raw hp :param base: the base stat :param ev: HP Effort Value (EV) :param iv: HP Individual Value (IV) :param level: pokemon level :return: the raw hp """ s = math.floor((math.floor(ev / 4) + iv + 2 * base) * level / 100) + level + 10 return int(s) # 计算原始统计值的函数 def compute_raw_stats( species: str, evs: List[int], ivs: List[int], level: int, nature: str, data: GenData ) -> List[int]: """Converts to raw stats :param species: pokemon species :param evs: list of pokemon's EVs (size 6) :param ivs: list of pokemon's IVs (size 6) :param level: pokemon level :param nature: pokemon nature :return: the raw stats in order [hp, atk, def, spa, spd, spe] """ assert len(evs) == 6 assert len(ivs) == 6 base_stats = [0] * 6 # 从数据中获取种类的基础统计值 for stat, value in data.pokedex[species]["baseStats"].items(): base_stats[STATS_TO_IDX[stat]] = value nature_multiplier = [1.0] * 6 # 从数据中获取自然属性的统计值倍增器 for stat, multiplier in data.natures[nature].items(): if stat != "num": nature_multiplier[STATS_TO_IDX[stat]] = multiplier raw_stats = [0] * 6 # 如果精灵种类是"shedinja",则将生命值设为1 if species == "shedinja": raw_stats[0] = 1 # 否则,根据基础状态值、努力值、个体值和等级计算生命值 else: raw_stats[0] = _raw_hp(base_stats[0], evs[0], ivs[0], level) # 遍历除生命值外的其他五项状态值 for i in range(1, 6): # 根据基础状态值、努力值、个体值、等级和性格系数计算状态值 raw_stats[i] = _raw_stat( base_stats[i], evs[i], ivs[i], level, nature_multiplier[i] ) # 返回计算后的状态值列表 return raw_stats
.\PokeLLMon\poke_env\teambuilder\constant_teambuilder.py
"""This module defines the ConstantTeambuilder class, which is a subclass of ShowdownTeamBuilder that yields a constant team. """ # 导入Teambuilder类 from poke_env.teambuilder.teambuilder import Teambuilder # 定义ConstantTeambuilder类,继承自Teambuilder类 class ConstantTeambuilder(Teambuilder): # 初始化方法,接受一个team字符串作为参数 def __init__(self, team: str): # 如果team字符串中包含"|",则直接将其赋值给converted_team属性 if "|" in team: self.converted_team = team # 如果team字符串中不包含"|",则解析team字符串并将解析后的结果赋值给converted_team属性 else: mons = self.parse_showdown_team(team) self.converted_team = self.join_team(mons) # 返回converted_team属性的值 def yield_team(self) -> str: return self.converted_team
.\PokeLLMon\poke_env\teambuilder\teambuilder.py
"""This module defines the Teambuilder abstract class, which represents objects yielding Pokemon Showdown teams in the context of communicating with Pokemon Showdown. """ # 导入所需的模块 from abc import ABC, abstractmethod from typing import List from poke_env.stats import STATS_TO_IDX from poke_env.teambuilder.teambuilder_pokemon import TeambuilderPokemon # 定义 Teambuilder 抽象类 class Teambuilder(ABC): """Teambuilder objects allow the generation of teams by Player instances. They must implement the yield_team method, which must return a valid packed-formatted showdown team every time it is called. This format is a custom format described in Pokemon's showdown protocol documentation: https://github.com/smogon/pokemon-showdown/blob/master/PROTOCOL.md#team-format This class also implements a helper function to convert teams from the classical showdown team text format into the packed-format. """ @abstractmethod def yield_team(self) -> str: """Returns a packed-format team.""" @staticmethod @staticmethod def join_team(team: List[TeambuilderPokemon]) -> str: """Converts a list of TeambuilderPokemon objects into the corresponding packed showdown team format. :param team: The list of TeambuilderPokemon objects that form the team. :type team: list of TeambuilderPokemon :return: The formatted team string. :rtype: str""" # 将给定的 TeambuilderPokemon 对象列表转换为对应的打包格式的 showdown 队伍格式 return "]".join([mon.formatted for mon in team])