Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.51 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import pymysql.cursors
  4. import re
  5. from __main__ import user_allowed, send_cmd_help
  6. import os
  7.  
  8. def conconnect():
  9.     """
  10.    Connect to the database
  11.    """
  12.     try:
  13.         self.connection.ping(True)
  14.     except:
  15.         connection = pymysql.connect(host='amethyst.relentlesshosting.com.au',
  16.         user='',
  17.         password='',
  18.         db='',
  19.         charset='utf8mb4',
  20.         cursorclass=pymysql.cursors.DictCursor)
  21.         return connection
  22.  
  23. def convertTime(wrtime):    
  24.     """
  25.    Float -> String
  26.    Takes float server time and changes it to min:sec.milliseconds
  27.    """
  28.  
  29.     readtime = int(wrtime)
  30.     timeMin = readtime//60
  31.     timeSec = readtime % 60
  32.     timeMilli = int((wrtime*1000)%1000)
  33.  
  34.     if (timeMin<10):
  35.         strMin = "0" + str(timeMin)
  36.     else:
  37.         strMin = str(timeMin)
  38.  
  39.     if (timeSec<10):
  40.         strSec = "0" + str(timeSec)
  41.     else:
  42.         strSec = str(timeSec)
  43.     return strMin + ":" + strSec + "." + str(timeMilli)
  44.  
  45. def checkBool(cbool):
  46.     if cbool:
  47.         print("test")
  48.  
  49. class Surftimer:
  50.     """Surftimer commands"""
  51.    
  52.     #inuse = False
  53.  
  54.     def __init__(self, bot):
  55.         self.bot = bot
  56.        
  57.     @commands.command(pass_context=True)
  58.     async def wr(self, ctx, text : str):
  59.         """<mapname> - Displays wr on map"""
  60.         connection = conconnect();
  61.         if text == ():
  62.                 await send_cmd_help(ctx)
  63.                 return
  64.         server = ctx.message.server
  65.         to_replace = ctx.message.content.find(text[0])
  66.         text = ctx.message.content[to_replace:]
  67.         with connection.cursor() as cursor:
  68.             sqlwr = "SELECT db2.runtimepro, db1.name, db1.steamid FROM ck_playertimes as db2 INNER JOIN ck_playerrank as db1 on db1.steamid = db2.steamid WHERE db2.mapname LIKE %s AND db2.runtimepro  > -1.0 ORDER BY db2.runtimepro ASC LIMIT 1"
  69.             cursor.execute(sqlwr, (text))
  70.             sqlwrresult = cursor.fetchone()
  71.             playername = sqlwrresult.get('name')
  72.             wrtime = sqlwrresult.get('runtimepro')
  73.  
  74.             await self.bot.say("SurfTimer | **%s** holds the record with time: **%s** on **%s**" % (playername, convertTime(wrtime), text))
  75.  
  76.     @commands.command()
  77.     async def top(self):
  78.         """ - Shows top 10 players on the server"""
  79.         #inuse = True
  80.         #if inuse = True:
  81.         connection = conconnect();
  82.         with connection.cursor() as cursor:
  83.             sqltop = "SELECT * FROM `ck_playerrank` ORDER BY `points` DESC LIMIT 0, 10"
  84.             cursor.execute(sqltop)
  85.  
  86.             sqltopresult = cursor.fetchone()
  87.             await self.bot.say("SurfTimer | **Top 10 Players:**")
  88.             playerrank = 1
  89.  
  90.             while sqltopresult is not None:
  91.                 checkBool(inuse)
  92.                 playertopname = sqltopresult.get('name')
  93.                 points = sqltopresult.get('points')
  94.                 finishedmaps = sqltopresult.get('finishedmapspro')
  95.                 country = sqltopresult.get('country')
  96.  
  97.                 await self.bot.say("Rank: **%i** Name: **%s** Points: **%i** Finished Maps: **%i** Country: **%s**" % (playerrank, playertopname, points, finishedmaps, country))
  98.                 playerrank += 1
  99.                 sqltopresult = cursor.fetchone()
  100.         #inuse = False
  101.         #else:
  102.            # await self.bot.say("Surftimer | Command already in use.")
  103.  
  104.     @commands.command(pass_context=True)
  105.     async def mtop(self, ctx, text : str):
  106.         """<mapname> - Shows top 10 players on map"""
  107.         connection = conconnect();
  108.         if text == ():
  109.                 await send_cmd_help(ctx)
  110.                 return
  111.         server = ctx.message.server
  112.         to_replace = ctx.message.content.find(text[0])
  113.         text = ctx.message.content[to_replace:]
  114.         with connection.cursor() as cursor:
  115.             sqlmtop = "SELECT * FROM `ck_playertimes` WHERE `mapname` = %s ORDER BY runtimepro ASC LIMIT 0, 10"
  116.             cursor.execute(sqlmtop, (text))
  117.  
  118.             sqlmtopresult = cursor.fetchone()
  119.             await self.bot.say("SurfTimer | Top 10 Players on map: **%s**" % (text))
  120.             playerrank = 1
  121.  
  122.             while sqlmtopresult is not None:
  123.                 playername = sqlmtopresult.get('name')
  124.                 maptime = sqlmtopresult.get('runtimepro')
  125.  
  126.                 await self.bot.say("Rank: **%i** Name: **%s** Time: **%s**" % (playerrank, playername, convertTime(maptime)))
  127.                 playerrank += 1
  128.                 sqlmtopresult = cursor.fetchone()
  129.  
  130.  
  131.     @commands.command(pass_context=True)
  132.     async def m(self, ctx, text : str):
  133.         """<mapname> - shows map info for map"""
  134.         connection = conconnect();
  135.         if text == ():
  136.                 await send_cmd_help(ctx)
  137.                 return
  138.         server = ctx.message.server
  139.         to_replace = ctx.message.content.find(text[0])
  140.         text = ctx.message.content[to_replace:]
  141.         with connection.cursor() as cursor:
  142.             sqlmtier = "SELECT `tier` FROM `ck_maptier` WHERE `mapname` = %s"
  143.             cursor.execute(sqlmtier, (text))
  144.  
  145.             sqlmtierresult = cursor.fetchone()
  146.             maptier = sqlmtierresult.get('tier')
  147.  
  148.             sqlmtype = "SELECT * FROM `ck_zones` WHERE `mapname` = %s AND `zonetype` = '3'"
  149.             cursor.execute(sqlmtype, (text))
  150.  
  151.             #sqlmtyperesult = cursor.fetchone()
  152.             maptyperesult = cursor.rowcount
  153.             maptype = ""
  154.  
  155.             sqlbcount = "SELECT COUNT(DISTINCT zonegroup) as bcount FROM `ck_zones` WHERE `mapname` = %s AND `zonegroup` >= 1"
  156.             cursor.execute(sqlbcount, (text))
  157.  
  158.             sqlbcountresult = cursor.fetchone()
  159.             bcount = sqlbcountresult.get('bcount')
  160.  
  161.  
  162.             if maptyperesult:
  163.                 maptype = 'Staged'
  164.                 sqlstagecount = "SELECT COUNT(zonetype) as stagecount FROM `ck_zones` WHERE `mapname` = %s AND `zonegroup` = 0 AND `zonetype` = 3"
  165.                 cursor.execute(sqlstagecount, (text))
  166.  
  167.                 sqlstagecountresult = cursor.fetchone()
  168.                 stagecount = sqlstagecountresult.get('stagecount') + 1
  169.  
  170.                 await self.bot.say("SurfTimer | Map: **%s** - Type: **%s** - Tier: **%s** - Stages: **%i** - Bonuses: **%i**" % (text, maptype, maptier, stagecount, bcount))
  171.             else:
  172.                 maptype = 'Linear'
  173.                 await self.bot.say("SurfTimer | Map: **%s** - Type: **%s** - Tier: **%s** - Bonuses: %i" % (text, maptype, maptier, bcount))
  174.  
  175.  
  176.  
  177. def setup(bot):
  178.     bot.add_cog(Surftimer(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement