def choose_troop(self, troop_num=0, troop_dict=None): if troop_dict is not None: self.march_param.troop_dict = troop_dict self.march_param.troop_str = util.dict_to_str( troop_dict, self.__troop_list) else: if troop_num == 0: troop_size = self.march_param.march_buff.march_size else: troop_size = troop_num troop_dict = {} troop_list = sorted( [idx for idx in range(0, len(game_table.game_troop))], reverse=True) select_troop = 0 for troop in troop_list: troop_num_in_city = self.__data.svr_troop.get_troop_num_in_city( troop) if troop_size <= troop_num_in_city: troop_dict[troop] = troop_size select_troop += troop_size break else: troop_dict[troop] = troop_num_in_city troop_size -= troop_num_in_city select_troop += troop_num_in_city self.march_param.troop_dict = troop_dict # logging.info("troop_dict : %s" % self.march_param.troop_dict) self.march_param.troop_str = util.dict_to_str( troop_dict, self.__troop_list) self.march_param.svr_id = self.__data.svr_player.sid
def user_register(self, user_name, password, e_mail, key_value={}): result = False description = "Unknow reason." if str(user_name).strip() == "": logging.info("username is empty.") result = False description = "username is empty." else: if self.user_is_existing(user_name.strip()) == True: result = False description = "User is existing." logging.debug("[{0}] user is created before registration.") else: now_time = util.time_to_str() kv_str = util.dict_to_str(key_value) cur = self.sqlite_conn.cursor() cur.execute( "insert into user_info(user_name, password, e_mail, register_time, key_value) values (?, ?, ?, ?, ?)", (user_name.strip(), password.strip(), e_mail.strip(), now_time, kv_str)) #commit data change self.sqlite_conn.commit() result = True description = "User is created." logging.info("[{0}] user is created.".format(user_name)) return result, description
def save(self): directory = self.save_dir if not DefaultIO._prepare(directory): return destination = os.path.join(directory, self.filename) with open(destination, 'w') as file_: file_.write(dict_to_str(self.serializable.export_to())) debug('Data saved to %s' % destination)
def load(self): directory = self.load_dir if not directory is None: destination = os.path.join(directory, self.filename) if os.path.exists(destination): data = DefaultIO._load_dict(destination) debug('Loaded data: %s from %s' % (dict_to_str(data), destination)) self.serializable.import_from(data) return self.serializable.import_from({})
def get_typing_record_with_kv(self, user_name, key_value={}, max_record=10): typing_record_list = [] cur = self.sqlite_conn.cursor() kv_str = util.dict_to_str(key_value) for row in cur.execute( 'select typing_record, key_value from user_record where user_name=? ' \ , (user_name, )): typing_record_list.append(ast.literal_eval(row[0])) if len(typing_record_list) >= max_record and row[1].find( kv_str) >= 0: return typing_record_list return typing_record_list
def run(self, defines : dict = None): from os import chdir from os import getcwd # change cwd to working dir self.log("Changing CWD to: %s ..." % (self.working_dir)) chdir(self.working_dir) # clean output directory #self.clean_wrk_dir() #TODO # create output directory self.gen_run_dirs() # open log if not self.logger.log_to_file: logname = os.path.join("", *[self.log_dir, self.testcase_name + ".log"]) self.logger.open_file_log(filename=logname) else: self.logger.log() self.logger.log() self.logger.log_header() self.logger.log("CWD : %s" % (getcwd())) self.logger.log("Testcase : %s" % (self.testcase_name)) self.logger.log("Testcase files:", log_to_stdout=False) from util import dict_to_str filesstr = dict_to_str(self.files).split("\n") for line in filesstr : self.logger.log(" " + line, log_to_stdout=False) # repository info from repository import Repository try: repo = Repository(self.working_dir) self.logger.log(self.logger.hr(), log_to_stdout=True) repo_info = repo.info() self.logger.log(repo_info, log_to_stdout=False) if "modified" in repo_info: self.logger.log("WARNING: repository NOT clean!") else: self.logger.log("INFO: repository IS clean.") except ValueError: self.logger.log("INFO: No GIT repository detected, skipping GIT checks.") #self.logger.log(self.logger.hr(), log_to_stdout=False) # run runner ret = self.runner.run(files = self.files, top=None, defines=self.defines, waves=self.waves, dirs=self.dirs) #self.logger.close() # TODO testcase should be able to close its logger by itself, not just from testset! self.logger.fd.flush() os.fsync(self.logger.fd.fileno()) return ret
def user_typing_record(self, user_name, typing, key_value={}): result = False description = "Unknow reason." if self.user_is_existing(user_name.strip()) == True: now_time = util.time_to_str() kv_str = util.dict_to_str(key_value) cur = self.sqlite_conn.cursor() cur.execute( "insert into user_record(user_name, typing_record, record_time, key_value) values (?, ?, ?, ?)", (user_name.strip(), typing.strip(), now_time, kv_str)) #commit data change self.sqlite_conn.commit() result = True description = "Data is record." else: result = False description = "User is not existing." return result, description
def alliance_assist_send(self, rss_dict: dict): aid = self.__data.svr_player.aid if aid == 0: logging.error("no alliance".title()) return Response key0 = 1 rss_request_num = self.__data.get_base_buff_by_id( Enum.BuffId.MarketRequestResource) request_rss_num = 0 for k, v in rss_dict.items(): request_rss_num += int(v) if request_rss_num > rss_request_num: logging.error("Rss request too much") return Response rss = util.dict_to_str(dict_data=rss_dict, target_list=[x * 0 for x in range(0, 5)]) coord = self.__data.svr_player.city_pos return self.__request.al_assist_send(aid=aid, assist_type=key0, rss_list=rss, cid=coord)
def test_dict_to_str(self): key_value = {} key_value["a"]="1" kv_str = "a=1" print(kv_str) self.assertEqual(kv_str, dict_to_str(key_value))