def create (request): form_data = h.update_request_params(request.matchdict) form = forms.CreateGameSession(form_data) if not form.validate(): return HTTPBadRequest() user = request.user session = models.GameSession() form.populate_obj(session) session.board_image = "" session.status = constants.GameSessionStatuses.AVAILABLE session.add() character = models.SessionCharacter() character.user_id = user.id character.user_role = constants.SessionCharacterRoles.MASTER character.name = user.nickname character.game_session = session character.token = hashlib.sha256('{0}{1}{2}'.format(user.id, time.time(), random.randint(0, 1000))).hexdigest() character.add() DBSession.flush() return HTTPFound(location = request.route_url('play', game_session_id = session.id))
def join (request): user = request.user form_data = h.update_request_params({'user_id': user.id}, request.matchdict) form = forms.JoinGameSession(form_data) if not form.validate(): return HTTPBadRequest() game_session = models.GameSession.query.filter_by(id = form.game_session_id.data, status = constants.GameSessionStatuses.AVAILABLE).first() if not game_session: return HTTPBadRequest() already_joined = models.SessionCharacter.query.filter_by(game_session_id = form.game_session_id.data, user_id = user.id).first() if already_joined: return HTTPFound(location = request.route_url('play', game_session_id = form.game_session_id.data)) if game_session.players_joined >= game_session.rules.max_players: # TODO: Valid message? return HTTPFound(location = request.route_url('games.list')) skills_by_cat = {} for skill_category in game_session.rules.skills_categories: skills_by_cat[skill_category.id] = [] skills_by_cat[None] = [] # No category for skill in game_session.rules.skills: skills_by_cat[skill.category_id].append(skill) free_skill_points = dict([(cat.id, cat.base_value) for cat in game_session.rules.skills_categories]) rules = game_session.rules form_data = h.update_request_params({'game_session_id': game_session.id, 'name': request.POST.get('name', user.nickname)}, request.POST) create_form = forms.CreateSessionCharacter(form_data) create_form.race.query = rules.races_query.filter_by(is_disabled = False) create_form.cls.query = rules.classes_query.filter_by(is_disabled = False) if request.method == 'POST': if create_form.validate(): return _create_character(request, create_form, game_session) return { 'game_session': game_session, 'skills_by_cat': skills_by_cat, 'free_skill_points': free_skill_points, 'rules': rules, 'form': create_form }
def get_form_params (self): if 'hash' in self.request.POST: return self.request.POST _hash = hashlib.sha256(str(time.time())).hexdigest() form_params = helpers.update_request_params(self.request.POST, {'hash': _hash}) return form_params
def _authenticate (self, data, request): request_params = h.update_request_params({'email': data['userid'], 'password': data['password']}, {}) form = forms.AuthLogin(request_params) if not form.validate(): return False user = models.User.query.filter_by(email = form.email.data).first() if not user or user.status != constants.UserStatus.ACTIVE: return False user.last_login = datetime.datetime.now() user.add() return True
def edit (request): user = request.user password_form = forms.ProfilePasswordChange() action = None if 'change_password' in request.POST: form_data = h.update_request_params({'email': user.email}, request.POST) password_form = forms.ProfilePasswordChange(form_data) if password_form.validate(): user.set_password(password_form.password.data) user.add() action = 'change_password' return { 'password_form': password_form, 'success_action': action, }
def restore_password (request): req_hash = request.matchdict['rhash'] form_params = h.update_request_params(request.POST, {'req_hash': req_hash}) form = forms.AuthRestorePassword(form_params) form.req_hash.query = models.RestorePasswordRequest.query.filter_by(is_used = False) if request.method == 'POST' and form.validate(): restore_request = form.req_hash.data user = restore_request.user user.set_password(form.password.data) user.add() restore_request.is_used = True restore_request.add() return HTTPFound(location = request.route_url('auth.login')) return {'form': form}
def get_form_params (self): obj_id = self.request.matchdict['id'] form_params = helpers.update_request_params(self.request.POST, {'id': obj_id}) return form_params
def play (request): form_data = h.update_request_params(request.matchdict) form = forms.Play(form_data) if not form.validate(): return HTTPUnprocessableEntity() game_session_id = form.game_session_id.data game_session = models.GameSession.query.filter_by(id = game_session_id, status = constants.GameSessionStatuses.AVAILABLE).first() if not game_session: return HTTPFound(location = request.route_url('games.list')) user_role = request.user.get_role_for_session(game_session_id) if user_role is None: return HTTPFound(location = request.route_url('games.join', game_session_id = game_session_id)) rules_id_sq = DBSession.query(models.GameSession.rules_id).filter_by(id = game_session_id).subquery() rules = models.GameRules.query.filter_by(id = rules_id_sq).first() if not rules: return HTTPUnprocessableEntity() master = models.SessionCharacter.query.filter_by(game_session_id = game_session_id, user_role = "master").first() character = models.SessionCharacter.query.filter_by(game_session_id = game_session_id, user_id = request.user.id).first() players = [] the_player = None the_player_id = -1 character_token = None if not character: user_role = "spectator" elif master.id == character.id: user_role = "master" character_token = master.token else: the_player = character the_player_id = character.id players.append(character) user_role = "player" character_token = character.token other_players = models.SessionCharacter.query.filter(models.SessionCharacter.game_session_id == game_session_id, models.SessionCharacter.user_role == "player", models.SessionCharacter.user_id != request.user.id).order_by('id').all() players += other_players players_dicts = [] for player in players: with_items = the_player is not None and player.id == the_player.id players_dicts.append(player.as_dict(with_items = with_items)) cols_for_char_tbl = _get_cols_for_characters_table(rules) item_groups = [] for item_group_query_tpl in rules.get_child_query_by_priority('item_groups').filter_by(is_disabled = False): item_groups.append({ 'id': item_group_query_tpl.ItemGroup.id, 'title': item_group_query_tpl.ItemGroup.title, }) skills_categories = [cat.as_dict() for cat in models.SkillCategory.query.filter_by(rules_id = rules.id, is_disabled = False)] items = [x.as_dict() for x in rules.items_query.filter_by(is_disabled = False)] skills = {} for skill in rules.skills_query.filter_by(is_disabled = False): skills.setdefault(skill.id, {}) skills[skill.id].update(skill.as_dict()) skills[skill.id].setdefault('related_skills', []) if skill.formula.strip() and master.id != character.id: node = ast.parse(skill.formula, mode = 'eval') # TODO: exceptions? visitor = formula.VariablesCollectionVisitor({}) visitor.visit(node) related_skills = rules.skills_query.filter(models.Skill.title.in_(visitor.VARIABLE_NAMES)) for related_skill in related_skills: skills.setdefault(related_skill.id, {}) skills[related_skill.id].setdefault('related_skills', []) skills[related_skill.id]['related_skills'].append(skill.id) token_tools = [] defaults = constants.PlayfieldObject.get_defaults(request.static_url('boardless:static/')) for obj_type in constants.PlayfieldObject.GENERAL_OBJECTS_ORDER: token_tools.append({ 'type': obj_type, 'title': defaults[obj_type]['title'], 'img_url': defaults[obj_type]['img_url'], }) return { 'colors': COLORS, 'game_session_id': game_session_id, 'the_player_id': the_player_id, 'character_token': character_token, 'master': master, 'players': players_dicts, 'cols_for_char_tbl': cols_for_char_tbl, 'skills': skills, 'item_groups': item_groups, 'rules': rules, 'user_role': user_role, 'skills_categories': skills_categories, 'token_tools': token_tools, 'items': items, 'dices': {d.id: d.as_dict(static_path = request.static_url('boardless:static/')) for d in rules.dices}, 'constants': constants, }