def __init__(self, loglevel): 'Create object that works out the jobs' logger = Logger(loglevel) # configure logging self.logger = logger.get() # get the logging function self.storage = Storage(self.logger) # object for file system accesss self.chrome = Chrome( self.logger) # object to work with chrome/chromium self.modulenames = [i['name'] for i in self.MODULES] self.logins = dict() self.options = dict() for i in self.MODULES: if i['login'] != None: self.logins[i['name']] = {j: '' for j in i['login']} else: self.logins[i['name']] = None if i['options'] != None: self.options[i['name']] = { j: i['options'][j] for j in i['options'] } else: self.options[i['name']] = None self.options_defaults = { i: {j: self.options[i][j]['default'] for j in self.options[i]} for i in self.options }
def __init__(self, database_config): self.cnx = None self.cursor = None self.db_is_connected = False self.username = database_config[Config.Keys.Database.username] self.password = database_config[Config.Keys.Database.password] self.host = database_config[Config.Keys.Database.host] self.logger = Logger(LoggerConfig.file_path)
def article(self, name, value): Logger.get_logger('login.log').info('正在发布文章') self.input_article_name(name) self.input_article(value) self.click_option_image() sleep(1) self.option_channel() sleep(2) self.click_publish()
def check_article(self, title, channel): Logger.get_logger('login.log').info('正在审核文章') self.option_info() self.input_title(title) self.input_channel(channel) self.option_state() self.click_find() self.id = self.get_id() self.click_pass() self.click_sure() sleep(3)
def __init__(self, deviceCode, socket_addr, aes_key): self.deviceCode = deviceCode self.socket_addr = socket_addr self.aes_key = aes_key self.log = Logger() self.simulate_socket_conn() self.simulate_device_login() g1 = gevent.spawn(self.simulate_device_beat) g1.join()
def __init__(self, room_id, room_uuid, group_id, guild_id, match, owner, kwargs): super(Table, self).__init__() self.chairs = 6 # 默认人数 self.room_id = room_id #桌子ID self.room_uuid = room_uuid self.owner = owner #房主ID self.group_id = group_id # 群id self.guild_id = guild_id # 公会ID self.guild_admins = [] #公会管理员Id self.match = match # 比赛房 self.owner_info = None self.kwargs = str(kwargs) self.player_dict = {} self.seat_dict = {} #桌子上的玩家对象 self.lookon_player_dict = {} # 旁观玩家列表 self.machine = None self.state = None self.dismiss_state = False self.dismiss_sponsor = None self.dismiss_time = 0 self.logger = Logger() # 战斗日志 self.dealer_seat = -1 # 庄家位置 self.active_seat = -1 self.active_card = 0 self.discard_seat = -1 self.event = None self.cards_total = 136 self.base_score = 0 # 庄家压分 诈金牛奖金池 self.conf = None # 桌子配置 self.sport_id = 0 self.st = time.time() self.dt = self.st + 20 * 60 #20分钟后解散房间 if match > 0: self.dt = self.st + 72 * 3600 self.et = 0 self.replay = [] # 桌子记录 self.cards_on_desk = [] # 当前桌子牌堆 self.cur_round = 1 # 当前回合数 self.timers = {} self.timer_active = True self.show_card_end_time = 0 # 洗牌使用 self.niu_max = 0 # 大牌型出现次数 Machine(self)
def __init__(self, session=False, verbose=False): # 解决控制台输出 InsecureRequestWarning 的问题 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) self.log = Logger() self.headers = {} self.verbose = verbose self.info = dict() if session: self.request = requests.session() else: self.request = requests
def __init__(self, username=None, password=None, headers=None, user_type=None, grant_type=None): if not username: username = config.get_data_username if not password: password = config.get_data_password if not user_type: user_type = 1 if not grant_type: grant_type = password if headers is None: self.headers = {"Content-Type": "application/json;charset=UTF-8"} self.username = username self.session = requests.session() self.log = Logger() if len(password) == 32: self.password = password else: self.password = "******" self._config = self.base_yaml_info(curr_file=__file__, module_key="dataUser") self._get_header()
def on_match_act(self, act, room_id, user_id, score): table = TableMgr().room_dict.get(room_id) if table and table.match > 0: player = table.player_dict.get(user_id) if not player: player = table.lookon_player_dict.get(user_id) if player: # 同步分数 player.match_score = score msg_dict = { "player": user_id, "score": player.get_total_score() } table.send_table_msg(SYNC_SCORE, msg_dict) # 处理动作 if act == "ready": if player.state == "InitState" or player.state == "SettleState" or player.state == "PauseState": if player.get_total_score( ) < player.table.conf.get_sit_score(): player.send(SCORE_LEAK, {}) else: player.ready() elif act == "enter": pass else: Logger().warn("match room {0} ready user {1} not exist".format( room_id, user_id))
class BaseAgent(object): name = "BaseAgent" def __init__(self, log_file=None, name=""): self.name = self.name + name if log_file is None: log_file = self.name self.logger = Logger(log_file + ".log") self.functions = [] def act(self, state, train=False): raise NotImplementedError def train(self, episodes): raise NotImplementedError def save(self): for f in self.functions: f.save(CHECK_PATH + self.name) def load(self): try: for f in self.functions: f.load(CHECK_PATH + self.name) except: C.warning("No Checkpoint Found for:" + CHECK_PATH + self.name) def print(self): max_l = max(list(map(len, self.history.keys()))) for k, v in self.history.items(): print(k + (max_l - len(k)) * " ", end="") if v is not None: print(" : %s" % str(v[-1])) def log(self, key, val): self.logger.log(key, val) def play(self, name='play'): name = str(name) + self.env.name + self.name state = self.env.reset(record=True) done = False while not done: action = self.act(state, train=False) state, _, done, info = self.env.step(action) self.env.save_episode(name)
def test_back_login(self, user, pwd, expect): self.PageBackLogin.back_login(user, pwd) ret = self.driver.current_url try: assert expect in ret print('断言通过,测试后台登录通过') except AssertionError: self.PageBackLogin.base_get_image() Logger().get_logger('login.log').error('断言失败,测试后台登录不通过') raise
def test_check_article(self, title, channel): self.PageCheckArticle.check_article(title, channel) ret = self.PageCheckArticle.find_article() try: assert ret print('断言成功,测试审核文章通过') except AssertionError: self.PageCheckArticle.base_get_image() Logger().get_logger('login.log').error('断言失败,测试审核文章不通过') raise
def initPlugin(): #parse plugin config loggerData = LoggerConfig() if not pluginConfig(loggerData): return False logger = Logger.getInstance().init(loggerData) return True
def _train_auto_encoders(): n_folds = 1 top = None val_top = None train_bsize = 256 test_b_size = 64 num_planes = 2 scores = [] one_transform = transforms.Compose([ Flip(axis=2, targets_also=True, rnd=True), Flip(axis=1, targets_also=True, rnd=True), Rotate(90, targets_also=True, rnd=True), Rotate(180, targets_also=True, rnd=True), Ravel(), ToTensor() ]) val_transform = transforms.Compose([Ravel(), ToTensor()]) for f in range(n_folds): main_logger = Logger("../logs/enc/", erase_folder_content=False) train_set = AutoEncoderDataset("../data/all.npy", transform=one_transform, top=top) train_loader = DataLoader(train_set, batch_size=train_bsize, num_workers=12, pin_memory=True, shuffle=True) val_set = AutoEncoderDataset("../data/folds/test_1.npy", transform=val_transform, top=val_top) val_loader = DataLoader(val_set, batch_size=test_b_size, num_workers=6, pin_memory=True) encoder = VariationalAutoEncoder(num_planes, fold_number=None) if torch.cuda.is_available(): encoder.cuda() optim = torch.optim.Adam(encoder.parameters(), lr=0.001, weight_decay=0) best = encoder.fit(optim, loss_function, train_loader, val_loader, 100, logger=main_logger) scores.append(best) print() print("Best was ", best) print(scores) return scores
def on_body(self, data): try: msg_dict = msgpack.unpackb(data) except Exception, e: print e, self.address self.close() from base.logger import Logger Logger().local( "fake_ip.log", self.address[0] + ":" + str(self.address[1]) + ":unpack") return
def on_head(self, data): self.last_time = time.time() self.head_data = data size = ord(data[0]) * 256 + ord(data[1]) if size <= 0 or size > 4096: print "bad package, size:", size, " ", self.address self.close() from base.logger import Logger Logger().local( "fake_ip.log", self.address[0] + ":" + str(self.address[1]) + ":size") return self._stream.read_bytes(size, self.on_body)
def test_article(self, name, content, expect): self.PageArticle.option_content() sleep(2) self.PageArticle.article(name, content) ret = self.PageArticle.get_info() sleep(2) try: assert ret == expect print('实际结果为{},预期结果为{},测试发布文章通过'.format(ret, expect)) except AssertionError: print('实际结果为{},预期结果为{},测试没有通过'.format(ret, expect)) self.PageArticle.base_get_image() Logger().get_logger('login.log').error('断言失败,测试发布文章没有通过') raise
class Check: _logger = Logger.getInstance() @classmethod def checkPluginModeParam(cls, params): if NUMBER_ZERO == len(params): return True return False @classmethod def checkTotalModeParam(cls, params): if NUMBER_ZERO == len(params): return True if NUMBER_TWO == len(params): return cls.checkTargetParam(params) return False @classmethod def checkFileModeParam(cls, params): if NUMBER_ONE == len(params): if os.path.isfile(params[0]) and os.path.splitext( params[0])[1] == ".xml": cls._logger.error( "check file mode: the host config file is invalid when param len is two" ) return True if NUMBER_THREE == len(params): if os.path.isfile(params[0]) and os.path.splitext( params[0])[1] == ".xml": return cls.checkTargetParam(params[1:]) return False #结果文件存放参数校验 @classmethod def checkTargetParam(cls, params): if "-r" != params[0]: cls._logger.error( "check target param: the second command is not -r.") return False if not os.path.isdir(params[1]): cls._logger.error( "check target param: the target path is invalid.") return False return True
def __init__(self, username=None, password=None, headers=None): if not username: username = config.get_b2b_username if not password: password = config.get_b2b_password if headers is None: self.headers = {"Content-Type": "application/json;charset=UTF-8"} self.username = username self.session = requests.session() self.log = Logger() if len(password) == 32: self.password = password else: self.password = "******" self._config = self.base_yaml_info(curr_file=__file__, module_key="b2bUser") self._get_header()
def train(): from cnn.simple_ds import SimpleIcebergDataset from cnn.dataset import IcebergDataset, Ravel, ToTensor from torch.utils.data import DataLoader import torch from base.logger import Logger main_logger = Logger("../logs/simple", erase_folder_content=True) ds = SimpleIcebergDataset("../data/for_train_encoder.npy", transform=ToTensor()) val_ds = SimpleIcebergDataset("../data/for_test_encoder.npy", transform=ToTensor()) ldr = DataLoader(ds, batch_size=64) val_ldr = DataLoader(val_ds, batch_size=64) model = SimpleMLP() optim = torch.optim.Adam(model.parameters(), lr=0.0001, weight_decay=0.005) loss_fn = nn.BCELoss() if torch.cuda.is_available(): model.cuda() loss_fn.cuda() best = model.fit(optim, loss_fn, ldr, val_ldr, 250, logger=main_logger) print(best)
def handler(cmd, msg_dict, session): if cmd == CG_CREATE_ROOM: create_room(msg_dict, session) elif cmd == CG_DISMISS_ROOM: dismiss_room(msg_dict, session) elif cmd == CG_SYNC_ROOM: sync_room(msg_dict, session) elif cmd == CG_USER_TOKEN: user_token(msg_dict, session) elif cmd == CG_MATCH_SCORE: match_score(msg_dict, session) elif cmd == GC_ROOM_RESET: room_reseted(msg_dict, session) elif cmd == CG_EMO_AMOUNT: emo_amount(msg_dict, session) elif cmd == GC_MATCH_ACT: match_act(msg_dict, session) elif cmd == CG_CREATE_SPORT_ROOM: create_sport_room(msg_dict, session) elif cmd == CG_DISMISS_SPORT: dismiss_sport(msg_dict, session) else: Logger().warn("center client error cmd:%d"%cmd)
def enter(self, room_id, player_id, info, user_token, session): print "enter room %d" % room_id table = self.room_dict.get(room_id) if not table: if room_id in self.room_settle_cache: send(SETTLEMENT_FOR_ROOM_DN, self.room_settle_cache[room_id], session) msg_dict = dict() msg_dict["code"] = 0 send(DISMISS_ROOM, msg_dict, session) else: # 给前端返回房间不存在的错误 msg_dict = dict() msg_dict["code"] = 1 send(ENTER_ROOM, msg_dict, session) print("room {0} not exist, player {1} enter failed".format( room_id, player_id)) return #检测token是否合法 token = redis.get("token:{0}".format(player_id)) if token == None or token != user_token: msg_dict = dict() msg_dict["code"] = 3 #token验证不通过,非法用户 send(ENTER_ROOM, msg_dict, session) table.logger.info("token error: server;{0} client:{1}".format( token, user_token)) return if table.room_id != room_id: self.room_dict[table.room_id] = table del self.room_dict[room_id] msg_dict = dict() msg_dict["code"] = 1 send(ENTER_ROOM, msg_dict, session) table.logger.fatal( "room id map error: msg {0} actually {1}".format( room_id, table.room_id)) return from base.match_mgr import MatchMgr player = table.player_dict.get(player_id) if player: # 服务重启后player没有session if player.session: if session != player.session: player.table.logger.info( "player {0} cancel old session {1}".format( player_id, player.session.uuid)) # SessionMgr().cancel(player.session) player.session.close() SessionMgr().register(player, session) player.table.logger.info( "player {0} register new session {1}".format( player_id, player.session.uuid)) if player.seat == -1: seat = -1 for seat in range(table.chairs): if seat in table.seat_dict.keys(): continue break player.seat = seat MatchMgr().player_enter(player) player.reconnect() player.reload_extra() player.online_status(True) else: player = table.lookon_player_dict.get(player_id) if player and player.session and session != player.session: player.table.logger.info( "player {0} cancel old session {1}".format( player_id, player.session.uuid)) player.session.close() table.enter_room(player_id, info, session) Logger().local("ip.log", session.address[0] + ":" + str(session.address[1]))
class ParseHostXml: _logger = Logger.getInstance() @classmethod def parseHostXml(cls, hostPath, hosts): if not os.path.exists(hostPath): cls._logger.error("parse host xml: file is not exist") return False root = Common.getRoot(hostPath) if root is None: cls._logger.error("parser host xml: host xml format is not valid.") return False for node in Common.getChild(root): if "host" != node.tag: cls._logger.error( "parser host xml: child node name is not host.") return False host = Host() if not cls.__parseHostNode(node, host): cls._logger.error( "parser host xml: host node format is not valid.") return False hosts.append(host) return True @classmethod def __parseHostNode(cls, node, host): elements = Common.getChild(node) if elements is None: cls._logger.error("parser host node: host node has no child node.") return False for element in elements: if "device" == element.tag: if not cls.__parseDeviceNode(element, host): return False elif "collect" == element.tag: if not cls.__parseCollectNode(element, host): return False elif "alarm" == element.tag: if not cls.__parseAlarmNode(element, host): return False else: cls._logger.error( "parser host node: host node should not has %s node." % element.tag) return False return True @classmethod def __parseDeviceNode(cls, node, host): elements = Common.getChild(node) if elements is None: cls._logger.error( "parser device node: device node has no child node.") return False for element in elements: if "hostname" == element.tag: host.setHostName(element.text.strip()) elif "ipaddress" == element.tag: host.setIpAddress(element.text.strip()) elif "devicetype" == element.tag: host.setDeviceType(element.text.strip()) elif "port" == element.tag: host.setPort(element.text.strip()) else: cls._logger.error( "parser device node: device node should not has %s node." % element.tag) return False return True @classmethod def __parseCollectNode(cls, node, host): elements = Common.getChild(node) if elements is None: cls._logger.error( "parser collect node: collect node has no child node.") return False for element in elements: if "snmpversion" == element.tag: host.setCollectVersion(element.text.strip()) elif "user" == element.tag: host.setUserName(element.text.strip()) elif "pass" == element.tag: host.setPassword(cls.__dencrypt(element.text)) elif "authprotocol" == element.tag: host.setAuthProtocol(element.text.strip()) elif "privprotocol" == element.tag: host.setEncryptionProtocol(element.text.strip()) elif "community" == element.tag: host.setCollectCommunity(cls.__dencrypt(element.text)) else: cls._logger.error( "parser collect node: collect node should not has %s node." % element.tag) return False host.setCollectBasic( cls.__getDevcieCollect(host.getDeviceType(), "basic")) host.setCollectExtension( cls.__getDevcieCollect(host.getDeviceType(), "extension")) return True @classmethod def __parseAlarmNode(cls, node, host): elements = Common.getChild(node) if elements is None: cls._logger.error( "parser alarm node: alarm node has no child node.") return False for element in elements: if "snmpversion" == element.tag: host.setAlarmVersion(element.text.strip()) elif "community" == element.tag: host.setAlarmCommunity(cls.__dencrypt(element.text)) else: pass return True @classmethod def __getDevcieCollect(cls, deviceType, collectType): if "Rack" == deviceType: if "basic" == collectType: return ["system", "power", "fan", "cpu", "memory", "hardDisk"] elif "extension" == collectType: return [ "system", "power", "fan", "cpu", "memory", "hardDisk", "pCIe", "raid", "logical", "component", "sensor", "firmwareVersion" ] elif "HighDensity" == deviceType: if "basic" == collectType: return ["system", "power", "fan", "cpu", "memory", "hardDisk"] elif "extension" == collectType: return [ "system", "power", "fan", "cpu", "memory", "hardDisk", "pCIe", "raid", "logical", "component", "sensor", "firmwareVersion" ] elif "E9000" == deviceType: return ["system", "power", "fan", "switch", "blade"] else: cls._logger.error("get device collect failed, device type:%s." % deviceType) return [] @classmethod def __readKey(cls): try: kfilepath = Common.getEtcPath( ) + 'huawei_server' + os.path.sep + 'configInfo.cfg' file = open(kfilepath, 'r') key = file.readline() except Exception, e: cls._logger.exception("read key: get key exception:%s." % e) return "" finally:
class Connection(object): uuid = None last_time = 0 def __init__(self, stream, address): from base.session_mgr import SessionMgr self.uuid = str(uuid4()) SessionMgr().add(self) self._stream = stream self.address = address self.last_time = time.time() self._stream.set_close_callback(self.on_close) self._stream.read_bytes(8, self.on_head) self.head_data = None print "connection opened.", address, "count:", len( SessionMgr().session_set) def on_head(self, data): self.last_time = time.time() self.head_data = data size = ord(data[0]) * 256 + ord(data[1]) if size <= 0 or size > 4096: print "bad package, size:", size, " ", self.address self.close() from base.logger import Logger Logger().local( "fake_ip.log", self.address[0] + ":" + str(self.address[1]) + ":size") return self._stream.read_bytes(size, self.on_body) def checkcrc(self, cmd, body_data): size = ord(self.head_data[0]) * 256 + ord(self.head_data[1]) crc_data = "" if size < 122: crc_data = self.head_data[4:8] + chr(int(cmd / 256)) + chr( cmd % 256) + body_data[0:size] else: crc_data = self.head_data[4:8] + chr(int(cmd / 256)) + chr( cmd % 256) + body_data[0:122] if get_crc(crc_data) != ord(self.head_data[2]) * 256 + ord( self.head_data[3]): return False return True def on_body(self, data): try: msg_dict = msgpack.unpackb(data) except Exception, e: print e, self.address self.close() from base.logger import Logger Logger().local( "fake_ip.log", self.address[0] + ":" + str(self.address[1]) + ":unpack") return if isinstance(msg_dict, dict): cmd = msg_dict.get("cmd") if not self.checkcrc(cmd, data): print "check crc fail", self.address self.close() from base.logger import Logger Logger().local( "fake_ip.log", self.address[0] + ":" + str(self.address[1]) + ":crc") return if cmd is not None: from protocol.deserialize import receive receive(cmd, msg_dict, self) else: print "bad msg dict.", self.address self.close() return try: self._stream.read_bytes(8, self.on_head) except StreamClosedError: pass
from time import sleep import page from base.logger import Logger from base.webbase import WebBase logger = Logger().get_logger('login.log') class PageLogin(WebBase): def login_input(self, user): self.base_input(page.login_input, user) def pwd_input(self, pwd): self.base_input(page.pwd_input, pwd) def login_click(self): self.base_click(page.login_btn) def login(self, user, pwd): logger.info('正在登录,用户名:{},密码:{}'.format(user, pwd)) self.login_input(user) self.pwd_input(pwd) self.login_click() sleep(3) def get_image(self): self.base_get_image() def get_nickname(self): return self.base_get_text(page.nickname)
from base.logger import Logger import time from selenium.webdriver.common.by import By from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait from selenium.common.exceptions import * from selenium.webdriver.support import expected_conditions as ec from conftest import IMG_DIR #logger实例 logger = Logger(logger="BasePage").getlog() class BasePage(object): def __init__(self, login): """ :param login:conftest.py的login """ self.driver = login #后退操作 def back(self): self.driver.back() logger.info("Click back on current page.") def forward(self): self.driver.forward() logger.info('Click forward on current page.') def open(self, url):
from base.base import BasePage from base.logger import Logger logger = Logger(logger="HomePage").getlog() class HomePage(BasePage): # locators _search_input = "kw" _search_button = "su" def __init__(self, driver): super().__init__(driver) self.driver = driver def enter_search(self, content): self.element_send_keys(self._search_input, content, "id") def click_search_button(self): self.element_click(self._search_button, "id") def search(self, content): self.enter_search(content) self.click_search_button()
#!/usr/bin/python # -*- coding: UTF-8 -*- import threading import traceback import time from base.dataprocessor import DataProcessor from base.logger import Logger monitor_logger = Logger.get_logger(__file__) class StateMonitor(DataProcessor): """ 任务状态监控线程,定时获取数据同步状态并输出到日志文件和数据库。 """ def __init__(self): DataProcessor.__init__(self) self.__stop = False self.__sync_thread = None def start(self): if not self.init_connecton(): monitor_logger.error("Can't init data connection for state monitor.") return False self.__sync_thread = threading.Thread(target=self.monitor_runner) self.__sync_thread.setDaemon(True) self.__sync_thread.start() return True
class Table(object): def __init__(self, room_id, room_uuid, group_id, guild_id, match, owner, kwargs): super(Table, self).__init__() self.chairs = 6 # 默认人数 self.room_id = room_id #桌子ID self.room_uuid = room_uuid self.owner = owner #房主ID self.group_id = group_id # 群id self.guild_id = guild_id # 公会ID self.guild_admins = [] #公会管理员Id self.match = match # 比赛房 self.owner_info = None self.kwargs = str(kwargs) self.player_dict = {} self.seat_dict = {} #桌子上的玩家对象 self.lookon_player_dict = {} # 旁观玩家列表 self.machine = None self.state = None self.dismiss_state = False self.dismiss_sponsor = None self.dismiss_time = 0 self.logger = Logger() # 战斗日志 self.dealer_seat = -1 # 庄家位置 self.active_seat = -1 self.active_card = 0 self.discard_seat = -1 self.event = None self.cards_total = 136 self.base_score = 0 # 庄家压分 诈金牛奖金池 self.conf = None # 桌子配置 self.sport_id = 0 self.st = time.time() self.dt = self.st + 20 * 60 #20分钟后解散房间 if match > 0: self.dt = self.st + 72 * 3600 self.et = 0 self.replay = [] # 桌子记录 self.cards_on_desk = [] # 当前桌子牌堆 self.cur_round = 1 # 当前回合数 self.timers = {} self.timer_active = True self.show_card_end_time = 0 # 洗牌使用 self.niu_max = 0 # 大牌型出现次数 Machine(self) def dumps(self): self.logger.info("table state: {0}".format(self.state)) data = {} for key, value in self.__dict__.items(): if key in ("logger", "conf", "request", "lookon_player_dict"): continue elif key == "player_dict": data[key] = value.keys() elif key == "seat_dict": data[key] = {k: v.uuid for k, v in value.items()} elif key == "machine": data[key] = [None, None] if value.cur_state: data[key][1] = value.cur_state.name if value.last_state: data[key][0] = value.last_state.name else: data[key] = value redis.set("table:{0}".format(self.room_id), pickle.dumps(data)) def delete(self): self.player_dict = {} self.lookon_player_dict = {} self.seat_dict = {} redis.delete("table:{0}".format(self.room_id)) def enter_room(self, player_id, info, session): newinfo = info.replace("\\'", "") newinfo = newinfo.replace('\\"', "") newinfo = newinfo.replace('\\n', "") newinfo = newinfo.replace('\\t', "") if not self.owner_info and player_id == self.owner: self.owner_info = newinfo msg_dict = dict() msg_dict["room_id"] = self.room_id msg_dict["owner"] = self.owner msg_dict["owner_info"] = self.owner_info msg_dict["room_state"] = table_state_code_map[self.state] msg_dict["round"] = self.cur_round msg_dict["rounds"] = self.conf.rounds msg_dict["is_admin"] = (player_id in self.guild_admins) # if len(self.player_dict.keys()) + len(self.lookon_player_dict.keys()) >= self.chairs + 10: # if len(self.player_dict.keys()) >= self.chairs: # msg_dict["code"] = 2 # send(ENTER_ROOM, msg_dict, session) # self.logger.warn("room {0} is full, player {1} enter failed".format(self.room_id, player_id)) # return player = Player(player_id, newinfo, session, self) from base.match_mgr import MatchMgr MatchMgr().player_enter(player) from base.state_base.player.init import InitState player.machine.trigger(InitState()) player.is_wait = True self.lookon_player_dict[player_id] = player SessionMgr().register(player, session) msg_dict["code"] = 0 msg_dict["kwargs"] = self.kwargs msg_dict["rest_cards"] = self.cards_total msg_dict["state"] = player_state_code_map[player.state] msg_dict["player"] = list() msg_dict["dealer"] = self.dealer_seat msg_dict["player_status"] = player_state_code_map["InitState"] for k, v in self.seat_dict.items(): p = dict() p["seat"] = k p["player"] = v.uuid p["info"] = v.info p["state"] = player_state_code_map[v.state] p["is_online"] = v.is_online p["score"] = v.get_total_score() p["pledge"] = v.round.pledge p["loot_dealer"] = v.round.loot_dealer p["is_wait"] = v.is_wait p["niu_type"] = v.round.niu_type if v.session is not None: p["ip"] = v.session.address[0] msg_dict["player"].append(p) p["cards_in_hand"] = list() count = len(v.round.cards_in_hand) if v.state == "ShowCardState": p["cardsign_in_hand"] = list() cards_hand = copy.deepcopy(v.round.cards_in_hand) cardsign_in_hand = show_card_type2(v.round.cards_in_hand, v.round.niu_type) for w in cardsign_in_hand: p["cardsign_in_hand"].append(w) for c in cards_hand: p["cards_in_hand"].append(c) else: for _ in v.round.cards_in_hand: p["cards_in_hand"].append(0) send(ENTER_ROOM, msg_dict, session) ''' msg_dict = dict() msg_dict["player"] = player_id msg_dict["info"] = player.info msg_dict["seat"] = player.seat msg_dict["dealer"] = self.dealer_seat # 庄家位置 msg_dict["state"] = player_state_code_map[player.state] msg_dict["is_online"] = player.is_online msg_dict["score"] = player.room.score msg_dict["pledge"] = player.round.pledge msg_dict["loot_dealer"] = player.round.loot_dealer msg_dict["is_wait"] = player.is_wait msg_dict["ip"] = player.session.address[0] for i in self.player_dict.values(): if i.uuid == player_id: continue send(ENTER_ROOM_OTHER, msg_dict, i.session) ''' self.dumps() self.logger.info("player {0} enter room".format(player_id)) # reason: 0:结算解散 1:房主直接解散 2:投票解散 def dismiss_room(self, reason=2): self.logger.info("room {0}-{1} dismiss for reason {2}".format( self.room_id, self.room_uuid, reason)) # 如果是投票解散房间则进入大结算,否则直接推送房主解散命令 if reason == 2 and self.state != "InitState" and self.state != "SettleForRoomState": from state.table_state.settle_for_room import SettleForRoomState self.machine.trigger(SettleForRoomState()) return else: self.dismiss_room_msg(reason) from base.table_mgr import TableMgr TableMgr().dismiss(self.room_id) for player in self.player_dict.values(): try: player.session.close() except Exception: pass player.delete() for player in self.lookon_player_dict.values(): try: player.session.close() except Exception: pass player.delete() from base.center.request import dismiss_room dismiss_room(self) self.delete() def send(self, cmd, msg_dict): for player in self.player_dict.values(): player.send(cmd, msg_dict) def is_all_ready(self): ready_count = 0 pause_count = 0 if self.seat_dict[self.dealer_seat].state != "ReadyState": return for player in self.player_dict.values(): if player.state == "ReadyState" or player.state == "WaitState": ready_count += 1 if player.state == "PauseState": pause_count += 1 if self.cur_round == 1: if ready_count < 2: # self.chairs return from base.center.request import room_state room_state(self, 1) else: len_count = len(self.player_dict) if ready_count + pause_count != len_count: # self.chairs return if ready_count < 2: return self.machine.trigger(ReadyState()) def is_all_starting(self): if self.state != "ReadyState": return self.kill_timer("start_10") self.machine.trigger(StartingState()) if self.cur_round >= 1: self.kill_timer("ready_15") self.kill_timer("trusteeship_ready_15") def is_all_pledge(self): if self.conf.game_type == 5: if self.change_state("StartingState", "CashPledgeState", False, True) == False: return elif self.conf.game_type == 4 or self.conf.game_type == 7: if self.change_state("LootDealerState", "CashPledgeState", True, True) == False: return elif self.conf.game_type == 6: if self.change_state("LootDealerState", "CashPledgeState", True, True) == False: return else: if self.change_state("StartingState", "CashPledgeState", True, True) == False: return self.kill_timer("pledge_15") self.kill_timer("trusteeship_pledge_15") self.machine.trigger(CashPledgeState()) def is_all_loot_dealer(self): if self.conf.game_type == 6: if self.change_state("StartingState", "LootDealerState", False, True) == False: return else: if self.change_state("DealState", "LootDealerState", False, True) == False: return self.kill_timer("loot_dealer_15") self.kill_timer("trusteeship_loot_dealer_15") self.machine.trigger(LootDealerState()) def is_all_show_card(self): if self.conf.game_type == 5: if self.change_state("DealState", "ShowCardState", False, True) == False: return elif self.conf.game_type == 4 or self.conf.game_type == 7: if self.change_state("Deal2State", "ShowCardState", True, True) == False: return else: if self.change_state("DealState", "ShowCardState", True, True) == False: return self.kill_timer("show_card_15") self.kill_timer("trusteeship_show_card_15") dealer = self.seat_dict[self.dealer_seat] if dealer.state != "ShowCardState": dealer.show_card() # 庄家最后show card else: self.machine.trigger(ShowCardState()) def is_all_start(self): if self.change_state("ReadyState", "CashPledgeState", True, True) == False: return self.machine.trigger(CashPledgeState()) def change_state(self, former_state, new_stete, dealer_bo, init_bo): # 转换状态 :旧状态,新状态,是否过滤庄家,是否过滤旁观者 if self.state != former_state: return False for player in self.player_dict.values(): if init_bo: if not player.is_playing(): continue if dealer_bo: if player.seat == self.dealer_seat: # 庄家不用压 continue if player.state != new_stete: return False return True def is_all_players_do_action(self): self.dumps() self.clear_prompt() self.clear_actions() def clear_prompt(self): for player in self.player_dict.values(): player.action.clear_prompts() def clear_actions(self): for player in self.player_dict.values(): player.action.clear_actions() def set_name_timer(self, timer): count = self.get_name_timer_count(timer) self.set_timer(timer, count) if timer == "show_card_15": self.set_timer("trusteeship_show_card_15", (count - 5)) if timer == "pledge_15": self.set_timer("trusteeship_pledge_15", (count - 4)) if timer == "loot_dealer_15": self.set_timer("trusteeship_loot_dealer_15", (count - 3)) if timer == "ready_15": self.set_timer("trusteeship_ready_15", (count - 4)) def get_name_timer_count(self, timer): count = 8 if timer == "ready_15": left_time = int(self.show_card_end_time - time.time()) + 1 if left_time < 0: left_time = 0 if self.conf.is_not_wait() == False: count = 10 + left_time elif self.chairs == 10: if self.conf.game_type == 4 or self.conf.game_type == 6 or self.conf.game_type == 7: count = 5 + left_time else: count = 8 + left_time else: count = 5 + left_time return count elif timer == "show_card_15": count = 8 if self.conf.is_not_wait() == False: if self.conf.app_id != 101: count = 15 return 10 elif self.chairs == 10: count = 8 elif self.conf.is_rub_card() == False: count = 6 else: count = 8 return count elif timer == "pledge_15": if self.conf.app_id == 101: return 8 elif self.conf.is_not_wait() == False: count = 10 elif self.chairs == 10: count = 9 elif self.conf.game_type == 4 or self.conf.game_type == 7: count = 9 else: count = 5 return count elif timer == "loot_dealer_15": count = 8 if self.conf.app_id == 101: return 5 elif self.conf.is_not_wait() == False: count = 10 elif self.chairs == 10: count = 6 else: count = 6 return count return count def set_timer(self, timer, seconds): # 设置定时器 self.timers[timer] = seconds def kill_timer(self, timer): # 取消定时器 if self.timers.get(timer): del self.timers[timer] def get_timer_left_count(self, timer): # 获取定时器剩余时间 if self.timers.get(timer): return self.timers[timer] def get_timer_left(self): # 获取定时器剩余时间 for v in self.timers.values(): return v return 0 def get_timer_left_key(self): # 获取定时器KEY for v in self.timers.keys(): if "trusteeship_show_card_15" != v or "trusteeship_pledge_15" != v or "trusteeship_loot_dealer_15" != v or "trusteeship_ready_15" != v: return v else: continue return 0 def pause_timer(self): # 暂停定时器 self.timer_active = False def resume_timer(self): # 恢复定时器 self.timer_active = True def heartbeat(self): self.set_ai_time_type() #检测玩家是否开启离线托管 if not self.timer_active: return for k in self.timers.keys(): self.timers[k] -= 1 if self.timers[k] <= 0: self.on_timer(k) del self.timers[k] break def on_timer(self, timer): if timer == "call_later_5": pass elif timer == "show_card_15": self.show_card() elif timer == "pledge_15": self.pledge_15() elif timer == "loot_dealer_15": self.loot_dealer_15() elif timer == "ready_15": self.ready_15() elif timer == "start_10": self.start_10() elif timer == "trusteeship_show_card_15": self.trusteeship("show_card_15") elif timer == "trusteeship_pledge_15": self.trusteeship("pledge_15") elif timer == "trusteeship_loot_dealer_15": self.trusteeship("loot_dealer_15") elif timer == "trusteeship_ready_15": self.trusteeship("ready_15") def test(self): self.set_timer("call_later_5", 5) def show_card(self): for i in self.player_dict.values(): #得出所有人的牛 player = i #取出桌子中K位置的玩家对象 if not player.is_playing(): continue if self.conf.game_type != 5: # 非通比牛牛 if player.seat == self.dealer_seat: continue if player.state == "DealState" or player.state == "CashPledgeState" or player.state == "Deal2State": player.show_card() def pledge_15(self): for i in self.player_dict.values(): # 得出所有人的牛 player = i # 取出桌子中K位置的玩家对象 if player.state != "CashPledgeState": if player.seat == self.dealer_seat: continue if player.round.pledge != 0: continue if not player.is_playing(): continue count = 0 if self.conf.game_type == 7: count = 1 else: count = self.conf.score if self.conf.app_id == 101: if self.conf.pledge_res: if i.round.loot_dealer == self.conf.loot_dealer: count = self.conf.get_score_Max(self.conf.score) player.pledge_score(count, 0, i.round.loot_dealer) def loot_dealer_15(self, count=-1): for i in self.player_dict.values(): # 得出所有人的牛 player = i # 取出桌子中K位置的玩家对象 if player.state != "LootDealerState": if not player.is_playing(): continue player.loot_dealer_type(count) def ready_15(self): for i in self.player_dict.values(): # 得出所有人的牛 player = i # 取出桌子中K位置的玩家对象 if player.state == "InitState" or player.state == "SettleState": player.ready() def start_10(self): msg_dict = {"code": 0, "seat": 1} self.send_table_msg(START_DN, msg_dict, True, True) self.machine.trigger(StartingState()) def is_push_score(self): for i in self.player_dict.values(): if i.state == "ReadyState" or i.state == "WaitState": continue if self.conf.is_push_score(): if self.conf.pledge_res: if i.round.loot_dealer == self.conf.loot_dealer: pass else: i.room.push_pledge = 0 else: i.room.push_pledge = 0 def add_chat_msg(self, user_id, msg_dict, seat): if msg_dict.get("type") == 5 and msg_dict.get( "msg") == "gunshootFX_hit": if self.match > 0: from base.match_mgr import MatchMgr if not MatchMgr().dec_emo_amount(self.room_id, user_id): return msg_dict["user_id"] = user_id msg_dict["seat"] = seat self.send_table_msg(SPEAKER, msg_dict, True, True) def look_card_msg(self, seat): msg_dict = dict() msg_dict["seat"] = seat self.send_table_msg(LOOK_CARD_DN, msg_dict, True, True) def loot_dealer_msg(self, seat, loot_dealer): msg_dict = dict() msg_dict["code"] = 0 msg_dict["seat"] = seat msg_dict["loot_dealer"] = loot_dealer # 倍率 self.send_table_msg(LOOT_DEALER_DN, msg_dict, True, True) def pledge_msg(self, seat, pledge, code): msg_dict = dict() msg_dict["seat"] = seat msg_dict["pledge"] = pledge #押金 msg_dict["code"] = code # 错误1 self.send_table_msg(PLEDGE_DN, msg_dict, True, True) def double_pledge_msg(self, seat, code): msg_dict = dict() msg_dict["seat"] = seat msg_dict["code"] = code # 错误1 self.send_table_msg(DOUBLE_PLEDGE, msg_dict, True, True) def ready_msg(self, seat): msg_dict = dict() msg_dict["seat"] = seat self.send_table_msg(READY, msg_dict, True, True) def wait_msg(self, seat): msg_dict = dict() msg_dict["seat"] = seat self.send_table_msg(WAIT_DN, msg_dict, True, True) def show_card_msg(self, seat, cards, niu_type, cardsign_in_hand): msg_dict = dict() msg_dict["seat"] = seat msg_dict["cards"] = cards msg_dict["niu_type"] = niu_type msg_dict["cardsign_in_hand"] = cardsign_in_hand #亮牌 self.send_table_msg(SHOW_CARD_DN, msg_dict, True, True) def prompt_card_msg(self, flag): msg_dict = dict() msg_dict["time"] = self.get_name_timer_count("show_card_15") msg_dict["flag"] = flag self.send_table_msg(PROMPT_CARD_DN, msg_dict, True, True, False) def dealer_msg(self, seat, score): msg_dict = dict() msg_dict["seat"] = seat # 新的庄家位置 msg_dict["score"] = score # 庒家分數 msg_dict["room_state"] = table_state_code_map[self.state] self.send_table_msg(DEALER_SEAT, msg_dict, True, True) def prompt_start_msg(self, flag): msg_dict = dict() msg_dict["flag"] = flag # 提示庄家可以开始游戏(flag=0可以,flag=1不可以) self.send_dealer_msg(PROMPT_START_DN, msg_dict) def prompt_loot_dealer_msg(self, flag): #玩家可以抢庄 msg_dict = dict() msg_dict["time"] = self.get_name_timer_count("loot_dealer_15") msg_dict["flag"] = flag self.send_table_msg(PROMPT_LOOT_DEALER_DN, msg_dict, True, True, False) def exit_room_msg(self, uuid, seat): msg_dict = dict() msg_dict["player"] = uuid msg_dict["seat"] = seat self.send_table_msg(EXIT_ROOM, msg_dict, True, True) def dismiss_room_msg(self, reason): msg_dict = dict() msg_dict["code"] = reason self.send_table_msg(DISMISS_ROOM, msg_dict, True, True) def trusteeship_msg(self, seat, ai_type): msg_dict = dict() msg_dict["seat"] = seat msg_dict["ai_type"] = ai_type self.send_table_msg(TRUSTEESHIP, msg_dict, True, True) def enter_room_other_msg(self, player): msg_dict = dict() msg_dict["player"] = player.uuid msg_dict["info"] = player.info msg_dict["seat"] = player.seat msg_dict["dealer"] = self.dealer_seat # 庄家位置 msg_dict["state"] = player_state_code_map[player.state] msg_dict["is_online"] = player.is_online msg_dict["score"] = player.get_total_score() msg_dict["pledge"] = player.round.pledge msg_dict["loot_dealer"] = player.round.loot_dealer msg_dict["is_wait"] = player.is_wait msg_dict["ip"] = player.session.address[0] self.send_table_msg(ENTER_ROOM_OTHER, msg_dict, True, True) def prompt_pledge_msg(self, flag): msg_dict = dict() msg_dict["time"] = self.get_name_timer_count("pledge_15") for i in self.player_dict.values(): if i.seat != self.dealer_seat: if not i.is_playing(): msg_dict["show_max_score"] = False msg_dict["push_pledge"] = 0 msg_dict["flag"] = 1 i.send(PROMPT_PLEDGE_DN, msg_dict) continue msg_dict["pledge_double"] = 0 if self.conf.get_pledge_double(): if i.round.loot_dealer == self.conf.loot_dealer: msg_dict["pledge_double"] = 1 else: msg_dict["pledge_double"] = 0 msg_dict["show_max_score"] = False msg_dict["flag"] = flag msg_dict["push_pledge"] = i.room.push_pledge # 推注 if self.conf.get_pledge_res() == 1: if i.round.loot_dealer == self.conf.loot_dealer: msg_dict["show_max_score"] = True else: msg_dict["push_pledge"] = 0 # 推注 i.room.push_pledge = 0 i.send(PROMPT_PLEDGE_DN, msg_dict) else: msg_dict["show_max_score"] = False msg_dict["push_pledge"] = 0 msg_dict["flag"] = 1 i.send(PROMPT_PLEDGE_DN, msg_dict) self.prompt_pledge2_msg(flag) def pledge_prompt_msg(self): #推注提示 msg_dict = dict() msg_dict["player"] = list() for i in self.player_dict.values(): p = dict() p["seat"] = i.seat if i.seat != self.dealer_seat: if not i.is_playing(): p["push_pledge"] = 0 msg_dict["player"].append(p) continue p["push_pledge"] = i.room.push_pledge # 推注 msg_dict["player"].append(p) else: p["push_pledge"] = 0 msg_dict["player"].append(p) self.send_table_msg(PUSH_PLEDGE, msg_dict, True, True) def prompt_pledge2_msg(self, flag): msg_dict = dict() msg_dict["time"] = self.get_name_timer_count("pledge_15") msg_dict["show_max_score"] = False msg_dict["push_pledge"] = 0 msg_dict["flag"] = 1 self.send_table_msg(PROMPT_PLEDGE_DN, msg_dict, False, True, False) def send_dealer_msg(self, msg_name, msg_dict): # 发送给庄家 if self.conf.game_id == 3: #如果是牛爷,则需要房主或者公会会长开始游戏 all_player = self.player_dict.values() all_player.extend(self.lookon_player_dict.values()) for player in all_player: if self.guild_id > 0: #公会房间 if player.uuid in self.guild_admins: player.send(msg_name, msg_dict) else: #开房间 if player.uuid == self.owner: player.send(msg_name, msg_dict) return else: self.seat_dict[self.dealer_seat].send(msg_name, msg_dict) # 广播桌子消息 # include_dealer: 是否同时发给庄家 # include_lookon: 是否同时发给旁观者 def send_table_msg(self, cmd, msg_dict, include_dealer=True, include_lookon=True, exclude_state=False): if cmd not in (READY, ENTER_ROOM_OTHER, ROUND_STATE, SPEAKER): self.replay.append({ "type": 2, "cmd": cmd, "data": msg_dict, "time": int(time.time() * 1000) }) for i in self.player_dict.values(): if not include_dealer and i.seat == self.dealer_seat: continue if exclude_state: if not i.is_playing(): continue i.send(cmd, msg_dict) if include_lookon: for i in self.lookon_player_dict.values(): i.send(cmd, msg_dict) def add_replay(self, cmd, msg_dict): self.replay.append({ "type": 2, "cmd": cmd, "data": msg_dict, "time": int(time.time() * 1000) }) def get_replay_compress(self): buf = StringIO.StringIO() f = gzip.GzipFile(mode='wb', fileobj=buf) try: f.write(pickle.dumps(self.replay)) finally: f.close() return buf.getvalue() def is_negative(self): # 可否负分 if self.match > 0 and self.conf.ex_neg == 0: return False return True def ai_get_card_type(self, cb_card_data, conf): # 获取类型 cb_temp_data = [] for k, v in enumerate(cb_card_data): cb_temp_data.append(v) # 炸弹牌型 if conf.is_bomb(): b_same_count = 0 b_second_value = get_card_value(cb_temp_data[0]) for k, v in enumerate(cb_temp_data): if b_second_value == get_card_value(v): b_same_count = b_same_count + 1 if b_same_count >= 3: return 14 else: b_same_count = 0 b_second_value = get_card_value(cb_temp_data[1]) for k, v in enumerate(cb_temp_data): if b_second_value == get_card_value(v): b_same_count = b_same_count + 1 if b_same_count >= 3: return 14 # 五小牛牌型 #if conf.is_five_small(): # b_five_small = 0 # for k, v in enumerate(cb_temp_data): # b_five_small = b_five_small + get_card_value(v) # if b_five_small <= 10: # return 13 # 葫芦牛 if conf.is_calabash(): b_same_count = 0 b_second_value = get_card_value(cb_temp_data[0]) b_second_value_2 = 0 b_same_count_2 = 0 for k, v in enumerate(cb_temp_data): if b_second_value == get_card_value(v): b_same_count = b_same_count + 1 else: if b_second_value_2 == 0: b_second_value_2 = get_card_value(v) b_same_count_2 = b_same_count_2 + 1 elif b_second_value_2 == get_card_value(v): b_same_count_2 = b_same_count_2 + 1 if b_same_count == 2: if b_same_count_2 == 2: return 18 # 葫芦牛 elif b_same_count == 3: if b_same_count_2 == 1: return 18 # 葫芦牛 elif b_same_count_2 == 2: if b_same_count == 2: return 18 # 葫芦牛 elif b_same_count_2 == 3: if b_same_count == 1: return 18 # 葫芦牛 # 金牛牌型 有疑问 if conf.is_gold(): b_king_count = 0 for k, v in enumerate(cb_temp_data): if get_card_value(v) > 10: b_king_count = b_king_count + 1 if b_king_count == 4: return 12 # 银牛牌型 if conf.is_silver(): b_king_count = 0 b_10_count = 0 for k, v in enumerate(cb_temp_data): if get_card_value(v) >= 10: b_king_count = b_king_count + 1 if b_king_count == 4: return 11 # --牛一~牛牛牌型 或 一条龙 b_temp = [] b_sum = 0 for k, v in enumerate(cb_temp_data): b_temp.append(get_card_logic_value(v)) b_sum = b_sum + b_temp[k] # 一条龙之后判定牛几 b_temp.sort() b_temp2 = [] for k, v in enumerate(cb_temp_data): b_temp2.append(get_card_value(v)) b_temp2.sort() # 同花顺牛 if conf.is_straight_flush(): b_color_count = 0 color_type = get_card_color(cb_temp_data[0]) for k, v in enumerate(cb_temp_data): if get_card_color(v) == color_type: b_color_count = b_color_count + 1 if b_color_count == 4: is_sequence = False for e, r in enumerate(b_temp2): if e == 0: continue cb_count = get_card_value(b_temp2[e - 1]) + 1 cb_count2 = get_card_value(r) if cb_count != cb_count2: is_sequence = False break is_sequence = True if is_sequence == True: return 19 #if conf.is_long(): # yitiao_info = [1, 2, 3, 4, 5] # yitiaolong = -1 # yitiaolong = cmp(yitiao_info, b_temp) # if yitiaolong == 0: # return 15 # 一条龙 # 同花牛 if conf.is_identical(): b_color_count = 0 color_type = get_card_color(cb_temp_data[0]) for k, v in enumerate(cb_temp_data): if get_card_color(v) == color_type: b_color_count = b_color_count + 1 if b_color_count == 4: return 17 # 顺子牛 if conf.is_sequence(): is_sequence = False for e, r in enumerate(b_temp2): if e == 0: continue cb_count = get_card_value(b_temp2[e - 1]) + 1 cb_count2 = get_card_value(r) if cb_count != cb_count2: is_sequence = False break is_sequence = True if is_sequence == True: return 16 for e, r in enumerate(b_temp): if (b_sum - b_temp[e]) % 10 == 0: if b_temp[e] > 10: return b_temp[e] - 10 else: return b_temp[e] return 0 def trusteeship(self, timer): for i in self.player_dict.values(): if not i.is_playing(): continue if i.room.ai_type == 0: continue #timer =self.get_timer_left_key() self.set_ai(i, timer) def set_ai_time(self, seat): if self.conf.game_type == 7: pass elif self.conf.game_type == 4: pass else: return ai_time = time.time() ai_time = 20 * 1000 self.seat_dict[seat].room.ai_time = ai_time def set_ai_time_type(self): ai_time = time.time() for i in self.player_dict.values(): if not i.is_playing(): continue if i.room.ai_type != 0: continue if i.room.ai_time == 0: continue if ai_time >= i.room.ai_time: i.trusteeship(1, 0, 0, 0, 0, 0) #设置玩家默认托管 def set_ai(self, player, timer): log = {} log["player"] = player.uuid log["seat"] = player.seat log["room_id"] = self.room_id log["timer_type"] = timer log["player_state"] = player.state log["table_state"] = self.state if player.table.conf.game_type == 7: pass elif player.table.conf.game_type == 4: pass else: return if player.room.ai_type != 0: if timer == "show_card_15": self.ai_show_card(player) if timer == "pledge_15": niu_type = 0 if len(player.round.cards_in_hand) != 0: niu_type = self.ai_get_card_type( player.round.cards_in_hand, self.conf) log["cards_rest"] = player.round.cards_in_hand log["niu_type"] = niu_type pledge_type = 0 count = 0 if player.room.pledge_type == 1: if niu_type >= 8: count = player.room.pledge else: count = player.room.pledge if player.room.push_pledge != 0 and player.room.push_pledge_type != 0: # 推注 push_pledge_niu_type = 0 if player.room.push_pledge_type == 1: push_pledge_niu_type = 8 if player.room.push_pledge_type == 2: push_pledge_niu_type = 9 if player.room.push_pledge_type == 3: push_pledge_niu_type = 10 if push_pledge_niu_type != 0: if niu_type >= push_pledge_niu_type: count = player.room.push_pledge pledge_type = 1 # 没分 填补最低 if count == 0: if self.conf.game_type == 7: count = 1 else: count = self.conf.score log["pledge_score"] = count log["pledge_type"] = pledge_type self.ai_pledge(player, count, pledge_type) if timer == "loot_dealer_15": niu_type = 0 if len(player.round.cards_in_hand) != 0: niu_type = self.ai_get_card_type( player.round.cards_in_hand, self.conf) log["cards_rest"] = player.round.cards_in_hand log["niu_type"] = niu_type loot_dealer_niu_type = 0 count = -1 if player.room.loot_dealer_type != 0: # 抢庄 loot_dealer_niu_type = 0 if player.room.loot_dealer_type == 1: loot_dealer_niu_type = 8 if player.room.loot_dealer_type == 2: loot_dealer_niu_type = 9 if player.room.loot_dealer_type == 3: loot_dealer_niu_type = 10 if loot_dealer_niu_type != 0: if niu_type >= loot_dealer_niu_type: count = player.room.loot_dealer log["loot_dealer"] = count self.ai_loot_dealer_15(player, count) if timer == "ready_15": self.ai_ready_15(player) self.logger.info(log) def ai_pledge(self, player, count, pledge_type): if player.state != "CashPledgeState": if player.seat == self.dealer_seat: return if player.round.pledge != 0: return if not player.is_playing(): return if self.conf.pledge_res: if player.round.loot_dealer == self.conf.loot_dealer: count = self.conf.get_score_Max(self.conf.score) if self.conf.game_type == 4 or self.conf.game_type == 6 or self.conf.game_type == 7: if self.state == "LootDealerState": player.pledge_score(count, pledge_type, player.round.loot_dealer) return def ai_show_card(self, player): if not player.is_playing(): return if self.conf.game_type != 5: # 非通比牛牛 if player.seat == self.dealer_seat: return if player.state == "DealState" or player.state == "CashPledgeState" or player.state == "Deal2State": player.show_card() def ai_loot_dealer_15(self, player, count=-1): if player.state != "LootDealerState": if not player.is_playing(): return if self.state == "DealState": if self.conf.game_type == 4 or self.conf.game_type == 7: player.loot_dealer_type(count) return if self.state == "StartingState": if self.conf.game_type == 6: player.loot_dealer_type(count) return def ai_ready_15(self, player): if player.state == "InitState" or player.state == "SettleState": player.ready()
import pytest from conftest import cases_path, max_fail from base.logger import Logger import os logger = Logger('root').getlog() if __name__ == '__main__': pytest.main(["-s", "--alluredir", "./report/xml", "--maxfail", max_fail]) os.system('allure generate report/xml/ -o report/html --clean') logger.info("test end! Generate test report")
class Request(object): class Method(Enum): ''' 请求动作类型 ''' GET = 1 POST = 2 PUT = 3 DELETE = 4 def __init__(self, session=False, verbose=False): # 解决控制台输出 InsecureRequestWarning 的问题 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) self.log = Logger() self.headers = {} self.verbose = verbose self.info = dict() if session: self.request = requests.session() else: self.request = requests def send_request(self, request_method, request_type, request_url, request_data=None, auth=None, headers=None, cookies=None): ''' 功能:http请求的基础方法,支持不同的请求方法,请求类型 :param request_method: 请求方法 :param request_type: 请求内容类型 :param request_url: 请求URL :param request_data: 请求数据 :return: ''' ssl._create_default_https_context = ssl._create_unverified_context if self.verbose: self.log.log_info("接口请求地址:{}".format(request_url)) self.info['request_addrPath'] = "/" + "/".join( request_url.split("/")[3:]) try: if self.verbose: self.log.log_info("接口请求数据:{}".format(request_data)) self.info['request_data'] = request_data if request_method == "post" and request_type == "urlencoded": response = requests.post(url=request_url, data=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "post" and request_type == "json": response = requests.post(url=request_url, json=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "put" and request_type == "json": response = requests.put(url=request_url, json=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "delete" and request_type == "json": response = requests.delete(url=request_url, json=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "post" and request_type == "file": data = MultipartEncoder(fields=request_data, boundary="%s" % uuid.uuid4()) response = self.request.post(url=request_url, data=data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "get" and request_type == "urlencoded": response = requests.get(url=request_url, params=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) elif request_method == "get" and request_type == "json": response = requests.get(url=request_url, params=request_data, headers=headers, auth=auth, cookies=cookies, verify=False) else: raise exceptions.HttpRequestException( "当前的请求方法:{},请求类型:{}".format(request_method, request_type)) except Exception as e: self.log.log_error("http请求异常:{}".format(e)) raise exceptions.HttpRequestException("http请求异常") self.info['request_header'] = headers return self.__setting_response_format(response) def __setting_response_format(self, response): response_code = response.status_code self.info['response_code'] = response_code self.info['response_time'] = response.elapsed.total_seconds() self.info['local_time'] = TimeHelper.get_time_from_timestamp() self.info['response_data'] = response.text self.info['response_header'] = dict(response.headers) # 进行http的状态码的检测 ''' 200 请求成功 401 接口未授权 404 接口请求地址不存在 500 接口服务器内部错误 502 请求失败:接口服务器运行错误:服务是否启动,端口是否可用,网络能否ping通 ''' # if response_code == 200 or 201: if response_code == 200: response_ContentType = response.headers.get("Content-Type") if "application/json" in response_ContentType: try: self.info['response_data'] = response.json() except Exception: self.log.log_error(self.info) raise exceptions.HttpResponseException( "请求类型是json,但接口返回内容却不是json格式") elif "image" in response_ContentType: self.info['response_data'] = response.content else: self.log.log_warning("响应内容类型不是json也不是image,是其他类型:{}".format( response_ContentType)) self.info['response_data'] = response.text if self.verbose: self.log.log_info("接口响应信息:{}".format( self.info['response_data'])) return self.info self.log.log_warning("请求请求地址:{}".format(self.info['request_addrPath'])) self.log.log_warning("请求响应的http状态码:{}".format(response_code)) try: self.info['response_data'] = response.json() except: self.info['response_data'] = response.text return self.info