49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
from collections import Counter
|
|
from typing import List
|
|
|
|
from mesa.visualization.modules import TextElement
|
|
|
|
from data.Item import Item
|
|
from data.enum.ItemType import ItemType
|
|
|
|
|
|
class DisplayItemListAttributeElement(TextElement):
|
|
def __init__(self, attr_name):
|
|
'''
|
|
Create a new text attribute element.
|
|
|
|
Args:
|
|
attr_name: The name of the attribute to extract from the model.
|
|
|
|
Example return: "happy: 10"
|
|
'''
|
|
self.attr_name = attr_name
|
|
|
|
def render(self, model):
|
|
val = getattr(model, self.attr_name)
|
|
|
|
itemList: List[Item] = val
|
|
|
|
itemList = map(lambda x: x.real_type, itemList)
|
|
|
|
itemCounter = Counter(itemList)
|
|
|
|
# return self.attr_name + ":" + pprint.pformat(itemCounter)
|
|
res = self.attr_name + ":" + "<ul>"
|
|
|
|
for e in itemCounter:
|
|
key = e
|
|
val = itemCounter[key]
|
|
|
|
key_str = ""
|
|
if key == ItemType.DOOR:
|
|
key_str = "Door"
|
|
elif key == ItemType.SHELF:
|
|
key_str = "Shelf"
|
|
|
|
res += "<li>{}:{}</li>".format(key, val)
|
|
|
|
res += "</ul>"
|
|
|
|
return res
|