summaryrefslogtreecommitdiff
path: root/curses/tmwcli.py
blob: 93ae52da24e2b3447caaf8b33bdb530654ee51a7 (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
#!/usr/bin/python2

import os
import sys
import logging
import time
import asyncore
import threading
from ConfigParser import ConfigParser

# add .. to PYTHONPATH
parent, _ = os.path.split(os.getcwd())
sys.path.insert(0, parent)
sys.path.insert(1, os.path.join(parent, "plugins"))

try:
    import construct
    del construct
except ImportError:
    sys.path.insert(1, os.path.join(parent, "external"))

del parent

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


class SideBarUpdater(threading.Thread):

    def __init__(self, window, online_users_obj, update_interval=20):
        self._active = True
        self._timer = 0
        self._update_interval = update_interval
        self._online_users_obj = online_users_obj
        self._window = window
        threading.Thread.__init__(self)

    def run(self):
        while self._active:
            if (time.time() - self._timer) > self._update_interval:
                self._window.clear()
                for user in self._online_users_obj.online_users:
                    print user
                    self._window.addstr(user + '\n')
                self._window.refresh()
                self._timer = time.time()
            else:
                time.sleep(1.0)

    def stop(self):
        self._active = False


class CursesDebugLogHandler(logging.Handler):
    def emit(self, record):
        msg = self.format(record)
        cui.chatlog_append(msg)


def loop():
    try:
        while True:
            asyncore.loop(timeout=0.2, count=5)
            logic_manager.tick()
    except KeyboardInterrupt:
        return


if __name__ == "__main__":
    config = ConfigParser()
    if len(sys.argv) > 1:
        config.read(sys.argv[1])
    else:
        config.read('../config/manachat.ini')

    rootLogger = logging.getLogger('')
    rootLogger.addHandler(logging.NullHandler())

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

    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)

    cui.init()

    plugins.load_plugins(config)

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

    online_users = OnlineUsers(config.get('Other', 'online_txt_url'))
    online_users.start()
    side_bar_updater = SideBarUpdater(cui.players_win, online_users)
    side_bar_updater.start()

    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'))

    t = threading.Thread(target=loop)
    t.setDaemon(True)
    t.start()

    cui.input_loop(process_line)

    side_bar_updater.stop()
    online_users.stop()
    cui.finalize()

    mapserv.cleanup()