Ejemplo n.º 1
0
    def generate_session(self, user_key: str, level: Level,
                         difficulty_score: float) -> str:

        # Variable intended to store the key
        # that will be created within the function.
        session_id: int = 0

        # Fetches the ids of all previously played sessions.
        session_ids: [int] = self.get_session_ids(user_key)

        # Checks whether at least a single session has been
        # stored before for a given user.
        if len(session_ids) > 0:

            # Sets the id for the session that should be
            # created next to one higher than previous
            # session id.
            session_id = max(session_ids) + 1

        # Generates a key that corresponds to the previously
        # created id like 'session_042' if the session's id
        # is '42'
        session_key: str = Helper().generate_key('session', session_id)

        # Defines a new performance object which values are
        # initialized with zeros.
        performance: Performance = Performance(0, 0, 0, 0, 0, 0, 0,
                                               difficulty_score)

        # Creates a new session object based on the previously
        # defined key and id pair.
        session: Session = Session(session_key, session_id, 'created',
                                   server_timestamp, performance)

        # Defines a new database reference pointing towards
        # the place at which the new session sould be stored.
        ref = db.document(f'users/{user_key}/sessions/{session_key}')

        # Writes the generated session to Firestore.
        ref.set(session.to_dict())

        # Defines a new database reference at which the session's
        # performance should be stored.
        ref = db.document(
            f'users/{user_key}/sessions/{session_key}/data/performance')

        # Stores the session's performance.
        ref.set(performance.to_dict())

        # Defines a new database reference pointing to the place at
        # which the level for the generated session should be stored.
        ref = db.document(
            f'users/{user_key}/sessions/{session_key}/data/level')

        # Stores the session's level at Firestore.
        ref.set(level.to_dict())

        # Returns the key of the generated session.
        return session_key
Ejemplo n.º 2
0
    def get_sessions(self) -> [Performance]:

        performances: [Performance] = []

        for doc in db.collection(f'performances').get():

            d = doc.to_dict()

            user_key = d['user_key']

            performance: Performance = Performance(
                d['defeated_by_gaps'], d['defeated_by_opponent_type_1'],
                d['defeated_by_opponent_type_2'],
                d['defeated_by_opponent_type_3'], d['score'], d['time'],
                d['progress'], d['difficulty'])

            if user_key != 'user_001':

                performances.append(performance)

        return performances
Ejemplo n.º 3
0
def jixiao_insert():
    status = 0
    try:
        if request.method == "GET":
            raise Exception("method must be post")
        corporation_id = request.form.get('corporation_id')
        value = request.form.get('value')
        user_id = request.form.get('user_id')
        post = request.form.get('post')
        description = request.form.get('description')
        department = request.form.get('department')
        hr_id = request.form.get('hr_id')
        if not all([value, post, description, department, hr_id]):
            raise Exception("all values must not be empty: value,"
                            "post, description, department, hr_id")
        if not user_id:
            user_id = session.get("user_id", "")
        if user_id == "":
            raise Exception("user id must be set as: 1, through log in 2, through form post. "
                            "while the form have fhe first priority")
        if not corporation_id:
            corporation_id = session.get("corporation_id", "")
        if corporation_id == "":
            raise Exception("corporation_id id must be set as: 1, through log in 2, through form post. "
                            "while the form have fhe first priority")
        try:
            corporation_id = int(corporation_id)
            value = int(value)
            user_id = int(user_id)
            hr_id = int(hr_id)
        except:
            raise Exception("corporation_id, value, user_id, hr_id must be int type of value")
        if value < 0 or value > 10:
            status = 2
            raise Exception("value must in between 0 and 10, include")

        have_corporation_id = db.session.query(Corporation.corporation_id). \
            filter(Corporation.corporation_id == corporation_id).first()
        if not have_corporation_id:
            status = 3
            raise Exception("corporation id is not valid")
        have_user_id = db.session.query(Person.user_id). \
            filter(Person.user_id == user_id).first()
        if not have_user_id:
            status = 3
            raise Exception("user id is not valid")
        have_hr_id = db.session.query(Hr.hr_id). \
            filter(Hr.hr_id == hr_id).first()
        if not have_hr_id:
            status = 3
            raise Exception("hr id is not valid")
        performance = Performance(corporation_id=corporation_id,
                                  user_id=user_id,
                                  hr_id=hr_id,
                                  value=value,
                                  description=description,
                                  department=department,
                                  post=post)
        performance.save()
        return dict(
            status=1,
            message="success",
            data="none"
        )
    except Exception as e:
        return dict(
            status=status,
            message=str(e),
            data="none"
        )
Ejemplo n.º 4
0
class Samples:

    ###############
    ## Variables ##
    ###############
    db: Database = Database()

    # sample_user: User = User('user_042', 42, 'german')

    sample_performance_dict = {
        "defeated_by_gaps": 1,
        "defeated_by_opponent_type_1": 1,
        "defeated_by_opponent_type_2": 1,
        "defeated_by_opponent_type_3": 0,
        "score": 600,
        "time": 45,
        "progress": 15,
        "difficulty": 80,
    }

    sample_performance: Performance = Performance(
        defeated_by_gaps=1,
        defeated_by_opponent_type_1=1,
        defeated_by_opponent_type_2=1,
        defeated_by_opponent_type_3=0,
        score=600,
        time=45,
        progress=15,
        difficulty=80,
    )

    sample_session: Session = Session('session_001', 1, 'finished',
                                      db.generate_timestamp(),
                                      sample_performance)

    sample_level: Level = Level.from_dict({
        'key':
        'level_01',
        'id':
        1,
        'line_00': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'
        ],
        'line_01': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'
        ],
        'line_02': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'
        ],
        'line_03': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', 'D', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'
        ],
        'line_04': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'B'
        ],
        'line_05': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_06': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', 'S', 'S', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_07': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_08': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', 'S', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', 'C', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_09': [
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', 'B', 'B', 'B', 'B', '.', '.', '.', '.', '.',
            '.', '.', '.', 'D', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_10': [
            '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', 'C', '.', '.', '.', '.', '.', 'C', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_11': [
            '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', 'B', '.', 'B',
            'B', 'B', 'B', 'B', 'B', '.', '.', '.', 'D', 'D', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_12': [
            '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', '.', '.', '.',
            'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', '.',
            'D', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_13': [
            '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
            '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', '.', '.', '.',
            'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', 'D',
            'D', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D'
        ],
        'line_14': [
            'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X',
            'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', '.', '.',
            'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X',
            'X', 'X', 'X', 'X', '.', '.', 'Z', 'Z', 'Z', 'D', 'D'
        ]
    })

    sample_user_1: User = User('user_001', 1, 'english')
    sample_user_2: User = User('user_002', 2, 'english')
    sample_user_3: User = User('user_003', 3, 'english')
    sample_user_4: User = User('user_004', 4, 'english')

    sample_session_1: Session = Session(
        'session_001', 1, 'finished', db.generate_timestamp(),
        Performance(0, 1, 0, 2, 1500, 48, 200, 80))
    sample_session_2: Session = Session(
        'session_002', 2, 'finished', db.generate_timestamp(),
        Performance(0, 2, 0, 2, 3000, 130, 156, 100))
    sample_session_3: Session = Session(
        'session_003', 3, 'finished', db.generate_timestamp(),
        Performance(2, 0, 0, 2, 4500, 38, 137, 110))
    sample_session_4: Session = Session(
        'session_001', 1, 'finished', db.generate_timestamp(),
        Performance(1, 0, 0, 1, 4300, 48, 149, 90))
    sample_session_5: Session = Session(
        'session_002', 2, 'finished', db.generate_timestamp(),
        Performance(0, 0, 0, 0, 4300, 243, 200, 180))
    sample_session_6: Session = Session(
        'session_001', 1, 'finished', db.generate_timestamp(),
        Performance(0, 3, 1, 0, 2200, 38, 113, 65))
    sample_session_7: Session = Session(
        'session_002', 2, 'finished', db.generate_timestamp(),
        Performance(0, 2, 2, 0, 1100, 52, 200, 79))
    sample_session_8: Session = Session(
        'session_001', 1, 'finished', db.generate_timestamp(),
        Performance(2, 0, 0, 1, 500, 74, 31, 103))