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 Anonymous on Tue 31st Jul 19:09
download | new post

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

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