예제 #1
0
def upload_media(request, gameID):
    c = all_page_infos(request)

    replay = get_object_or_404(Replay, gameID=gameID)
    if not request.user in get_owner_list(replay.uploader):
        return HttpResponseRedirect(replay.get_absolute_url())

    UploadMediaFormSet = formset_factory(UploadMediaForm, extra=5)
    c["replay"] = replay
    c["replay_details"] = True

    if request.method == 'POST':
        logger.debug("request.FILES: %s", request.FILES)
        formset = UploadMediaFormSet(request.POST, request.FILES)
        if formset.is_valid():
            c["media_files"] = list()
            for form in formset:
                if form.cleaned_data:
                    logger.info("form.cleaned_data=%s", form.cleaned_data)
                    media = form.cleaned_data['media_file']
                    image = form.cleaned_data['image_file']
                    comment = form.cleaned_data['comment']
                    if media:
                        media.seek(0)
                        media_magic_text = magic.from_buffer(media.read(1024))
                        media.seek(0)
                        media_magic_mime = magic.from_buffer(media.read(1024),
                                                             mime=True)
                        media.seek(0)
                    else:
                        media_magic_text = None
                        media_magic_mime = None
                    erm = ExtraReplayMedia.objects.create(
                        replay=replay,
                        uploader=request.user,
                        comment=comment,
                        media=media,
                        image=image,
                        media_magic_text=media_magic_text,
                        media_magic_mime=media_magic_mime)
                    c["media_files"].append(erm)
                    logger.info(
                        "User '%s' uploaded for replay:'%s' media:'%s' img:'%s' with comment:'%s'.",
                        request.user, replay, erm.media, erm.image,
                        erm.comment[:20])
            return render(request, 'upload_media_success.html', c)
        else:
            logger.error("formset.errors: %s", formset.errors)
            logger.error("request.FILES: %s", request.FILES)
            logger.error("request.POST: %s", request.POST)
    else:
        formset = UploadMediaFormSet()
    c["formset"] = formset
    return render(request, 'upload_media.html', c)
예제 #2
0
def upload_media(request, gameID):
    c = all_page_infos(request)

    replay = get_object_or_404(Replay, gameID=gameID)
    if not request.user in get_owner_list(replay.uploader):
        return HttpResponseRedirect(replay.get_absolute_url())

    UploadMediaFormSet = formset_factory(UploadMediaForm, extra=5)
    c["replay"] = replay
    c["replay_details"] = True

    if request.method == 'POST':
        logger.debug("request.FILES: %s", request.FILES)
        formset = UploadMediaFormSet(request.POST, request.FILES)
        if formset.is_valid():
            c["media_files"] = list()
            for form in formset:
                if form.cleaned_data:
                    logger.info("form.cleaned_data=%s", form.cleaned_data)
                    media = form.cleaned_data['media_file']
                    image = form.cleaned_data['image_file']
                    comment = form.cleaned_data['comment']
                    if media:
                        media.seek(0)
                        media_magic_text = magic.from_buffer(media.read(1024))
                        media.seek(0)
                        media_magic_mime = magic.from_buffer(media.read(1024), mime=True)
                        media.seek(0)
                    else:
                        media_magic_text = None
                        media_magic_mime = None
                    erm = ExtraReplayMedia.objects.create(replay=replay, uploader=request.user, comment=comment,
                                                          media=media, image=image, media_magic_text=media_magic_text,
                                                          media_magic_mime=media_magic_mime)
                    c["media_files"].append(erm)
                    logger.info("User '%s' uploaded for replay:'%s' media:'%s' img:'%s' with comment:'%s'.",
                                request.user, replay, erm.media, erm.image, erm.comment[:20])
            return render(request, 'upload_media_success.html', c)
        else:
            logger.error("formset.errors: %s", formset.errors)
            logger.error("request.FILES: %s", request.FILES)
            logger.error("request.POST: %s", request.POST)
    else:
        formset = UploadMediaFormSet()
    c["formset"] = formset
    return render(request, 'upload_media.html', c)
예제 #3
0
def replay(request, gameID):
    c = all_page_infos(request)

    if not gameid_re.findall(gameID):
        raise Http404("Malformed gameID: %r." % gameID)

    try:
        replay = Replay.objects.prefetch_related().get(gameID=gameID)
        c["replay"] = replay
        c["comment_obj"] = replay
    except ObjectDoesNotExist:
        raise Http404("No replay with gameID '" + strip_tags(gameID) + "' found.")

    if not replay.published:
        return render(request, 'replay_unpublished.html', c)

    game = replay.game_release.game
    match_type = replay.match_type_short

    try:
        match_skills = get_sldb_match_skills([replay.gameID])
        if match_skills:
            match_skills = match_skills[0]
    except SLDBConnectionError as exc:
        logger.error("get_sldb_match_skills(%s): %s", [replay.gameID], exc)
        match_skills = {"status": 3}
        # ignore, we'll just use the old values from the DB in the view
    else:
        if match_skills and match_skills["status"] == 0:
            # update skill data in DB
            logger.debug("got match data for %s from sldb", replay)
            for player in match_skills["players"]:
                pa = player["account"]
                pa_skill = pa.get_rating(game, match_type)
                mu, si = player["skills"][1]
                if pa_skill.trueskill_mu != mu or pa_skill.trueskill_sigma != si:
                    try:
                        playername = Player.objects.get(account=pa, replay=replay, spectator=False).name
                    except ObjectDoesNotExist:
                        playername = "??"
                    pa_skill.trueskill_mu = mu
                    pa_skill.trueskill_sigma = si
                    if pa_skill.playername == "" or pa_skill.playername == "??":
                        pa_skill.playername = playername
                    pa_skill.save()
                    defaults = dict(
                        match_date=replay.unixTime,
                        playername=playername,
                        trueskill_mu=mu,
                        trueskill_sigma=si
                    )
                    RatingHistory.objects.update_or_create(match=replay,
                                                           game=game,
                                                           match_type=match_type,
                                                           playeraccount=pa,
                                                           defaults=defaults)
                if pa.sldb_privacy_mode != player["privacyMode"]:
                    pa.sldb_privacy_mode = player["privacyMode"]
                    pa.save()
                if not replay.rated:
                    replay.rated = True
                    replay.save()
        else:
            # ignore, we'll just use the old values from the DB in the view
            logger.debug("no match data from SLDB")
            pass

    # fill cache prefetching all entries from DB in one call
    all_players = Player.objects.filter(replay=replay)
    allyteams = Allyteam.objects.filter(replay=replay)
    if not allyteams.filter(winner=True).exists() or (
            replay.upload_date.year >= 2016 and allyteams.filter(winner=True, num=0).exists()):
        # workaround for issue #89: guess winner from ratings
        fix_missing_winner(replay)

    c["allyteams"] = []
    match_rating_history = RatingHistory.objects.filter(match=replay, match_type=match_type)
    [entry for entry in match_rating_history]  # prefetch all ratings of this match by hitting the DB only once
    for at in allyteams:
        playeraccounts = PlayerAccount.objects.filter(player__team__allyteam=at).order_by("player__team__num")
        teams = Team.objects.filter(allyteam=at)
        players = all_players.filter(account__in=playeraccounts)
        players_w_rating = list()
        old_rating = 0
        new_rating = 0
        lobby_rank_sum = 0
        if replay.rated == False or replay.notcomplete or not players.exists() or not replay.game_release.game.sldb_name or all_players.filter(
                account__accountid=0).exists():
            # notcomplete, no SLDB rating or bot present -> no rating
            players_w_rating = [(player, None, None) for player in players]
        else:
            # TrueSkill ratings
            for pa in playeraccounts:
                if match_skills and match_skills["status"] == 0:
                    # use SLDB-provided values
                    def _get_players_skills(pa):
                        for pl in match_skills["players"]:
                            if pl["account"] == pa: return pl

                    pl = _get_players_skills(pa)
                    pl_new = pl["skills"][1][0]
                    pl_old = pl["skills"][0][0]
                else:
                    # use old method of DB lookups for current and previous matchs DB entries
                    try:
                        pl_new = match_rating_history.get(playeraccount=pa).trueskill_mu
                    except ObjectDoesNotExist:
                        # no rating on this replay
                        pl_new = None
                    try:
                        # find previous TS value
                        pl_old = RatingHistory.objects.filter(playeraccount=pa, game=game, match_type=match_type,
                                                              match__unixTime__lt=replay.unixTime).order_by(
                            "-match__unixTime")[0].trueskill_mu
                    except IndexError:
                        pl_old = 25  # default value for new players

                # privatize?
                if playeraccounts.count() > 2 or pa.sldb_privacy_mode == 0:
                    new_rating += pl_new if pl_new else 0
                    old_rating += pl_old if pl_old else 0
                else:
                    new_rating += privatize_skill(pl_new) if pl_new else 0
                    old_rating += privatize_skill(pl_old) if pl_old else 0

                if pa.sldb_privacy_mode != 0 and (
                    not request.user.is_authenticated() or pa.accountid != request.user.userprofile.accountid):
                    if pl_new:
                        pl_new = privatize_skill(pl_new)
                    if pl_old:
                        pl_old = privatize_skill(pl_old)
                players_w_rating.append((all_players.get(account=pa, spectator=False), pl_old, pl_new))

        if teams:
            lobby_rank_sum = reduce(lambda x, y: x + y, [pl.rank for pl in all_players.filter(team__allyteam=at)], 0)
            c["allyteams"].append((at, players_w_rating, old_rating, new_rating, lobby_rank_sum))

    c["has_bot"] = replay.tags.filter(name="Bot").exists()
    c["specs"] = all_players.filter(replay=replay, spectator=True).order_by("id")
    c["upload_broken"] = UploadTmp.objects.filter(replay=replay).exists()
    c["mapoptions"] = MapOption.objects.filter(replay=replay).order_by("name")
    c["modoptions"] = ModOption.objects.filter(replay=replay).order_by("name")
    c["replay_details"] = True
    c["was_stopped"] = not allyteams.filter(winner=True).exists()
    if c["was_stopped"]:
        logger.info("was_stopped=True: allyteams=%r replay=%r", allyteams, replay)
    c["is_draw"] = allyteams.filter(winner=True).count() > 1
    c["pagedescription"] = "%s %s %s match on %s (%s)" % (
    replay.num_players, replay.match_type, replay.game_release.game.name, replay.map_info.name, replay.unixTime)
    c["replay_owners"] = get_owner_list(replay.uploader)
    c["extra_media"] = ExtraReplayMedia.objects.filter(replay=replay)
    c["known_video_formats"] = ["video/webm", "video/mp4", "video/ogg", "video/x-flv", "application/ogg"]
    c["has_video"] = c["extra_media"].filter(media_magic_mime__in=c["known_video_formats"]).exists()
    c["metadata"] = list()
    if replay.map_info.width > 128:
        # api.springfiles.com returnd pixel size
        map_px_x = replay.map_info.width / 512
        map_px_y = replay.map_info.height / 512
    else:
        # api.springfiles.com returnd Spring Map Size
        map_px_x = replay.map_info.width
        map_px_y = replay.map_info.height
    try:
        c["metadata"].append(("Size", "{} x {}".format(map_px_x, map_px_y)))
        try:
            c["metadata"].append(("Wind", "{} - {}".format(replay.map_info.metadata["metadata"]["MinWind"],
                                                           replay.map_info.metadata["metadata"]["MaxWind"])))
        except KeyError:
            pass
        try:
            c["metadata"].append(("Tidal", str(replay.map_info.metadata["metadata"]["TidalStrength"])))
        except KeyError:
            pass
        for k, v in replay.map_info.metadata["metadata"].items():
            if type(v) == str and not v.strip():
                continue
            elif type(v) == list and not v:
                continue
            elif k.strip() in ["", "Width", "TidalStrength", "MapFileName", "MapMinHeight", "Type", "MapMaxHeight",
                               "Resources", "Height", "MinWind", "MaxWind", "StartPos"]:
                # either already added above, or ignore uninteresting data
                continue
            else:
                c["metadata"].append((k.strip(), v))
        try:
            if replay.map_info.metadata["version"]:
                c["metadata"].append(("Version", replay.map_info.metadata["version"]))
        except KeyError:
            pass
    except Exception as exc:
        c["metadata"].append(("Error", "Problem with metadata. Please report to Dansan."))
        logger.error("FIXME: to broad exception handling.")
        logger.error("Problem with metadata (replay.id '%d'), replay.map_info.metadata: %s", replay.id,
                     replay.map_info.metadata)
        logger.exception("Exception: %s", exc)
    c["xtaward_heroes"] = XTAwards.objects.filter(replay=replay, isAlive=1)
    c["xtaward_los"] = XTAwards.objects.filter(replay=replay, isAlive=0)

    page_history = request.session.get("page_history")
    if page_history and isinstance(page_history, list):
        # check data (session data is user input)
        for page in list(page_history):
            if not gameid_re.findall(page):
                page_history.remove(page)
        if gameID not in page_history:
            if len(page_history) > 4:
                page_history.remove(page)
            page_history.insert(0, gameID)
    else:
        page_history = [gameID]
    request.session["page_history"] = page_history

    return render(request, 'replay.html', c)
예제 #4
0
def replay(request, gameID):
    c = all_page_infos(request)

    if not gameid_re.findall(gameID):
        raise Http404("Malformed gameID: %r." % gameID)

    try:
        replay = Replay.objects.prefetch_related().get(gameID=gameID)
        c["replay"] = replay
        c["comment_obj"] = replay
    except ObjectDoesNotExist:
        raise Http404("No replay with gameID '" + strip_tags(gameID) +
                      "' found.")

    if not replay.published:
        return render(request, 'replay_unpublished.html', c)

    game = replay.game_release.game
    match_type = replay.match_type_short

    sldb_connection_error = False
    try:
        match_skills = get_sldb_match_skills([replay.gameID])
        if match_skills:
            match_skills = match_skills[0]
    except SLDBConnectionError as exc:
        sldb_connection_error = True
        logger.error("get_sldb_match_skills(%s): %s", [replay.gameID], exc)
        match_skills = {"status": 3}
        # ignore, we'll just use the old values from the DB in the view
    else:
        if match_skills and match_skills["status"] == 0:
            # update skill data in DB
            logger.debug("got match data for %s from sldb", replay)
            for player in match_skills["players"]:
                pa = player["account"]
                pa_skill = pa.get_rating(game, match_type)
                mu, si = player["skills"][1]
                if pa_skill.trueskill_mu != mu or pa_skill.trueskill_sigma != si:
                    try:
                        playername = Player.objects.get(account=pa,
                                                        replay=replay,
                                                        spectator=False).name
                    except ObjectDoesNotExist:
                        playername = "??"
                    pa_skill.trueskill_mu = mu
                    pa_skill.trueskill_sigma = si
                    if pa_skill.playername == "" or pa_skill.playername == "??":
                        pa_skill.playername = playername
                    pa_skill.save()
                    defaults = dict(match_date=replay.unixTime,
                                    playername=playername,
                                    trueskill_mu=mu,
                                    trueskill_sigma=si)
                    RatingHistory.objects.update_or_create(
                        match=replay,
                        game=game,
                        match_type=match_type,
                        playeraccount=pa,
                        defaults=defaults)
                if pa.sldb_privacy_mode != player["privacyMode"]:
                    pa.sldb_privacy_mode = player["privacyMode"]
                    pa.save()
                if not replay.rated:
                    replay.rated = True
                    replay.save()
        else:
            # ignore, we'll just use the old values from the DB in the view
            logger.debug("no match data from SLDB")
            pass

    # fill cache prefetching all entries from DB in one call
    all_players = Player.objects.filter(replay=replay)
    allyteams = Allyteam.objects.filter(replay=replay)
    if not allyteams.filter(winner=True).exists() or (
            replay.upload_date.year >= 2016 and allyteams.filter(
                winner=True, num=0).exists()) and not sldb_connection_error:
        # workaround for issue #89: guess winner from ratings
        fix_missing_winner(replay)

    c["allyteams"] = []
    match_rating_history = RatingHistory.objects.filter(match=replay,
                                                        match_type=match_type)
    [entry for entry in match_rating_history
     ]  # prefetch all ratings of this match by hitting the DB only once
    for at in allyteams:
        playeraccounts = PlayerAccount.objects.filter(
            player__team__allyteam=at).order_by("player__team__num")
        teams = Team.objects.filter(allyteam=at)
        players = all_players.filter(account__in=playeraccounts)
        players_w_rating = list()
        old_rating = 0
        new_rating = 0
        lobby_rank_sum = 0
        if replay.rated == False or replay.notcomplete or not players.exists(
        ) or not replay.game_release.game.sldb_name or all_players.filter(
                account__accountid=0).exists():
            # notcomplete, no SLDB rating or bot present -> no rating
            players_w_rating = [(player, None, None) for player in players]
        else:
            # TrueSkill ratings
            for pa in playeraccounts:
                if match_skills and match_skills["status"] == 0:
                    # use SLDB-provided values
                    def _get_players_skills(pa):
                        for pl in match_skills["players"]:
                            if pl["account"] == pa: return pl

                    pl = _get_players_skills(pa)
                    pl_new = pl["skills"][1][0]
                    pl_old = pl["skills"][0][0]
                else:
                    # use old method of DB lookups for current and previous matchs DB entries
                    try:
                        pl_new = match_rating_history.get(
                            playeraccount=pa).trueskill_mu
                    except ObjectDoesNotExist:
                        # no rating on this replay
                        pl_new = None
                    try:
                        # find previous TS value
                        pl_old = RatingHistory.objects.filter(
                            playeraccount=pa,
                            game=game,
                            match_type=match_type,
                            match__unixTime__lt=replay.unixTime).order_by(
                                "-match__unixTime")[0].trueskill_mu
                    except IndexError:
                        pl_old = 25  # default value for new players

                # privatize?
                if playeraccounts.count() > 2 or pa.sldb_privacy_mode == 0:
                    new_rating += pl_new if pl_new else 0
                    old_rating += pl_old if pl_old else 0
                else:
                    new_rating += privatize_skill(pl_new) if pl_new else 0
                    old_rating += privatize_skill(pl_old) if pl_old else 0

                if pa.sldb_privacy_mode != 0 and (
                        not request.user.is_authenticated()
                        or pa.accountid != request.user.userprofile.accountid):
                    if pl_new:
                        pl_new = privatize_skill(pl_new)
                    if pl_old:
                        pl_old = privatize_skill(pl_old)
                players_w_rating.append(
                    (all_players.get(account=pa,
                                     spectator=False), pl_old, pl_new))

        if teams:
            lobby_rank_sum = reduce(
                lambda x, y: x + y,
                [pl.rank for pl in all_players.filter(team__allyteam=at)], 0)
            c["allyteams"].append(
                (at, players_w_rating, old_rating, new_rating, lobby_rank_sum))

    c["has_bot"] = replay.tags.filter(name="Bot").exists()
    c["specs"] = all_players.filter(replay=replay,
                                    spectator=True).order_by("id")
    c["upload_broken"] = UploadTmp.objects.filter(replay=replay).exists()
    c["mapoptions"] = MapOption.objects.filter(replay=replay).order_by("name")
    c["modoptions"] = ModOption.objects.filter(replay=replay).order_by("name")
    c["replay_details"] = True
    c["was_stopped"] = not allyteams.filter(winner=True).exists()
    if c["was_stopped"]:
        logger.info("was_stopped=True: allyteams=%r replay=%r", allyteams,
                    replay)
    c["is_draw"] = allyteams.filter(winner=True).count() > 1
    c["pagedescription"] = "%s %s %s match on %s (%s)" % (
        replay.num_players, replay.match_type, replay.game_release.game.name,
        replay.map_info.name, replay.unixTime)
    c["replay_owners"] = get_owner_list(replay.uploader)
    c["extra_media"] = ExtraReplayMedia.objects.filter(replay=replay)
    c["known_video_formats"] = [
        "video/webm", "video/mp4", "video/ogg", "video/x-flv",
        "application/ogg"
    ]
    c["has_video"] = c["extra_media"].filter(
        media_magic_mime__in=c["known_video_formats"]).exists()
    c["metadata"] = list()
    if replay.map_info.width > 128:
        # api.springfiles.com returnd pixel size
        map_px_x = replay.map_info.width / 512
        map_px_y = replay.map_info.height / 512
    else:
        # api.springfiles.com returnd Spring Map Size
        map_px_x = replay.map_info.width
        map_px_y = replay.map_info.height
    try:
        c["metadata"].append(("Size", "{} x {}".format(map_px_x, map_px_y)))
        try:
            c["metadata"].append(("Wind", "{} - {}".format(
                replay.map_info.metadata["metadata"]["MinWind"],
                replay.map_info.metadata["metadata"]["MaxWind"])))
        except KeyError:
            pass
        try:
            c["metadata"].append(
                ("Tidal",
                 str(replay.map_info.metadata["metadata"]["TidalStrength"])))
        except KeyError:
            pass
        for k, v in replay.map_info.metadata["metadata"].items():
            if type(v) == str and not v.strip():
                continue
            elif type(v) == list and not v:
                continue
            elif k.strip() in [
                    "", "Width", "TidalStrength", "MapFileName",
                    "MapMinHeight", "Type", "MapMaxHeight", "Resources",
                    "Height", "MinWind", "MaxWind", "StartPos"
            ]:
                # either already added above, or ignore uninteresting data
                continue
            else:
                c["metadata"].append((k.strip(), v))
        try:
            if replay.map_info.metadata["version"]:
                c["metadata"].append(
                    ("Version", replay.map_info.metadata["version"]))
        except KeyError:
            pass
    except Exception as exc:
        c["metadata"].append(
            ("Error", "Problem with metadata. Please report to Dansan."))
        logger.error("FIXME: to broad exception handling.")
        logger.error(
            "Problem with metadata (replay.id '%d'), replay.map_info.metadata: %s",
            replay.id, replay.map_info.metadata)
        logger.exception("Exception: %s", exc)
    c["xtaward_heroes"] = XTAwards.objects.filter(replay=replay, isAlive=1)
    c["xtaward_los"] = XTAwards.objects.filter(replay=replay, isAlive=0)

    page_history = request.session.get("page_history")
    if page_history and isinstance(page_history, list):
        # check data (session data is user input)
        for page in list(page_history):
            if not gameid_re.findall(page):
                page_history.remove(page)
        if gameID not in page_history:
            if len(page_history) > 4:
                page_history.remove(page)
            page_history.insert(0, gameID)
    else:
        page_history = [gameID]
    request.session["page_history"] = page_history

    return render(request, 'replay.html', c)