Skip to content

Commit

Permalink
Improved performance. Closes YogurtTheHorse#291
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia Konnov [email protected] committed Jun 29, 2018
1 parent 0cbf202 commit b36b630
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 103 deletions.
8 changes: 6 additions & 2 deletions items/itemloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_context():
global context
return context

def load_item(name, buff, cntxt={}, usr=None):
def load_item(name, buff, cntxt={}, usr=None, count=1):
path = 'items/{0}/{1}.py'.format(buff, name)
is_compiled = False

Expand All @@ -37,7 +37,11 @@ def load_item(name, buff, cntxt={}, usr=None):
else:
item = imp.load_source(name, path)

return check_item(item, name, buff) if item is not None else None
if item is None:
return None
item = check_item(item, name, buff)
item.count=count
return item

def check_item(item, name, buff):
item.code_name = name
Expand Down
17 changes: 8 additions & 9 deletions rooms/default/special/remains.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import usermanager
import items.itemloader as itemloader
from items.item_info import item_info

from collections import Counter

Expand Down Expand Up @@ -37,7 +38,7 @@ def enter(user, reply):

def action(user, reply, text):
if text == actions[0]:
items = [ (it[0], it[1]) for it in user.get_room_temp('items', def_val=[]) if len(it) < 3 or len(it[2]) == 0 ]
items = [ i for i in user.get_room_temp('items', def_val=[]) if i.is_simple() ]

if len(items) == 0:
reply('У него ничего не было.')
Expand All @@ -46,18 +47,16 @@ def action(user, reply, text):

user.give_gold(random.randrange(12, 72))

items.append(('loot', 'tooth'))
items.append(('loot', 'tooth'))
user.add_item('loot', 'tooth', count=2)

for it in items:
user.add_item(it[0], it[1])
user.add_item(it.group, it.name, count=it.count)

counter_items = Counter(items)
items_str = [ ]
for it, cnt in counter_items.most_common():
loaded_item = itemloader.load_item(it[1], it[0], usr=user)
items_str = []
for i in items:
loaded_item = itemloader.load_item(i.name, i.group, usr=user, count=i.count)
if loaded_item is not None:
items_str.append('*{0}* ({1} шт.)'.format(loaded_item.name, cnt))
items_str.append('*{0}* ({1} шт.)'.format(loaded_item.name, loaded_item.count))

reply('Его рюкзак вмещал в себя следующие вещи: {0}'.format(', '.join(items_str)))

Expand Down
20 changes: 11 additions & 9 deletions rooms/default/usual/orc_shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ def get_actions(user):
return [ 'Зуб', 'Палка', 'Парашок', 'Уйти' ]

def action(user, reply, text):
teeths = user.get_item_by_name('tooth')
if teeths is None:
user_teeth_cnt = 0
else:
user_teeth_cnt = teeths.count
if text == 'Зуб':
teeth_cnt = user.get_room_temp('teeth_cnt', def_val=0)


if user.has_item('tooth'):
if user_teeth_cnt >= 1:
if teeth_cnt > 2:
reply('Ты давно понимаешь, что что-то идет не так, но орк выглядит вполне счастливым, и ты слышишь, как он бормочет что-то вроде: «какая харошая тарговля, босс будит даволен»')
else:
Expand All @@ -38,24 +42,22 @@ def action(user, reply, text):
reply('«Нет зубов — нет товара» — хотел сказать орк, но просто ударил тебя по лицу.')
user.make_damage(1, 10, reply, death=False)
elif text == 'Палка':
if user.items.count(('loot', 'tooth', {})) >= 5:
if user_teeth_cnt >= 5:
reply('Забирай')
user.add_item('good', 'mage_stick')

for i in range(5):
user.remove_item('tooth')
user.remove_item('tooth', count=5)
else:
reply('«Нет зубов — нет товара» — хотел сказать орк, но просто ударил тебя по лицу.')
user.make_damage(1, 10, reply, death=False)
elif text == 'Парашок':
if user.items.count(('loot', 'tooth', {})) >= 5:
if user_teeth_cnt >= 5:
reply('Забирай')
user.add_item('neutral', 'protein')

for i in range(5):
user.remove_item('tooth')
user.remove_item('tooth', count=5)
else:
reply('«Нет зубов — нет товара» — хотел сказать орк, но просто ударил тебя по лицу.')
user.make_damage(1, 10, reply, death=False)
else:
user.leave(reply)
user.leave(reply)
2 changes: 1 addition & 1 deletion rooms/default/usual/some_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def action(user, reply, text):
user.throw_dice(reply)
else:
reply('Ты уходишь отсюда.')
user.leave(reply)
user.leave(reply)
21 changes: 11 additions & 10 deletions rooms/vietnam/special/remains.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import random
import usermanager
import items.itemloader as itemloader
from items.item_info import item_info

from collections import Counter

from constants import REMAINS_STICKER

name = 'Останки'

actions = [ 'Забрать себе', 'Уйти' ]
Expand All @@ -29,13 +32,13 @@ def enter(user, reply):
reply('Здесь лежат останки игрока {0}'.format(found_user.name), photo='BQADAgADFwkAAmrZzgf5q0m1CmsDggI')
user.set_room_temp('items', found_user.items)
else:
reply('Здесь лежат останки лягушки. Воняет. Ты уходишь отсюда побыстрее.')
reply('Здесь лежат останки лягушки. Воняет. Ты уходишь отсюда побыстрее.', photo='BQADAgADFwkAAmrZzgf5q0m1CmsDggI')
user.leave(reply)


def action(user, reply, text):
if text == actions[0]:
items = [ (it[0], it[1]) for it in user.get_room_temp('items', def_val=[]) if len(it) < 3 or len(it[2]) == 0 ]
items = [ i for i in user.get_room_temp('items', def_val=[]) if i.is_simple() ]

if len(items) == 0:
reply('У него ничего не было.')
Expand All @@ -44,18 +47,16 @@ def action(user, reply, text):

user.give_gold(random.randrange(12, 72))

items.append(('loot', 'tooth'))
items.append(('loot', 'tooth'))
user.add_item('loot', 'tooth', count=2)

for it in items:
user.add_item(it[0], it[1])
user.add_item(it.group, it.name, count=it.count)

counter_items = Counter(items)
items_str = [ ]
for it, cnt in counter_items.most_common():
loaded_item = itemloader.load_item(it[1], it[0], usr=user)
items_str = []
for i in items:
loaded_item = itemloader.load_item(i.name, i.group, usr=user, count=i.count)
if loaded_item is not None:
items_str.append('*{0}* ({1} шт.)'.format(loaded_item.name, cnt))
items_str.append('*{0}* ({1} шт.)'.format(loaded_item.name, loaded_item.count))

reply('Его рюкзак вмещал в себя следующие вещи: {0}'.format(', '.join(items_str)))

Expand Down
1 change: 1 addition & 0 deletions user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def message(self, reply, text):
from user.items_defenition import remove_item, remove_items_with_tag, deactivate_item_by_name
from user.items_defenition import remove_item_by_name, get_item_by_name, get_items, get_active_items
from user.items_defenition import add_item, get_active_slots_len, has_item, get_counted_items
from user.items_defenition import get_active_item_by_name, iterate_over_items
from user.meet_defenition import name_confirm, name_given, first
from user.money_defenition import paid, steal, give_gold
from user.room_defenition import make_damage, set_room_temp, get_room_temp, open_room, in_room
Expand Down
2 changes: 1 addition & 1 deletion user/corridor_defenition.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def open_corridor(self, reply):
if self.state == 'room':
for item in self.get_items():
for item in self.iterate_over_items():
item.on_corridor(self, reply)

self.state = 'corridor'
Expand Down
14 changes: 5 additions & 9 deletions user/fight_defenition.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ def get_fight_actions(self):
locale_manager.get('USE') + locale_manager.get('IMAGINATION')
]

items = self.get_items()
counter_items = Counter(items)
for i, cnt in counter_items.most_common():
counter_items = self.get_counted_items()
for i, cnt in counter_items:
if i.fightable:
act = locale_manager.get('USE') + i.name + ' ({0} шт.)'.format(cnt)
if act not in actions:
Expand Down Expand Up @@ -64,10 +63,7 @@ def fight_action(self, reply, text):
name = text[len(locale_manager.get('USE')):]
name = name[:name.rindex('(')-1]

for i in self.get_items():
if i.name == name:
item = i
break
item = self.get_item_by_name(name)

if item:
dmg = 0
Expand Down Expand Up @@ -117,7 +113,7 @@ def fight_answer(self, reply):
reply(locale_manager.get('USER_DAMAGED').format(dmg))

def escape(self, reply, success=True):
for i in self.get_items():
for i in self.iterate_over_items():
i.on_escape(self, reply, success)

if success:
Expand Down Expand Up @@ -157,4 +153,4 @@ def won(self, reply, tornament=False, boss=None):
reply(locale_manager.get('YOU_WON').format(loot))

if room.room_type != 'boss':
self.leave(reply)
self.leave(reply)
2 changes: 1 addition & 1 deletion user/gods_defenition.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def prayto(self, reply, god):
if god_num >= 0:
self.gods_level[god_num] += 1

for item in self.get_items():
for item in self.iterate_over_items():
item.on_pray(self, reply, god_num)


Expand Down
24 changes: 15 additions & 9 deletions user/inventory_defenition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def open_inventory(self, reply):
actions = [ ]
msg = locale_manager.get('INVENTORY_MESSAGE')

counter_items = Counter(items)
counter_items = self.get_counted_items()
selected_items = list(counter_items)

begin = min(self.inventory_page * INVENTORY_PAGE_SIZE, len(selected_items) - 1)
end = min((self.inventory_page + 1) * INVENTORY_PAGE_SIZE, len(selected_items))

for i in selected_items[begin:end]:
for i, cnt in selected_items[begin:end]:
if i is not None:
acts = [ ]
is_atcive = ''#'(Надето: {0} шт.)'.format(active_items.count(i)) if i in active_items else ''
msg += '{0} ({2} шт. ценой {4} злт.) {1}:\n{3}\n\n'.format(i.name, is_atcive, counter_items[i], i.description, round(i.price * 0.7))
msg += '{0} ({2} шт. ценой {4} злт.) {1}:\n{3}\n\n'.format(i.name, is_atcive, i.count, i.description, round(i.price * 0.7))
if i.usable:
acts.append(i.name)

Expand Down Expand Up @@ -56,12 +56,18 @@ def inventory(self, reply, text):
elif False and text.startswith(locale_manager.get('ACTIVATE')):
name = text[len(locale_manager.get('ACTIVATE')):]

item = self.get_item_by_name(name)

items = self.get_items()
active_items = self.get_active_items()

if item is not None and active_items.count(item) < items.count(item) and (len(active_items) < self.get_active_slots_len()):

item = self.get_item_by_name(name)
active_item = self.get_active_item_by_name(name)
if (
item is not None
and (
active_item is None
or active_item.count < item.count
)
and len(active_items) < self.get_active_slots_len()
):
self.active_items.append((item.buff, item.code_name))
reply(locale_manager.get('ACTIVATED'))
else:
Expand Down Expand Up @@ -112,4 +118,4 @@ def inventory(self, reply, text):
break

if self.state == 'inventory':
self.open_corridor(reply)
self.open_corridor(reply)
Loading

0 comments on commit b36b630

Please sign in to comment.