forked from luizzeroxis/uno-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
196 lines (145 loc) · 5.06 KB
/
server.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Database dealing code (shared between interfaces)
import logging
import os
import pickle
import psycopg2
from psycopg2 import sql
# All possible settings and its possible values (first one is the default)
all_settings = {
'style': ('short', 'emoji', 'circle', 'heart', 'long',),
'show_play_number': ('false', 'true',),
}
# All possible room configs and its possible values (first one is the default)
all_configs = {
# TODO
'draw_4_on_draw_4': ('false', 'true',),
'draw_2_on_draw_4': ('false', 'true', 'true_any_color',),
'disable_call_bluff': ('false', 'true',),
'allow_play_non_drawn_cards': ('false', 'true',),
'allow_pass_without_draw': ('false', 'true',),
'draw_pass_behavior': ('single_draw', 'multiple_draws', 'multiple_draws_disable_pass',),
'allow_highlight_playable_cards': ('false', 'true',),
# 'number_starting_cards': 7,
}
conn, cur = None, None
def main():
# Environment vars
DATABASE_URL = os.environ.get('DATABASE_URL')
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# Database setup
global conn, cur
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
## Database functions
def get_user_settings(user_id):
all_settings_list = list(all_settings)
cur.execute(
sql.SQL("select {fields} from uno_users where user_id=%s limit 1;")
.format(
fields=sql.SQL(',').join(sql.Identifier(n) for n in all_settings_list)
),
(user_id,)
)
result = cur.fetchone()
if not result:
return {k: v[0] for k, v in all_settings.items()} # default values for all settings
else:
settings = dict(zip(all_settings_list, result))
return settings
def get_room_configs(room_id):
all_configs_list = list(all_configs)
cur.execute(
sql.SQL("select {fields} from uno_rooms where id=%s limit 1;")
.format(
fields=sql.SQL(',').join(sql.Identifier(n) for n in all_configs_list)
),
(room_id,)
)
result = cur.fetchone()
if not result: # this is never supposed to happen!
# return {k: v[0] for k, v in all_configs.items()} # default values for all configs
return None
else:
configs = dict(zip(all_configs_list, result))
return configs
def get_current_room(user_id):
cur.execute("select room_id from uno_joins where user_id=%s limit 1;", (user_id,))
result = cur.fetchone()
if result:
return result[0]
return None
def select_users_info_in_room(room_id):
cur.execute("select player_number, user_id from uno_joins where room_id=%s order by player_number, user_id;", (room_id,))
return [(row[0], row[1],) for row in cur]
def select_users_ids_in_room(room_id):
cur.execute("select user_id from uno_joins where room_id=%s order by user_id;", (room_id,))
return [row[0] for row in cur]
def select_player_number(room_id, user_id):
cur.execute("select player_number from uno_joins where room_id=%s and user_id=%s limit 1;", (room_id, user_id))
return cur.fetchone()[0]
def select_user_id_from_player_number(room_id, player_number):
cur.execute("select user_id from uno_joins where room_id=%s and player_number=%s limit 1;", (room_id, player_number))
return cur.fetchone()[0]
def select_game(room_id):
cur.execute("select game_pickle from uno_rooms where id=%s limit 1;", (room_id,))
result = cur.fetchone()[0]
if result:
return pickle.loads(result)
else:
return None
def check_room_empty(room_id):
cur.execute("select room_id from uno_joins where room_id=%s limit 1;", (room_id,))
result = cur.fetchone()
if result:
return False
return True
def check_room_exists(room_id):
cur.execute("select id from uno_rooms where id=%s limit 1;", (room_id,))
result = cur.fetchone()
if result:
return True
return False
def insert_room():
cur.execute("insert into uno_rooms default values returning id;")
room_id = cur.fetchone()[0]
# conn.commit()
return room_id
def insert_user_to_room(room_id, user_id):
cur.execute("insert into uno_joins (room_id, user_id) values (%s, %s);", (room_id, user_id,))
# conn.commit()
def update_game(room_id, game):
cur.execute("update uno_rooms set game_pickle=%s where id=%s;", (pickle.dumps(game), room_id,))
# conn.commit()
def update_player_number(room_id, user_id, player_number):
cur.execute("update uno_joins set player_number=%s where room_id=%s and user_id=%s;", (player_number, room_id, user_id,))
# conn.commit()
def update_user_settings(user_id, setting, value):
cur.execute(
sql.SQL("insert into uno_users (user_id, {settings}) values (%s, %s) "
"on conflict (user_id) do update set {settings} = excluded.{settings};")
.format(
settings=sql.Identifier(setting)
),
(user_id, value,)
)
# conn.commit()
def update_room_config(room_id, config, value):
cur.execute(
sql.SQL("update uno_rooms set {configs}=%s where id=%s;")
.format(
configs=sql.Identifier(config)
),
(value, room_id,)
)
# conn.commit()
def delete_user_from_room(user_id):
cur.execute("delete from uno_joins where user_id=%s;", (user_id,))
# conn.commit()
def delete_room(room_id):
cur.execute("delete from uno_rooms where id=%s;", (room_id,))
# conn.commit()
def commit():
conn.commit()
main()