-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.py
265 lines (225 loc) · 7.29 KB
/
backend.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
'''
Created on Aug 15, 2009
@author: koshi
'''
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import *
from datetime import datetime
from dbentities import *
class ElementExistsException( Exception ):
def __init__(self, element):
self.element = element
def __str__(self):
return "Element %s already exists in db"%(self.element)
class ElementNotFoundException( Exception ):
def __init__(self, element):
self.element = element
def __str__(self):
return "Element %s not found in db"%(self.element)
class Backend(object):
'''
classdocs
'''
def __init__(self,alchemy_uri):
'''
Constructor
'''
self.engine = create_engine(alchemy_uri, echo=False)
self.metadata = Base.metadata
self.metadata.bind = self.engine
self.metadata.create_all(self.engine)
self.sessionmaker = sessionmaker( bind=self.engine )
self.usersessions = dict()
self.InitialData()
print 'db init;'
def AddUser(self, nick, country, cpu ):
session = self.sessionmaker()
user = session.query( User ).filter( User.nick == nick ).first()
if not user: #new user
user = User( nick, country, cpu )
user.firstlogin = datetime.now()
user.lobby = self.GetDefaultLobby()
user.lobbyrev = self.GetDefaultLobbyRevision()
user.os = self.GetDefaultOs()
session.add( user )
#session.commit() #commit so we get valid ids??? breaks next commit...
#update user info
user.country = country
user.cpu = cpu
user.lastlogin = datetime.now()
session.add( user )
session.commit()
session.close()
def GetUser( self, nick ):
session = self.sessionmaker()
user = session.query( User ).filter( User.nick == nick ).first()
if not user: #new user
raise ElementNotFoundException(nick)
session.close()
return user
def SetUser(self, user ):
session = self.sessionmaker()
session.add( user )
session.commit()
session.close()
def UpdateUser(self, nick, lobbyname, lobbyrev_name, osname ):
session = self.sessionmaker()
user = session.query( User ).filter( User.nick == nick ).first()
if not user:
#some error output
return
lobby = session.query( Lobby ).filter( Lobby.name == lobbyname ).first()
if not lobby: #new lobby
lobby = Lobby( lobbyname )
session.add( lobby )
session.commit()
lobby = session.query( Lobby ).filter( Lobby.name == lobbyname ).first()
lobbyrev = session.query( LobbyRevision ).filter( LobbyRevision.revision == lobbyrev_name ).first()
if not lobbyrev:
lobbyrev = LobbyRevision( lobbyrev_name, lobby.id )
session.add( lobbyrev )
session.commit()
lobbyrev = session.query( LobbyRevision ).filter( LobbyRevision.revision == lobbyrev_name ).first()
os = session.query( OperatingSystem ).filter( OperatingSystem.name == osname ).first()
if not os:
os = OperatingSystem( osname )
session.add( os )
session.commit()
os = session.query( OperatingSystem ).filter( OperatingSystem.name == osname ).first()
user.os_id = os.id
if user.lobby: #previously set lobby?
if user.lobby_id != lobby.id:
switch = LobbySwitch( user.lobby_id, lobby.id, user.id )
session.add( switch )
elif user.lobbyrev_id != lobbyrev.id : #same lobby, but diff rev
update = LobbyUpdate( user.lobbyrev_id, lobbyrev.id, user.id )
session.add( update )
user.lobbyrev_id = lobbyrev.id
user.lobby_id = lobby.id
session.add( user )
session.commit()
session.close()
def PrintAll(self):
session = self.sessionmaker()
print '-'*60
for instance in session.query(User).order_by(User.id):
print instance.nick, instance.country
for sess in instance.sessions:
print sess.start, ' -- ', sess.end
print '-'*60
session.close()
def StartUsersession(self,nick):
session = self.sessionmaker()
user = session.query(User).filter(User.nick==nick).first()
usersession = Usersession(user.id)
self.usersessions[nick] = usersession
session.close()
def EndUsersession(self,nick):
session = self.sessionmaker()
if nick in self.usersessions:
usersession = self.usersessions[nick]
usersession.end = datetime.now()
session.add(usersession)
session.commit()
del self.usersessions[nick]
session.close()
def GetDefaultLobby(self):
session = self.sessionmaker()
ret = session.query( Lobby ).filter( Lobby.name == 'other' ).first()
session.close()
return ret
def GetDefaultLobbyRevision(self):
session = self.sessionmaker()
ret = session.query( LobbyRevision ).filter( LobbyRevision.revision == 'unknown' ).first()
session.close()
return ret
def GetDefaultOs(self):
session = self.sessionmaker()
ret = session.query( OperatingSystem ).filter( OperatingSystem.name == 'unknown' ).first()
session.close()
return ret
def InitialData(self):
session = self.sessionmaker()
lobby = self.GetDefaultLobby()
if not lobby:
lobby = Lobby( 'other' )
session.add( lobby )
session.commit()
lobby = self.GetDefaultLobby()
lobbyrev = self.GetDefaultLobbyRevision()
if not lobbyrev:
lobbyrev = LobbyRevision( 'unknown', lobby.id )
session.add( lobbyrev )
os = self.GetDefaultOs()
if not os:
os = OperatingSystem( 'unknown' )
session.add( os )
session.commit()
session.close()
def CloseAllSessions(self):
names = []
for name in self.usersessions:
names.append( name )
for name in names:
self.EndUsersession( name )
def GetLobbyUsers( self, lobbyname ):
session = self.sessionmaker()
lobby = session.query( Lobby ).filter( Lobby.name == lobbyname ).first()
ret = 0
if lobby:
ret = session.query( User ).filter( User.lobby_id == lobby.id ).count()
session.close()
return ret
def GetAllUsers( self ):
session = self.sessionmaker()
ret = session.query( User ).count()
session.close()
return ret
def SetPrimaryGame( self, nick, game ):
session = self.sessionmaker()
user = session.query( User ).filter( User.nick == nick ).first()
if user and user.primary_game != 'multiple':
user.primary_game = game
session.commit()
session.close()
def GetGameUsers( self, game_name ):
session = self.sessionmaker()
users = session.query( User ).filter( User.primary_game == game_name )
ret = []
for user in users:
ret.append( user.nick )
session.close()
return ret
def GetUpdates( self, since ):
session = self.sessionmaker()
num = session.query( LobbyUpdate ).filter( LobbyUpdate.date >= since ).count()
session.close()
return num
def GetSessionStats( self ):
ret = dict()
session = self.sessionmaker()
lobbies = session.query( Lobby ).all()
ret['all'] = session.query( Usersession ).count()
for lobby in lobbies:
ret[lobby.name] = session.query(Usersession, User).filter(Usersession.user_id == User.id ).filter(lobby.id == User.lobby_id ).count()
session.close()
return ret
def GetOSstats( self,lobbyname):
ret = dict()
session = self.sessionmaker()
osses = session.query( OperatingSystem ).all()
lobby = session.query( Lobby ).filter( Lobby.name == lobbyname ).first()
for os in osses:
ret[os.name] = session.query(OperatingSystem,User).filter(lobby.id == User.lobby_id ).filter(os.id == User.os_id ).count() / len(osses)
session.close()
return ret
def GetLobbyNames(self):
ret = []
session = self.sessionmaker()
lobbies = session.query( Lobby ).all()
for lobby in lobbies:
ret.append( lobby.name )
session.close()
return ret