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


Python SimpleWebSocketServer.finished方法代码示例

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


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

示例1: WebViewer

# 需要导入模块: from SimpleWebSocketServer import SimpleWebSocketServer [as 别名]
# 或者: from SimpleWebSocketServer.SimpleWebSocketServer import finished [as 别名]
class WebViewer(Viewer):

  def __init__(self):
    self.running = False
    self.host_address = ''
    self.port = 8500
    self.server = SimpleWebSocketServer(self.host_address, self.port, SimpleMessager)
    self.game = None

  def init_viewer(self, board, game=None):
    self.board = board
    if not self.game:
      self.game = game
      self.server.game = self.game
    if game:
      if type(self.game.agents[0]) == WebViewer and type(self.game.agents[1]) == WebViewer:
        self.server.configuration = CONFIG_HvH
      elif type(self.game.agents[0]) == WebViewer and type(self.game.agents[1]) != WebViewer:
        self.server.configuration = CONFIG_HvA
      elif type(self.game.agents[0]) != WebViewer and type(self.game.agents[1]) == WebViewer:
        self.server.configuration = CONFIG_AvH
      elif type(self.game.agents[0]) != WebViewer and type(self.game.agents[1]) != WebViewer:
        self.server.configuration = CONFIG_AvA
    else:
      self.server.configuration = CONFIG_R
    if not connectedEvent.is_set():
      self.serverThread = threading.Thread(target=self.server.serveforever)
      self.serverThread.start()
      connectedEvent.wait()

  def run(self):
    """Launch the GUI."""
    if self.running:
      return
    self.running = True
    #self.server.serveforever()

  def replay(self, trace, speed, show_end=False):
    """Replay a game given its saved trace.

    Attributes:
    trace -- trace of the game
    speed -- speed scale of the replay
    show_end -- start with the final state instead of the initial state

    """
    self.trace = trace
    self.speed = speed
    # generate all boards to access them backwards
    self.boards = [trace.get_initial_board()]
    for step in range(len(trace.actions)):
      player, action, t = trace.actions[step]
      b = self.boards[-1].clone()
      b.play_action(action)
      self.boards.append(b)
    self.step = 0
    self.server.initialize_replay(self.trace, self.speed, self.boards)
    self.init_viewer(self.boards[0], None)

  def close_sig_handler(signal, frame):
    self.server.close()
    sys.exit()

  def playing(self, step, player):
    print("Step", step, "- Player", player, "is playing")

  def update(self, step, action, player):
    print("Step", step, "- Player", player, "has played", action)
    acknowledgementEvent.clear()
    self.server.update(step, action, player)
    acknowledgementEvent.wait()

  def play(self, percepts, player, step, time_left):
    try:
      hasPlayedEvent.clear()
      self.server.play(percepts, player, step, time_left)
      hasPlayedEvent.wait()
    except EOFError:
      exit(1)
    try:
      return lastActionPlayed
    except (ValueError, AssertionError):
      pass

  def finished(self, steps, winner, reason=""):
    if winner == 0:
      print("Draw game")
    else:
      print("Player 1" if winner > 0 else "Player 2", "has won!")
    if reason:
      print("Reason:", reason)
    self.server.initialize_replay(self.game.trace, 1.0, [])
    self.server.finished(steps, winner, reason)

  signal.signal(signal.SIGINT, close_sig_handler)
  signal.signal(signal.SIGTERM, close_sig_handler)
开发者ID:BenJneB,项目名称:Avalam,代码行数:98,代码来源:gui.py


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