# -*- coding: UTF-8 -*-
from Action import *


# 连接游戏服务器
class ConnectAction(BaseAction):
    def __init__(self, client, timeout=0):
        self.server_ip = ''
        self.server_port = 0
        self.client = client
        self.time_out = timeout  # 超时时间, 0-不超时, 单位: 秒
        self.action_name = '连接游戏服务器'  # 任务名称
        self.state = ActionState.none  # 状态
        self.time_begin = 0  # 任务开始时间
        self.failed_continue = True  # 任务失败是否执行下一个任务,用于某些非重要任务

    def do(self):
        if self.server_ip is None or self.server_ip == '':
            self.on_failed("未设置服务器ip,port")
        else:
            self.client.connect_server(self.server_ip, self.server_port)
            self.on_begin()

    def update(self):
        if super(ConnectAction, self).update():
            return True

        state = self.client.client_state
        if state == ClientState.connected:
            self.on_success()
        return False
