Server Help

ASSS Questions - Custom datafile. Calling back data and storing it.

BDwinsAlt - Sat Jul 15, 2006 7:37 am
Post subject: Custom datafile. Calling back data and storing it.
I posted this on another python forum and explained it very very clearly. I saw hardly any replies on other topics so I decided to post here. I am new to python. I can make ?buy modules you know play with values and check them and all. Bounty rabbit, easy. I can do everything so far I have tried except being able to make my own little config like file. I can do bits and peices of this but I can't seem to get it to go all together to work completely.

I can get the file to write the values and all but I can't get it to overwrite it if it already exists.

Here's what I need done.

Code: Show/Hide

Rank:BDwinsAlt = 5
Rank:Smong = 5
Money:BDwinsAlt = 5000
Money:Cyan~fire = 2540
Exp:BDwinsAlt = 5000
Exp:Solo Ace = 0


Something that looks like that. I need to be able to overwrite existing values or create them if they don't exist. I need to be able to call them back as well. I can make it work by using the cfg function in asss, but why would anyone want to save the data to arena.conf. We may need to replace it or something. I need to make my own custom file. I think you understand what I'm saying.

Basically when a player leaves it needs to search for the Rank:PlayerName and overwrite it if that lines exists. If it doesn't then it needs to create that line. It needs to be able to be called back later when the player enters again.

Something like

Code: Show/Hide

p.money_cash = (code to call value of Rank:PlayerName)
p.money_rank = (code to call value of Money:PlayerName)
ect...


Can you please help me. I searched all over google. I've posted on other forums. I am new to python so a little code won't hurt. Thanks in advance.
Solo Ace - Sat Jul 15, 2006 10:18 am
Post subject:
I don't think this is the best way to do this, and I'm not sure why exactly you want to do this, but maybe this helps:

pdata.tmp

Code: Show/Hide
BDwinsAlt:money:0
BDwinsAlt:rank:1
Smong:rank:2
Smong:money:3000
Solo Ace:exp:300
Solo Ace:money:2000


test.py

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()


The output of this is:

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


Eventually, pdata.tmp will look like this:

pdata.tmp

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!]


I'm also pretty new to python, and I'm not a programmer, so uh... "bare" ? with me.
Why I wrote this? Well, I'm like... bored... yeah, that's what you could name it.
BDwinsAlt - Sat Jul 15, 2006 11:53 am
Post subject:
First of all I would like to say, "Thank you very much."
Seconly, it says "ValueError: Unpack list of wrong size." in getpdata
Smong - Sat Jul 15, 2006 3:54 pm
Post subject:
Maybe add error checking to opening the file in the getpdata function, savepdata has it, just copy that as an example.
Solo Ace - Sat Jul 15, 2006 5:12 pm
Post subject:
I didn't think it'd make a difference because I got an exception for both methods.
BDwinsAlt - Sat Jul 15, 2006 5:29 pm
Post subject:
It has something to do with the name, var, val line.split(':') thing. I don't know what it is though.
Solo Ace - Sat Jul 15, 2006 5:50 pm
Post subject:
It runs completely fine for me on Python 2.4.2 @ Linux and Python 2.4.3 @ WinXP...
Could you show us what you're exactly doing? Your description of your problem is a.. little vague...
BDwinsAlt - Sat Jul 15, 2006 6:27 pm
Post subject:
I modified some things and used my update python. It works! Thank you so much Solo Ace. Your on my cool list now. <3

Edit: Hmm getting ValueError:Unpack list of wrong size.
Grelminar - Sat Jul 15, 2006 6:47 pm
Post subject:
This is exactly what the persistent data feature was designed for. You don't want to make your own file. You want to do something like this:

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())


See http://asss.yi.org/asss/files/devguide.html#%_sec_7
BDwinsAlt - Sat Jul 15, 2006 7:11 pm
Post subject:
I see, i'll try that next.
Solo Ace - Sun Jul 16, 2006 4:02 am
Post subject:
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

icon_sad.gif

Oh well, I haven't touched AS3 in years. AND NO I'M NOT TALKING ABOUT TOUCHING ASS.
Smong - Sun Jul 16, 2006 7:06 am
Post subject:
How are you going to make a ?top10 and ?rank <name> command with the persist module?
BDwinsAlt - Sun Jul 16, 2006 7:47 am
Post subject:
I don't know. I just wanted somewhere to save my data incase the server went offline. I just need to be able to store and load values to a file, overwrite the ones that exists already. I.E.: BDwinsAlt:Rank:5

Solo, thanks for trying. It means a lot to me that you took that time out for me. It will actually store the values but won't load them. I may be able to work with that.

EDIT: Maybe I would give people their own file user /rank.
Like
rank/BDwinsAlt
rank/Smong

with this code.

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()


Of course I will modify the values :), I'll store it as strings and call it back as ints later.
BDwinsAlt - Sun Jul 16, 2006 8:25 am
Post subject:
I GOT IT WORKING!!!
IT WORKS

I did what my above post says and it works. Thanks for all your support guys.

http://www-128.ibm.com/developerworks/library/l-pypers.html

For anyone else trying to do this read the above link.
It is pretty nice. It overwrites old values. I use try: and except: So I can create the file if it doesn't exist.

Thanks so much guys.

Outut:
Code: Show/Hide

u 3040q .u 1.u 0.  //Where 3040 = money : 1 = rank : 0 = exp.

Grelminar - Sun Jul 16, 2006 3:16 pm
Post subject:
persist alone doesn't support stuff like top 10 lists or getting people's data when they're not online. I might add support for loading data for offline players. And you can handle the top 10 list by writing some code and tracking it yourself in per-arena data. But that's not a great solution.

What BD is doing, though, can easily use persist, and should. Writing pickles to files is just duplicating what persist+pymod already do, except slower, less flexible, and with more code.
BDwinsAlt - Sun Jul 16, 2006 5:55 pm
Post subject:
This was simpler. And it works. But thank you.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group