pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by Willow on Wed 1st Aug 17:06
download | new post

  1. #!//usr/bin/env python
  2. import os
  3. import os.path
  4. import random
  5.  
  6. def createTrackDict(sourcePath):
  7.         #Argument is the /Maps directory: path /trackmania2/UserData/Maps
  8.         blackListDirs = ('2 Challenge', 'Beta1Tracks', 'Campaigns', 'Downloaded', 'Shootmania', 'Less Common'
  9.                                          'maphist.txt', 'My Maps', 'MatchSettings', 'Misc', 'MX', 'temp')
  10.         #List items in maps directory, and collate a list of authors
  11.         files = os.listdir(sourcePath)
  12.         authors = [author for author in files if (author not in blackListDirs) and (os.path.isdir(os.path.join(sourcePath,author)))]
  13.         # create a dict of lists (authors)
  14.         # the key for each is the author name, the value is the list of tracks. 
  15.         trackDict = dict.fromkeys(authors)
  16.         for author in trackDict:
  17.                 authorDirectory = os.path.join(sourcePath, author)
  18.                 trackDict[author] = [map for map in os.listdir(authorDirectory) if map.endswith("Map.Gbx") and (os.path.isfile(os.path.join(authorDirectory,map)))]
  19.                 random.shuffle(trackDict[author])
  20.         return trackDict
  21.  
  22. def countTracks(trackDict, previousAuthor):
  23.         mostRemainingChoices = 0
  24.         #Iterate through each author's tracklist and count remaining maps
  25.         for author in trackDict:
  26.                 if author != previousAuthor:
  27.                         if len(trackDict[author]) > mostRemainingChoices:
  28.                                 mostRemainingChoices = len(trackDict[author])
  29.         #Can only happen if the only remaining choice is the previousAuthor
  30.         if mostRemainingChoices == 0:
  31.                 mostRemainingChoices = len(trackDict[previousAuthor])
  32.         return mostRemainingChoices
  33.  
  34. def createTrackList(trackDict):
  35.         stillStuffToDo = True
  36.         previousAuthor = None
  37.         trackList = []
  38.         while stillStuffToDo == True:
  39.                 mostRemainingChoices = countTracks(trackDict, previousAuthor)
  40.                 choices = []
  41.                 #Create a list of all authors with most choices and choose a random one
  42.                 for author in trackDict.keys():
  43.                         if len(trackDict[author]) == mostRemainingChoices:
  44.                                 choices.append(author)
  45.                 random.shuffle(choices)
  46.                 previousAuthor = choices.pop()
  47.                 currentTrack = trackDict[previousAuthor].pop()
  48.                 trackList.append((previousAuthor, currentTrack))
  49.  
  50.                 stillStuffToDo = False
  51.                 for author in trackDict.keys():
  52.                         if len(trackDict[author]) > 0: #there's still tracks to be added
  53.                                 stillStuffToDo = True
  54.                                 break
  55.         trackList = [os.path.join(track[0], track[1]) for track in trackList]
  56.         trackList.reverse()
  57.         return trackList
  58.  
  59. def createConfigFile(trackList,path=""):
  60.         f = open(path, 'w')
  61.         #Boring generic config file stuff
  62.         f.write('<?xml version="1.0" encoding="utf-8" ?>\n')
  63.         f.write('<playlist>\n')
  64.  
  65.         f.write('\t<gameinfos>\n')
  66.         f.write('\t\t<game_mode>1</game_mode>\n')
  67.         f.write('\t\t<chat_time>10000</chat_time>\n')
  68.         f.write('\t\t<finishtimeout>1</finishtimeout>\n')
  69.         f.write('\t\t<allwarmupduration>1</allwarmupduration>\n')
  70.         f.write('\t\t<disablerespawn>0</disablerespawn>\n')
  71.         f.write('\t\t<forceshowallopponents>0</forceshowallopponents>\n')
  72.         f.write('\t\t<script_name></script_name>\n')
  73.         f.write('\t\t<rounds_pointslimit>21</rounds_pointslimit>\n')
  74.         f.write('\t\t<rounds_usenewrules>0</rounds_usenewrules>\n')
  75.         f.write('\t\t<rounds_forcedlaps>0</rounds_forcedlaps>\n')
  76.         f.write('\t\t<rounds_pointslimitnewrules>5</rounds_pointslimitnewrules>\n')
  77.         f.write('\t\t<team_pointslimit>5</team_pointslimit>\n')
  78.         f.write('\t\t<team_maxpoints>6</team_maxpoints>\n')
  79.         f.write('\t\t<team_usenewrules>0</team_usenewrules>\n')
  80.         f.write('\t\t<team_pointslimitnewrules>5</team_pointslimitnewrules>\n')
  81.         f.write('\t\t<timeattack_limit>300000</timeattack_limit>\n')
  82.         f.write('\t\t<timeattack_synchstartperiod>0</timeattack_synchstartperiod>\n')
  83.         f.write('\t\t<laps_nblaps>5</laps_nblaps>\n')
  84.         f.write('\t\t<laps_timelimit>0</laps_timelimit>\n')
  85.         f.write('\t\t<cup_pointslimit>100</cup_pointslimit>\n')
  86.         f.write('\t\t<cup_roundsperchallenge>5</cup_roundsperchallenge>\n')
  87.         f.write('\t\t<cup_nbwinners>3</cup_nbwinners>\n')
  88.         f.write('\t\t<cup_warmupduration>2</cup_warmupduration>\n')
  89.         f.write('\t</gameinfos>\n')
  90.  
  91.         f.write('\t<hotseat>\n')
  92.         f.write('\t\t<game_mode>0</game_mode>\n')
  93.         f.write('\t\t<time_limit>300000</time_limit>\n')
  94.         f.write('\t\t<rounds_count>5</rounds_count>\n')
  95.         f.write('\t</hotseat>\n')
  96.  
  97.         f.write('\t<filter>\n')
  98.         f.write('\t\t<is_lan>1</is_lan>\n')
  99.         f.write('\t\t<is_internet>1</is_internet>\n')
  100.         f.write('\t\t<is_solo>0</is_solo>\n')
  101.         f.write('\t\t<is_hotseat>0</is_hotseat>\n')
  102.         f.write('\t\t<sort_index>1000</sort_index>\n')
  103.         f.write('\t\t<random_map_order>0</random_map_order>\n')
  104.         f.write('\t\t<force_default_gamemode>0</force_default_gamemode>\n')
  105.         f.write('\t</filter>\n')
  106.  
  107.         f.write('\t<startindex>0</startindex>\n')
  108.         #less boring but still dull part (insert maps)
  109.         for track in trackList:
  110.                 f.write('\t<map>\n')
  111.                 f.write('\t\t<file>' + track + '</file>\n')
  112.                 f.write('\t</map>\n')
  113.         f.write('</playlist>\n')
  114.         f.flush()
  115.         os.fsync(f.fileno())
  116.         f.close()
  117.  
  118. if __name__=="__main__":
  119.        
  120. #>>> trackDict = trackRandomiser.createTrackDict('C:/Temp/ScriptTest/Maps')
  121. #>>> trackList = trackRandomiser.createTrackList(trackDict)
  122. #>>> trackRandomiser.createConfigFile(trackList)
  123.  
  124.         trackDict = createTrackDict('UserData/Maps')
  125.         trackList = createTrackList(trackDict)
  126.         createConfigFile(trackList,'UserData/Maps/MatchSettings/racemode-random.txt')

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings