Need Some Help With Python.
Posted: 2010.01.04 (01:06)
OK, so if you haven't read the thread I posted in Highscores I am trying to port over runs from the metanet servers to the nreality server, but it a very tedious process and I am wondering if I could use Nhigh to my advantage. The hard part of importing these runs is getting the demo data for each run. Nhigh gathers the demo data from any run in the top-20 you ask for, however it then changes the demo data in to a text format for ease of reading, but I am wondering if I could edit the nhigh python files so that it does not do this last step, and instead will only display the demo data.
Here are the section of python code that are applicable for the demo analysis function.[/spoiler]
[/spoiler]
The first set of code I got from the Nhigh.pyw file which you run to use nhigh. The second set I got from the nhighlib.py file.
Now I am almost complete illiterate when it comes to python, so any help would be great. Again I am wondering if I could edit one of these files, or if someone could tell me another quick way like running something in python shell, so that I could get the demo data for these runs quickly.
Also you can download Nhigh here.
Here are the section of python code that are applicable for the demo analysis function.
Code 1
Code: Select all
def demoAnalysis(self):
SEL_TOP20, SEL_DEMO = 'top20', 'demo'
frames = [
AskDlgSelectionSubFrame(
[
AskDlgHorizSubFrame([
IntField('episode','Episode',minValue=0,maxValue=99, width=3),
IntField('level','Level',minValue=0,maxValue=4, width=3),
IntField('rank','Rank',minValue=0,maxValue=19, width=3),
]),
MultiLineField('demo','Demo data'),
],
['Online Top-20 Score', 'Demo Data'],
[SEL_TOP20, SEL_DEMO]),
]
dlg = AskDialog(self.root, 'Demo Analysis',
'Description: Analyzes a demo and shows\nwhich keys were pressed for each frame',
frames)
res = dlg.run()
if not res:
return
if res.selection==SEL_TOP20:
player, score, demo = downloadReplay(res.episode, res.level, res.rank)
title = 'Demo analysis for %s (%s rank) - [%s, %s]' % \
(self.dispLevel(res.episode, res.level),
getOrdinal(res.rank), self.dispPlayer(player),
self.dispTime(score*FRAME_TIME))
elif res.selection==SEL_DEMO:
demo = res.demo
title = 'Demo analysis'
else:
return
arr = parseAndCompactReplay(demo)
self.setText(title+'\n\n')
for frame, key, num in arr:
self.addLine('%03d. %-7s - %d frame%s' %
(frame, getReplayKeyName(key), num, getPlural(num)))
Code 2
Code: Select all
##### replay download/analysis #####
def getReplayKey(ep, lvl, rank):
if ep<0 or ep>=NUM_EPISODES or lvl<0 or lvl>4 or rank<0 or rank>19:
return None
url = 'http://www.harveycartel.org/metanet/n/data13/get_topscores_query_jg.php'
postdata = 'episode%5Fnumber=' + str(ep)
allscores = openURL(url, postdata).replace('\r','')
searchstr = r'&%dpkey%d=(\d+)' % (lvl, rank)
m=re.search(searchstr, allscores)
if not m:
return None
return int(m.group(1))
def downloadReplayByPKey(pkey):
url = 'http://www.harveycartel.org/metanet/n/data13/get_lv_demo.php'
postdata = 'pk='+str(pkey)
return openURL(url, postdata).replace('\r','')
def downloadReplay(ep, lvl, rank):
'Returns tuple: (player,score,demo)'
try:
pkey = getReplayKey(ep, lvl, rank)
if pkey is None:
raise NHighError('Unable to download replay - replay key not found')
alldata = downloadReplayByPKey(pkey)
m_name=re.search('&name=([^&]+)',alldata)
m_score=re.search('&score=([^&]+)',alldata)
m_demo=re.search('&demo=([^&]+)',alldata)
try:
player = m_name.group(1)
demo = m_demo.group(1)
score = int(m_score.group(1))
except (ValueError, AttributeError):
raise NHighError('Unable to download replay - received invalid data')
return (player, score, demo)
except IOError:
raise NHighError('Error downloading replay data')
def getReplayKeyName(key):
ret = ''
if key&1: ret += 'L'
if key&2: ret += 'R'
if key&4: ret += 'J'
if key==0: ret = 'Nothing'
return ret
def parseReplay(data):
'''Parse the replay and return array of numbers (1 per frame).
The numbers can be passed to getReplayKeyName.'''
if data and data[0]=='$':
#skip level data
m = re.match(r'^\$[^#]*#[^#]*#[^#]*#[^#]*#([^#]*)',data)
if not m:
raise NHighError('Invalid demo data')
data = m.group(1)
m=re.match(r'^(\d+):((\d+\|)*\d+)$', data)
if not m:
raise NHighError('Invalid demo data')
frames = int(m.group(1))
keystring = m.group(2)
nums = [int(x) for x in re.findall(r'\d+',keystring)]
ret = []
for num in nums:
numbits = min(frames, 7)
for bit in xrange(numbits):
ret.append(num & 0xF)
num >>= 4
frames -= numbits
if frames==0 and num!=0:
#framecount is lower than number of provided frames
return ret
return ret
def parseAndCompactReplay(data):
'''Parse the replay, and then find sequences of unchanged keys.
Return array of tuples (frame_num, key_num, # of occurences)
The key numbers can be passed to getReplayKeyName.'''
ret = []
keys=parseReplay(data)
if not keys:
return ret
keys.append(-1)
lastkey=-1
count=0
frame=0
for key in keys:
key = key&7
if key==lastkey:
count+=1
else:
if lastkey!=-1:
ret.append((frame-count, lastkey, count))
lastkey = key
count = 1
frame += 1
return ret
The first set of code I got from the Nhigh.pyw file which you run to use nhigh. The second set I got from the nhighlib.py file.
Now I am almost complete illiterate when it comes to python, so any help would be great. Again I am wondering if I could edit one of these files, or if someone could tell me another quick way like running something in python shell, so that I could get the demo data for these runs quickly.
Also you can download Nhigh here.