mirror of
https://github.com/0O0o0oOoO00/Alas.git
synced 2026-05-20 04:39:29 +08:00
Rft: account switch
This commit is contained in:
@@ -109,43 +109,56 @@ class Account(BaseModel):
|
||||
class AccountGroup(BaseModel):
|
||||
group_name: str
|
||||
notify: bool = False
|
||||
current_account_index: int = 0
|
||||
current_account: str = ""
|
||||
start_time: datetime = None
|
||||
accounts: List[Account] = []
|
||||
|
||||
def is_current_alas_alive(self):
|
||||
return ProcessManager.get_manager(self[self.current_account_index].name).alive
|
||||
if self.current_account:
|
||||
return ProcessManager.get_manager(self.current_account).alive
|
||||
else:
|
||||
return False
|
||||
|
||||
def start_first_alas(self):
|
||||
self.start_alas_by_index(0)
|
||||
self.start_alas_by_name(self[self.accounts[0].name])
|
||||
|
||||
def start_current_alas(self):
|
||||
self.start_alas_by_index(self.current_account_index)
|
||||
if self.current_account:
|
||||
self.start_alas_by_name(self.current_account)
|
||||
else:
|
||||
self.start_first_alas()
|
||||
|
||||
def start_alas_by_index(self, index):
|
||||
self.current_account_index = index
|
||||
def start_alas_by_name(self, name):
|
||||
self.start_time = datetime.now()
|
||||
alas_obj = ProcessManager.get_manager(self[index].name)
|
||||
self.current_account = name
|
||||
alas_obj = ProcessManager.get_manager(name)
|
||||
alas_obj.start(None, updater.event)
|
||||
|
||||
def stop_current_alas(self):
|
||||
alas_obj = ProcessManager.get_manager(self[self.current_account_index].name)
|
||||
alas_obj = ProcessManager.get_manager(self.current_account)
|
||||
alas_obj.stop()
|
||||
|
||||
def stop_current_and_start_next_alas(self):
|
||||
if self.is_current_alas_alive():
|
||||
self.stop_current_alas()
|
||||
next_index = self.current_account_index + 1
|
||||
if next_index < len(self.accounts):
|
||||
self.start_alas_by_index(next_index)
|
||||
self.start_alas_by_name(self.get_next_account().name)
|
||||
|
||||
def get_account_index_by_name(self, name):
|
||||
for i in range(len(self.accounts)):
|
||||
if self.accounts[i].name == name:
|
||||
return i
|
||||
return 0
|
||||
|
||||
def get_next_account(self):
|
||||
return self[self.get_account_index_by_name(self.current_account) + 1]
|
||||
|
||||
def stop_current_and_reset(self):
|
||||
if self.is_current_alas_alive():
|
||||
self.stop_current_alas()
|
||||
self.current_account_index = 0
|
||||
self.current_account = ""
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.accounts[item]
|
||||
return self.accounts[item % len(self.accounts)]
|
||||
|
||||
|
||||
def parse_account_data(dict_data):
|
||||
@@ -165,8 +178,6 @@ def get_account_dict(account: List[AccountGroup]):
|
||||
result = []
|
||||
for i in account:
|
||||
group_dict = i.dict()
|
||||
group_dict.pop("current_account_index")
|
||||
group_dict.pop("start_time")
|
||||
group_name = group_dict.pop("group_name")
|
||||
result.append({group_name: group_dict})
|
||||
return result
|
||||
@@ -587,17 +598,19 @@ class AlasGUI(Frame):
|
||||
while self.enable_switch:
|
||||
_now = datetime.now()
|
||||
for group in self.account_data:
|
||||
if _now.day != group.start_time.day:
|
||||
if group.start_time is not None and _now.day != group.start_time.day:
|
||||
group.stop_current_and_reset()
|
||||
|
||||
if group.current_account_index == 0:
|
||||
group.start_first_alas()
|
||||
else:
|
||||
current_index = group.current_account_index
|
||||
time_limit = timedelta(minutes=group[current_index].time_limit)
|
||||
if not group.is_current_alas_alive() or _now >= group.start_time + time_limit:
|
||||
group.stop_current_and_start_next_alas()
|
||||
|
||||
if group.current_account and not group.is_current_alas_alive():
|
||||
group.start_current_alas()
|
||||
|
||||
time_limit = timedelta(minutes=group[group.get_account_index_by_name(group.current_account)].time_limit)
|
||||
if not group.is_current_alas_alive() or _now >= group.start_time + time_limit:
|
||||
group.stop_current_and_start_next_alas()
|
||||
time.sleep(5)
|
||||
with open("./config/account.yaml", mode="w") as f:
|
||||
yaml.dump(get_account_dict(self.account_data), f, yaml.Dumper, indent=2)
|
||||
time.sleep(5)
|
||||
|
||||
def _alas_thread_update_config(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user