import es, os, os.path, random, pickle, popuplib, ojiiupdater, playerlib, gamethread, sys, traceback info = es.AddonInfo() info.name = "Guess The Song" info.version = "1.0.1" info.url = "http://addons.eventscripts.com/addons/view/guessthesong" info.author = "Ojii" info.basename = "guessthesong" updateurl = "http://www.gkclan.com/members/uploads/01112@guessthesong.update" path = es.getAddonPath('') path = path.replace('\\','/') cfgfile = path.replace('addons/eventscripts/','cfg/gts_config.cfg') players = {} score = {} mp3dir = 'guessthesong/' path = es.getAddonPath('') path = path.replace('\\','/') sounddir = path.replace('addons/eventscripts','sound/') active = 0 def load(): global gts_rank, gts ver = es.ServerVar('guess_the_song',info.version,'GTS version') ver.makepublic() es.dbgmsg(1, '[GTS_DBG] loaded') load_score() es.addons.registerSayFilter(gts_filter) es.regsaycmd('rank','guessthesong/rank') es.regsaycmd('top10','guessthesong/top10') es.regclientcmd('gts','guessthesong/gts') es.regcmd('gts_addmp3','guessthesong/addmp3') es.regcmd('gts_getsongs','guessthesong/get_songs') es.regcmd('gts_update','guessthesong/update') es.regcmd('gts_checkversion','guessthesong/checkversion') es.regclientcmd('togglegts','guessthesong/togglegts') es.regcmd('togglegts','guessthesong/stogglegts') gts = 1 get_players() get_songs() def get_players(): global players, score playerlist = playerlib.getUseridList('#human') for p in playerlist: s = es.getplayersteamid(p) if score.has_key(s): players[s] = score[s] else: players[s] = (1, 0, es.getplayername(p)) def map_start(): get_songs() def unload(): es.dbgmsg(1, '[GTS_DBG] unloaded') es.addons.unregisterSayFilter(gts_filter) save_score() es.unregsaycmd('rank') es.unregsaycmd('top10') es.unregclientcmd('gts') es.unregclientcmd('togglegts') def gts_filter(userid, message, teamonly): global guess, active text = message.strip('"') if text == '!stop': stop(userid) elif active == 1: try: text.index('-') check(userid, text) return (0, None, None) except: for line in traceback.format_exception(*sys.exc_info()): es.dbgmsg(0, line[:-1]) es.tell(userid, '[GTS] To guess use the following syntax: title - interpret or interpret - title') return (0, None, None) else: return (userid, text, teamonly) def gts(): global active, gts, players, mp3dir, guessthis, currentsong es.msg('[GTS] Guess this song!!!') read_configs() if gts == 1: if active == 1: es.dbgmsg(0,'[GTS_DBG] Already active!') else: random_song() currentsong = mp3dir + guessthis[2] playerlist = playerlib.getUseridList('#human') for u in playerlist: s = es.getplayersteamid(u) if players.has_key(s): if players[s][0] == 1: es.playsound(u, currentsong, 1.0) else: es.msg('[GTS] Chat temporarily disabled because of GuessTheSong') else: es.msg('[GTS] Chat temporarily disabled because of GuessTheSong') active = 1 gamethread.delayed(30, deac) def check(userid, guess): global players, active, score, guessthis sid = es.getplayersteamid(userid) guess.index('-') split = guess.find('-') a = guess[:split] b = guess[split + 1:] guesslist = [a.replace(' ','').lower(), b.replace(' ','').lower()] if guessthis[0] in guesslist: if guessthis[1] in guesslist: es.tell(userid, 'Correct!') name = es.getplayername(userid) es.msg('[GTS] ' + name + ' guessed right! The song is: ' + guessthis[3]) players[sid] = (players[sid][0], players[sid][1] + 1, name) rank = get_rank(userid) rank = str(rank + 1) if rank.endswith('1'): suffix = 'st' elif rank.endswith('2'): suffix = 'nd' elif rank.endswith('3'): suffix = 'rd' else: suffix = 'th' if players[sid][1] > 1: point = 'points' else: point = 'point' es.msg('[GTS] ' + name + ' has now ' + str(players[sid][1]) + ' ' + point + ' and is ranked ' + rank + suffix) active = 0 else: es.tell(userid, 'Wrong!') def get_rank(u): return map(lambda x: players[x][2], sorted(players, key=lambda x: players[x][1], reverse=True)).index(es.getplayername(u)) def get_songs(): global songslist try: database = open(es.getAddonPath('guessthesong') + '/gts_database.pkl','rb') songslist = pickle.load(database) database.close() for song in songslist: es.stringtable('downloadables', 'sound/guessthesong/' + song[2]) es.dbgmsg(0,'[GTS] Following songs are in the GTS database:') for song in songslist: es.dbgmsg(0,'[GTS] ' + song[3]) except: for line in traceback.format_exception(*sys.exc_info()): es.dbgmsg(0, line[:-1]) es.dbgmsg(0,'[GTS_ERROR] Could not get gts_databse, add mp3s using gts_addmp3...') def addmp3(): if es.getargc() == 4: if not os.path.lexists(es.getAddonPath('guessthesong') + '/gts_database.pkl'): songslist = [] database = open(es.getAddonPath('guessthesong') + '/gts_database.pkl','wb') pickle.dump(songslist,database) database.close() database = open(es.getAddonPath('guessthesong') + '/gts_database.pkl','rb') songslist = pickle.load(database) database.close() nr = len(songslist) + 1 file = es.getargv(1) path = es.getAddonPath('') path = path.replace('\\','/') path = path.replace('addons/eventscripts','sound/guessthesong') os.rename(file, path + str(nr) + '.mp3') title = es.getargv(2).replace(' ','').lower() interpret = es.getargv(3).replace(' ','').lower() realname = "'" + es.getargv(2) + "' by '" + es.getargv(3) + "'" songslist.append([title,interpret,str(nr) + '.mp3', realname]) database = open(es.getAddonPath('guessthesong') + '/gts_database.pkl','wb') pickle.dump(songslist,database) database.close() es.dbgmsg(0,'[GTS] Added ' + file + ' to the guessthesong database') else: es.dbgmsg(0,'[GTS_ERROR] Use following syntax: [rcon] gts_addmp3 <interpret>') es.dbgmsg(0,'[GTS_ERROR] For example: rcon gts_addmp3 "C:/guessthesong/My Interpret - Title04.mp3" "My Interpret" "Title04"') get_songs() def random_song(): global guessthis guessthis = songslist[random.randrange(0, len(songslist))] def rank(): es.dbgmsg(1, '[GTS_DBG] rank') u = es.getcmduserid() rank = get_rank(u) rank = str(rank + 1) if rank.endswith('1'): suffix = 'st' elif rank.endswith('2'): suffix = 'nd' elif rank.endswith('3'): suffix = 'rd' else: suffix = 'th' es.tell(u, 'You are ranked ' + rank + suffix) def top10(): es.dbgmsg(1, '[GTS_DBG] top10') u = es.getcmduserid() s = es.getplayersteamid(u) list = [] dict = players for p in score: if not dict.has_key(p): dict[p] = score[p] x = 0 for d in dict: if x < 10: rank = get_ranktop10(d,dict) rank = str(rank + 1) if rank.endswith('1'): suffix = 'st' elif rank.endswith('2'): suffix = 'nd' elif rank.endswith('3'): suffix = 'rd' else: suffix = 'th' if players[d][1] > 1: point = ' points' else: point = ' point' list.append([rank + suffix, dict[d][2],str(dict[d][1]) + point]) else: break top10 = popuplib.easymenu('top10', '_popup_choice', nothing) top10.settitle('Guess the song top 10:') for p in list: top10.addoption('', p[0] + ': ' + p[1] + ' with ' + p[2]) top10.send(u) def get_ranktop10(s, dict): return map(lambda x: players[x][2], sorted(players, key=lambda x: players[x][1], reverse=True)).index(dict[s][2]) def save_score(): es.dbgmsg(1, '[GTS_DBG] Saving score...') scorefile = open(es.getAddonPath('guessthesong') + '/score.pkl', 'wb') pickle.dump(score,scorefile) scorefile.close() es.dbgmsg(1, '[GTS_DBG] score saved') def load_score(): global score es.dbgmsg(1, '[GTS_DBG] Loading score') if os.path.lexists(es.getAddonPath('guessthesong') + '/score.pkl'): scorefile = open(es.getAddonPath('guessthesong') + '/score.pkl', 'rb') score = pickle.load(scorefile) scorefile.close() es.dbgmsg(1, '[GTS_DBG] score loaded') else: score = {} es.dbgmsg(1, '[GTS_DBG] No score found, starting empty one') def player_activate(event_var): global players, score es.dbgmsg(1, '[GTS_DBG] Player activated') u = event_var['es_steamid'] if score.has_key(u): es.dbgmsg(1, '[GTS_DBG] Player is in score database') players[u] = score[u] es.dbgmsg(1, '[GTS_DBG] Player: ' + str(players[u])) else: es.dbgmsg(1, '[GTS_DBG] Player is new!') players[u] = (1, 0, es.getplayername(event_var['userid'])) es.dbgmsg(1, '[GTS_DBG] Player: ' + str(players[u])) es.dbgmsg(1, '[GTS_DBG] Players: ' + str(players)) if gst == 1: es.tell(event_var['userid'], 'This server uses Guess The Song! ' + info.version) es.tell(event_var['userid'], 'Use gts in the console to start it') es.tell(event_var['userid'], 'Guess the song with the following syntax: <title> - <interpret>') es.tell(event_var['userid'], 'Use rank or top10 in chat to get information about the rankings') es.tell(event_var['userid'], 'Use togglegts in console to turn gts on or off') def player_disconnect(event_var): global players, score es.dbgmsg(1, '[GTS_DBG] Player disconnecting') u = event_var['es_steamid'] if players.has_key(u): score[u] = players[u] es.dbgmsg(1, '[GTS_DBG] Player written to score db') es.dbgmsg(1, '[GTS_DBG] Player score: ' + str(score[u])) del players[u] es.dbgmsg(1, '[GTS_DBG] Player deleted from players') def stop(u): global currentsong try: es.stopsound(u, currentsong) except: es.dbgmsg(1, '[GTS_DBG] User has no sound playing') def togglegts(): global players u = es.getcmduserid s = es.getplayersteamid(u) if players[s][0] == 1: players[s] = (0, players[s][1], players[s][2]) es.tell(u, 'You have disabled GTS for yourself') else: players[s] = (1, players[s][1], players[s][2]) es.tell(u, 'You have enabled GTS for yourself') def stogglegts(): global gts read_configs() if gts == 1: gts = 0 es.dbgmsg(0, '[GTS] has been disabled') else: gts = 1 write_configs() es.dbgmsg(0, '[GTS] has been enabled') def deac(): global active active = 0 def nothing(a,b,c): pass def update(): ojiiupdater.update(info, updateurl) def checkversion(): check = ojiiupdater.checkversion(info, updateurl) if check.newversion == 1: es.dbgmsg(0, '[GTS] New version available: ' + check.latest_version) else: es.dbgmsg(0, "[GTS] You're version (" + check.latest_version + ') is up to date') def es_map_start(event_var): for song in songslist: es.stringtable('downloadables', 'sound/guessthesong/' + song[2]) es.dbgmsg(0,'[GTS] Following songs are in the GTS database:') for song in songslist: es.dbgmsg(0,'[GTS] ' + song[3]) playerlist = playerlib.getPlayerList('#human') if len(playerlist) == 0: check = ojiiupdater.checkversion(info, updateurl) if check.newversion == 1: ojiiupdater.update(info, updateurl) def read_configs(): global gts if not os.path.lexists(cfgfile): tempfile = open(cfgfile, 'w') tempfile.write('enable 1') tempfile.close() tempfile = open(cfgfile, 'r') templines = tempfile.read().splitlines() tempfile.close() gts = 0 for line in templines: if line.startswith('enable'): split = line.find(' ') + 1 if int(line[split:]) in [1,0]: gts = int(line[split:]) def write_configs(): global gts tempfile = open(cfgfile, 'r') templines = tempfile.read().splitlines() tempfile.close() writestring = '' for line in templines: if not line.startswith('enable'): writestring = writestring + line + '\n' else: writestring = writestring + 'enable ' + str(gts) + '\n' tempfile = open(cfgfile, 'w') tempfile.write(writestring[:-1]) tempfile.close()