def receive_trade_payment(self): db = DbManager().db trades = self.trades now = datetime.now() all_trades_complete = True for trade in trades: if trade.status != STATUS_COMPLETE: return if all_trades_complete: try: db.execute('begin;') for trade in trades: if trade.pay_method == PAYMETHOD_ACCOUNT: sql = 'update {table} set balance=balance-%s where user_id=%s'.format( table=Account.table) db.execute(sql, float(trade.amount), self.creator_id) sql = 'update {table} set balance=balance+%s where room_id=%s'.format( table=StarFund.table) db.execute(sql, float(self.amount), self.room_id) sql = 'update {table} set status=%s, pay_time=%s where id=%s'.format( table=self.table) db.execute(sql, STATUS_COMPLETE, now, self.id) sql = 'insert into {table} (user_id, source, order_type, order_id, amount, create_time) values ' \ '(%s, %s, %s, %s, %s, %s)'.format(table=WalletRecord.table) db.execute(sql, self.creator_id, WALLET_RECORD_ROOM_RENT, ORDER_ROOM, self.id, -self.amount, now) db.execute('commit;') except: db.execute('rollback;') raise RoomUser.add(self.room_id, self.creator_id) self.flush_fund_cache()
def post(self): try: room_id = self.get_argument('room_id') user_id = self.get_argument('user_id') except MissingArgumentError: return self.error(MISSING_PARAMS) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) == int(user_id): return self.error(CREATOR_NOT_NEED_JOIN_ROOM) if RoomUser.room_exists_user(room_id, user_id): return self.error(ROOM_EXISTS_USER) if RoomBlackUser.is_room_black_user(room_id, user_id): return self.error(USER_IN_ROOM_BLACK_LIST) if room.private_not_join: return self.error(ROOM_NOT_JOIN) try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) return self.render({ "status": 0, "data": room.jsonify(), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR)
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) == int(user_id): return self.error(CREATOR_NOT_NEED_JOIN_ROOM) if RoomUser.room_exists_user(room_id, user_id): return self.error(ROOM_EXISTS_USER) if RoomBlackUser.is_room_black_user(room_id, user_id): return self.error(USER_IN_ROOM_BLACK_LIST) # 房主自身也属于房间成员之一 if room.limit_user_number: room_user_amount = RoomUser.get_user_amount_by_room(room_id) if room_user_amount == room.limit_user_number: return self.error(ROOM_USER_FULL) if room.private_not_join: return self.error(ROOM_NOT_JOIN) if room.private_need_verify: room_question = RoomQuestion.get_question_by_room(room_id) return self.render({ 'status': 0, 'data': room_question.jsonify() if room_question else None }) else: try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) # handle_join_room_integral.delay(user_id, room_id) # user_amount = room.user_amount # if user_amount == 500: # handle_room_user_over_500_integral.delay(room.creator_id, room.id) return self.render({ "status": 0, "data": room.jsonify(user), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR)
def add(cls, creator_id, name, intro, tag_ids, avatar, limit_user_number, create_time, status=ROOM_PUBLIC, question_info=None): from starmachine.model.room_tag import RoomTag from starmachine.model.room_user import RoomUser db = DbManager().db cache = CacheManager().cache db.execute('begin;') try: sql = 'insert into {table} (creator_id, name, intro, avatar, limit_user_number, status, create_time) ' \ 'values (%s, %s, %s, %s, %s, %s, %s)'.format(table=cls.table) room_id = db.execute(sql, creator_id, name, intro, avatar, limit_user_number, status, create_time) if int(status) == ROOM_PRIVATE_NEED_VERIFY and question_info: question_name = question_info.get('question_name') options = question_info.get('options') sql = 'insert into {table} (room_id, name, create_time) values (%s, %s, %s)'.format( table=RoomQuestion.table) room_question_id = db.execute(sql, room_id, question_name, create_time) for option in options: text = option.get('text') is_right_answer = option.get('is_right_answer') sql = 'insert into {table} (question_id, text, is_right_answer) values (%s, %s, %s)'.format( table=RoomQuestionOption.table) db.execute(sql, room_question_id, text, is_right_answer) for tag_id in tag_ids: sql = 'insert into {table} (room_id, tag_id) values (%s, %s)'.format( table=RoomTag.table) db.execute(sql, room_id, tag_id) sql = 'insert into {table} (room_id, balance) values (%s, %s)'.format( table=StarFund.table) db.execute(sql, room_id, '0.00') db.execute('commit;') except: db.execute('rollback;') raise cache_key = USER_CREATED_ROOM_KEY % creator_id cache.sadd(cache_key, room_id) RoomUser.add(room_id, creator_id, ROOM_USER_CREATOR) return room_id
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') question_id = self.get_argument('question_id') option_id = self.get_argument('option_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) room = Room.get(room_id) question = RoomQuestion.get(question_id) option = RoomQuestionOption.get(option_id) if not question: return self.error(ROOM_QUESTION_NOT_FOUND) if int(question.room_id) != int(room_id) or option.question_id != int(question_id): return self.error(ACCESS_NOT_ALLOWED) user_room_question_24_hours_limit = 'user:%s:room:%s:question:24:limit' LIMIT_SECONDS = 24 * 60 * 60 redis_key = user_room_question_24_hours_limit % (user_id, room_id) cache = CacheManager().cache if cache.exists(redis_key): return self.error(ROOM_QUESTION_ANSWER_LIMIT) cache.set(redis_key, 'true') cache.expire(redis_key, LIMIT_SECONDS) if option.is_right_answer: try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) return self.render({ "status": 0, "data": room.jsonify(user), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR) else: return self.error(ROOM_QUESTION_ANSWER_WRONG)