python - 当我在自己的电脑上运行游戏时如何停止脚本?
问题描述
嗨朋友们,我正在尝试制作一个蒸汽小时机器人。我在我的 ubuntu 系统上像这样在 "screen -S idle python3 xxx.py" 上运行这个机器人。我想要做的是在我的电脑上进入游戏时关闭屏幕,我希望机器人自行关闭。我使用的代码如下。
import logging
from steam.client import SteamClient
from steam.enums import EResult
from steam.enums import EFriendRelationship, EPersonaState, EChatEntryType
from steam.client import user
# setup logging
logging.basicConfig(format="%(asctime)s | %(message)s", level=logging.INFO)
LOG = logging.getLogger()
client = SteamClient()
client.set_credential_location(".") # where to store sentry files and other stuff
server_ip=client.current_server_addr
@client.on("error")
def handle_error(result):
LOG.info("Logon result: %s", repr(result))
@client.on("channel_secured")
def send_login():
if client.relogin_available:
client.relogin()
@client.on("connected")
def handle_connected():
LOG.info("Connected to %s", client.current_server_addr)
@client.on("reconnect")
def handle_reconnect(delay):
LOG.info("Out...")
LOG.info("Reconnect in %ds...", delay)
client.logout()
raise SystemExit
@client.on("disconnected")
def handle_disconnect():
LOG.info("Disconnected.")
client.disconnect()
if client.relogin_available:
LOG.info("Reconnecting...")
client.reconnect(maxdelay=30)
@client.on("logged_on")
def handle_after_logon():
LOG.info("-"*30)
LOG.info("Logged on as: %s", client.user.name)
LOG.info("Community profile: %s", client.steam_id.community_url)
LOG.info("Last logon: %s", client.user.last_logon)
LOG.info("Last logoff: %s", client.user.last_logoff)
LOG.info("-"*30)
LOG.info("Press ^C to exit")
# main bit
LOG.info("Persistent logon recipe")
LOG.info("-"*30)
try:
result = client.cli_login("username","password")
if result != EResult.OK:
LOG.info("Failed to login: %s" % repr(result))
raise SystemExit
else:
client.get_web_session("English")
client.games_played([730])
client.change_status(persona_state=EPersonaState.Online, player_name='xxx')
client.run_forever()
except KeyboardInterrupt:
if client.connected:
LOG.info("Logout")
client.logout()
我使用的图书馆文档;
https://steam.readthedocs.io/en/latest/api/steam.client.html#steam.client.SteamClient.set_credential_location
当我在自己的电脑上从 Steam 进入游戏时,它会捕捉到我的连接并继续脚本我希望它关闭。
解决方案
推荐阅读
- .net - 在 .NET 对象上设置 Enum 属性
- python - 选择查询:错误代码 1292 - 截断不正确的 DOUBLE 值
- php - 按过期日期对结果进行排序,最后过期
- python - 由 emojize 生成的表情符号不会显示在电子邮件中
- javascript - 主机“xxxx”、用户“没有 pg_hba.conf 条目
“,数据库” ", SSL 关闭 - c++ - 指针算术题(数组大小)[C++]
- regression - 相关性和系数之间的关系?
- date-formatting - 如何在弹性搜索 7.6.2 中格式化日期
- sql - 从另一个表中填充表的外键的最佳解决方案是什么?行数?
- c++ - 为什么类不能继承其父类的成员类型?