def save_level_for_editor(request, levelId=None): """ Processes a request on creation of the map in the level editor """ data = json.loads(request.POST['data']) if levelId is not None: level = get_object_or_404(Level, id=levelId) else: level = Level(default=False, anonymous=data['anonymous']) if permissions.can_create_level(request.user): level.owner = request.user.userprofile if not permissions.can_save_level(request.user, level): return HttpResponseUnauthorized() pattern = re.compile("^(\w?[ ]?)*$") if pattern.match(data['name']): level_management.save_level(level, data) # Add the teacher automatically if it is a new level and the student is not independent if ((levelId is None) and hasattr(level.owner, 'student') and not level.owner.student.is_independent()): level.shared_with.add(level.owner.student.class_field.teacher.user.user) level.save() level_management.email_new_custom_level(level.owner.student.class_field.teacher.new_user.email, request.build_absolute_uri(reverse('level_moderation')), request.build_absolute_uri(reverse('play_custom_level', kwargs={'levelId': level.id})), request.build_absolute_uri(reverse('home')), str(level.owner.student), level.owner.student.class_field.name) response = {'id': level.id} return HttpResponse(json.dumps(response), content_type='application/javascript') else: return HttpResponseUnauthorized()
def create_save_level(teacher): data = { u"origin": u'{"coordinate":[3,5],"direction":"S"}', u"pythonEnabled": False, u"decor": [], u"blocklyEnabled": True, u"blocks": [ {u"type": u"move_forwards"}, {u"type": u"turn_left"}, {u"type": u"turn_right"}, ], u"max_fuel": u"50", u"pythonViewEnabled": False, u"character": u"3", u"name": u"abc", u"theme": 1, u"anonymous": False, u"cows": u"[]", u"path": u'[{"coordinate":[3,5],"connectedNodes":[1]},{"coordinate":[3,4],"connectedNodes":[0]}]', u"traffic_lights": u"[]", u"destinations": u"[[3,4]]", } level = Level(default=False, anonymous=data["anonymous"]) level.owner = teacher.user.user.userprofile level_management.save_level(level, data) level.save() return level.id
def create_save_level(teacher): data = {u'origin': u'{"coordinate":[3,5],"direction":"S"}', u'pythonEnabled': False, u'decor': [], u'blocklyEnabled': True, u'blocks': [{u'type': u'move_forwards'}, {u'type': u'turn_left'}, {u'type': u'turn_right'}], u'max_fuel': u'50', u'pythonViewEnabled': False, u'character': u'3', u'name': u'abc', u'theme': 1, u'anonymous': False, u'cows': u'[]', u'path': u'[{"coordinate":[3,5],"connectedNodes":[1]},{"coordinate":[3,4],"connectedNodes":[0]}]', u'traffic_lights': u'[]', u'destinations': u'[[3,4]]'} level = Level(default=False, anonymous=data['anonymous']) level.owner = teacher.user.user.userprofile level_management.save_level(level, data) level.save() return level.id
def setUpClass(cls): BaseGameTest.setUpClass() grass = get_theme(name='grass') van = get_character('Van') TestCowCrashes.cow_level = Level(name='Cow crashing', anonymous=False, blocklyEnabled=True, character=van, cows='[{"minCows":"7","maxCows":"7","potentialCoordinates":[{"x":4,"y":4},{"x":2,"y":4},{"x":3,"y":7},{"x":4,"y":6},{"x":2,"y":6},{"x":3,"y":1},{"x":4,"y":2}],"type":"WHITE"}]', default=False, destinations='[[4,5]]', direct_drive=True, fuel_gauge=False, max_fuel=50, model_solution='[1]', origin='{"coordinate":[2,5],"direction":"E"}', path='[{"coordinate":[2,5],"connectedNodes":[1]},{"coordinate":[3,5],"connectedNodes":[0,4,2,5]},{"coordinate":[4,5],"connectedNodes":[1]},{"coordinate":[3,7],"connectedNodes":[4]},{"coordinate":[3,6],"connectedNodes":[8,3,6,1]},{"coordinate":[3,4],"connectedNodes":[10,1,11,16]},{"coordinate":[4,6],"connectedNodes":[4,7]},{"coordinate":[4,7],"connectedNodes":[6]},{"coordinate":[2,6],"connectedNodes":[9,4]},{"coordinate":[2,7],"connectedNodes":[8]},{"coordinate":[2,4],"connectedNodes":[13,5,12]},{"coordinate":[4,4],"connectedNodes":[5,14,15]},{"coordinate":[2,3],"connectedNodes":[10]},{"coordinate":[1,4],"connectedNodes":[10]},{"coordinate":[5,4],"connectedNodes":[11]},{"coordinate":[4,3],"connectedNodes":[11,19]},{"coordinate":[3,3],"connectedNodes":[5,17]},{"coordinate":[3,2],"connectedNodes":[18,16,19,20]},{"coordinate":[2,2],"connectedNodes":[17]},{"coordinate":[4,2],"connectedNodes":[17,15,23,22]},{"coordinate":[3,1],"connectedNodes":[21,17,22]},{"coordinate":[2,1],"connectedNodes":[20]},{"coordinate":[4,1],"connectedNodes":[20,19]},{"coordinate":[5,2],"connectedNodes":[19]}]', pythonEnabled=False, theme=grass, threads=1, traffic_lights='[]', ) TestCowCrashes.cow_level.save() blocks = Block.objects.filter(type__in=["move_forwards", "turn_left", "turn_right"]) for block in blocks: new_block = LevelBlock(type=block, number=None, level=TestCowCrashes.cow_level) new_block.save()
def create_save_level(teacher): data = { u"origin": u'{"coordinate":[3,5],"direction":"S"}', u"pythonEnabled": False, u"decor": [], u"blocklyEnabled": True, u"blocks": [ { u"type": u"move_forwards" }, { u"type": u"turn_left" }, { u"type": u"turn_right" }, ], u"max_fuel": u"50", u"pythonViewEnabled": False, u"character": u"3", u"name": u"abc", u"theme": 1, u"anonymous": False, u"cows": u"[]", u"path": u'[{"coordinate":[3,5],"connectedNodes":[1]},{"coordinate":[3,4],"connectedNodes":[0]}]', u"traffic_lights": u"[]", u"destinations": u"[[3,4]]", } level = Level(default=False, anonymous=data["anonymous"]) level.owner = teacher.user.user.userprofile level_management.save_level(level, data) level.save() return level.id
def save_level_for_editor(request, levelId=None): """ Processes a request on creation of the map in the level editor """ data = json.loads(request.POST['data']) if levelId is not None: level = get_object_or_404(Level, id=levelId) else: level = Level(default=False, anonymous=data['anonymous']) if permissions.can_create_level(request.user): level.owner = request.user.userprofile if not permissions.can_save_level(request.user, level): return HttpResponseUnauthorized() level_management.save_level(level, data) # Add the teacher automatically if it is a new level and the student is not independent if ((levelId is None) and hasattr(level.owner, 'student') and not level.owner.student.is_independent()): level.shared_with.add(level.owner.student.class_field.teacher.user.user) level.save() response = {'id': level.id} return HttpResponse(json.dumps(response), content_type='application/javascript')
def save_level_for_editor(request, levelId=None): """ Processes a request on creation of the map in the level editor """ data = json.loads(request.POST["data"]) if levelId is not None: level = get_object_or_404(Level, id=levelId) else: level = Level(default=False, anonymous=data["anonymous"]) if permissions.can_create_level(request.user): level.owner = request.user.userprofile if not permissions.can_save_level(request.user, level): return HttpResponseUnauthorized() pattern = re.compile("^(\w?[ ]?)*$") if pattern.match(data["name"]): level_management.save_level(level, data) # Add the teacher automatically if it is a new level and the student is not # independent if ( (levelId is None) and hasattr(level.owner, "student") and not level.owner.student.is_independent() ): level.shared_with.add(level.owner.student.class_field.teacher.user.user) level.save() if not data["anonymous"]: level_management.email_new_custom_level( level.owner.student.class_field.teacher.new_user.email, request.build_absolute_uri(reverse("level_moderation")), request.build_absolute_uri( reverse("play_custom_level", kwargs={"levelId": level.id}) ), request.build_absolute_uri(reverse("home")), str(level.owner.student), level.owner.student.class_field.name, ) response = {"id": level.id} return HttpResponse(json.dumps(response), content_type="application/javascript") else: return HttpResponseUnauthorized()
def generate_level(character: Character): # Build the tiles level = Level( name="The Beginning", depth=1, tiles=""" .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... """, character=character, ) level.save() # Add the character to this level character.current_level = level character.x = 5 character.y = 5 character.save() # Create a creature ctype = random.choice([c for c in CreatureType.objects.all()]) c = Creature( current_level=level, x=0, y=0, type=ctype, current_hp=ctype.base_hp ) c.save() return level