コード例 #1
0
    def delete_by(self, uid=None, gid=None):
        cond_str = []
        cond_tup = []
        if not gid is None:
            cond_str.append('gid=?')
            cond_tup.append(gid)
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)

        if 0 == len(cond_tup):
            raise DatabaseError('删除成员的条件有误')

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                cur = conn.execute(
                    '''
                    DELETE FROM {0} WHERE {1}
                    '''.format(self._table, cond_str), cond_tup)
                return cur.rowcount
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[MemberDao.delete_by] {e}')
                raise DatabaseError('删除成员失败')
コード例 #2
0
    def find_by(self, uid=None, gid=None):
        cond_str = []
        cond_tup = []
        if not gid is None:
            cond_str.append('gid=?')
            cond_tup.append(gid)
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)

        if 0 == len(cond_tup):
            return self.find_all()

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                ret = conn.execute(
                    '''
                    SELECT {1} FROM {0} WHERE {2}
                    '''.format(self._table, self._columns, cond_str),
                    cond_tup).fetchall()
                return [self.row2item(r) for r in ret]
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[MemberDao.find_by] {e}')
                raise DatabaseError('查找成员失败')
コード例 #3
0
async def clan_statistics_api(bm: BattleMaster, apikey):
    try:
        clan = check_clan(bm)
        report = get_report(bm, None, 0)
        mems = bm.list_member()
        members = [{'uid': m['uid'], 'nickname': m['name']} for m in mems]
        groupinfo = {
            'group_id': bm.group,
            'group_name': clan['name'],
            'game_server': SERVER_NAME[clan['server']],
            'battle_id': 0,
        }
        response = await make_response(
            jsonify(
                code=0,
                message='OK',
                api_version=1,
                challenges=report,
                groupinfo=groupinfo,
                members=members,
            ))
        #if (group.privacy & 0x2):
        #    response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    except KeyError as e:
        logger.error(e)
        return jsonify(code=31, message='missing key: ' + str(e))
    except Exception as e:
        logger.exception(e)
        return jsonify(code=40, message='server error')
コード例 #4
0
    def find_by(self, uid=None, alt=None, order_by_user=False):
        cond_str = []
        cond_tup = []
        order = 'round, boss, eid' if not order_by_user else 'uid, alt, round, boss, eid'
        if not uid is None:
            cond_str.append('uid=?')
            cond_tup.append(uid)
        if not alt is None:
            cond_str.append('alt=?')
            cond_tup.append(alt)
        if 0 == len(cond_tup):
            return self.find_all()

        cond_str = " AND ".join(cond_str)

        with self._connect() as conn:
            try:
                ret = conn.execute(
                    '''
                    SELECT {1} FROM {0} WHERE {2} ORDER BY {3}
                    '''.format(self._table, self._columns, cond_str, order),
                    cond_tup).fetchall()
                return [self.row2item(r) for r in ret]
            except (sqlite3.DatabaseError) as e:
                logger.error(f'[BattleDao.find_by] {e}')
                raise DatabaseError('查找记录失败')
コード例 #5
0
 def delete_by_time(self, time):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 DELETE FROM {0} WHERE auth_cookie_expire_time<?
                 '''.format(self._table), (time, ))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[LoginDao.delete_by_time] {e}')
             raise DatabaseError('删除登录状态失败')
コード例 #6
0
 def modify(self, clan):
     with self._connect() as conn:
         try:
             conn.execute('''
                 UPDATE {0} SET name=?, server=? WHERE gid=? AND cid=?
                 '''.format(self._table),
                 (clan['name'], clan['server'], clan['gid'], clan['cid']) )
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.modify] {e}')
             raise DatabaseError('修改公会失败')       
コード例 #7
0
 def add(self, clan):
     with self._connect() as conn:
         try:
             conn.execute('''
                 INSERT INTO {0} ({1}) VALUES (?, ?, ?, ?)
                 '''.format(self._table, self._columns),
                 (clan['gid'], clan['cid'], clan['name'], clan['server']) )
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.add] {e}')
             raise DatabaseError('添加公会失败')
コード例 #8
0
 def modify(self, challenge):
     with self._connect() as conn:
         try:
             conn.execute('''
                 UPDATE {0} SET uid=?, alt=?, time=?, round=?, boss=?, dmg=?, flag=? WHERE eid=?
                 '''.format(self._table),
                 (challenge['uid'], challenge['alt'], challenge['time'], challenge['round'], challenge['boss'], challenge['dmg'], challenge['flag'], challenge['eid']) )
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[BattleDao.modify] {e}')
             raise DatabaseError('修改记录失败')
コード例 #9
0
 def modify(self, member):
     with self._connect() as conn:
         try:
             conn.execute('''
                 UPDATE {0} SET name=?, gid=?, cid=? WHERE uid=? AND alt=?
                 '''.format(self._table),
                 (member['name'], member['gid'], member['cid'], member['uid'], member['alt']) )
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.modify] {e}')
             raise DatabaseError('修改成员失败')       
コード例 #10
0
 def add(self, member):
     with self._connect() as conn:
         try:
             conn.execute('''
                 INSERT INTO {0} ({1}) VALUES (?, ?, ?, ?, ?)
                 '''.format(self._table, self._columns),
                 (member['uid'], member['alt'], member['name'], member['gid'], member['cid']) )
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.add] {e}')
             raise DatabaseError('添加成员失败')
コード例 #11
0
 def delete(self, uid, alt):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 DELETE FROM {0} WHERE uid=? AND alt=?
                 '''.format(self._table), (uid, alt))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.delete] {e}')
             raise DatabaseError('删除成员失败')
コード例 #12
0
 def delete(self, uid):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 DELETE FROM {0} WHERE uid=?
                 '''.format(self._table), (uid, ))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[LoginDao.delete] {e}')
             raise DatabaseError('删除登录状态失败')
コード例 #13
0
 def delete(self, gid, cid):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 DELETE FROM {0} WHERE gid=? AND cid=?
                 '''.format(self._table), (gid, cid))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.delete] {e}')
             raise DatabaseError('删除公会失败')
コード例 #14
0
 def find_one(self, gid, cid):
     with self._connect() as conn:
         try:
             ret = conn.execute('''
                 SELECT {1} FROM {0} WHERE gid=? AND cid=?
                 '''.format(self._table, self._columns),
                 (gid, cid) ).fetchone()
             return self.row2item(ret)
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.find_one] {e}')
             raise DatabaseError('查找公会失败')
コード例 #15
0
 def add(self, uid, auth_cookie, auth_cookie_expire_time):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 INSERT INTO {0} ({1}) VALUES (?, ?, ?)
                 '''.format(self._table, self._columns),
                 (uid, auth_cookie, auth_cookie_expire_time))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[LoginDao.add] {e}')
             raise DatabaseError('添加登录状态失败')
コード例 #16
0
 def get_or_add(self, uid, salt):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 INSERT OR IGNORE INTO {0} (uid, salt) VALUES (?, ?)
                 '''.format(self._table), (uid, salt))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[UserDao.get_or_add] {e}')
             raise DatabaseError('添加用户失败')
     return self.find_one(uid)
コード例 #17
0
 def find_one(self, uid, alt):
     with self._connect() as conn:
         try:
             ret = conn.execute('''
                 SELECT {1} FROM {0} WHERE uid=? AND alt=?
                 '''.format(self._table, self._columns),
                 (uid, alt) ).fetchone()
             return self.row2item(ret)
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.find_one] {e}')
             raise DatabaseError('查找成员失败')
コード例 #18
0
 def add(self, challenge):
     with self._connect() as conn:
         try:
             cur = conn.execute('''
                 INSERT INTO {0} ({1}) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)
                 '''.format(self._table, self._columns),
                 (challenge['uid'], challenge['alt'], challenge['time'], challenge['round'], challenge['boss'], challenge['dmg'], challenge['flag']) )
             return cur.lastrowid
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[BattleDao.add] {e}')
             raise DatabaseError('添加记录失败')
コード例 #19
0
 def find_all(self):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''
                 SELECT {1} FROM {0}
                 '''.format(self._table, self._columns), ).fetchall()
             return [self.row2item(r) for r in ret]
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[ClanDao.find_all] {e}')
             raise DatabaseError('查找公会失败')
コード例 #20
0
 def modify(self, login: dict):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''
                 UPDATE {0} SET auth_cookie_expire_time=?
                 WHERE uid=? AND auth_cookie=?
                 '''.format(self._table),
                 (login['auth_cookie_expire_time'], login['uid'],
                  login['auth_cookie']))
             ret = conn.execute(
                 '''
                 UPDATE user SET last_login_time=?, last_login_ipaddr=? WHERE uid=?
                 ''', (login['last_login_time'], login['last_login_ipaddr'],
                       login['uid']))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[LoginDao.find_one] {e}')
             raise DatabaseError('修改登录状态失败')
コード例 #21
0
 def modify(self, user: dict):
     with self._connect() as conn:
         try:
             conn.execute(
                 '''
                 UPDATE {0} SET  last_login_time=?, last_login_ipaddr=?,
                 login_code=?, login_code_available=?, login_code_expire_time=?,
                 must_change_password=?, password=?, privacy=?, salt=?
                 WHERE uid=?
                 '''.format(self._table),
                 (user['last_login_time'], user['last_login_ipaddr'],
                  user['login_code'], user['login_code_available'],
                  user['login_code_expire_time'],
                  user['must_change_password'], user['password'],
                  user['privacy'], user['salt'], user['uid']))
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[UserDao.modify] {e}')
             raise DatabaseError('修改用户失败')
コード例 #22
0
 def find_by_uid(self, uid):
     with self._connect() as conn:
         try:
             ret = conn.execute(
                 '''
                 SELECT M.uid, C.gid, C.name
                 FROM MEMBER M INNER JOIN CLAN C
                 ON M.gid = C.gid
                 WHERE M.uid=?
                 ''', (uid, )).fetchall()
             return [{
                 'uid': r[0],
                 'gid': r[1],
                 'name': r[2]
             } if r else None for r in ret]
         except (sqlite3.DatabaseError) as e:
             logger.error(f'[MemberDao.find_by_uid] {e}')
             raise DatabaseError('查找成员及公会失败')
コード例 #23
0
def clan_setting_api(bm: BattleMaster, payload):
    try:
        action = payload['action']
        clan = check_clan(bm)
        if action == 'get_setting':
            return jsonify(
                code=0,
                groupData={
                    'group_name': clan['name'],
                    'game_server': SERVER_NAME[clan['server']],
                    'battle_id': 0,
                },
                privacy=3,
                notification=1023,
            )
        elif action == 'put_setting':
            # clan['server'] = payload['game_server']
            # clan['notification = payload['notification']
            # clan['privacy'] = payload['privacy']
            # clan.save()
            # logger.info('网页 成功 {} {} {}'.format(
            #     uid, group_id, action))
            # return jsonify(code=0, message='success')
            return jsonify(code=22, message='unfinished action')
        elif action == 'get_data_slot_record_count':
            # counts = self.get_data_slot_record_count(group_id)
            # logger.info('网页 成功 {} {} {}'.format(
            #     uid, group_id, action))
            # return jsonify(code=0, message='success', counts=counts)
            return jsonify(code=22, message='unfinished action')
        else:
            return jsonify(code=32, message='unknown action')
    except KeyError as e:
        logger.error(e)
        return jsonify(code=31, message='missing key: ' + str(e))
    except Exception as e:
        logger.exception(e)
        return jsonify(code=40, message='server error')
コード例 #24
0
def clan_api(bm: BattleMaster, payload):
    try:
        clan = check_clan(bm)
        zone = bm.get_timezone_num(clan['server'])
        action = payload['action']
        if payload['uid'] == 0:
            # 允许游客查看
            if action not in ['get_member_list', 'get_challenge']:
                return jsonify(code=10, message='Not logged in')
        if action == 'get_member_list':
            mems = bm.list_member()
            members = [{'uid': m['uid'], 'nickname': m['name']} for m in mems]
            return jsonify(code=0, members=members)
        elif action == 'get_data':
            return jsonify(code=0,
                           bossData=get_boss_data(bm),
                           groupData={
                               'group_id': bm.group,
                               'group_name': clan['name'],
                               'game_server': SERVER_NAME[clan['server']],
                               'level_4': False
                           },
                           selfData={
                               'is_admin': False,
                               'user_id': payload['uid'],
                               'today_sl': False
                           })
        elif action == 'get_challenge':
            d = int(
                (datetime.now().timestamp() + (zone - 5) * 3600) / 86400) + 1
            report = get_report(bm, None, payload['ts'])
            if report is None:
                return jsonofy(code=20, message="Group dosen't exist")
            return jsonify(
                code=0,
                challenges=report,
                today=d,
            )
        elif action == 'get_user_challenge':
            report = get_report(bm, payload['target_uid'], 0)
            try:
                visited_user = get_member(bm, payload['target_uid'])
            except:
                return jsonify(code=20, message='user not found')
            return jsonify(code=0,
                           challenges=report,
                           game_server=SERVER_NAME[clan['server']],
                           user_info={
                               'uid': payload['target_uid'],
                               'nickname': visited_user['name']
                           })
        elif action == 'send_remind':

            return jsonify(code=0, notice='发送成功')
        else:
            return jsonify(code=32, message='unknown action')
    except KeyError as e:
        logger.error(e)
        return jsonify(code=31, message='missing key: ' + str(e))
    except Exception as e:
        logger.exception(e)
        return jsonify(code=40, message='server error')