Exemplo n.º 1
0
def edit():
    try:
        user_key = request.headers.get("X-SecondLife-Owner-Key")
        user = get_user(user_key)
        region_name, cx, cy = get_region(request.headers.get("X-SecondLife-Region"))
        region = Region.get_or_insert(region_name, cx=cx, cy=cy)
        course_key = request.args.get("coursekey")
        action = request.args.get("action")
        course_name = request.args.get("coursename")
        mark_name = request.args.get("markname")
        mark = request.args.get("mark")
        turn = request.args.get("turn")
        comment = request.args.get("comment")

        if action in ("REPLM", "ADDM") and mark:
            x, y, _ = vector2floats(mark)
            gx = cx + x
            gy = cy + y

        course = Course.get(Key(course_key))
        if course is None or course.user.key() != user.key():
            return Response("\nSomething went wrong, course was not modified.")

        now = datetime.now()
        state = get_state(user)
        if state is None:
            raise ValueError("State missing!")

        memcache.delete("regioncourses-%s" % region_name)
        memcache.delete("course-%s" % course_key)
        i = state.mark
        if action == "DELC":
            deleted = deactivate_course(course)
            return Response("Course %s deleted. %d unused marks deleted." % (course.name, deleted))
        elif action == "RENC" and course_name:
            old_name = course.name
            course.name = course_name.strip()
            course.last_modified = now
            course.put()
            return Response("\nCourse %s was renamed to %s." % (old_name, course.name))
        elif action == "TPP":
            course.public = not course.public
            course.last_modified = now
            course.put()
            return Response("Course %s is now %s." % (course.name, "public" if course.public else "private"))
        elif action == "REPLM":
            msg = check_new_mark(region, course, turn, i, gx, gy)
            if msg:
                return Response(msg, status=406)

            mark = Mark.get(course.marks[i])
            mark.x = x
            mark.y = y
            mark.gx = gx
            mark.gy = gy
            mark.put()

            course.turns[i] = turn
            course.length = course_length(course)
            course.last_modified = now
            course.put()
            return Response("\nMark %d was replaced by a new mark." % (i + 1))
        elif action == "DELM":
            if len(course.marks) == MIN_COURSE_LENGTH:
                return Response(
                    "\nYou cannot delete a mark from a %d mark course, you can only replace them." % MIN_COURSE_LENGTH
                )
            elif 1 <= i < len(course.marks):
                mark = Mark.get(course.marks[i])
                del course.marks[i]
                del course.turns[i]
                dec_mark(mark)
                course.length = course_length(course)
                course.last_modified = now
                course.put()
                return Response("\nMark %d of course %s was deleted." % (i + 1, course.name))
            else:
                return Response("\nYou cannot delete the start/end mark, you can only replace them.")
        elif action == "ADDM":
            msg = check_new_mark(region, course, turn, i, gx, gy)
            if msg:
                return Response(msg, status=406)

            if 0 <= i < len(course.marks):
                mark = Mark(region=region, x=x, y=y, gx=gx, gy=gy, parent=user.key())
                mark.put()
                course.marks.insert(i + 1, mark.key())
                course.turns.insert(i + 1, turn)
                course.length = course_length(course)
                course.last_modified = now
                course.put()
                return Response("\nA %s mark was placed after mark %d of course %s." % (turn, i + 1, course.name))
            else:
                return Response("\nYou cannot add a mark after the finish mark.")
        elif action == "ANNC":
            course.comment = comment[:499]
            course.last_modified = now
            course.put()
            if len(comment) < 500:
                return Response("\nA comment was added to course %s." % course.name)
            else:
                return Response("\nThe comment was truncated to 500 characters and added to course %s." % course.name)
        elif action == "GMN" and mark_name:
            mark = Mark.get(course.marks[i])
            old_name = mark.name
            mark.name = mark_name.strip()
            mark.put()
            if old_name:
                return Response("\Mark %s was renamed to %s." % (old_name, mark.name))
            else:
                return Response("\nMark %d was named %s" % (i, mark.name))
        elif action == "ANNM":
            mark = Mark.get(course.marks[i])
            mark.comment = comment[:499]
            mark.put()
            if len(comment) < 500:
                return Response("\nA comment was added to mark %d of course %s." % (i + 1, course.name))
            else:
                return Response(
                    "\nThe comment was truncated to 500 characters and added to mark %d of course %s."
                    % (i + 1, course.name)
                )
    except db.Timeout:
        msg = "\nCourse was not modified. The database timed out."
        logging.error(msg)
        return Response(msg, status=500)
    except CapabilityDisabledError:
        msg = "\nCourse was not modified. Database could not be written to."
        logging.error(msg)
        return Response(msg, status=500)
    except TransactionFailedError:
        msg = "\nCourse was not modified. Transaction failed."
        logging.error(msg)
        return Response(msg, status=500)
    except Exception, e:
        if DEBUG:
            print_exc()
        logging.error(e)
        return Response("\nCourse was not modified. %s." % e, status=500)
Exemplo n.º 2
0
def addmark():
    try:
        user_key = request.headers.get("X-SecondLife-Owner-Key")
        user = get_user(user_key)
        region_name, cx, cy = get_region(request.headers.get("X-SecondLife-Region"))
        x, y = get_position(request.headers.get("X-SecondLife-Local-Position"))
        turn = request.args.get("turn")
        n = int(request.args.get("n", 0))

        course = deserialize_entities(memcache.get("course-%s" % user_key))
        if course is None:
            query = Course.all()
            query.filter("user ="******"saved =", False)
            query.filter("deactivated =", False)
            course = query.get()
            if not course:
                return Response("Course was not saved, something went wrong.", status=406)

        gx = cx + x
        gy = cy + y
        region = Region.get_or_insert(region_name, name=region_name, cx=cx, cy=cy)
        msg = check_new_mark(region, course, turn, n, gx, gy)
        if msg:
            return Response(msg, status=406)

        marks = deserialize_entities(memcache.get("marks-%s" % user_key))
        if marks is None:
            query = Mark.all()
            query.ancestor(user.key())
            query.filter("region =", region)
            query.filter("deactivated =", False)
            marks = [m for m in query]
            memcache.set(key="marks-%s" % user_key, value=serialize_entities(marks), time=CACHE_TIME)

        close_marks = sorted(
            [(m, dist(gx, gy, m.gx, m.gy)) for m in marks if close(gx, gy, m.gx, m.gy)], key=lambda o: o[1]
        )

        if close_marks:
            mark = close_marks[0][0]
            mark.used += 1
            mark.put()
        else:
            mark = Mark(region=region, x=x, y=y, gx=gx, gy=gy, parent=user.key())
            mark.put()
        course.marks.append(mark.key())
        course.turns.append(turn)
        course.put()

        if not marks is None:
            marks.append(mark)
        else:
            marks = [mark]
        memcache.set(key="marks-%s" % user_key, value=serialize_entities(marks), time=CACHE_TIME)
        memcache.set(key="course-%s" % user_key, value=serialize_entities(course), time=CACHE_TIME)

        if turn == "START":
            s = "\nStart mark saved. Go to the next mark and click the HUD."
        elif turn == "FINISH":
            s = "\nFinish mark saved."
        else:
            s = "\nMark %d saved. Go to the next mark and click the HUD." % len(course.marks)

        if close_marks:
            d = dist(gx, gy, mark.gx, mark.gy)
            return Response(s + " This mark was merged with a predefined mark %.1fm. away." % round(d, 1))
        else:
            return Response(s)
    except db.Timeout:
        msg = "\nMark was not saved. The database timed out."
        logging.error(msg)
        return Response(msg, status=500)
    except CapabilityDisabledError:
        msg = "\nMark was not saved. Database could not be written to."
        logging.error(msg)
        return Response(msg, status=500)
    except TransactionFailedError:
        msg = "\nMark was not saved. Transaction failed."
        logging.error(msg)
        return Response(msg, status=500)
    except Exception, e:
        if DEBUG:
            print_exc()
        logging.error(e)
        return Response("\nMark was not saved. %s." % e, status=500)