summaryrefslogtreecommitdiff
path: root/utils.py
blob: b8b8aad791c610ac2491b9ee4b63065ae3b7a298 (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
#################################################################################
#     This file is part of Spheres.
#     Copyright (C) 2019  Jesusalva

#     This library is free software; you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public
#     License as published by the Free Software Foundation; either
#     version 2.1 of the License, or (at your option) any later version.

#     This library 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
#     Lesser General Public License for more details.

#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#################################################################################
# Util Module, forcefully inheirted by all modules

import time, hashlib, datetime, uuid
import json, zlib, base64

# Be verbose
debug=True

# Create a variable called "salt", which contains the token saltening sequence
s=open("salt.txt", "r")
for l in s:
    salt=l
s.close()

def ifte(ifs, then, elses):
    if (ifs):
        return then
    else:
        return elses

def md5(string):
    return hashlib.md5(string.encode()).hexdigest()

def md5salt(string):
    global salt
    fs=salt+string
    return hashlib.md5(fs.encode()).hexdigest()

def create_token():
    return uuid.uuid4().hex

def now():
    return int(time.time())

# [0]- Day, [1]- Month, [2]- Year, [3]-Weekday, [4]-Hour, [5]-Minute
def return_date(dt):
    day=dt.timetuple()[2]
    month=dt.timetuple()[1]
    year=dt.timetuple()[0]

    hour=dt.timetuple()[3]
    minute=dt.timetuple()[4]

    weekday=dt.timetuple()[6]

    #second=5, days_since_01/01=7
    return day, month, year, weekday, hour, minute

# Date from a SQL DATETIME string
def date_from_sql(date):
    # We assume date was taken from SQL DATETIME field
    dt=datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
    return return_date(dt)

# Current date (NOT UTC)
def date_from_now():
    dt=datetime.datetime.now()
    return return_date(dt)


def dbgprint(mx):
    print(mx)
    if (debug):
        dfile=open("debug.txt", 'a')
        dt=datetime.datetime.now().timetuple()
        date="[%04d-%02d-%02d %02d:%02d:%02d]" % (dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])
        dfile.write("%s %s\n" % (date, mx))
        dfile.close()

def stdout(mx):
    dbgprint(mx)

def compress(string):
    dbgprint("Compress: Received JSON data: %s" % (string))
    sjs=json.dumps(string)
    sjs=sjs.encode("ascii")
    sjs=zlib.compress(sjs)
    sjs=base64.b64encode(sjs)
    sjs=sjs.decode("ascii")
    dbgprint("Compress: Sending base64+zlib JSON ASCII data: %s" % (sjs))
    return sjs

# Search for array[?][key]==search in an array of dicts
# Returns the dictionary, or returns "ERROR"
def dl_search(array, key, search):
    return next((item for item in array if item[key] == search), "ERROR")

# Global classes
# We need to override standard list method. Original by Triptych (stackoverflow)
class dlist(list):

    def __setitem__(self, index, value):
        size = len(self)
        if index >= size:
            self.extend(None for _ in range(size, index + 1))

        list.__setitem__(self, index, value)

# Search for a client by user ID
# Returns the dictionary, or returns "ERROR"
def cli_search(search):
    global clients
    return next((item for item in clients if item.userid == search), "ERROR")

# Global structures
Player={}
Battle={}
ApTimer={}
clients = []

# Databases
# Load quest database
f=open("quests.json", "r")
allquests=json.load(f)
f.close()

# Load units database
f=open("units.json", "r")
allunits=json.load(f)
f.close()

# Load skills database
f=open("skills.json", "r")
allskills=json.load(f)
f.close()

# Load summons database
f=open("summons.json", "r")
allsummons=json.load(f)
f.close()

# Load taverns database
# Chance total must be 100% (or 10000). Rolled ONCE
f=open("bar.json", "r")
allbars=json.load(f)
f.close()

###########################################################################
# Comments
"""
abc=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
     'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
     'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
"""