-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbHandler.py
151 lines (124 loc) · 3.75 KB
/
dbHandler.py
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
#!/usr/bin/python
# -*- coding: utf8 -*-
import sys
import sqlite3
import time
import hashlib
import re
def encodePassword(password):
return hashlib.md5(password).hexdigest()
def isValidName(name):
if len(name)==0:
return False
if len(name)>20:
return False
if re.match("^[A-Za-z0-9_-]*$", name):
return True
return False
def isValidPassword(password):
if len(password)<4:
return False
return True
class DbHandler(object):
def __init__(self, dbfile):
self.dbfile = dbfile
self.connection = sqlite3.connect(self.dbfile)
self.cursor = self.connection.cursor()
def execute(self, args):
#print "args were : %s" % (args)
self.cursor.execute(args)
result = self.cursor.fetchall()
self.connection.commit()
return result
#-------------------------------------------------------------------
# game db init
#-------------------------------------------------------------------
def initTables(self):
self.createPlayersTable()
self.createCharTable()
self.createItemTable()
self.createInventoryTable()
self.createStorageTable()
def reset(self):
self.execute("""drop table players""")
self.execute("""drop table characters""")
self.execute("""drop table items""")
self.execute("""drop table inventory""")
self.execute("""drop table storage""")
#-------------------------------------------------------------------
# players table
#-------------------------------------------------------------------
def createPlayerTable(self):
self.execute("""create table players(
id integer primary key autoincrement,
name text,
password text,
date text)""")
def addPlayer(self, name, password):
if self.hasPlayer(name):
print "Couldn't create player %s : that name already exists." % (name)
return False
if len(name)>30:
print "Couldn't create player %s : that name is way too long!" % (name)
return False
if " " in name:
print "Couldn't create player %s : no space allowed in name!" % (name)
return False
today = time.strftime("%Y%m%d%H%M")
msg = """insert into players values(NULL, '%s', '%s', '%s')""" % (name, encodePassword(password), today)
self.execute(msg)
print "Created player %s" % (name)
def delPlayer(self, name):
msg = """delete from players where name='%s'""" % (name)
self.execute(msg)
def getPlayer(self, name):
info = self.execute("""select * from players where name = '%s'""" % (name))
if len(info)>0:
return info[0]
return None
def hasPlayer(self, name):
if len(self.execute("""select * from players where name = '%s'""" % (name)))>0:
return True
return False
def checkLogin(self, name, password):
res = self.execute("""select * from players where name = '%s'""" % (name))
if len(res)>0:
if res[0][2]==encodePassword(password):
return True
return False
#-------------------------------------------------------------------
# characters
#-------------------------------------------------------------------
def createCharTable(self):
self.execute("""create table characters(
playerName integer,
name text,
sex text,
gmLevel integer,
map text,
x text,
y text
)""")
#-------------------------------------------------------------------
# items, equipment, inventory, storage
#-------------------------------------------------------------------
def createItemTable(self):
self.execute("""create table items(
itemId integer,
name text,
category text,
weight text
)""")
def createInventoryTable(self):
self.execute("""create table inventory(
ownerName text,
itemId integer,
quantity integer
)""")
def createStorageTable(self):
self.execute("""create table storage(
ownerName text,
itemId integer,
quantity integer
)""")
#dbhandler = DbHandler("db/essai.db")