summaryrefslogtreecommitdiff
path: root/status.py
blob: 6b497a34238673c26432fef14397a8c190e2ccca (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
from collections import OrderedDict
import net.mapserv as mapserv
import net.stats as st
import itemdb
from utils import encode_str
import mapnames


def stats_repr(*stat_types):
    ps = mapserv.player_stats
    sd = OrderedDict()

    if 'stats' in stat_types:
        sd['stats'] = 'STR:{} AGI:{} VIT:{} INT:{} DEX:{} LUK:{}'.format(
            ps[st.STR], ps[st.AGI], ps[st.VIT], ps[st.INT],
            ps[st.DEX], ps[st.LUK])

    if 'hpmp' in stat_types:
        sd['hpmp'] = 'HP:{}/{} MP:{}/{}'.format(ps[st.HP], ps[st.MAX_HP],
                                                ps[st.MP], ps[st.MAX_MP])

    if 'weight' in stat_types:
        sd['weight'] = 'WG: {}/{}'.format(ps[st.TOTAL_WEIGHT],
                                          ps[st.MAX_WEIGHT])

    if 'points' in stat_types:
        sd['points'] = 'LVL: {} EXP:{}/{} CP:{} SP:{}'.format(
            ps[st.LEVEL], ps[st.EXP], ps[st.EXP_NEEDED],
            ps[st.CHAR_POINTS], ps[st.SKILL_POINTS])

    if 'zeny' in stat_types:
        sd['zeny'] = 'GP:{}'.format(ps[st.MONEY])

    if 'attack' in stat_types:
        sd['attack'] = 'ATK:{} DEF:{} MATK:{} MDEF:{}'.format(
            ps[st.ATK], ps[st.DEF], ps[st.MATK], ps[st.MDEF])

    if 'skills' in stat_types:
        sl = []
        ps = mapserv.player_skills
        skill_names = {339: 'focusing', 45: 'mallard', 350: 'brawling',
                       352: 'speed', 353: 'resist', 354: 'astral',
                       355: 'raging', 340: 'magic', 341: 'life',
                       342: 'war', 343: 'transmut', 344: 'nature',
                       345: 'astralm'}

        for s_id, s_v in ps.items():
            if s_v > 0:
                sl.append('{}:{}'.format(skill_names.get(s_id,
                                                         str(s_id)), s_v))

        sd['skills'] = ' '.join(sl)

    return sd


def invlists(max_items=1000):
    inventory = OrderedDict()

    for id_, amount in mapserv.player_inventory.values():
        inventory[id_] = inventory.setdefault(id_, 0) + amount

    lists = []
    data = '\302\202B1'
    i = 0
    for id_, amount in inventory.items():
        i += 1
        if i > max_items:
            i = 0
            lists.append(data)
            data = '\302\202B1'
        data += encode_str(id_, 2)
        data += encode_str(1, 4)
        data += encode_str(amount, 3)

    lists.append(data)
    return lists


def invlists2(max_length=255, source='inventory'):
    inventory = OrderedDict()

    if source == 'inventory':
        source = mapserv.player_inventory
    elif source == 'storage':
        source = mapserv.player_storage
    else:
        return []

    for id_, amount in source.values():
        inventory[id_] = inventory.setdefault(id_, 0) + amount

    lists = []
    data = ''
    for id_, amount in inventory.items():
        s = itemdb.item_name(id_, True) + ', '
        if amount > 1:
            s = str(amount) + ' ' + s
        if len(data + s) > max_length:
            lists.append(data)
            data = ''
        data += s

    lists.append(data[:-2])
    return lists


def player_position():
    pp = mapserv.player_pos
    map_name = mapnames.map_names.get(pp['map'], 'Unknown')
    s = "Map: {} ({}), coor: {}, {}".format(
        map_name, pp['map'], pp['x'], pp['y'])
    return s


def split_names(names, origin=[''], maxlen=255, separator=', '):
    origin = origin[:]
    if len(origin) < 1:
        origin = ['']

    for n in names:
        ns = n + separator
        if len(origin[-1] + ns) > maxlen:
            origin.append(ns)
        else:
            origin[-1] += ns

    origin[-1] = origin[-1][:-len(separator)]

    return origin


def nearby(btype=''):
    nearby_beings = {'player': {}, 'npc': {}, 'monster': {}, 'portal': []}
    for being in mapserv.beings_cache.itervalues():
        if btype and being.type != btype:
            continue

        if being.type == 'portal':
            nearby_beings['portal'].append((being.x, being.y))
            continue

        if being.type == 'npc' and len(being.name) < 1:
            nearby_beings[being.type][str(being.job)] = 1
        elif being.name in nearby_beings[being.type]:
            nearby_beings[being.type][being.name] += 1
        else:
            nearby_beings[being.type][being.name] = 1

    del_types = []
    for t in nearby_beings:
        if not nearby_beings[t]:
            del_types.append(t)
    for t in del_types:
        del nearby_beings[t]

    lines = []
    for bt in ('monster', 'player', 'npc'):
        if bt not in nearby_beings:
            continue
        lines.append(bt + 's : ')
        names_s = []
        for bname in sorted(nearby_beings[bt].iterkeys()):
            count = nearby_beings[bt][bname]
            if count > 1:
                names_s.append('{} ({})'.format(bname, count))
            else:
                names_s.append(bname)

        lines = split_names(names_s, lines)

    if 'portal' in nearby_beings:
        lines.append('portals : ')
        coor_s = []
        for cx, cy in nearby_beings['portal']:
            coor_s.append('({},{})'.format(cx, cy))
        lines = split_names(coor_s, lines)

    return lines