-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectMapUpload.py
427 lines (386 loc) · 18.7 KB
/
DirectMapUpload.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from PluginInterface import *
from Manialink import *
from WindowElements import *
from GBXChallengeFetcher import GBXChallengeFetcher
"""
\file DirectUpload.py
\brief Contains methods for direct uploads of maps
"""
class DirectMapUpload(PluginInterface):
"""
\class DirectMapUpload
\brief Manage direct uploads of maps
"""
def __init__(self, pipes, args):
"""
\brief Create a new instance
\param pipes The communication pipes to the PluginManager
\param args Additional startup arguments
"""
super(DirectMapUpload, self).__init__(pipes)
self.__directUploadPath = 'direct_upload' #the name of the map folder for direct uploads
def initialize(self, args):
"""
\brief Initialize the instance
\param args Additional startup arguments
"""
self.callMethod(('Acl', 'rightAdd'), 'DirectMapUpload.directMapUpload',
'Upload maps directly to the server via HTTP connection.')
#self.callMethod(('TmChat', 'registerChatCommand'), 'upload',
# ('DirectMapUpload', 'chat_upload'),
# 'Upload a map file to the server from within the game.')
self.callMethod(('TmChat', 'registerChatCommand'), 'browse',
('DirectMapUpload', 'chat_browse'),
'Browse your directly uploaded maps.')
self.callMethod(('Http', 'registerPath'), '/map/upload/',
('DirectMapUpload', 'web_upload'))
def web_upload(self, entries, login):
"""
\brief Display the upload formular
\param entries (Do not matter)
\param login The login of the calling player
"""
manialink = Manialink()
#the instruction text
instruction = Label()
instruction['text'] = 'Please select the map you want to upload and click upload!'
instruction['sizen'] = '30 3'
instruction['posn'] = '0 4'
manialink.addChild(instruction)
#the label of the submit button
label = Label()
label['posn'] = '19 -1'
label['text'] = 'Upload!'
manialink.addChild(label)
#the submit butten background
quad = Quad()
quad['posn'] = '18 0'
quad['sizen'] = '9 4'
quad['style'] = 'Bgs1'
quad['substyle'] = 'PlayerCard'
ml = self.callFunction(('Http', 'getUploadToken'),
('DirectMapUpload', 'directMapUpload'),
login)
quad['manialink'] = ('POST(http://' + str(ml[1][0]) + ':' + str(ml[1][1])
+ '?token=' + str(ml[0]) +
'&map=inputTrackFile,inputTrackFile)')
manialink.addChild(quad)
#the entry
entry = FileEntry()
entry['posn'] = "2 -1"
entry['sizen'] = "15 3"
entry['name'] = "inputTrackFile"
entry['folder'] = "Maps"
entry['default'] = "Pick Map"
manialink.addChild(entry)
return '<?xml version="1.0" encoding="utf-8" ?>' + manialink.getXML()
def chat_upload(self, login, param):
"""
\brief Chat callback for upload command (display upload form)
\param login The login of the calling player
\param param Additional params, should be ignored
"""
if not self.callFunction(('Acl', 'userHasRight'), login, 'DirectMapUpload.directMapUpload'):
self.callFunction(('TmConnector', 'ChatSendServerMessageToLogin'),
'You have insufficient rights to directly upload maps to this server!',
login)
return False
frame = Frame()
#the label of the submit button
label = Label()
label['posn'] = '19 -1'
label['text'] = 'Upload!'
frame.addChild(label)
#the submit butten background
quad = Quad()
quad['posn'] = '18 0'
quad['sizen'] = '9 4'
quad['style'] = 'Bgs1'
quad['substyle'] = 'PlayerCard'
ml = self.callFunction(('Http', 'getUploadToken'),
('DirectMapUpload', 'directMapUpload'),
login)
quad['manialink'] = ('POST(http://' + str(ml[1][0]) + ':' + str(ml[1][1])
+ '?token=' + str(ml[0]) +
'&map=inputTrackFile,inputTrackFile)')
frame.addChild(quad)
#the entry
entry = FileEntry()
entry['posn'] = "2 -1"
entry['sizen'] = "15 3"
entry['name'] = "inputTrackFile"
entry['folder'] = "Maps"
entry['default'] = "Pick Map"
frame.addChild(entry)
w = Window('Choose map for upload')
w.setSize((30, 10))
w.setPos((-15, 5))
w.addChild(frame)
self.callMethod(('WindowManager', 'displayWindow'),
login, 'DirectMapUpload.directMapUpload', w)
def directMapUpload(self, entries, data, login):
"""
\brief Callback for the direct http upload of maps
\param entries The entries dict of the request (containing the filename)
\param data The file data of the file uploaded
\param login The login of the uploading user
"""
#hide the upload form as the token is now expired
self.callMethod(('ManialinkManager', 'hideManialinkToLogin'),
'DirectMapUpload.directMapUpload', login)
if self.callFunction(('Acl', 'userHasRight'), login,
'DirectMapUpload.directMapUpload'):
#get the map folder path
mapPath = (os.path.dirname(self.callFunction(
('TmConnector', 'GetMapsDirectory')))
+ os.path.sep)
#get the filename
fileName = os.path.basename(entries['map'])
#assemble the relative path for the users map files
relPath = self.__directUploadPath + os.path.sep + login + os.path.sep
#create dir if not already existent
if not os.path.isdir(mapPath + relPath):
os.mkdir(mapPath + relPath)
#test if this file already exists
if os.path.isfile(mapPath + relPath + fileName):
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="$f11$oError$o$fff: This file already exists!"/>
</manialink>
"""
#try to write the file
try:
mapFile = open(mapPath + relPath + fileName, "w")
mapFile.write(data)
mapFile.close()
except:
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="$f11$oError$o$fff: Could not write file. Try again later!"/>
</manialink>
"""
if self.callFunction(('TmConnector', 'AddMap'), relPath + fileName):
info = self.callFunction(('TmConnector', 'GetMapInfo'), relPath + fileName)
self.callFunction(('TmConnector', 'RemoveMap'), relPath + fileName)
if info['Author'] != login:
os.remove(mapPath + relPath + fileName)
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="You can only upload maps where the author is identical to your login"/>
</manialink>
"""
else:
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="Thank you for uploading this map!"/>
</manialink>
"""
else:
os.remove(mapPath + relPath + fileName)
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="Could not add map to list. Is this a map file?"/>
</manialink>
"""
else:
self.callFunction(('TmConnector', 'ChatSendServerMessageToLogin'),
'You have insufficient rights to directly upload maps to this server!',
login)
return """
<?xml version="1.0" encoding="utf-8" ?>
<manialink>
<label text="You have insufficient rights to directly upload maps to this server!"/>
</manialink>
"""
def chat_browse(self, login, args):
"""
\brief Browse the directly uploaded files
"""
mapPath = (os.path.dirname(self.callFunction(
('TmConnector', 'GetMapsDirectory')))
+ os.path.sep)
relativePersonalMapPath = self.__directUploadPath + os.path.sep + login + os.path.sep
personalMapPath = mapPath + relativePersonalMapPath
if not os.path.isdir(personalMapPath):
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'You do not have an upload path to browse!',
login)
return False
files = os.listdir(personalMapPath)
if len(files) == 0:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'You have no uploaded files yet!',
login)
return False
files.sort()
mapRotationFileNames = [i
for i in self.callFunction(('TmConnector', 'GetMapList'), 100000, 0)]
myMapsInRotation = filter(lambda i: os.path.relpath(personalMapPath + i , mapPath)
in mapRotationFileNames, files)
myMapsOutRotation = filter(lambda i: os.path.relpath(personalMapPath + i , mapPath)
not in mapRotationFileNames, files)
lines = []
for m in myMapsOutRotation:
line = []
nameLabel = Label()
nameLabel['text'] = m
line.append(nameLabel)
commentsLabel = Label()
commentsLabel['text'] = 'read'
commentsLabel.setCallback(('DirectMapUpload', 'cb_readComments'),
self.callFunction(('Maps', 'getMapIdFromUid'),
GBXChallengeFetcher.FetchChallenge(personalMapPath + m,
True).uid))
line.append(commentsLabel)
publishLabel = Label()
publishLabel['text'] = 'publish'
publishLabel.setCallback(('DirectMapUpload', 'cb_publishMap'), None,
relativePersonalMapPath + m )
line.append(publishLabel)
deleteLabel = Label()
deleteLabel['text'] = 'delete'
deleteLabel.setCallback(('DirectMapUpload', 'cb_deleteMap'), None,
relativePersonalMapPath + m )
line.append(deleteLabel)
lines.append(line)
for m in myMapsInRotation:
line = []
nameLabel = Label()
nameLabel['text'] = os.path.basename(m)
line.append(nameLabel)
commentsLabel = Label()
commentsLabel['text'] = 'read'
commentsLabel.setCallback(('DirectMapUpload', 'cb_readComments'),
self.callFunction(('Maps', 'getMapIdFromUid'),
GBXChallengeFetcher.FetchChallenge(personalMapPath + m,
True).uid))
line.append(commentsLabel)
unpublishLabel = Label()
unpublishLabel['text'] = 'unpublish'
unpublishLabel.setCallback(('DirectMapUpload', 'cb_unpublishMap'), None,
relativePersonalMapPath + m)
line.append(unpublishLabel)
deleteLabel = Label()
deleteLabel['text'] = 'delete'
deleteLabel.setCallback(('DirectMapUpload', 'cb_deleteMap'), None,
relativePersonalMapPath + m)
line.append(deleteLabel)
lines.append(line)
nick = self.callFunction(('Players', 'getPlayerNickname'), login)
window = TableWindow(nick + '$z$g\'s uploaded maps')
window.setSize((80, 60))
window.setPos((-40, 30))
window.setTable(lines, 10, (50, 15, 10, 5), ('Filename', 'Maprotation', 'Comments', 'File'))
self.callMethod(('WindowManager', 'displayWindow'), login, 'DirectMapUpload.browse', window, True)
def cb_readComments(self, entries, login, mapId):
"""
\brief Callback for reading comments
\param entries Should be empty
\param login The login of the calling player
\param mapId The id of the map to read the comments of
"""
self.callMethod(('WindowManager', 'closeWindow'), None, login, 'DirectMapUpload.browse')
self.callMethod(('Maps', 'chat_comment'), login, 'display ' + str(mapId))
def cb_deleteMap(self, entries, login, yes, filePath):
"""
\brief Delete the map file from disk (and remove it from the map list
\param entries Should be empty
\param login The login of the calling player
\param yes Is this already confirmed
\param filePath The path to the file to delete (relative to the maps folder)
"""
if yes == None:
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.browse')
confirm = YesNoDialog('Do you really want to delete the file ' + os.path.basename(filePath))
confirm.setAnswerCallback(('DirectMapUpload', 'cb_deleteMap'), filePath)
confirm.setSize((40, 10))
confirm.setPos((-20, 5))
self.callMethod(('WindowManager', 'displayWindow'), login, 'DirectMapUpload.deleteConfirm', confirm)
return
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.deleteConfirm')
if yes == True:
mapPath = os.path.dirname(self.callFunction(
('TmConnector', 'GetMapsDirectory')))
myPath = (mapPath
+ os.path.sep + self.__directUploadPath + os.path.sep
+ login + os.path.sep)
if os.path.samefile(myPath,
os.path.dirname(mapPath + os.path.sep + filePath)):
self.cb_unpublishMap(entries, login, True, filePath)
try:
os.remove(mapPath + os.path.sep + filePath)
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Successfully deleted file ' +
os.path.basename(filePath),
login)
except:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Error while deleting file {} try again later'.
format(os.path.basename(filePath)),
login)
else:
pass
else:
pass
def cb_publishMap(self, entries, login, yes, fileName):
"""
\brief Add a map to the map rotation
\param entries Should be empty
\param login The login of the calling player
\param yes Is this already confirmed
\param fileName The file to publish
"""
if yes == None:
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.browse')
confirm = YesNoDialog('Do you really want to publish ' + os.path.basename(fileName))
confirm.setAnswerCallback(('DirectMapUpload', 'cb_publishMap'), fileName)
confirm.setSize((40, 10))
confirm.setPos((-20, 5))
self.callMethod(('WindowManager', 'displayWindow'), login, 'DirectMapUpload.publishConfirm', confirm)
return
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.publishConfirm')
if yes == True:
if self.callFunction(('TmConnector', 'AddMap'), fileName):
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Successfully added file to map rotation!', login)
else:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Could not add file to map rotation.' +
'Is this a map file?',
login)
else:
pass
def cb_unpublishMap(self, entries, login, yes, fileName):
"""
\brief Remove a map from the map rotation
\param entries Should be empty
\param login The login of the calling player
\param yes Is this already confirmed
\param fileName The file to unpublishr
"""
if yes == None:
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.browse')
confirm = YesNoDialog('Do you really want to unpublish ' + os.path.basename(fileName))
confirm.setAnswerCallback(('DirectMapUpload', 'cb_unpublishMap'), fileName)
confirm.setSize((40, 10))
confirm.setPos((-20, 5))
self.callMethod(('WindowManager', 'displayWindow'), login, 'DirectMapUpload.unpublishConfirm', confirm)
return
self.callMethod(('WindowManager', 'closeWindow'), {}, login, 'DirectMapUpload.unpublishConfirm')
if yes == True:
if self.callFunction(('TmConnector', 'RemoveMap'), fileName):
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Successfully removed map from rotation',
login)
else:
self.callMethod(('TmConnector', 'ChatSendServerMessageToLogin'),
'Could not remove map from rotation!' +
'(Maybe it was not in the rotation?)',
login)
else:
pass