-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtips.py
178 lines (139 loc) · 4.85 KB
/
tips.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
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
# Tips system
from lib.database import FunDb
import globals as g
import random
randint = random.randint
cursor = g.cur
# connections in number forms and by names
connections = []
connected_names = 0
connected_pair = []
# Amount of different tips
positive_tips = 1
negative_tips = 3
false_tips = 1
tips = []
first_tip = True
def create_connections():
global connections
query = "SELECT Person_Id FROM Persons WHERE Connectable=1 ORDER BY RAND() LIMIT 4"
cursor.execute(query)
result = cursor.fetchall()
raw_connections = []
for i in result:
raw_connections.append(i[0])
if g.debug:
raw_connections=[1,5,3,6]
half = len(raw_connections) // 2
connections = [raw_connections[:half], raw_connections[half:]]
for connection in connections:
query1 = "UPDATE Persons " \
"SET Connects_Person_Id=CASE Person_Id " \
"WHEN %(id1)s THEN %(id2)s " \
"WHEN %(id2)s THEN %(id1)s " \
"END " \
"WHERE Person_Id IN(%(id1)s, %(id2)s)"
cursor.execute(query1, {'id1': connection[0], 'id2': connection[1]})
return 0
def succesful_connection(connection):
global connected_names
for person_id in connection:
cursor.execute("UPDATE Persons SET Is_Connected=1 WHERE Person_Id=%s", (person_id,))
del(connections[connections.index(connection)])
connected_names += 1
if len(connections) > 0:
create_tip('positive')
create_tip('false')
create_tip('negative')
# this function is not used anywhere, but the SQL is too great to be deleted...
def get_names():
query = "SELECT DISTINCT Persons1.Name AS Person1, Persons2.Name AS Person2 FROM Persons AS Persons1 "\
"JOIN Persons As Persons2 ON Persons1.Person_Id=Persons2.Connects_Person_Id "
cursor.execute(query)
return cursor.fetchall()
def generate_tips():
global positive_tips
global negative_tips
for i in range(positive_tips):
create_tip('positive')
for i in range(negative_tips):
create_tip('negative')
for i in range(false_tips):
create_tip('false')
random.shuffle(tips)
def create_tip(tip_type):
if tip_type == 'negative':
connected = False
is_positive = 0
elif tip_type == 'positive':
connected = True
is_positive = 1
elif tip_type == 'false':
connected = randint(0, 1)
if connected:
is_positive = 0
else:
is_positive = 1
else:
return ''
ids = random_pair(connected)
query = "SELECT Text FROM Line_templates WHERE Positive =" + str(is_positive) + " ORDER BY RAND() LIMIT 1"
cursor.execute(query)
result = cursor.fetchall()
line_template = result[0][0]
query = "SELECT Name FROM Persons WHERE Person_ID IN " + str(ids)
cursor.execute(query)
result = cursor.fetchall()
names = (result[0][0], result[1][0])
line = line_template % names
while True:
person_id = randint(1, 8)
if ids.count(person_id) == 0:
break
cursor.execute("SELECT MAX(Lines_Id) From Line")
result = cursor.fetchall()
line_id = result[0][0] + 1
query = "INSERT INTO Line (Lines_Id, Line_Text, Person_Id, Connects_Person_Id, Is_tip) VALUES (%s, %s, %s, %s, 1)"
cursor.execute(query, (line_id, line, person_id, ids[0]))
def random_pair(connected):
pair = []
if connected:
query = "Select Person_Id FROM Persons Where NOT Connects_Person_Id IS NULL AND Is_Connected = '0'" \
"ORDER BY RAND() LIMIT 1"
cursor.execute(query)
result = cursor.fetchall()
for connection in connections:
if result[0][0] in connection:
pair = connection
return tuple(pair)
else:
query = "Select Person_Id FROM Persons Where Connects_Person_Id IS NULL AND Connectable='1'" \
"ORDER BY RAND() LIMIT 2"
cursor.execute(query)
result = cursor.fetchall()
return result[0][0], result[1][0]
def give_tip(person_id):
global first_tip
result = ""
found_row = False
query = "SELECT Lines_Id, Line_text FROM Line WHERE Is_tip = 1 AND Person_Id=%s ORDER BY RAND () LIMIT 1"
cursor.execute(query, (person_id,))
if cursor.rowcount > 0:
result = cursor.fetchall()
line_id = result[0][0]
result = result[0][1]
found_row = True
propability = randint(0, (4 - g.days))
if propability < 2 or first_tip is True:
if found_row:
cursor.execute("UPDATE Line SET Is_said='1' WHERE Lines_Id=%s", (line_id,))
first_tip = False
else:
result = ""
return result
def show_tips():
sql = "SELECT Line_text, Person_Id FROM Line WHERE Is_tip = 1"
cursor.execute(sql)
if cursor.rowcount > 0:
for row in cursor.fetchall():
print(row)