def get_user_server_list(req): """# login: 交给前端用户的server_list(此步不需要验证) args: req: --- arg returns: 0 --- """ account = req.get_argument('account', '') version = req.get_argument('version', '') # 给前端填坑 account = account.replace('37wanA545_', '37wan_') # 用37wan_去替换37wanA545_ account = account.replace('pipa_new_', 'pipa_') replace_lua_url = True if settings.ENV_NAME in [settings.ENV_IOS, settings.ENV_STG_IOS, settings.ENV_TEST_IOS] \ and version >= '1.2.7' else False if not UnameUid.check_exist(account): return 0, None, { # 查无此人 'server_list': ServerConfig.get().server_list(replace_lua_url=replace_lua_url), # lua的url不一样 'current_server': '', } uu = UnameUid.get(account) sid, expired = uu.get_or_create_session_and_expired() server_list = ServerConfig.get().server_list(replace_lua_url=replace_lua_url) device_mark = req.get_argument('device_mark', '') device_mem = req.get_argument('device_mem', '') ip = req.request.headers.get('X-Real-Ip', '') for s in server_list: uid = s['uid'] = uu.servers.get(s['server'], '') s['level'] = 0 if uid: u = User.get(uid) s['level'] = u.level if u.regist_time and u.name: commit = False if not (u.device_mark and u.device_mark == device_mark): u.device_mark = device_mark commit = True if not (u.device_mem and u.device_mem == device_mem): u.device_mem = device_mem commit = True if not (u.ip and u.ip == ip): u.ip = ip commit = True if u.update_session_and_expired(sid, expired): commit = True if commit: u.save() return 0, None, { 'server_list': server_list, 'current_server': '', 'ks': sid, }
def pay(env, *args, **kwargs): ''' 接受充值请求,成功则返回ok,后台进行相关修改 :param env: :param args: :param kwargs: :return: ''' result = { # 应答信息模板 'result_code': 'failed', # 查询应答码,ok为成功, 'result_msg': '', 'record': { 'timestamp': time.time(), 'game_amount': 0, } } qid = env.get_argument('qid', '') app_key = env.get_argument('app_key', '') server_id = env.get_argument('server_id', '') order_id = env.get_argument('order_id', '') amount = env.get_argument('amount', '') # 传入的amount以分为单位 sign = env.get_argument('sign', '') # 验证签名 sign_str = '%s#%s#%s#%s#%s#%s' % (amount, app_key, order_id, qid, server_id, APP_SECRET) if sign != hashlib.md5(sign_str).hexdigest(): result['result_msg'] = 'sign err' return result # 获取用户uuid account = '%s_%s' % ('360', qid) if not UnameUid.check_exist(account): result['result_msg'] = 'user not exist!' return result um = UnameUid.get(account) servers = um.servers if server_id not in servers: result['result_msg'] = 'server_id not exist!' return result u = UserL.get(servers[server_id]) pass
def login(req): """# login: 登录用户 args: req: --- arg returns: 0 --- 1: unicode('查无此人', 'utf-8'), 2: unicode('接头暗号不对', 'utf-8'), """ account = req.get_argument('account', '') platform = req.get_argument('pt', '') if not UnameUid.check_exist(account): return 1, None, {} # 查无此人 device_mark = req.get_argument('device_mark', '') device_mem = req.get_argument('device_mem', '') uu = UnameUid.get(account) sid, expired = uu.get_or_create_session_and_expired(force=True) server_list = ServerConfig.get().server_list() for s in server_list: uid = s['uid'] = uu.servers.get(s['server'], '') if uid: u = User.get(uid) commit = False if not (u.device_mark and u.device_mark == device_mark): u.device_mark = device_mark commit = True if not (u.device_mem and u.device_mem == device_mem): u.device_mem = device_mem commit = True if u.update_session_and_expired(sid, expired): commit = True if commit: u.save() uu.cur_platform = platform uu.save() return 0, None, { 'server_list': server_list, 'current_server': uu.current_server, 'ks': sid, }
def login_verify(authorization_code, app_type): """ sid用户会话验证 Args: authorization_code: authorization_code app_type: 标识ios类型,1表示正式版,0表示越狱版, 2表示android Returns: 用户标识id """ platform_mapping = {0: 'cmge', 1: 'cmgeapp', 2: 'androidcmge'} conf = PLATFORM_CMGE_SETTINGS[app_type] access_token = get_access_token(authorization_code, conf) if not access_token: return None userinfo_post_data = urllib.urlencode({ 'access_token': access_token, }) http_code, content = http.post(conf['user_info_uri'], userinfo_post_data) if http_code != 200: return None obj = json.loads(content) # {u'username': u'U12638056', u'cmStatus': 0, u'codes': u'0', u'id': 10721935, u'sdkuserid': u'U12638056'} if int(obj['codes']) != 0: return None openid = obj['id'] tel = obj.get('tel', '') if tel: # TODO: 前端发新包之后删除 # 2014年5月20日12:00~5月30日12:00 end_time = 1401422400 # 5月30日12:00 if time.time() < 30 * 24 * 3600 + end_time: from models.user import UnameUid, User account = '%s_%s' % (platform_mapping.get(app_type, ''), openid) uu = UnameUid.get(account) if uu.current_server: uid = uu.servers.get(uu.current_server) if uid: u = User.get(uid, uu.current_server) if not u.mobile: u.moblie = tel u.save() return { 'openid': openid, 'access_token': access_token, 'sdkuserid': obj['sdkuserid'], 'username': obj['username'], 'code': authorization_code, 'tel': tel, }
def new_account(req): """# new_account: 创建一个账户,作为用户绑定账户前的替代物 args: env: --- arg returns: 0 --- """ version = req.get_argument('version', '') f_account = 'fake_account_' + rand_string(8) while UnameUid.check_exist(f_account): f_account = 'fc' + rand_string(8) replace_lua_url = True if settings.ENV_NAME in [settings.ENV_IOS, settings.ENV_STG_IOS, settings.ENV_TEST_IOS] and \ version >= '1.2.7' else False return 0, None, { 'fake_account': f_account, 'server_list': ServerConfig.get().server_list(replace_lua_url=replace_lua_url) }
def api(self): import game_config if not game_config.is_config_out(): game_config.load_all() method = self.get_argument('method', 'new_user') from views import user msg = '' is_login = True if method == 'loading': rc, user, data = 0, None, {} # 通过version配置的格式 来判断是否提交了新版本的version # 如果提交了新版version 开始强制更新前端,则老版本前端包不再下载各个 game_config 配置,等到更新完新版客户端之后再去下载game_config 配置 # 老版本的version配置格式 {'version': '','url': '','msg': unicode('''《超级英雄》敬请期待!''','utf-8'),} # 新版本的version配置格式 按平台区分更新信息 {'sogou':[],'androidcmge':[],'itools':[], ...} # 这代码为了解决 前端 遗留bug, 等更新完新客户端之后 可删除 version_info = {} for version_info in game_config.version.itervalues(): break need_upgrade = 'tongbu' in version_info or 'cmge' in version_info or 'itools' in version_info version = self.get_argument('version', '1.0.2') if version < '1.0.7' and need_upgrade: is_login = False else: func = getattr(user, method) rc, user, data = func(self) method_param = '%s.%s' % ('user', func.__name__) if rc != 0: msg = game_config.return_msg_config.get(method_param, {}).get( rc, method_param + '_error_%d' % rc) # get_user_server_list 接口判断客户端是否需要强制更新 if method == 'new_user': from models.user import UnameUid account = self.get_argument('account', '') uu = UnameUid.get(account) cur_server = uu.current_server if cur_server and cur_server in game_config.version: client_upgrade = { 'url': 'http://www.baidu.com', # 前端需要跳转到的新包下载地址,一般是官网什么的 'msg': u'当前测试已经结束啦。感谢您对《禁区》的支持,我们将于2月中旬进行第二轮测试,敬请期待。', 'version': '', 'need_upgrade': False } _platform = account.split('_')[0].lower() _version_config = game_config.version[cur_server] _platform_config = _version_config.get(_platform, []) if _platform_config and len(_platform_config) == 3: # [[version], [url], [msg]] client_upgrade['version'] = _platform_config[0][0] client_upgrade['url'] = _platform_config[1][0] client_upgrade['msg'] = _platform_config[2][0] version = self.get_argument('version', '1.0.2') new_version = client_upgrade.get('version', '') if new_version and version < new_version: client_upgrade['need_upgrade'] = True d = { 'status': 0, 'data': {}, 'msg': '', 'user_status': {}, 'client_upgrade': client_upgrade } self.set_header('content_type', 'application/json; charset=UTF-8') self.set_header('Content-Type', 'application/json; charset=UTF-8') r = json.dumps(d, ensure_ascii=False, encoding="utf-8", indent=2, default=to_json) self.write(r) self.finish() return -99009, {}, '', None ######################################## self.set_header('content_type', 'application/json; charset=UTF-8') self.set_header('Content-Type', 'application/json; charset=UTF-8') try: r = handler_tools.result_generator(rc, data, msg, user, login=True, request=self) self.write(r) finally: self.finish() return rc, data, msg, user
def user(env, *args, **kwargs): ''' 根据qid查询玩家信息 :param env: :param args: :param kwargs: :return: ''' result = { # 应答信息模板 'result_code': '', # 查询应答码,0000为成功,0001为签名错误,0002为用户不存在 'result_msg': '', 'record': { 'timestamp': time.time(), 'user_info': '' } } # 签名算法 qid = env.get_argument('qid', '') sign = env.get_argument('sign', '') timestamp = env.get_argument('timestamp', '') sign_str = '%s#%s#%s#%s' % (APP_KEY, qid, timestamp, APP_SECRET) if sign != hashlib.md5(sign_str).hexdigest(): result['result_code'] = '0001' result['result_msg'] = 'sign err!' return result user_info_keys = [ 'server_id', # 区服编号 'server_name', # 区服名称(url编码) 'role_name', # 角色名称(url编码) 'gender', # 性别 'last_active_time', # 最后登陆时间 'time', # 在线时长 'association_name', # 帮派或阵容(url编码) 'occupation', # 职业 'level', # 角色等级 'state', # 角色状态,是否被禁用,0/1 'exp', # 经验值 'create_time', # 角色创建时间 ] account = '%s_%s' % ('360', qid) if not UnameUid.check_exist(account): # 判断该qid是否存在 result['result_msg'] = 'qid not exist!' result['result_code'] = '0003' return result uu = UnameUid.get(account) if not uu.servers: # 用户在所有服务器里都没有数据 result['result_msg'] = 'no user!' result['result_code'] = '0002' return result user_info_list = [] # 查询各服务器上的用户信息 for server, uid in uu.servers.iteritems(): result['result_msg'] = 'uid is %s' % uid # 将uid信息放进result_msg返回,方便调使用 user_info = {} user_info['server_id'] = server user_info['server_name'] = urllib.quote( game_config.get_server_name(server).encode('utf8')) u = User.get(uid) user_info['role_name'] = urllib.quote(u.name.encode('utf8')) user_info['last_active_time'] = u.active_time user_info['level'] = u.level user_info['state'] = u.is_ban user_info['occupation'] = u.uid user_info_str = '^'.join( [str(user_info.get(k, '')) for k in user_info_keys]) user_info_list.append(user_info_str) # 返回数据 result['result_code'] = '0000' result['record']['user_info'] = '|'.join(user_info_list) return result
def register(req): """# register: 注册新用户,并且将当前的uid绑定到用户上 args: req: --- arg returns: 0 --- 1: unicode('没有用户名或者密码', 'utf-8'), 3: unicode('这个账户已经有人了', 'utf-8'), 5: unicode('已经绑定的账户', 'utf-8'), 6: unicode('缺少uid', 'utf-8'), 7:unicode('账号只能为6-20位的字母数字组合', 'utf-8') """ account = req.get_argument('account', '') if not (account.isalnum() and 6 <= len(account) <= 20): return 7, None, {} print account, 'zzzzzzzzzzzzzzz00000000000' password = req.get_argument('passwd', '') old_account = req.get_argument('old_account', '') uid = req.get_argument('user_token', '') if not account or not password: return 1, None, {} # 没有用户名或者密码 # if not old_account: # return 2, None, {} # 没有老账户 if UnameUid.check_exist(account): return 3, None, {} # 这个账户已经有人了 if 'fake_account_' not in old_account or not UnameUid.check_exist(old_account): if old_account != account: uu = UnameUid.get(account) uu.passwd = hashlib.md5(password).hexdigest() server_list = ServerConfig.get().server_list() sid, expired = uu.get_or_create_session_and_expired() current_server = server_list[0]['server'] uu.current_server = current_server uu.save() print 'ddddddddddddddddd' return 0, None, { 'server_list': server_list, 'current_server': uu.current_server, 'ks': sid, # now + uid 唯一标识 } return 5, None, {} # 已经绑定的账户 if uid: user = User.get(uid) else: return 6, None, {} # 缺少uid # server_key = settings.SERVICE_NAME uu = UnameUid.get(old_account) uu.change_account_name(account) uu.passwd = hashlib.md5(password).hexdigest() # uu.servers[server_key] = user.uid # uu.current_server = server_key sid, expired = uu.get_or_create_session_and_expired(force=True) uu.save() for uid in uu.servers.itervalues(): us = UidServer.get(uid) us.account = account us.save() device_mark = req.get_argument('device_mark', '') device_mem = req.get_argument('device_mem', '') user.device_mark = device_mark user.device_mem = device_mem user.update_session_and_expired(sid, expired) user.account = account user.save() server_list = ServerConfig.get().server_list() for s in server_list: s['uid'] = user.uid return 0, user, { 'server_list': server_list, 'current_server': uu.current_server, 'ks': sid, }
def new_user(req): """# new_user: 新进入一个分服 args: env: --- arg returns: 0 --- return 1, None, {} # 必须指定account return 2, None, {} # 在这个服务器上已经有账户了 """ role = int(req.get_argument('role', '1')) account = req.get_argument('account', '') # 如果不是空字符串 # 给前端填坑 account = account.replace('37wanA545_', '37wan_') account = account.replace('pipa_new_', 'pipa_') platform = req.get_argument('pt', '') server_name = req.get_argument('server_name', '') if not server_name: server_list = ServerConfig.get().server_list() server_name = server_list[0]['server'] if not account: return 1, None, {} # 必须指定account try: for_ip = req.request.headers.get('X-Forwarded-For', '') ip = for_ip.replace('127.0.0.1', '').replace(',', '').replace(' ', '') except: ip = '' uid = new_uid(server_name) user = User.get(uid) user.account = account user.ip = ip us = UidServer.get(uid) us.server = server_name us.account = account uu = UnameUid.get(account) if server_name in uu.servers: return 2, None, {} # 在这个服务器上已经有账户了 uu.servers[server_name] = uid uu.current_server = server_name uu.init_platform = platform sid, expired = uu.get_or_create_session_and_expired(force=True) # 服务器中各个session_sid, session_expired过期时间 user.update_session_and_expired(sid, expired) # 更新本服服务器的session_sid, session_expired user.role = role role_detail_config = game_config.role_detail[role] role_config = game_config.role user.cards.position_num = role_config[1]['position_num'] # 位置数量 user.cards.alternate_num = role_config[1]['alternate_num'] # 助威数量 # user.cards.open_position = [int(x) for x in role_config[1]['open_position']] # 开启的位置 # user.cards.formation['own'] = [role_config[1]['open_formation']] # 阵型 # user.cards.formation['current'] = [role_config[1]['open_formation']] # 当前阵型 user.food_ability = role_detail_config['food_produce'] # 生食物能力 user.metal_ability = role_detail_config['metal_produce'] # 生铁能力 user.energy_ability = role_detail_config['energy_produce'] user.harbor_ability = role_config[1]['harbor'] # 避难所等级 user.school_ability = role_config[1]['school'] # 学校等级 user.factory_ability = role_config[1]['factory'] user.hospital_ability = role_config[1]['hospital'] user.laboratory_ability = role_config[1]['laboratory'] # 初始 food、metal 都为1000 # user.food = 99999 # user.metal = 99999 # user.crystal = 99999 # 能晶 # user.energy = 99999 # user.coin = 99999 user.food = 3000 user.metal = 1000 user.energy = 1000 # 精力 user.coin = 20 user.silver = 300 # 银币 user.crystal = 50 for i in xrange(1, 11): card = role_detail_config['position%d' % i] if not card: continue card_id = user.cards.new(card) user.cards.set_alignment(card_id, i if i <= 5 else i + 5) for i in role_detail_config['character_bag']: for j in xrange(i[1]): card_id = user.cards.new(i[0]) for i in role_detail_config['item']: user.item.add_item(i[0], i[1]) for i in role_detail_config['equip']: for ii in xrange(i[1]): user.equip.new(i[0]) user.is_new = 0 user.regist_time = int(time.time()) user.update_regist_status(user.regist_time) # user.exp += 20450 # 给渠道升级爽一爽 us.save() uu.save() user.save() user.cards.save() user.item.save() user.equip.save() return 0, user, {'uid': user.uid, 'ks': sid}
def get_user_server_list_huawei(req): """# login: 交给前端用户的server_list(此步不需要验证) args: req: --- arg returns: 0 --- """ account = req.get_argument('account') old_account = req.get_argument('old_account') if not UnameUid.check_exist(account) and not UnameUid.check_exist(old_account): return 0, None, { # 查无此人 'server_list': ServerConfig.get().server_list(), 'current_server': '', } if UnameUid.check_exist(account): uu = UnameUid.get(account) else: if UnameUid.check_exist(old_account): uu = UnameUid.get(account) old_uu = UnameUid.get(old_account) for k in uu._attrs.iterkeys(): setattr(uu, k, getattr(old_uu, k)) uu.save() old_uu_copy_key = old_account + '____copy' uu_copy = UnameUid.get(old_uu_copy_key) for k in uu_copy._attrs.iterkeys(): setattr(uu_copy, k, getattr(old_uu, k)) uu_copy.save() old_uu.redis.delete(old_uu._model_key) del old_uu else: return 0, None, { # 查无此人 'server_list': ServerConfig.get().server_list(), 'current_server': '', } sid, expired = uu.get_or_create_session_and_expired() device_mark = req.get_argument('device_mark', '') device_mem = req.get_argument('device_mem', '') ip = req.request.headers.get('X-Real-Ip', '') server_list = ServerConfig.get().server_list() for s in server_list: uid = s['uid'] = uu.servers.get(s['server'], '') s['level'] = 0 if uid: u = User.get(uid) s['level'] = u.level if u.regist_time and u.name: commit = False if not (u.device_mark and u.device_mark == device_mark): u.device_mark = device_mark commit = True if not (u.device_mem and u.device_mem == device_mem): u.device_mem = device_mem commit = True if not (u.ip and u.ip == ip): u.ip = ip commit = True if u.update_session_and_expired(sid, expired): commit = True if commit: u.save() return 0, None, { 'server_list': server_list, 'current_server': '', 'ks': sid, }