diff options
author | Jesusaves <cpntb1@ymail.com> | 2020-12-16 16:16:27 -0300 |
---|---|---|
committer | Jesusaves <cpntb1@ymail.com> | 2020-12-16 16:16:27 -0300 |
commit | b8d0795d491e8a68abe968d92898abeb1b9c4c26 (patch) | |
tree | 168eef73bb36cf050defe40cbe1ce313ed8a05cb /utils.py | |
parent | 467fc51d8886b4973bf572ba58b05f20f3f14c85 (diff) | |
download | server-b8d0795d491e8a68abe968d92898abeb1b9c4c26.tar.gz server-b8d0795d491e8a68abe968d92898abeb1b9c4c26.tar.bz2 server-b8d0795d491e8a68abe968d92898abeb1b9c4c26.tar.xz server-b8d0795d491e8a68abe968d92898abeb1b9c4c26.zip |
Add utils for improved logging.
Add the placeholder JSONs from earlier.
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..ae4e6fa --- /dev/null +++ b/utils.py @@ -0,0 +1,145 @@ +######################################################################################## +# 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 threading, time, hashlib, datetime +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 md5(string): + return hashlib.md5(string.encode()).hexdigest() + +def md5salt(string): + global salt + fs=salt+string + return hashlib.md5(fs.encode()).hexdigest() + +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): + if (debug): + print(mx) + 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=zlib.compress(sjs) + sjs=base64.b64encode(sjs) + dbgprint("Compress: Sending base64+zlib JSON 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) + +# Global structures +Player={} +Battle={} +ApTimer={} + +# 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'] +""" + |