Esempio n. 1
0
 def load_from_db(self, connection, user_id):
     sql_data = {
         'user_id': user_id
     }
     sql_query = 'SELECT id, nickname, login, password, type FROM User WHERE id = %(user_id)s LIMIT 1'
     with connection:
         cur = connection.cursor()
         cur.execute(sql_query, sql_data)
         loaded_data = cur.fetchall()[0]
         self.user_id = loaded_data[0]
         self.nickname = loaded_data[1]
         self.login = loaded_data[2]
         self.password = loaded_data[3]
         if loaded_data[4] == 0:
             self.__class__ = User
         elif loaded_data[4] == 1:
             self.__class__ = Moderator
         else:
             self.__class__ = Administrator
         sql_query = 'SELECT id FROM Session WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Session()
             loaded_object.load_from_db(connection, x)
             self.sessions.append(loaded_object)
         sql_query = 'SELECT id FROM Currency WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Currency()
             loaded_object.load_from_db(connection, x)
             self.currencies[loaded_object.name] = loaded_object
         sql_query = 'SELECT id FROM Achievment WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Achievement()
             loaded_object.load_from_db(connection, x)
             self.achievements.append(loaded_object)
         sql_query = 'SELECT id FROM Counter WHERE user_id=%(user_id)s LIMIT 1'
         cur.execute(sql_query, sql_data)
         id_data = cur.fetchall()[0][0]
         self.counters = Counter()
         self.counters.load_from_db(connection, id_data)
Esempio n. 2
0
class Player(object):

    def __init__(self, name=None, money=None, score=None, level=None):
        self.name = name
        self.money = money
        self.level = level
        self.score = score
        self.session = Session()

    def as_dict(self):
        d = {}
        d["score"] = self.score
        d["level"] = self.level
        d["session"] = self.session.as_dict()
        d["type"] = self.__class__.__name__
        d["name"] = self.name
        d["money"] = self.money
        return d

    def save(self, file_object):
        json.dump(self.as_dict(),file_object)

    def load(self, file_object):
        object_as_dict = json.load(file_object)
        self.level = object_as_dict["level"]
        self.lastSession = Session(object_as_dict['session']['starttime'], object_as_dict['session']['stoptime'])
        self.name = object_as_dict["name"]
        self.money = object_as_dict["money"]
        return object_as_dict

    def load_from_db(self, id):
        self.session.load_from_db(player_id=id)
        sql_data = {
            'id': id,
        }
        sql_query = 'SELECT name, score, level, money FROM player WHERE id=%(id)s'
        with connection:
            cur = connection.cursor()
            cur.execute(sql_query, sql_data)
            for row in cur.fetchall():
                if row:
                    print "row is {}".format(row)
                    self.name = row[0]
                    self.score = int(row[1])
                    self.level = int(row[2])
                    self.money = int(row[3])
                else:
                    raise MySQLdb.IntegrityError("Record not found. Check input 'id'=={} parameter!".format(id))

        return (self.name, self.score, self.level, self.money)

    def delete_from_db(self, id):
        self.session.delete_from_db(id)
        sql_data = {
            'id': id,
        }
        sql_query = 'DELETE FROM player WHERE id=%(id)s'
        with connection:
            cur = connection.cursor()
            cur.execute(sql_query, sql_data)
            print("Player with id={} has been deleted".format(id))

        return True

    def save_to_db(self, id):
        sql_data = {
            'id': id,
            'name': self.name,
            'score': self.score,
            'level': self.level,
            'money': self.money,
        }
        sql_query = 'INSERT INTO player (id, name, score, level, money) VALUES (%(id)s, %(name)s, %(score)s, %(level)s, %(money)s)'
        with connection:
            cur = connection.cursor()
            try:
                cur.execute(sql_query, sql_data)
                print "New record was inserted"
            except MySQLdb.IntegrityError:
                sql_query = 'UPDATE player SET name=%(name)s, score=%(score)s, level=%(level)s, money=%(money)s WHERE id=%(id)s'
                cur.execute(sql_query, sql_data)
                print "Record was updated!"

        self.session.save_to_db(player_id=id)

        return True

    def login(self):
        self.session.startSession()
        print 'Login successfull'
        return self.session.starttime

    def logout(self):
        self.session.stopSession()
        print 'Logout successfull'
        return self.session.stoptime

    def __str__(self):
         return '{}(name="{}", money="{}", score="{}", level="{}")'.format(self.__class__.__name__, self.name, self.money, self.score, self.level) + self.session.__str__()