当前位置: 首页>>代码示例>>Python>>正文


Python Session.disconnect方法代码示例

本文整理汇总了Python中session.Session.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Python Session.disconnect方法的具体用法?Python Session.disconnect怎么用?Python Session.disconnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在session.Session的用法示例。


在下文中一共展示了Session.disconnect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Bot

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import disconnect [as 别名]
class Bot(object):

    def __init__(self, game):
        self.game = game

        # core variables
        # self.running = False  # no point, is there? we have is_connected() and is_alive()
        # self.thread = None  # instances are updated by their Game or Server if no game has been found yet
        self.session = Session()
        self.buffer = Buffer()

        # game information
        self.name = 'Test'  #''.join([random.choice('0123456789abcdefghijlkmnopqrstuvwxyz') for i in range(8)])
        self.last_x = 0  # last sent mouse X coordinate
        self.last_y = 0  # last sent mouse Y coordinate
        self.view_x = 0  # viewport x
        self.view_y = 0  # viewport y
        self.view_w = 0  # viewport width
        self.view_h = 0  # viewport height

        # our state
        self.has_sent_init = False
        self.last_sent_spawn = 0
        self.last_update = 0

        # cell information
        self.ids = []  # list of ids (to get cell, query id in all_cells)
        # no longer track cell ids
        self.ladder = []
        self.mode = 'ffa'

    def connect(self, host, port):
        if not self.is_connected() and (time.time() - self.game.last_connect > 15):
            if self.session.connect(host, port):
                print('[' + self.name + '] Connected')
                # reset game variables
                self.last_x = 0
                self.last_y = 0
                self.view_x = 0
                self.view_y = 0
                self.view_w = 0
                self.view_h = 0

                # reset some more variables
                self.game.last_connect = time.time()
                self.has_sent_init = False
                self.last_sent_spawn = 0

                # clear our lists
                self.ids = []
                self.ladder = {}

                # try and become ALIIIIVE!
                self.send_init()
                self.send_spawn()
                self.send_move_relative(0, 0)  # cuz fuck moving, thats why

                self.n_updates = 0
                return True
            print('[' + self.name + '] Failed to connect')
        return False
    
    # version 520
    def disconnect(self):
        if self.is_connected():
            # disconnect
            self.session.disconnect()
            
            # remove ourselves from all cell watchers
            # in game cell objects
            for cell in self.game.cells.values():
                cell.remove_watcher(self)
            
            # remove all bot.ids from game.ids
            for id in self.ids:
                self.game.remove_id(id)
                if self.has_id(id):
                    self.remove_id(id)
                    self.game.remove_id(id)
            # game deletes all cells w/o watchers
            return True
        return False
    
    # version 520
    def update(self):
        # connect if not connected
        if not self.is_connected():
            self.connect(self.game.host, self.game.port)
            return False

        # spawn if not alive
        if not self.is_alive():
            self.send_spawn()
            # dont return: if we do, we dont parse spawn packet

        # get all data
        all = []
        all.extend(self.session.inbound)
        self.session.inbound = self.session.inbound[len(all):]

#.........这里部分代码省略.........
开发者ID:Raeon,项目名称:pygar,代码行数:103,代码来源:bot.py


注:本文中的session.Session.disconnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。