Запрос Python Mysql через хранилище запросов для B3 (BigBrotherBot)

Я пытаюсь получить данные MysqlDatabase из заданной базы данных (сопоставление). Когда я вызываю команду !maprating, я получаю следующую ошибку:

230205 14:19:09 DEBUG 'AdminPlugin: команда обработки !maprating'230205 14:19:09 VERBOSE "Отправка RCON (127.0.0.1:27960)" 0 ^3: ^8[pm]^7 ^7Произошла ошибка обработка вашей команды» 230205 14:19:09 ОШИБКА «Обработчик AdminPlugin не может обработать событие Скажите: AttributeError: объект «Курсор» не имеет атрибута «fetchone»

Это код, который я использую для запроса сопоставления. Это должно дать мне средний рейтинг карт.

Важный! синтаксис Python должен быть для Python 2.7.*, поскольку бот B3 может работать только с этой версией Python.

      def cmd_maprating(self, data, client, cmd=None):
        map_name = self.console.game.mapName
        query = "SELECT map_name, SUM(rating) AS total_rating FROM mapratings WHERE map_name = %s GROUP BY map_name"
        cursor = self.console.storage.query(query, (map_name,))
        if cursor.rowcount == 0:
            client.message("No ratings for map %s" % map_name)
        else:
            result = cursor.fetchone()
            client.message("Map %s has a total rating of %d" % (result['map_name'], result['total_rating']))

** Вот полный код. **

      import os
import sys
import b3
import b3.events
import b3.plugin


class MapratingPlugin(b3.plugin.Plugin):
    def onLoadConfig(self):
        self.rated_players = {}
        self.map_ratings = {}
    def onStartup(self):
        self.registerEvent(b3.events.EVT_CLIENT_SAY)

        self._adminPlugin = self.console.getPlugin('admin')
        if self._adminPlugin:
            self._adminPlugin.registerCommand(self, 'rategood', 0, self.cmd_rategood)
            self._adminPlugin.registerCommand(self, 'ratebad', 0, self.cmd_ratebad)
            self._adminPlugin.registerCommand(self, 'maprating', 0, self.cmd_maprating)

    def is_rated(self, client, map_name):
        cursor = self.console.storage.query("""
            SELECT rating FROM mapratings WHERE guid = %s AND map_name = %s
        """, (client.guid, map_name))
        
        if cursor.rowcount:
            return (True, cursor.getRow()['rating'])
        else:
            return (False, 0)

    def add_rating(self, client, map_name, rating):
        self.console.storage.query("""
            INSERT INTO mapratings (guid, player_name, map_name, rating)
            VALUES (%s, %s, %s, %s)
        """, (client.guid, client.name, map_name, rating))

    def update_rating(self, client, map_name, rating):
        self.console.storage.query("""
            UPDATE mapratings SET rating = %s WHERE guid = %s AND map_name = %s
        """, (rating, client.guid, map_name))

    def cmd_rategood(self, data, client, cmd=None):
        map_name = self.console.game.mapName
        (is_rated, existing_rating) = self.is_rated(client, map_name)
        rating = 1
        if is_rated:
            if existing_rating == rating:
                client.message('You have already rated this map as good')
            else:
                self.update_rating(client, map_name, rating)
                client.message('Map %s rated as good by you' % map_name)
        else:
            self.add_rating(client, map_name, rating)
            client.message('Map %s rated as good by you' % map_name)

    def cmd_ratebad(self, data, client, cmd=None):
        map_name = self.console.game.mapName
        (is_rated, existing_rating) = self.is_rated(client, map_name)
        rating = -1
        if is_rated:
            if existing_rating == rating:
                client.message('You have already rated this map as bad')
            else:
                self.update_rating(client, map_name, rating)
                client.message('Map %s rated as bad by you' % map_name)
        else:
            self.add_rating(client, map_name, rating)
            client.message('Map %s rated as bad by you' % map_name)

    def cmd_maprating(self, data, client, cmd=None):
        map_name = self.console.game.mapName
        query = "SELECT map_name, SUM(rating) AS total_rating FROM mapratings WHERE map_name = %s GROUP BY map_name"
        cursor = self.console.storage.query(query, (map_name,))
        if cursor.rowcount == 0:
            client.message("No ratings for map %s" % map_name)
        else:
            result = cursor.fetchone()
            client.message("Map %s has a total rating of %d" % (result['map_name'], result['total_rating']))

Спасибо.

0 ответов

Другие вопросы по тегам