Code: Show/Hide Rank:BDwinsAlt = 5 Rank:Smong = 5 Money:BDwinsAlt = 5000 Money:Cyan~fire = 2540 Exp:BDwinsAlt = 5000 Exp:Solo Ace = 0 |
Code: Show/Hide p.money_cash = (code to call value of Rank:PlayerName) p.money_rank = (code to call value of Money:PlayerName) ect... |
Code: Show/Hide BDwinsAlt:money:0
BDwinsAlt:rank:1 Smong:rank:2 Smong:money:3000 Solo Ace:exp:300 Solo Ace:money:2000 |
Code: Show/Hide #!/usr/bin/env python
# This python script file is just an example for BDwinsAlt. # It gives a simple illustration of how to read, write and parse a file. class Player: def __init__(me): me.rank = 0 me.money = 0 me.exp = 0 def savepdata(pname, p): # overwrite the lines with new values, otherwise write new lines lines = [] moneyset = rankset = expset = False # for line in open("pdata.tmp", "r").readlines(): file = open("pdata.tmp", "r") if file: for line in file.readlines(): name, var, val = line.split(':') if name == pname: if var == 'money': val = p.money moneyset = True elif var == 'rank': val = p.rank rankset = True else: val = p.exp expset = True line = "%s:%s:%s\n" % (pname, var, val) # the line already existed, "overwrite" with new value lines.append(line) file.close() # if there was no data to be overwritten then we'll have to create new lines to make sure # all data for this player is stored in the textfile. if not moneyset: line = "%s:money:%d\n" % (pname, p.money) lines.append(line) if not rankset: line = "%s:rank:%d\n" % (pname, p.rank) lines.append(line) if not expset: line = "%s:exp:%d\n" % (pname, p.exp) lines.append(line) # eventually save the lines to the temporary file file = open("pdata.tmp", "w") if file: for line in lines: file.write(line) file.close() def getpdata(pname): # this will read the textfile, then it'll return the data for the requested player # if a player's variable couldn't be found the value of the var will be zero (0) p = Player() # iterate through the temporary file and process it line by line for line in open("pdata.tmp", "r").readlines(): # this operation might have to be optimized for larger files name, var, val = line.split(':') # split the line (should we provide a maxsplit here?) if name == pname: val = int(val) # make sure we're using an int-type variable if var == 'money': p.money = val elif var == 'rank': p.rank = val else: p.exp = val return p def main(): # Warning: you might have to check for weird characters in player names before calling getpdata. # Also, it'd be wise to lowercase a name before storing it and when requesting player data. # Anyway, this is just to help you "get started", all these other problems are yours. :) pname = 'BDwinsAlt' p = getpdata(pname) print "Data read for %s:\r\n\tRank: %8d\r\n\tMoney: %8d\r\n\tExp: %8d\r\n" % (pname, p.rank, p.money, p.exp) pname = 'Solo Ace' p = getpdata(pname) print "Data read for %s:\r\n\tRank: %8d\r\n\tMoney: %8d\r\n\tExp: %8d\r\n" % (pname, p.rank, p.money, p.exp) p.rank = 4000; # new values p.exp = 4111; print "Saving new data for %s...\r\n" % pname savepdata(pname, p) # we modified Solo Ace's rank/exp after reading it, so we're saving it here again # now let's see what reading Solo Ace's data gives us, it should return the modified values! p = getpdata(pname) print "Data read for %s:\r\n\tRank: %8d\r\n\tMoney: %8d\r\n\tExp: %8d\r\n" % (pname, p.rank, p.money, p.exp) pname = 'Smong' p = getpdata(pname) print "Data read for %s:\r\n\tRank: %8d\r\n\tMoney: %8d\r\n\tExp: %8d\r\n" % (pname, p.rank, p.money, p.exp) if __name__ == '__main__': main() |
Code: Show/Hide Data read for BDwinsAlt:
Rank: 1 Money: 0 Exp: 0 Data read for Solo Ace: Rank: 0 Money: 2000 Exp: 300 Saving new data for Solo Ace... Data read for Solo Ace: Rank: 4000 Money: 2000 Exp: 4111 Data read for Smong: Rank: 2 Money: 3000 Exp: 0 |
Code: Show/Hide BDwinsAlt:money:0
BDwinsAlt:rank:1 Smong:rank:2 Smong:money:3000 Solo Ace:exp:4111 Solo Ace:money:2000 Solo Ace:rank:4000 [<-- a new line!] |
Code: Show/Hide class myppd:
key = 1234 interval = asss.INTERVAL_FOREVER scope = asss.PERSIST_ALLARENAS def get(self, p): return p.rank, p.money, p.exp def set(self, p, stuff): p.rank, p.money, p.exp = stuff def clear(self, p): p.rank = p.money = p.exp = 0 ppd = asss.reg_player_persistent(myppd()) |
I wrote: |
I don't think this is the best way to do this, and I'm not sure why exactly you want to do this |
Code: Show/Hide Listing 2. Example of dump() and load() >>> a1 = 'apple' >>> b1 = {1: 'One', 2: 'Two', 3: 'Three'} >>> c1 = ['fee', 'fie', 'foe', 'fum'] >>> f1 = file('temp.pkl', 'wb') >>> pickle.dump(a1, f1, True) >>> pickle.dump(b1, f1, True) >>> pickle.dump(c1, f1, True) >>> f1.close() >>> f2 = file('temp.pkl', 'rb') >>> a2 = pickle.load(f2) >>> a2 'apple' >>> b2 = pickle.load(f2) >>> b2 {1: 'One', 2: 'Two', 3: 'Three'} >>> c2 = pickle.load(f2) >>> c2 ['fee', 'fie', 'foe', 'fum'] >>> f2.close() |
Code: Show/Hide u 3040q .u 1.u 0. //Where 3040 = money : 1 = rank : 0 = exp. |