This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
677 lines (603 loc) · 24.9 KB
/
api.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
""" Api File For App.py
this file does all background things like getting information from database and process them and make them readable
in this way,we can easily use them in main app and HTML rendering. """
""" Imports Libs used in this file
Json used for proccessing game objects,userinformation"""
import json
""" urllib used for connecting to UCS Pro API and catching Information.
Used from for easier access to function,if you want to make your own function,Take care of this. """
from urllib.request import urlopen
""" Config Parser can be used or : ` from backbeat import configparser`
Used for getting Config Variables like Database or ... """
import configparser
# Core File is used for checking if server is UP or not
import core
import re,datetime
from bs4 import BeautifulSoup
#Getting Admin Login Information
config = configparser.ConfigParser()
config.read('config.ini')
#Catching UCS Server Information From Config file
#ucs pro api for stats page
debug=config['ucs pro api']
debug_port=debug['port']
debug_key=debug['key']
debug_url=debug['url']
#server information
server=config['server']
#ucs normal api.used if cant access to pro api.this wont have all information!
normal_api=config['ucs api']
normal_api_port=normal_api['port']
normal_api_url=normal_api['url']
#troop Names/Need some Editing.TODO:Correct names that are different in game,like ProtoType or Gargoyle or ...
troop = [
'Barbarian','Archer','Goblin','Giant','Wall Breaker','Balloon',
'Wizard','Healer','Dragon','PEKKA','Minion','Hog Rider','Valkyride','Golem',
'Golem Secondary','witch','AirDefenceSeeker','Lava Hound',
'TrapSkeletonGround','GargoyleTrap','TrapSkeletonAir',
'Prototype_1','Prototype_2','Prototype_3',
]
#spell Names/Need some Editing.TODO:Correct names that are different in game,like HasteWave,BoostDefences or ...
spell = [
'LighningStorm', 'HealingWave', 'HasteWave',
'JumpSpell', 'xmas', 'FreezeSpell',
'xmas2013', 'Slow', 'BoostDefences',
'Poison', 'Earthquake', 'SpeedUp',
]
#Building Names/Need some Editing.TODO:Correct names that are different in game,like Communications mast or ...
building = [
'Army Camp','Town Hall','Elixir Collector','Elixir Storage',
'Gold Mine','Gold Storage','Barrack','Laboratory','Cannon',
'Archer Tower','Wall','Wizard Tower','Air Defense','Mortar',
'Clan Castle','Builder Nut','Communications mast',
'Goblin Town Hull','Goblin hut','Tesla Tower',
'Spell Factory','XBow','Barbarian King','Dark Elixir Collector',
'Dark Elixir Storage','Archer Queen','Dark Elixir Barrack',
'Inferno Tower','Air Sweeper','Dark Spell Factory'
]
""" Decoration Names/Need some Editing.
TODO:Correct names that are different in game,like GBR Flag(i guess it is Great Britain or ... """
deco = [
'Barbarian Statue','Torch','Goblin Pole','White Flag','Skull Flag',
'Flower box 1','Flower box 2','Windmeter','Down Arrow Flag',
'Up Arrow Flag','Skull Altar','USA Flag','Canada Flag','Italia Flag',
'Germany Flag','Finland Flag','Spain Flag','France Flag','GBR Flag',
'Brazil Flag','China Flag','Norway Flag','Thailand Flag','Thailand Flag',
'India Flag','Australia Flag','South Korea Flag','Japan Flag',
'Turkey Flag','Indonesia Flag','Netherlands Flag','Philippines Flag',
'Singapore Flag','PEKKA Statue','Russia Flag','Russia Flag','Greece Flag'
]
#obstacles/Need Lots of Editing.TODO:Correct names that are different in game.
obstacle = [
'Pine Tree', 'Large Stone', 'Small Stone 1', 'Small Stone 2',
'Square Bush', 'Square Tree', 'Tree Trunk 1', 'Tree Trunk 2', 'Mushrooms',
'TombStone', 'Fallen Tree', 'Small Stone 3', 'Small Stone 4', 'Square Tree 2',
'Stone Pillar 1', 'Large Stone', 'Sharp Stone 1', 'Sharp Stone 2', 'Sharp Stone 3',
'Sharp Stone 4', 'Sharp Stone 5', 'Xmas tree', 'Hero TombStone', 'DarkTombStone',
'Passable Stone 1', 'Passable Stone 2', 'Campfire', 'Campfire', 'Xmas tree2013',
'Xmas TombStone', 'Bonus Gembox', 'Halloween2014', 'Xmas tree2014', 'Xmas TombStone2014',
'Npc Plant 1', 'Npc Plant 2', 'Halloween2015'
]
#Country Names/Need some Editing.TODO:Check Names
country = [
'Europe', 'North America', 'South America', 'Asia', 'Australia', 'Africa',
'International', 'Afghanistan', 'Åland Islands', 'Albania ', 'Algeria', 'American Samoa',
'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua and Barbuda', 'Argentina',
'Armenia', 'Aruba', 'Ascension Island', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas',
'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda',
'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Bouvet Island', 'Brazil',
'British Indian Ocean Territory', 'British Virgin Islands', 'Brunei', 'Bulgaria',
'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Canary Islands',
'Cape Verde', 'Caribbean Netherlands', 'Cayman Islands', 'Central African Republic',
'Ceuta and Melilla', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands',
'Colombia', 'Comoros', 'Congo (DRC)', 'Congo (Republic)', 'Cook Islands', 'Costa Rica',
'Côte d’Ivoire', 'Croatia', 'Cuba', 'Curaçao', 'Cyprus', 'Czech Republic', 'Denmark',
'Diego Garcia', 'Djibouti', 'Dominica', 'Dominican Republic', 'Ecuador', 'Egypt',
'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islands',
'Faroe Islands', 'Fiji', 'Finland', 'France', 'French Guiana', 'French Polynesia',
'French Southern Territories', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana',
'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala',
'Guernsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Heard & McDonald Islands',
'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq',
'Ireland', 'Isle of Man', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan',
'Kazakhstan', 'Kenya', 'Kiribati', 'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia',
'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg',
'Macau', 'Macedonia (FYROM)', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali',
'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico',
'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Montserrat', 'Morocco', 'Mozambique',
'Myanmar (Burma)', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'New Caledonia', 'New Zealand',
'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'North Korea',
'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestine', 'Panama',
'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn Islands', 'Poland',
'Portugal', 'Puerto Rico', 'Qatar', 'Réunion', 'Romania', 'Russia', 'Rwanda', 'Saint Barthélemy',
'Saint Helena', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Martin', 'Saint Pierre and Miquelon',
'Samoa', 'San Marino', 'São Tomé and Pr�\xadncipe', 'Saudi Arabia', 'Senegal', 'Serbia', 'Seychelles',
'Sierra Leone', 'Singapore', 'Sint Maarten', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa',
'South Korea', 'South Sudan', 'Spain', 'Sri Lanka', 'St. Vincent & Grenadines', 'Sudan', 'Suriname',
'Svalbard and Jan Mayen', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania',
'Thailand', 'Timor-Leste', 'Togo', 'Tokelau', 'Tonga', 'Trinidad and Tobago', 'Tristan da Cunha', 'Tunisia',
'Turkey', 'Turkmenistan', 'Turks and Caicos Islands', 'Tuvalu', 'U.S. Outlying Islands', 'U.S. Virgin Islands',
'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan',
'Vanuatu', 'Vatican City', 'Venezuela', 'Vietnam', 'Wallis and Futuna', 'Western Sahara', 'Yemen',
'Zambia', 'Zimbabwe', '', '', '', '', ''
]
#Hero Names/COC version 8 or higher is not supported.
hero = ['Barbarian King','Archer Queen']
#Account Status.0=normal and 00=banned.
status = {0:'Normal',99:'Banned'}
#resources/need some editing.Correct names that are different in game.
resource = [
'Diamonds', 'Gold', 'Elixir', 'Dark Elixir', 'War Gold', 'War Elixir', 'War Dark Elixir'
]
#traps
trap = [
'Mine', 'Ejector', 'Super bomb', 'Halloween bomb', 'Slow bomb',
'Air Trap', 'Mega Air Trap', 'Santa Trap', 'Halloween skells'
]
#administrator Status/Need some Editing.TODO:Correct names that are Different with UCS defaults
admin_status={
0:'Normal',
1:'admin',
2:'Super admin',
3:'UnAssigned',
4:'Moderator',
5:'Owner'
}
#Clan role/Need Editing.TODO:Improve and Correct Ranks.
clan_role={ 0:'Normal Member',
1:'Normal Member',
2:'Leader'
}
#database Connection type.MySQL OR sqlite
database={
'ucsdbEntities':'MySQL',
'sqliteEntities':'Sqlite',
}
#Player Vs. Goblins Difficulty
pve={
'true':'Hard',
'false':'Easy',
}
#Online Status.Please Reffer to Core.py for Better Understanding.
online={
True:'Online',
False:'Offline',
}
#Maintenance Status.
maintenance={
'true':'Yes',
'false':'No',
}
# check if user exist
def does_exist_player(id,cur):
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result=cur.fetchone()
if result is None:#No match Founds so no user exists like that
return False#False is returned
else:#there is a result.so user Exists
return True#True is returned
# check if clan exists.Very similar to does_exist_player function
def does_exist_clan(id,cur):
query="SELECT * FROM `clan` WHERE `ClanId`=%s"%id
cur.execute(query)
result=cur.fetchone()
if result is None:#No match Founds so no clan exists like that
return False#False is returned
else:#there is a result.so clan Exists
return True#True is returned
# Gets player general informations
def get_player_info_general(n,cur):
global player_info#globals the variable,this maybe used instead of returning in future.
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%n
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
if result['alliance_id']!=0:#0=No Clan
clan=result['alliance_id']
else:#Player has No CLan
clan=None
player_info={
'name':result['avatar_name'],
'id':result2[0],
'status':status[result2[1]],
'privileges':admin_status[result2[2]],
'last_update':result2[3],
'townhall':result['townhall_level']+1,
'level':result['avatar_level'],
'experience':result['experience'],
'score':result['score'],
'gems':result['current_gems'],
'gold':result['resources'][0]['value'],
'elixir':result['resources'][1]['value'],
'darkelixir':result['resources'][2]['value'],
'clan':clan,
}
return player_info
# Gets player unit levels.Spell levels are seperated.
def get_player_units_level(id,cur):
global unit_level
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
unit_level={}
for i in result['unit_upgrade_levels']:
unit_level[troop[i['global_id']-4000000]]=i['value']+1#global_ids for units start from 4000000
return unit_level
# Gets player spell levels.unit levels are seperated.
def get_spell_level(id,cur):
global spell_level
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
spell_level={}
for i in result['spell_upgrade_levels']:
spell_level[spell[i['global_id']-26000000]]=i['value']+1#global_ids for spell start from 26000000
return spell_level
# Gets player Hero(s) Level.
def get_hero_level(id,cur):
global hero_level
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
hero_level={}
for i in result['hero_upgrade_levels']:
hero_level[hero[i['global_id']-28000000]]=i['value']+1#global_ids for units start from 28000000
return hero_level
# Gets player units,not their levels.
def get_player_units(id,cur):
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
units={}
for i in result['units']:
units[troop[i['global_id']-4000000]]=i['value']#global_ids for units start from 4000000
return units
# Gets player spells,not their levels.
def get_player_spells(id,cur):
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
spells={}
for i in result['spells']:
spells[spell[i['global_id']-26000000]]=i['value']#global_ids for spell start from 26000000
return spells
# Gets player buildings.we return just the number of them.
# TODO:Make a seperate building page or anything...
# TODO:Make a seperate Function for quantity...
def get_buildings(id,cur):
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result2[5]
result=json.loads(result)
building_t=[0]*len(building)
buildings={}
for i in result['buildings']:
ind=i['data']-1000000#global_ids for buildings start from 1000000
building_t[ind]+=1
for i in range(len(building)):
if building_t[i]!=0:
buildings[building[i]]=building_t[i]
return buildings
# Gets player obstacles.we return just the number of them.
# TODO:Make a seperate obstacle page or anything...
# TODO:Make a seperate Function for quantity...
def get_obstacles(id,cur):
try:
global obstacles
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[5]
result=json.loads(result)
obstacles_t=[0]*len(obstacle)
obstacles={}
for i in result['obstacles']:
ind=i['data']-8000000#global_ids for obstacles start from 8000000
obstacles_t[ind]+=1
for i in range(len(building)):
if obstacles_t[i]!=0:
obstacles[obstacle[i]]=obstacles_t[i]
return obstacles
except:
return []
# Gets player resources.
def get_resources(id,cur):
global resources
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[4]
result=json.loads(result)
resources={}
for i in result['resources']:
resources[resource[i['global_id']-3000000]]=i['value']#global_ids for resources start from 3000000
return resources
# Gets player Decorations
def get_decos(id,cur):
global decos
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[5]
result=json.loads(result)
decos={}
deco_t=[0]*len(deco)
for i in result['decos']:
ind=i['data']-18000000#global_ids for decos start from 18000000
deco_t[ind]+=1
for i in range(len(deco)):
if deco_t[i]!=0:
decos[deco[i]]=deco_t[i]
return decos
# Gets player Traps
def get_traps(id,cur):
global traps
query="SELECT * FROM `player` WHERE `PlayerId`=%s"%id
cur.execute(query)
result2=result=cur.fetchone()
result=result[5]
result=json.loads(result)
traps={}
trap_t=[0]*len(trap)
for i in result['traps']:
ind=i['data']-12000000#global_ids for traps start from 12000000
trap_t[ind]+=1
for i in range(len(trap)):
if trap_t[i]!=0:
traps[trap[i]]=trap_t[i]
return traps
# Gets a clan Info
def get_clan_info(id,cur):
if id!=None:
query="SELECT * FROM `clan` WHERE `ClanId`=%s"%id
cur.execute(query)
clan2=clan=cur.fetchone()
clan=clan[2]
clan=json.loads(clan)
members={}
messages=[0]*len(clan['chatMessages'])
for member in clan['members']:
#get_player_info_general(member,cur)
inf=get_player_info_general(member['avatar_id'],cur)
members[inf['name']]={
'role':clan_role[member['role']],
'id':inf['id'],
}
for i in range(len(clan['chatMessages'])):
message=clan['chatMessages'][i]
messages[i]={
'sender_name':message['sender_name'],
'sender_role':clan_role[message['sender_role']],
'sender_id':message['sender_id'],
'message':message['message'],
'pos':i,
}
if clan['alliance_origin']-32000000<0:#Clan origin id starts from 32000000
clan['alliance_origin']+=32000000
clan_info={
'name': clan['alliance_name'],
'id':clan['alliance_id'],
'score':clan['score'],
'needed_score':clan['required_score'],
'experience':clan['alliance_experience'],
'level':clan['alliance_level'],
'description':clan['description'],
'members':members,
'members_len':len(clan['members']),
'messages':messages,
'messages_len':len(clan['chatMessages']),
'location':country[clan['alliance_origin']-32000000],
}
return clan_info
# Gets General information for all players.Used in Index Page.
def get_info_all(cur,perpage,startat):
global out
query="SELECT * FROM `player`"
cur.execute(query)
total=cur.rowcount
result=cur.fetchall()
query="SELECT * FROM `player` ORDER BY `AccountPrivileges` DESC LIMIT %s,%s" %(startat,perpage)
cur.execute(query)
result=cur.fetchall()
len=cur.rowcount
out=['']*len
g=0
for i in result:
out[g]=get_player_info_general(result[g][0],cur)
out[g]['pos']=g
g+=1
return [out,total]
# Gets information for all banned players.Used in Bans Page.
def get_info_bans(cur,perpage,startat):
query="SELECT * FROM `player` WHERE `AccountStatus` > 0"
cur.execute(query)
total=cur.rowcount
result=cur.fetchall()
query="SELECT * FROM `player` WHERE `AccountStatus` > 0 LIMIT %s,%s" %(startat,perpage)
cur.execute(query)
result=cur.fetchall()
len=cur.rowcount
out=['']*len
g=0
for i in result:
get_player_info_general(result[g][0],cur)
out[g]=player_info
out[g]['pos']=g+1
g+=1
return [out,total]
# Gets information for all admins.Used in admins Page.
def get_info_admins(cur,perpage,startat):
query='SELECT * FROM `player` WHERE `AccountPrivileges`> 0'
cur.execute(query)
total=cur.rowcount
result=cur.fetchall()
query="SELECT * FROM `player` WHERE `AccountPrivileges`> 0 ORDER BY `AccountPrivileges` DESC LIMIT %s,%s" %(startat,perpage)
cur.execute(query)
result=cur.fetchall()
len=cur.rowcount
out=['']*len
g=0
for i in result:
out[g]=get_player_info_general(result[g][0],cur)
out[g]['pos']=g+1
g+=1
return [out,total]
# Gets information for all Clans.Used in Clans index page.
def get_info_clan_all(cur,perpage,startat):
query="SELECT * FROM `clan`"
cur.execute(query)
total=cur.rowcount
result1=cur.fetchall()
query="SELECT * FROM `clan` LIMIT %s,%s" %(startat,perpage)
cur.execute(query)
result2=cur.fetchall()
result=len(result2)
out=['']*result
for i in range(result):
out[i]=get_clan_info(result2[i][0],cur)
out[i]['pos']=i+1
return [out,total]
# Creates a connection to UCS Pro Debug page and catches information.
def get_ucs_pro_info():
try:
url='http://'+debug_url+':'+debug_port+'/'+debug_key
result=urlopen(url).read()
result=result.decode('utf-8')
result=json.loads(result)
return result['UCS']
except:
pass
def format_date(time):
year=str(time.year)
month=str(time.month)
day=str(time.day)
hour=str(time.hour)
minute=str(time.minute)
second=str(time.second)
if len(day)==1:
day='0'+day
if len(month)==1:
month='0'+month
if len(hour)==1:
hour='0'+hour
if len(minute)==1:
minute='0'+minute
if len(second)==1:
second='0'+second
time='"'+year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second+'"'
return time
def get_ucs_api(cur):
try:
url='http://'+normal_api_url+':'+normal_api_port+'/Debug'
result=urlopen(url).read()
result=result.decode('utf-8')
soup = BeautifulSoup(result, "html.parser")
all_text = ''.join(soup.findAll(text=True))
result=re.findall('\d+', all_text )
result={
'InMemoryClans':result[3],
'InMemoryPlayers':result[2],
'TotalConnectedClients':result[0],
}
return result
except Exception as e:
result={
'InMemoryClans':'-',
'InMemoryPlayers':'-',
'TotalConnectedClients':'-'
}
return result
def get_online_players_from_database(cur):
time_range=format_date(datetime.datetime.utcnow() - datetime.timedelta(minutes=3))
now=format_date(datetime.datetime.utcnow())
query="SELECT * FROM `player` WHERE `LastUpdateTime` BETWEEN "+time_range+ " AND "+now
cur.execute(query)
result=cur.fetchall()
total=len(result)
return [result,total]
def get_total_database_players(cur):
query="SELECT * FROM `player`"
cur.execute(query)
result=cur.fetchall()
total=len(result)
return [result,total]
def get_total_database_clans(cur):
query="SELECT * FROM `clan`"
cur.execute(query)
result=cur.fetchall()
total=len(result)
return [result,total]
# Formates the catches information from function above.
def get_ucs_detailed_info(cur):
out={}
try:
r=get_ucs_pro_info()
out['clientversion']=r['ClientVersion']
out['Codename']=r['Codename']
out['databasetype']=database[r['DatabaseType']]
out['pve']=pve[r['ExpertPVE'].lower()]
out['inmemoryclans']=r['InMemoryClans']
out['inmemoryplayers']=r['InMemoryPlayers']
out['logginglevel']=r['LoggingLevel']
out['maintenance']=maintenance[r['Maintenance'].lower()]
out['maintenancetimeleft']=r['MaintenanceTimeLeft']
out['onlineplayers']=r['OnlinePlayers']
out['serverport']=r['ServerPort']
out['serverversion']=r['ServerVersion']
out['startingdarkelixir']=r['StartingDarkElixir']
out['startingelixir']=r['StartingElixir']
out['startinggold']=r['StartingGold']
out['startinggems']=r['StartingGems']
out['startinglevel']=r['StartingLevel']
out['startingexperience']=r['StartingExperience']
out['startingshieldtime']=r['StartingShieldTime']
out['startingtrophies']=r['StartingTrophies']
out['totalclans']=r['TotalClans']
out['totalplayers']=r['TotalPlayer']
out['totalconnectedclients']=r['TotalConnectedClients']
out['serveronline']=online[core.isup(config['server']['host'],config['server']['port'])]
except:#this way we return -.TODO:Use better algorithm and method for returning offline server status
r=get_ucs_api(cur)
out['clientversion']='-'
out['Codename']='-'
out['databasetype']='-'
out['pve']='-'
out['inmemoryclans']=r['InMemoryClans']
out['inmemoryplayers']=r['InMemoryPlayers']
out['logginglevel']='-'
out['maintenance']='No'
out['maintenancetimeleft']='-'
out['onlineplayers']=get_online_players_from_database(cur)[1]
out['serverport']=server['port']
out['serverversion']='-'
out['startingdarkelixir']='-'
out['startingelixir']='-'
out['startinggold']='-'
out['startinggems']='-'
out['startinglevel']='0'
out['startingexperience']='0'
out['startingshieldtime']='-'
out['startingtrophies']='-'
out['totalclans']=get_total_database_clans(cur)[1]
out['totalplayers']=get_total_database_players(cur)[1]
out['totalconnectedclients']=r['TotalConnectedClients']
out['serveronline']=online[core.isup(config['server']['host'],config['server']['port'])]
return out