summaryrefslogtreecommitdiff
path: root/src/char/int_storage.cpp
blob: 32af231b67ba0442ec439e488f1a29f40c87184e (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "int_storage.hpp"
//    int_storage.cpp - Internal storage handling.
//
//    Copyright © ????-2004 Athena Dev Teams
//    Copyright © 2004-2011 The Mana World Development Team
//    Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "../strings/mstring.hpp"
#include "../strings/astring.hpp"
#include "../strings/xstring.hpp"
#include "../strings/literal.hpp"

#include "../generic/db.hpp"

#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"
#include "../io/lock.hpp"
#include "../io/read.hpp"
#include "../io/write.hpp"

#include "../proto2/char-map.hpp"

#include "../mmo/cxxstdio_enums.hpp"

#include "../high/extract_mmo.hpp"
#include "../high/mmo.hpp"

#include "../wire/packets.hpp"

#include "globals.hpp"
#include "inter_conf.hpp"

#include "../poison.hpp"


namespace tmwa
{
namespace char_
{
// 倉庫データを文字列に変換
static
AString storage_tostr(Storage *p)
{
    MString str;
    str += STRPRINTF(
            "%d,%d\t"_fmt,
            p->account_id, p->storage_amount);

    int f = 0;
    for (SOff0 i : SOff0::iter())
    {
        if (p->storage_[i].nameid && p->storage_[i].amount)
        {
            str += STRPRINTF(
                    "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d "_fmt,
                    0 /*id*/,
                    p->storage_[i].nameid,
                    p->storage_[i].amount,
                    p->storage_[i].equip,
                    0 /*identify*/,
                    0 /*refine*/,
                    0 /*attribute*/,
                    0 /*card[0]*/,
                    0 /*card[1]*/,
                    0 /*card[2]*/,
                    0 /*card[3]*/);
            // shouldn't that include 'broken' also? Oh, well ...
            f++;
        }
    }

    str += '\t';

    if (!f)
        return AString();
    return AString(str);
}
} // namespace char_

// 文字列を倉庫データに変換
static
bool impl_extract(XString str, Storage *p)
{
    std::vector<Item> storage_items;
    if (!extract(str,
                record<'\t'>(
                    record<','>(
                        &p->account_id,
                        &p->storage_amount),
                    vrec<' '>(&storage_items))))
        return false;

    if (!p->account_id)
        return false;

    if (storage_items.size() > MAX_STORAGE)
        return false;
    std::copy(storage_items.begin(), storage_items.end(), p->storage_.begin());

    if (p->storage_amount != storage_items.size())
        PRINTF("WARNING: storage desync for %d\n"_fmt, p->account_id);
    return true;
}

namespace char_
{
// アカウントから倉庫データインデックスを得る(新規倉庫追加可能)
Borrowed<Storage> account2storage(AccountId account_id)
{
    P<Storage> s = storage_db.init(account_id);
    s->account_id = account_id;
    return s;
}

//---------------------------------------------------------
// 倉庫データを読み込む
void inter_storage_init(void)
{
    int c = 0;

    io::ReadFile in(inter_conf.storage_txt);
    if (!in.is_open())
    {
        PRINTF("cant't read : %s\n"_fmt, inter_conf.storage_txt);
        return;
    }

    AString line;
    while (in.getline(line))
    {
        Storage s {};
        if (extract(line, &s))
        {
            storage_db.insert(s.account_id, s);
        }
        else
        {
            PRINTF("int_storage: broken data [%s] line %d\n"_fmt,
                    inter_conf.storage_txt, c);
        }
        c++;
    }
}

static
void inter_storage_save_sub(Storage *data, io::WriteFile& fp)
{
    AString line = storage_tostr(data);
    if (line)
        fp.put_line(line);
}

//---------------------------------------------------------
// 倉庫データを書き込む
int inter_storage_save(void)
{
    io::WriteLock fp(inter_conf.storage_txt);

    if (!fp.is_open())
    {
        PRINTF("int_storage: cant write [%s] !!! data is lost !!!\n"_fmt,
                inter_conf.storage_txt);
        return 1;
    }
    for (auto& pair : storage_db)
        inter_storage_save_sub(&pair.second, fp);
    return 0;
}

// 倉庫データ削除
void inter_storage_delete(AccountId account_id)
{
    storage_db.erase(account_id);
}

//---------------------------------------------------------
// map serverへの通信

// 倉庫データの送信
static
void mapif_load_storage(Session *ss, AccountId account_id)
{
    P<Storage> st = account2storage(account_id);
    Packet_Payload<0x3810> payload_10;
    payload_10.account_id = account_id;
    payload_10.storage = *st;
    send_ppacket<0x3810>(ss, payload_10);
}

// 倉庫データ保存完了送信
static
void mapif_save_storage_ack(Session *ss, AccountId account_id)
{
    Packet_Fixed<0x3811> fixed_11;
    fixed_11.account_id = account_id;
    fixed_11.unknown = 0;
    send_fpacket<0x3811, 7>(ss, fixed_11);
}

//---------------------------------------------------------
// map serverからの通信

// 倉庫データ要求受信
static __attribute__((warn_unused_result))
RecvResult mapif_parse_LoadStorage(Session *ss)
{
    Packet_Fixed<0x3010> fixed;
    RecvResult rv = recv_fpacket<0x3010, 6>(ss, fixed);
    if (rv != RecvResult::Complete)
        return rv;

    AccountId account_id = fixed.account_id;
    mapif_load_storage(ss, account_id);

    return rv;
}

// 倉庫データ受信&保存
static __attribute__((warn_unused_result))
RecvResult mapif_parse_SaveStorage(Session *ss)
{
    Packet_Payload<0x3011> payload;
    RecvResult rv = recv_ppacket<0x3011>(ss, payload);
    if (rv != RecvResult::Complete)
        return rv;

    AccountId account_id = payload.account_id;

    {
        P<Storage> st = account2storage(account_id);
        *st = payload.storage;
        mapif_save_storage_ack(ss, account_id);
    }

    return rv;
}

// map server からの通信
// ・1パケットのみ解析すること
// ・パケット長データはinter.cにセットしておくこと
// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない
// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない
RecvResult inter_storage_parse_frommap(Session *ms, uint16_t packet_id)
{
    RecvResult rv;
    switch (packet_id)
    {
        case 0x3010:
            rv = mapif_parse_LoadStorage(ms);
            break;
        case 0x3011:
            rv = mapif_parse_SaveStorage(ms);
            break;
        default:
            return RecvResult::Error;
    }
    return rv;
}
} // namespace char_
} // namespace tmwa