def unsub_game(): """取消订阅游戏 (GET|POST&LOGIN) :uri: /user/opt/unsubscribe-game :param game_id: 游戏id :returns: {} """ user = request.authed_user gid = request.values.get('game_id', None) game = Game.get_one(gid, check_online=False) if not game: return error.GameNotExist key = 'lock:unsubgame:%s' % (str(user._id)) with util.Lockit(Redis, key) as locked: if locked: return error.SubGameFailed('取消订阅失败') usg = UserSubGame.get_by_ship(str(user._id), gid) usg.delete_model() if usg else None return {}
def sub_game(): """订阅游戏 (GET|POST&LOGIN) :uri: /user/opt/subscribe-game :param game_id: 游戏id(批量订阅游戏id以逗号隔开) :returns: {} """ user = request.authed_user gids = request.values.get('game_id', None) if not gids: return error.GameNotExist gids = [gid.strip() for gid in gids.split(',')] games = Game.get_list(gids, check_online=False) if not games: return error.GameNotExist key = 'lock:subgame:%s' % (str(user._id)) with util.Lockit(Redis, key) as locked: if locked: return error.SubGameFailed sub_num = 0 for game in games: usg = UserSubGame.get_by_ship(str(user._id), str(game._id)) if not usg: usg = UserSubGame.init() usg.source = ObjectId(str(user._id)) usg.target = ObjectId(str(game._id)) usg.create_model() sub_num += 1 # 订阅游戏任务检查 if user: UserTask.check_user_tasks(str(user._id), SUB_GAME, sub_num) return {}
def user_sub_games(uid): """获取用户已订阅游戏 (GET) :uri: /users/<string:uid>/games :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'games': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values maxs = params.get('maxs', None) maxs = time.time() if maxs is not None and int( float(maxs)) == 0 else maxs and float(maxs) page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) games = list() gids = list() while len(games) < pagesize: gids = UserSubGame.sub_game_ids(uid, page, pagesize, maxs) # 下线游戏也需要展示在用户订阅列表中 games.extend([ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids, check_online=False) ]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = UserSubGame.get_by_ship(uid, gids[-1]) if gids else None maxs = obj.create_at if obj else 1000 if len(gids) < pagesize: break else: break return {'games': games, 'end_page': len(gids) != pagesize, 'maxs': maxs}