def on_exchange(self, uid, gid, mi): mo = MsgPack(Message.MSG_SYS_EXCHANGE | Message.ID_ACK) _id = mi.get_param('id') if not isinstance(_id, int): return mo.set_error(1, 'error param') conf = Context.Configure.get_game_item_json(gid, 'exchange.config') if _id >= len(conf): return mo.set_error(1, 'error id') info = conf[_id] to_type = info['type'] if to_type not in ('diamond', 'props', 'phone'): raise Exception( str(to_type) + '<----error type, please check config') real, final = Context.UserAttr.incr_coupon(uid, gid, -info['cost'], 'exchange.' + to_type) if real != -info['cost']: return mo.set_error(2, 'not enough') mo.set_param('coupon', final) record = { 'uid': uid, 'type': 'exchange', 'ts': Time.current_ts(), 'from': 'coupon', 'to': to_type, 'cost': info['cost'], 'count': info['count'], 'desc': info['desc'] } if info['type'] == 'diamond': # 兑换钻石 real, final = Context.UserAttr.incr_diamond( uid, gid, info['count'], 'exchange.diamond') mo.set_param('diamond', final) state = 1 elif info['type'] == 'props': # 兑换道具 real, final = FishProps.incr_props(uid, gid, info['id'], info['count'], 'exchange.props') mo.set_param('id', info['id']) mo.set_param('count', final) state = 1 record['id'] = info['id'] elif info['type'] == 'phone': state = 0 record['phone'] = mi.get_param('phone') else: raise Exception('something error, please check config') seq_num = Context.RedisMix.hash_incrby('game.%d.info.hash' % gid, 'exchange.history.seq', 1) Context.RedisCluster.hash_set(uid, 'history:%d:%d' % (gid, uid), seq_num, state) record = Context.json_dumps(record) Context.RedisMix.hash_mset('game.%d.exchange.record' % gid, seq_num, record) fmt = Time.current_time('%Y-%m-%d') Context.RedisStat.hash_set('history:%d:%s' % (gid, fmt), seq_num, uid) return mo
def set_daily_data(self, gid, *args, **kwargs): l = Tool.dict2list(kwargs) l.extend(args) if len(l) % 2 != 0: raise Exception('error param') key = '%s:%s:%s' % (self.prefix, gid, Time.current_time(self.format)) return self.ctx.RedisStat.hash_mset(key, *l)
def timedLogFormatter(cls, timestamp, request): from twisted.web.http import _escape referrer = _escape(request.getHeader("referer") or "-") agent = _escape(request.getHeader("user-agent") or "-") tc = round(time.time() - request.started, 4) line = u'%(fmt)s | "%(ip)s" %(tc)ss %(code)d %(length)s "%(method)s %(uri)s %(proto)s" "%(agent)s" "%(ref)s"' % { 'fmt': Time.current_time('%m-%d %H:%M:%S.%f'), 'ip': _escape(request.getClientIP() or "-"), 'tc': tc, 'method': _escape(request.method), 'uri': _escape(request.uri), 'proto': _escape(request.clientproto), 'code': request.code, 'length': request.sentLength or "-", 'agent': agent, 'ref': referrer, } return line
def mincr_daily_data(self, gid, *args, **kwargs): key = '%s:%s:%s' % (self.prefix, gid, Time.current_time(self.format)) return self.ctx.RedisStat.hash_mincrby(key, *args, **kwargs)
def incr_daily_data(self, gid, field, delta=1): key = '%s:%s:%s' % (self.prefix, gid, Time.current_time(self.format)) return self.ctx.RedisStat.hash_incrby(key, field, delta)
def setnx_daily_data(self, gid, field, value): key = '%s:%s:%s' % (self.prefix, gid, Time.current_time(self.format)) return self.ctx.RedisStat.hash_setnx(key, field, value)
def get_daily_data(self, gid, *field): return self.get_day_data(gid, Time.current_time(self.format), *field)
def pay_callback(self, request): data = request.get_args() param = self.parse(data) if param['pay_status'] != 'success': return 'failed' orderId = param['out_order_id'] thirdClientId = param['client_id'] price = param['amount'] / 100 parseInfo = Order.parse_order(orderId) if not parseInfo: Context.Log.info('order not exist') return 'failed' orderInfo = Order.getOrderInfo(orderId) Context.Log.debug('orderInfo-----', orderInfo) if not orderInfo: return 'failed' state = int(orderInfo['state']) if state >= Order.state_pre_deliver: # 可能并没有成功, 需要检查对单 return 'success' cost = int(orderInfo['cost']) # if price != cost: # Context.Log.info('price not equal', orderId, orderInfo, parseInfo) # # Context.Log.warn('price not equal', orderId, orderInfo, parseInfo) # return 'failed' userId = int(orderInfo['userId']) gameId = int(orderInfo['gameId']) channel = orderInfo['channel'] productId = orderInfo['productId'] all_product = Context.Configure.get_game_item_json( gameId, 'product.config') if productId not in all_product: Context.Log.info('productId not exist', orderId, productId, all_product) # Context.Log.error('productId not exist', orderId, productId, all_product) return 'failed' jiyu_app_info = Context.Configure.get_game_item_json( gameId, 'jiyu.app.info') if not jiyu_app_info: Context.Log.warn('jiyu.app.info miss', orderId, orderInfo, parseInfo) return 'failed' result = self.check_app_info(jiyu_app_info, param) Order.updateOrder(orderId, state=result) if result != Order.state_verify_success: return 'failed' payType = 'jiyu' Order.updateOrder(orderId, state=Order.state_pre_deliver, payType=payType) kvs = { 'payTime': param['timestamp'], 'deliverTime': Time.current_time(), 'thirdOrderId': param['out_order_id'] } if Order.deliver_product(userId, gameId, orderId, orderInfo, productId, payType): kvs['state'] = Order.state_deliver_success else: kvs['state'] = Order.state_deliver_failed Order.updateOrder(orderId, **kvs) return 'success'