-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4319df
commit 6b8cba7
Showing
9 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .chat import ChatClientModule | ||
|
||
__all__ = ["ChatClientModule"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from typing import List, Literal, Optional | ||
|
||
import dearpygui.dearpygui as dpg | ||
import dpg_tools | ||
from DMBotNetwork import Client | ||
from systems.loc import Localization as loc | ||
|
||
|
||
class ChatMessageType: | ||
__slots__ = ["_message_type", "message", "sender"] | ||
|
||
def __init__( | ||
self, | ||
message_type: Literal["ooc", "admin", "ic"], | ||
message: str, | ||
sender: Optional[str] = None, | ||
) -> None: | ||
self._message_type: str = message_type | ||
self.message: str = message | ||
self.sender: Optional[str] = sender | ||
|
||
@property | ||
def message_type(self) -> str: | ||
return self._message_type | ||
|
||
@message_type.setter | ||
def message_type(self, value: Literal["ooc", "admin", "ic"]) -> None: | ||
self._message_type = value | ||
|
||
|
||
class ChatClientModule: | ||
messages: List[ChatMessageType] = [] | ||
filters = {"ooc": True, "admin": True, "ic": True} | ||
|
||
@staticmethod | ||
async def send_chat_message(chat_message_type: ChatMessageType) -> None: | ||
"""Отправляет сообщение через сеть.""" | ||
await Client.req_net_func( | ||
"send_message", | ||
message=chat_message_type.message, | ||
message_type=chat_message_type.message_type, | ||
) | ||
|
||
@staticmethod | ||
async def net_get_chat_message( | ||
message: str, message_type: str, sender: Optional[str] = None | ||
) -> None: | ||
"""Получает сообщение и добавляет его в чат.""" | ||
ChatClientModule.messages.append(ChatMessageType(message_type, message, sender)) # type: ignore Кладём хуй на проверку с сервера) | ||
ChatClientModule.update_chat_display() | ||
|
||
@classmethod | ||
async def _iternal_send_message(cls, sender, app_data, user_data) -> None: | ||
"""Асинхронная отправка сообщения через UI.""" | ||
message = dpg_tools.decode_string(dpg.get_value("chat_input")) | ||
message_type = dpg.get_value("message_type_selector") | ||
chat_message = ChatMessageType(message_type, message) | ||
await cls.send_chat_message(chat_message) | ||
|
||
@classmethod | ||
def create_window(cls, sender, app_data, user_data): | ||
"""Создание основного окна чата с фильтрацией и отправкой сообщений.""" | ||
if dpg.does_item_exist("chat_window"): | ||
dpg.focus_item("chat_window") | ||
return | ||
|
||
with dpg.window( | ||
label=loc.get_string("chat_window_lable"), | ||
tag="chat_window", | ||
width=400, | ||
height=400, | ||
): | ||
with dpg.group(horizontal=True): | ||
dpg.add_checkbox( | ||
label="OOC", | ||
default_value=True, | ||
callback=cls.update_filter, | ||
user_data="ooc", | ||
tag="ooc_filter", | ||
) | ||
|
||
dpg.add_checkbox( | ||
label="Admin", | ||
default_value=True, | ||
callback=cls.update_filter, | ||
user_data="admin", | ||
tag="admin_filter", | ||
) | ||
|
||
dpg.add_checkbox( | ||
label="IC", | ||
default_value=True, | ||
callback=cls.update_filter, | ||
user_data="ic", | ||
tag="ic_filter", | ||
) | ||
|
||
dpg.add_child_window( | ||
label="Chat Display", autosize_x=True, height=200, tag="chat_display" | ||
) | ||
|
||
with dpg.group(horizontal=True): | ||
dpg.add_combo( | ||
items=["ooc", "admin", "ic"], | ||
default_value="ooc", | ||
tag="message_type_selector", | ||
width=70, | ||
) | ||
dpg.add_input_text(width=300, tag="chat_input") | ||
|
||
dpg.add_button(label="Отправить", callback=cls._iternal_send_message) | ||
|
||
@classmethod | ||
def update_filter(cls, sender, app_data, user_data): | ||
"""Обновляет фильтры в зависимости от состояния чекбоксов.""" | ||
cls.filters[user_data] = app_data | ||
cls.update_chat_display() | ||
|
||
@classmethod | ||
def update_chat_display(cls): | ||
"""Обновляет отображение сообщений в чате на основе фильтров.""" | ||
dpg.delete_item("chat_display", children_only=True) | ||
|
||
for chat_message in cls.messages: | ||
if cls.filters[chat_message.message_type]: | ||
display_message = f"[{chat_message.message_type}] {chat_message.sender}: {chat_message.message}" | ||
dpg.add_text(display_message, parent="chat_display", wrap=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .main import admin_main_window | ||
from .main import admin_menu_setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import user_menu_setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import dearpygui.dearpygui as dpg | ||
from api import ChatClientModule | ||
from systems.loc import Localization as loc | ||
|
||
|
||
async def user_menu_setup(): | ||
dpg.add_menu( | ||
label=loc.get_string("user_menu"), | ||
tag="user_menu_bar", | ||
parent="main_bar", | ||
) | ||
|
||
dpg.add_menu_item( | ||
label=loc.get_string("chat_window_lable"), | ||
parent="user_menu_bar", | ||
callback=ChatClientModule.create_window, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
dearpygui | ||
dearpygui-async | ||
DMBotNetwork==0.2.8 | ||
DMBotNetwork==0.3.0 | ||
DMBotTools | ||
msgpack | ||
Pillow | ||
|