当前位置: 首页>>代码示例>>Python>>正文


Python Db.get_user_best方法代码示例

本文整理汇总了Python中db.Db.get_user_best方法的典型用法代码示例。如果您正苦于以下问题:Python Db.get_user_best方法的具体用法?Python Db.get_user_best怎么用?Python Db.get_user_best使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在db.Db的用法示例。


在下文中一共展示了Db.get_user_best方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import get_user_best [as 别名]

#.........这里部分代码省略.........
                    name = base64.b64decode(name)

                    timestamp = int(time.time())

                    item = {}
                    item['name'] = name
                    item['score'] = score
                    item['timestamp'] = timestamp
                    item['record_id'] = record_id
                    if self.clients[client_id].game_type != 'tetcolor':
                        item['mode'] = self.clients[client_id].game_type + '_' + mode
                    else:
                        item['mode'] = mode

                    print('current best ' + str(self.clients[client_id].max_score) + '; score ' + str(score))
                    if int(score) > int(self.clients[client_id].max_score):
                        print('set this item as best because ' + str(score) + ' gt ' + str(self.clients[client_id].max_score))
                        self.clients[client_id].max_score = score
                        self.clients[client_id].best_item = item

                    items.append(item)

        self.db.check_unique_and_insert(items, self.clients[client_id].host)\
            .addCallback(self._on_db_insert_response, client_id)\
            .addErrback(self._on_db_error, client_id)

        return 'wait for db'

    def _on_db_insert_response(self, value, client_id):
        """
            Callback for DB insert function (class Db).
        """
        log.msg('Data inserted for client ' + str(client_id))

        self.retrieveTopResults(client_id)

    def retrieveTopResults(self, client_id):
        """
            Retrieve best results from DB and send to client,
        """

        self.db.get_top(client_id, self.clients[client_id].mode, self.clients[client_id].game_type)\
            .addCallback(self._on_db_get_top_response, client_id)\
            .addErrback(self._on_db_error, client_id)

    def _on_db_get_top_response(self, value, client_id):
        #log.msg('top10 number ' + str(len(self.clients[client_id].top)))
        #log.msg(str(self.clients[client_id].top[0]['name']))
        #for result in self.clients[client_id].top:
        #    log.msg('top item: ' + unicode(result['name']) + ' ' + str(result['score']))

        if self.clients[client_id].best_item is None or self.clients[client_id].best_item['score'] is None:
            score = 0
            self.clients[client_id].best_item = {}
            self.clients[client_id].best_item['score'] = 0
            self.clients[client_id].best_item['record_id'] = 'some-unexisting-value'
        else:
            score = self.clients[client_id].best_item['score']

        self.db.get_user_best(client_id, score, self.clients[client_id].mode, self.clients[client_id].game_type)\
            .addCallback(self._on_db_get_user_best_response, client_id)\
            .addErrback(self._on_db_error, client_id)

    def _on_db_get_user_best_response(self, value, client_id):
        log.msg('best score ' + str(self.clients[client_id].best_item['score']))
        log.msg('best id ' + self.clients[client_id].best_item['record_id'])
        log.msg('best place ' + str(self.clients[client_id].best_item_place))

        self.send_data_and_close_connection(client_id)


    def send_data_and_close_connection(self, client_id):
        result = {}
        result['top'] = self.clients[client_id].top
        result['place'] = self.clients[client_id].best_item_place

        j = json.dumps(result)
        c = self.crypt.crypt(j)

        self.clients[client_id].writeData(c)
        self.clients[client_id].transport.loseConnection()
        del self.clients[client_id]

    def _on_db_init_error(self, value):
        """
            DB initialization errorback.
        """
        log.msg('DB init errorback. Something went wrong in DB')

    def _on_db_error(self, value, client_id):
        """
            Errorback for client interaction with DB.
        """
        log.msg('DB errorback. Something went wrong in DB: ' + str(value))
        self.clients[client_id].writeData('4')
        self.clients[client_id].transport.loseConnection()

    def decodeUserRecords(self, data):
        log.msg('decodeUserRecords')
        return self.crypt.decrypt(data)
开发者ID:LeoHoward,项目名称:leaderboards-server,代码行数:104,代码来源:leaderboards.py


注:本文中的db.Db.get_user_best方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。