Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Version 1.1.1B
Browse files Browse the repository at this point in the history
Removed a unnecessary  Print.
added option to catch total online players, total players, total clans
from database when cant access to pro api.
it is largely untested now!
  • Loading branch information
TheHolyLoli committed Feb 5, 2016
1 parent 40ca471 commit 71a1871
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
68 changes: 58 additions & 10 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import configparser
# Core File is used for checking if server is UP or not
import core
import re
import re,datetime
from bs4 import BeautifulSoup

#Getting Admin Login Information
Expand Down Expand Up @@ -551,7 +551,29 @@ def get_ucs_pro_info():
return result['UCS']
except:
pass
def get_ucs_api():

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()
Expand All @@ -562,16 +584,43 @@ def get_ucs_api():
result={
'InMemoryClans':result[3],
'InMemoryPlayers':result[2],
'TotalConnectedClients':result[0],
}
return result
except:
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():
def get_ucs_detailed_info(cur):
out={}
try:
r=get_ucs_pro_info()
Expand Down Expand Up @@ -600,8 +649,7 @@ def get_ucs_detailed_info():
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()
print(r)
r=get_ucs_api(cur)
out['clientversion']='-'
out['Codename']='-'
out['databasetype']='-'
Expand All @@ -611,7 +659,7 @@ def get_ucs_detailed_info():
out['logginglevel']='-'
out['maintenance']='No'
out['maintenancetimeleft']='-'
out['onlineplayers']='-'
out['onlineplayers']=get_online_players_from_database(cur)[1]
out['serverport']=server['port']
out['serverversion']='-'
out['startingdarkelixir']='-'
Expand All @@ -622,8 +670,8 @@ def get_ucs_detailed_info():
out['startingexperience']='0'
out['startingshieldtime']='-'
out['startingtrophies']='-'
out['totalclans']='-'
out['totalplayers']='-'
out['totalconnectedclients']='-'
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
17 changes: 16 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def error2(page,result):
if not result and page != 1:
return True


#Home Index
@app.route('/', defaults={'page':1})
@app.route('/users/page/<int:page>')
Expand Down Expand Up @@ -71,6 +72,8 @@ def index(page):

cur.close()
con.close()


#Ban Users
@app.route('/bans', defaults={'page':1})
@app.route('/bans/page/<int:page>')
Expand Down Expand Up @@ -107,11 +110,15 @@ def bans(page):
error=str(e),
)


#Server Statistics
@app.route('/stats')
def stats():
con = core.connect_mdb()
con.commit()
cur = con.cursor()
try:
r=get_ucs_detailed_info()
r=get_ucs_detailed_info(cur)
return render_template('stats.html',
title='Stats',
info=r,
Expand All @@ -121,6 +128,8 @@ def stats():
title='Error',
error='Cannot Get Information:%s' % str(e),
)
cur.close()
con.close()


#Game Administrators
Expand Down Expand Up @@ -157,6 +166,7 @@ def admins(page):
error=str(e),
)


#Clans
@app.route('/clans', defaults={'page':1})#
@app.route('/clans/page/<int:page>')
Expand Down Expand Up @@ -191,6 +201,7 @@ def clans(page):
error=str(e),
)


#Player Profile
@app.route('/user/<userid>')
def show_user_profile(userid):
Expand Down Expand Up @@ -247,6 +258,7 @@ def show_user_profile(userid):
error=str(e),
)


#clan profile
@app.route('/clan/<clanid>')
def show_clan_profile(clanid):
Expand Down Expand Up @@ -277,11 +289,13 @@ def show_clan_profile(clanid):
error=str(e),
)


#Administrator Options
@app.route('/admin')
def admin_panel():
return render_template('admin.html',title='Admin Panel')


#Signin
@app.route('/signin', methods=['GET', 'POST'])
def signin():
Expand Down Expand Up @@ -324,6 +338,7 @@ def signout():
session.pop('username', None)
return redirect(url_for('index'))


#run the app.
if __name__ == '__main__':
app.debug = True
Expand Down

0 comments on commit 71a1871

Please sign in to comment.