summaryrefslogtreecommitdiff
path: root/hercules/code/server/party.py
blob: bcd7fc510db5fff66a8a7266ea6d6c4bba76858a (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
# -*- coding: utf8 -*-
#
# Copyright (C) 2014  Evol Online
# Author: Andrei Karas (4144)

import re

from code.fileutils import readFile
from code.stringutils import stripNewLine, escapeSqlStr

def findLeaderId(name, users):
    for userId in users:
        user = users[userId]
        if user.char_name == name:
            return user.char_id
    return 0

def convertParty(users):
    srcFile = "olddb/party.txt"
    dstFile = "newdb/party.sql"
    fieldsSplit = re.compile("\t")
    comaSplit = re.compile(",")
    tpl = readFile("templates/party.sql")
    firstLine = True
    with open(dstFile, "w") as w:
        w.write(tpl)
        w.write("INSERT INTO `party` VALUES ")
        with open(srcFile, "r") as r:
            for line in r:
                if line[:2] == "//":
                    continue
                line = stripNewLine(line)
                rows = fieldsSplit.split(line)
                if len(rows) == 2:
                    continue
                if len(rows) < 3:
                    print("wrong party.txt line: " + line)
                    continue

                partyId = rows[0]
                partyName = rows[1]

                tmp = comaSplit.split(rows[2])
                partyExp = tmp[0]
                partyItem = tmp[1]

                leaderId = 0
                leaderName = ""
                accountId = ""

                for f in xrange(3, len(rows), 2):

                    if rows[f] == "0,0" or rows[f] == "":
                        continue

                    tmp = comaSplit.split(rows[f])
                    accountId = tmp[0]
                    leader = tmp[1]
                    charName = rows[f + 1]
                    if leader == "1":
                        leaderId = accountId
                        leaderName = charName

                if firstLine == False:
                    w.write(",\n")
                else:
                    firstLine = False

                leaderCharId = findLeaderId(leaderName, users)

                w.write(("({party_id},'{name}',{exp},{item}," +
                    "{leader_id},{leader_char})").format(
                    party_id = partyId,
                    name = escapeSqlStr(partyName),
                    exp = partyExp,
                    item = partyItem,
                    leader_id = leaderId,
                    leader_char = leaderCharId
                ))
        w.write("\n")