summaryrefslogtreecommitdiff
path: root/gui/managui.py
blob: d9d4976589990fd3d7bcc387db4ee027ae67f0e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python2

import asyncore
import logging
import webbrowser

import kivy
kivy.require('1.9.1')

from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import BooleanProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup

from kivy.config import ConfigParser
config = ConfigParser()
config.read("manachat.ini")

import monsterdb
import itemdb
import net
import net.mapserv as mapserv
import gui.handlers as handlers
import plugins
from commands import process_line
from net.onlineusers import OnlineUsers
from loggers import netlog, debuglog
from logicmanager import logic_manager


class DebugLogHandler(logging.Handler):

    def __init__(self, app, **kwargs):
        self.app = app
        super(self.__class__, self).__init__(**kwargs)

    def emit(self, record):
        msg = self.format(record)
        self.app.root.messages_log.append_message(msg)


class MenuPopup(Popup):
    visible = BooleanProperty(False)

    def on_open(self, *args):
        self.visible = True

    def on_dismiss(self, *args):
        self.visible = False


class AboutPopup(Popup):
    pass


class RootWidget(FloatLayout):
    mobile = BooleanProperty(False)

    def _focus_chat_input(self, dt):
        self.chat_input.focus = True

    def on_command_enter(self, *args):
        process_line(self.chat_input.text.encode('utf-8'))
        self.chat_input.text = ''
        Clock.schedule_once(self._focus_chat_input, 0.1)  # dirty hack :^)


class ManaGuiApp(App):
    use_kivy_settings = BooleanProperty(False)

    def hook_keyboard(self, window, key, *largs):
        if key == 27:
            self.show_menu(not self._menu_popup.visible)
            # self.stop()
            return True
        return False

    def on_start(self):
        if config.getboolean('Other', 'log_network_packets'):
            import os
            import tempfile

            logfile = os.path.join(tempfile.gettempdir(), "netlog.txt")
            netlog.setLevel(logging.INFO)
            fh = logging.FileHandler(logfile, mode="w")
            fmt = logging.Formatter("[%(asctime)s] %(message)s",
                                    datefmt="%Y-%m-%d %H:%M:%S")
            fh.setFormatter(fmt)
            netlog.addHandler(fh)

        monsterdb.read_monster_db()
        itemdb.load_itemdb('itemdb.txt')

        dbgh = DebugLogHandler(self)
        dbgh.setFormatter(logging.Formatter("[%(asctime)s] %(message)s",
                                            datefmt="%H:%M"))
        debuglog.addHandler(dbgh)
        debuglog.setLevel(logging.INFO)

        net2 = DebugLogHandler(self)
        net2.setFormatter(logging.Formatter("[%(asctime)s] %(message)s",
                                            datefmt="%H:%M"))
        net2.setLevel(logging.ERROR)
        netlog.addHandler(net2)

        plugins.load_plugins(config)

        handlers.app = self

        import chat
        chat.pp_actions = ()

        # self.reconnect()

        Clock.schedule_once(self.update_online_list, 0.2)
        Clock.schedule_interval(self.update_online_list, 35)
        Clock.schedule_interval(self.update_loop, 0)
        Clock.schedule_once(self.show_menu, 1.5)

    def build(self):
        self.icon = 'icon.png'
        Window.bind(on_keyboard=self.hook_keyboard)
        return RootWidget()

    def build_settings(self, settings):
        from kivy.uix.settings import SettingString

        class SettingPassword(SettingString):
            def _create_popup(self, instance):
                SettingString._create_popup(self, instance)
                self.textinput.password = True

            def add_widget(self, *largs):
                if self.content is not None:
                    self.content.clear_widgets()
                return SettingString.add_widget(self, *largs)

        settings.register_type('password', SettingPassword)
        settings.add_json_panel('ManaChat', config,
                                filename='manachat.json')

    def update_loop(self, *l):
        asyncore.loop(timeout=0, count=10)
        logic_manager.tick()

    def on_pause(self):
        return True

    def on_stop(self):
        Clock.unschedule(self.update_loop)
        Clock.unschedule(self.update_online_list)

    def open_link(self, link):
        webbrowser.open(link)

    def update_online_list(self, *l):
        lst = OnlineUsers.dl_online_list(config.get('Other',
                                                    'online_txt_url'))
        if lst is not None:
            lst.sort()
        self.root.players_list.items = lst

    def reconnect(self):
        if mapserv.server is not None:
            mapserv.cleanup()

        net.login(host=config.get('Server', 'host'),
                  port=config.getint('Server', 'port'),
                  username=config.get('Player', 'username'),
                  password=config.get('Player', 'password'),
                  charname=config.get('Player', 'charname'))

        if hasattr(self, '_menu_popup'):
            self._menu_popup.dismiss()

    def show_about(self):
        AboutPopup().open()

    def show_menu(self, show=True):
        if not hasattr(self, '_menu_popup'):
            self._menu_popup = MenuPopup()
        if show:
            self._menu_popup.open()
        else:
            self._menu_popup.dismiss()


if __name__ == "__main__":
    ManaGuiApp().run()