def setUp(self): from db.session import Session, DBState from db.models import User, Item, DiscuzMember, DiscuzMemberCount from db.base import Model Model.metadata.drop_all(DBState.engine) Model.metadata.create_all(DBState.engine) s = Session() [ s.add(i) for i in [ User(id=1, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), User(id=2, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), DiscuzMember(uid=1, username='******'), DiscuzMember(uid=2, username='******'), DiscuzMemberCount(uid=1, jiecao=100000), DiscuzMemberCount(uid=2, jiecao=100000), Item(id=1, owner_id=1, sku='foo', status='backpack'), Item(id=2, owner_id=2, sku='bar', status='backpack'), ] if not options.freeplay or 'DiscuzMember' not in i.__class__.__name__ ] s.commit()
def draw(uid, currency): s = current_session() u = Account.find(uid) if not u: raise exceptions.UserNotFound helpers.require_free_backpack_slot(s, uid) if currency == 'ppoint': amount = constants.LOTTERY_PRICE Account.add_user_credit(u, [('ppoint', -amount)], exceptions.InsufficientFunds) elif currency == 'jiecao': amount = constants.LOTTERY_JIECAO_PRICE Account.add_user_credit(u, [('jiecao', -amount)], exceptions.InsufficientFunds) else: raise exceptions.InvalidCurrency reward = random.choice(constants.LOTTERY_REWARD_LIST) item = Item(owner_id=uid, sku=reward, status='backpack') s.add(item) s.flush() s.add(ItemActivity( uid=uid, action='lottery', item_id=item.id, extra=json.dumps({'currency': currency, 'amount': amount}), created=datetime.datetime.now(), )) return reward
async def _alias_match_name(name: str): """ Searches the alias table for aliases that match the name :param name: The name to find the alias of """ params = (name, ) * 2 sql = 'SELECT * ' \ 'FROM aliases ' \ 'JOIN _items ON item_id = id ' \ 'WHERE alias LIKE %s ' \ 'OR SOUNDEX(alias) LIKE SOUNDEX(%s) ' \ 'LIMIT 1' alias = await objects.execute(Alias.raw(sql, *params)) # No alis found if not alias: return None # Building item from join item = Item() item.name = alias.name item.id = alias.id item.members = alias.members item.high_alch = alias.high_alch item.low_alch = alias.low_alch item.description = alias.description item.price = alias.price item.category = alias.category item.last_updated = alias.last_updated item.runeday = alias.runeday return item
def give_item(adventurer_uid, item_name, description, count, attributes=[]): item = Item(item_name, description, count, attributes) with session_scope() as Session: adventurer = Session.query(Adventurer).get(adventurer_uid) inventory = adventurer.inventory match = list(filter(lambda i: i.name == item.name, inventory)) if match: match[0].count += item.count else: adventurer.inventory.append(item)
def add(uid, item_sku, reason=None): s = current_session() helpers.require_free_backpack_slot(s, uid) item = Item(owner_id=uid, sku=item_sku, status='backpack') s.add(item) s.flush() s.add( ItemActivity( uid=uid, action='get', item_id=item.id, extra=reason and json.dumps(reason), created=datetime.datetime.now(), )) return item.id
async def fuzzy_match_name(name: str): """ Fuzzy matches a "Maybe" partial name :param name: The name to match :return: The item and name of the matched item or none if the name was not matched """ # Checking DB first ret = await objects.execute( Item.raw( 'SELECT * ' 'FROM _items ' 'WHERE name LIKE %s ' 'OR SOUNDEX(name) LIKE SOUNDEX(%s) ' 'ORDER BY sys.jaro_winkler(Name, %s) DESC ' 'LIMIT 1', name, name, name)) # Returning if name was matched to something if ret: return ret[0] # Trying aliases ret = await objects.execute( Alias.raw( 'SELECT * ' 'FROM aliases ' 'JOIN _items ON item_id = id ' 'WHERE alias LIKE %s ' 'OR SOUNDEX(alias) LIKE SOUNDEX(%s) ' 'LIMIT 1', name, name)) if ret: ret = ret[0] item = Item() item.name = ret.name item.id = ret.id item.members = ret.members item.high_alch = ret.high_alch item.low_alch = ret.low_alch item.description = ret.description item.price = ret.price item.category = ret.category item.last_updated = ret.last_updated item.runeday = ret.runeday return item # Slowly fuzzy matching DB sanitized = db.get_conn().escape_string(name) ret = await objects.execute( Item.raw( 'SELECT * ' 'FROM _items ' 'WHERE name LIKE \'%%{}%%\'' 'ORDER BY sys.jaro_winkler(name, %s) DESC ' 'LIMIT 1'.format(sanitized), name)) if not ret: return None return ret[0]