def execute(userId, message, messageHandle): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): roomNo = redis.get(str(userId) + "_room") account = data_account.query_account_by_id(None, userId) if account is not None: redis.lock("lockroom_" + str(roomNo)) try: room = redis.getobj("room_" + str(roomNo)) seat = room.getWatchSeatByUserId(userId) inseat = False if seat is not None: seat.score = int(account.gold.quantize(Decimal('0'))) seat = room.getSeatByUserId(userId) if seat is not None: inseat = True seat.score = int(account.gold.quantize(Decimal('0'))) room.sendBetScore(messageHandle) room.save(redis) room.recUpdateScore(messageHandle, userId) if inseat: room.recUpdateScore(messageHandle, 0) except: print traceback.print_exc() redis.unlock("lockroom_" + str(roomNo))
def update_currency(self, userId): account = data_account.query_account_by_id(None, userId) if None is not account: currency = RecUpdateCurrency() currency.currency = int(account.gold.quantize(Decimal('0'))) currency.gold = int(account.gold.quantize(Decimal('0'))) currency.integral = int(account.integral.quantize(Decimal('0'))) self.send_to_gateway(UPDATE_CURRENCY, currency)
def execute(userId, message, messageHandle, room): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): return reqApplyEnterMatch = ReqApplyEnterMatch() reqApplyEnterMatch.ParseFromString(message.data) recMatchGame = RecMatchGame() recMatchGame.allocId = 2 recMatchGame.level = reqApplyEnterMatch.level account = data_account.query_account_by_id(None, userId) if account.gold < room.inScore: recMatchGame.state = 1 messageHandle.send_to_gateway(GAME_SVR_MATCH, recMatchGame) return if len(room.seatNos) == 0: recMatchGame.state = 3 messageHandle.send_to_gateway(GAME_SVR_MATCH, recMatchGame) return messageHandle.send_to_gateway(GAME_SVR_MATCH, recMatchGame) if room.getSeatByUserId(userId) is None: douniuSeat = DouniuSeat() douniuSeat.seatNo = room.seatNos[0] room.seatNos.remove(room.seatNos[0]) douniuSeat.userId = account.id douniuSeat.account = account.account_name douniuSeat.createDate = account.create_time douniuSeat.nickname = account.nick_name douniuSeat.head = account.head_url douniuSeat.sex = account.sex douniuSeat.score = int(account.gold.quantize(Decimal('0'))) douniuSeat.ip = account.last_address douniuSeat.gpsInfo = "" douniuSeat.total_count = account.total_count douniuSeat.introduce = account.introduce douniuSeat.phone = account.phone douniuSeat.level = account.level douniuSeat.experience = account.experience douniuSeat.intoDate = int(time.time()) douniuSeat.guanzhan = room.gameStatus > GameStatus.WAITING room.seats.append(douniuSeat) threading.Thread(target=ready_timeout.execute, args=(room.gameCount, room.roomNo, messageHandle, userId, douniuSeat.intoDate), name='ready_timeout').start() # 线程对象. room.recUpdateGameInfo(messageHandle) room.recUpdateScore(messageHandle, 0) if room.gameStatus == GameStatus.PLAYING: room.recReEnterGameInfo(messageHandle, userId) redis.set(str(userId) + "_room", room.roomNo) room.save(redis)
def execute(userId, message, messageHandle, room): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): return account = data_account.query_account_by_id(None, userId) reqApplyEnterMatch = ReqApplyEnterMatch() reqApplyEnterMatch.ParseFromString(message.data) recMatchGame = RecMatchGame() recMatchGame.allocId = 7 recMatchGame.level = reqApplyEnterMatch.level messageHandle.send_to_gateway(GAME_SVR_MATCH, recMatchGame) if room.getWatchSeatByUserId(userId) is None: tuitongziSeat = TuitongziSeat() tuitongziSeat.seatNo = 0 tuitongziSeat.userId = account.id tuitongziSeat.account = account.account_name tuitongziSeat.createDate = account.create_time tuitongziSeat.nickname = account.nick_name tuitongziSeat.head = account.head_url tuitongziSeat.sex = account.sex tuitongziSeat.score = int(account.gold.quantize(Decimal('0'))) tuitongziSeat.ip = account.last_address tuitongziSeat.gpsInfo = "" tuitongziSeat.total_count = account.total_count tuitongziSeat.introduce = account.introduce tuitongziSeat.phone = account.phone tuitongziSeat.level = account.level tuitongziSeat.experience = account.experience tuitongziSeat.intoDate = int(time.time()) room.watchSeats.append(tuitongziSeat) room.sendBetScore(messageHandle) room.recUpdateGameInfo(messageHandle) room.recUpdateScore(messageHandle, userId) room.updateBankerList(messageHandle, userId) room.updateWatchSize(messageHandle, 0) room.updateTrend(messageHandle, userId) if 0 == room.gameStatus == GameStatus.PLAYING or room.started: room.recReEnterGameInfo(messageHandle, userId) if room.gameStatus == GameStatus.PLAYING: room.executeAsk(messageHandle, userId, 2) else: room.executeAsk(messageHandle, userId, 1) redis.set(str(userId) + "_room", room.roomNo) room.save(redis)
def execute(userId, message, messageHandle): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): return reqApplyEnterMatch = ReqApplyEnterMatch() reqApplyEnterMatch.ParseFromString(message.data) account = data_account.query_account_by_id(None, userId) match_infos = gl.get_v("match_info") for match_info in match_infos: m = MatchInfo() m.__dict__ = match_info if m.level == reqApplyEnterMatch.level: if m.inScore <= account.gold: room = DouniuRoom(0, m.playerNum, 26 if m.level > 20 else 24, m.level, m.baseScore, m.inScore, m.leaveScore, 0, 1) redis.lock("lock2_rooms") try: if redis.exists("2_rooms"): rooms = redis.get("2_rooms") else: rooms = [] roomNo = random.randint(100000, 999999) while roomNo in rooms: roomNo = random.randint(100000, 999999) rooms.append(roomNo) room.roomNo = roomNo room.save(redis) redis.set(str(roomNo) + "_gameId", 2) redis.set("2_rooms", rooms) redis.lock("lockroom_" + str(roomNo)) join_match_room_cmd.execute(userId, message, messageHandle, room) redis.unlock("lockroom_" + str(roomNo)) except: traceback.print_exc() redis.unlock("lock2_rooms") return else: recMatchGame = RecMatchGame() recMatchGame.allocId = 2 recMatchGame.level = reqApplyEnterMatch.level recMatchGame.state = 1 messageHandle.send_to_gateway(GAME_SVR_MATCH, recMatchGame) break
def create_gold(type, source, user_id, gold): from data.database import data_account connection = None try: connection = mysql_connection.get_conn() account = data_account.query_account_by_id(connection, user_id) sql = config.get("sql", "sql_create_gold") % ( type, source, user_id, gold, account.gold, int(time.time())) with connection.cursor() as cursor: cursor.execute(sql) connection.commit() except: print traceback.print_exc() if connection is not None: connection.rollback() finally: if connection is not None: connection.close()
def execute(userId, message, messageHandle): redis = gl.get_v("redis") if redis.exists(str(userId) + "_room"): return reqApplyEnterMatch = ReqApplyEnterMatch() reqApplyEnterMatch.ParseFromString(message.data) account = data_account.query_account_by_id(None, userId) match_infos = gl.get_v("match_info") for match_info in match_infos: m = MatchInfo() m.__dict__ = match_info if m.level == reqApplyEnterMatch.level: if m.inScore <= account.gold: room = NiuniuRoom(0, m.playerNum, 0, m.level, m.baseScore, m.inScore, m.leaveScore) redis.lock("lock10_rooms") try: if redis.exists("10_rooms"): rooms = redis.get("10_rooms") else: rooms = [] roomNo = random.randint(100000, 999999) while roomNo in rooms: roomNo = random.randint(100000, 999999) rooms.append(roomNo) room.roomNo = roomNo room.save(redis) redis.set(str(roomNo) + "_gameId", 10) redis.set("10_rooms", rooms) redis.lock("lockroom_" + str(roomNo)) join_match_room_cmd.execute(userId, message, messageHandle, room) gamestart_cmd.execute(room, messageHandle) room.save(redis) redis.unlock("lockroom_" + str(roomNo)) except: traceback.print_exc() redis.unlock("lock10_rooms") return break
def handle(self, queue): while not self.__close: try: messages = queue.getall(20, True, 20) for message in messages: if message.opcode == LOGIN_SVR: self.send_to_gateway(LOGIN_SVR, None) elif message.opcode == CREATE_GAME: reqCreateGame = ReqCreateGame() reqCreateGame.ParseFromString(message.data) find = False games = gl.get_v("games") random.shuffle(games) for g in games: if g.alloc_id == reqCreateGame.allocId and g.state == RUNNING: self.sendToGame( g.uuid, CREATE_GAME, reqCreateGame.SerializeToString()) find = True break if not find: recCreateGame = RecCreateGame() recCreateGame.state = 1 self.send_to_gateway(CREATE_GAME, recCreateGame) elif message.opcode == JOIN_GAME: reqJoinGame = ReqJoinGame() reqJoinGame.ParseFromString(message.data) find = False if self.__redis.exists( str(reqJoinGame.gameId) + "_gameId"): gameId = self.__redis.get( str(reqJoinGame.gameId) + "_gameId") games = gl.get_v("games") random.shuffle(games) for g in games: if g.alloc_id == gameId and g.state == RUNNING: self.sendToGame(g.uuid, message.opcode, message.data) find = True break if not find: recJoinGame = RecJoinGame() recJoinGame.state = 1 recJoinGame.gameId = reqJoinGame.gameId self.send_to_gateway(JOIN_GAME, recJoinGame) elif message.opcode == APPLY_ENTER_MATCH: reqApplyEnterMatch = ReqApplyEnterMatch() reqApplyEnterMatch.ParseFromString(message.data) find = False games = gl.get_v("games") random.shuffle(games) for g in games: if g.alloc_id == reqApplyEnterMatch.allocId and g.state == RUNNING: self.sendToGame( g.uuid, APPLY_ENTER_MATCH, reqApplyEnterMatch.SerializeToString()) find = True recApplyEnterMatch = RecApplyEnterMatch() recApplyEnterMatch.state = recApplyEnterMatch.SUCCESS self.send_to_gateway(APPLY_ENTER_MATCH, recApplyEnterMatch) break if not find: recApplyEnterMatch = RecApplyEnterMatch() recApplyEnterMatch.state = recApplyEnterMatch.FAILD self.send_to_gateway(APPLY_ENTER_MATCH, recApplyEnterMatch) elif message.opcode == UPDATE_MATCH_INFO: reqUpdateMatchInfo = ReqUpdateMatchInfo() reqUpdateMatchInfo.ParseFromString(message.data) recUpdateMatchInfo = RecUpdateMatchInfo() for info in reqUpdateMatchInfo.infos: infos = recUpdateMatchInfo.infos.add() infos.allocId = info.allocId infos.level = info.level infos.games = 1 infos.players = 5 infos.totalPlayers = 10 self.send_to_gateway(UPDATE_MATCH_INFO, recUpdateMatchInfo) elif message.opcode == BANK_INFO: reqBankInfo = ReqBankInfo() reqBankInfo.ParseFromString(message.data) account = data_account.query_account_by_id( None, self.__userId) recBankInfo = RecBankInfo() recBankInfo.card = int( account.bank_gold.quantize(Decimal('0'))) recBankInfo.gold = int( account.bank_gold.quantize(Decimal('0'))) recBankInfo.integral = int( account.bank_integral.quantize(Decimal('0'))) self.send_to_gateway(BANK_INFO, recBankInfo) elif message.opcode == BANK_DEPOSIT or message.opcode == BANK_GET: reqOperateBank = ReqOperateBank() reqOperateBank.ParseFromString(message.data) account = data_account.query_account_by_id( None, self.__userId) if message.opcode == BANK_DEPOSIT and int( account.gold.quantize( Decimal('0'))) < reqOperateBank.card: break if message.opcode == BANK_DEPOSIT and int( account.gold.quantize( Decimal('0'))) < reqOperateBank.gold: break if message.opcode == BANK_DEPOSIT and int( account.integral.quantize( Decimal('0'))) < reqOperateBank.integral: break if message.opcode == BANK_GET and int( account.bank_gold.quantize( Decimal('0'))) < reqOperateBank.card: break if message.opcode == BANK_GET and int( account.bank_gold.quantize( Decimal('0'))) < reqOperateBank.gold: break if message.opcode == BANK_GET and int( account.bank_integral.quantize( Decimal('0'))) < reqOperateBank.integral: break gold = -reqOperateBank.card if message.opcode == BANK_DEPOSIT else reqOperateBank.card integral = -reqOperateBank.integral if message.opcode == BANK_DEPOSIT else reqOperateBank.integral data_account.update_currency(None, gold, integral, -gold, -integral, self.__userId) data_gold.create_gold(2, self.__userId, self.__userId, gold) recOprateBank = RecOprateBank() self.send_to_gateway(message.opcode, recOprateBank) self.update_currency(self.__userId) if self.__redis.exists(str(self.__userId) + "_room"): roomNo = self.__redis.get( str(self.__userId) + "_room") gameId = self.__redis.get(str(roomNo) + "_gameId") games = gl.get_v("games") random.shuffle(games) for g in games: if g.alloc_id == gameId and g.state == RUNNING: self.sendToGame(g.uuid, GAME_UPDATE_CURRENCY, None) break elif message.opcode == UPDATE_RANK: reqGameRank = ReqGameRank() reqGameRank.ParseFromString(message.data) accounts = data_account.ranking_by_gold( None, reqGameRank.number) recGameRank = RecGameRank() i = 0 for a in accounts: i += 1 playerRankInfo = recGameRank.playerDatas.add() playerRankInfo.rankId = i playerRankInfo.playerId = a.id playerRankInfo.rankVal = int( a.gold.quantize(Decimal('0'))) playerRankInfo.nick = a.nick_name playerRankInfo.headUrl = a.head_url if a.introduce is not None: playerRankInfo.introduce = a.introduce playerRankInfo.consumeVip = a.level self.send_to_gateway(message.opcode, recGameRank) elif message.opcode == MATCH_RECORD_INFO: reqMatchRecordInfo = ReqMatchRecordInfo() reqMatchRecordInfo.ParseFromString(message.data) records = data_record.get_records( reqMatchRecordInfo.allocIds, self.__userId) self.send_to_gateway(message.opcode, records) elif message.opcode == UPDATE_INTRODUCE: reqUpdateIntroduce = ReqUpdateIntroduce() reqUpdateIntroduce.ParseFromString(message.data) account = data_account.update_introduce( None, self.__userId, reqUpdateIntroduce.content) if None is not account: self.update_user_info(account) elif (5 < message.opcode < 23) or (27 < message.opcode < 34) or ( 99 < message.opcode < 200 ) or message.opcode == 38 or message.opcode == 1005: if self.__redis.exists(str(self.__userId) + "_room"): roomNo = self.__redis.get( str(self.__userId) + "_room") gameId = self.__redis.get(str(roomNo) + "_gameId") games = gl.get_v("games") random.shuffle(games) for g in games: if g.alloc_id == gameId and g.state == RUNNING: self.sendToGame(g.uuid, message.opcode, message.data) break elif message.opcode == PERSONAL: s = StringUtils.randomStr(32) self.__redis.setex( str(self.__userId) + "personalcode", s, 10) reqPersonalCode = ReqPersonalCode() reqPersonalCode.ParseFromString(message.data) code = RecPersonalCode() code.code = s code.opcode = reqPersonalCode.opcode self.send_to_gateway(PERSONAL, code) else: gl.get_v("serverlogger").logger.info( "无效协议%d,用户id%d" % (message.opcode, self.__userId)) except Empty: print("%d messagehandle received timeout close" % self.__userId) self.close() self.__server_receive.remove(self.__userId) except: print traceback.print_exc()