Page 1 of 1

Storing player information

Posted: Fri Aug 30, 2013 4:36 am
by arawra
I am not familiar with SQL. Its something I will learn, but probably not in the near future. I am wondering with the entire rewrite of the Eventscripts engine into Source-Python if it would be feasible to include the cPickle engine. Would I be able to copy/paste it into "_engines" or if there would be another method?

Posted: Fri Aug 30, 2013 6:41 am
by arawra
Using pickle.

Posted: Fri Aug 30, 2013 7:51 am
by Tuck

Posted: Fri Aug 30, 2013 3:26 pm
by satoon101
http://docs.python.org/3.1/whatsnew/3.0.html#library-changes

Search that page for cPickle to understand the changes made in Python3.

Satoon

Posted: Wed Sep 04, 2013 4:17 am
by arawra
Having some problems when loading the information. Whenever the new information loads and gets saved, it acts as if no existing data was loaded and overwrites with entirely new data.

Syntax: Select all

@Event
def round_prestart(game_event):
loadDatabase()
for players in CPlayerGenerator():
dndPlayer(players) # The class saves players into the global dictionary, player_database{}

@Event
def round_end(game_event):
saveDatabase()
msg('No more XP saved this round!')

def loadDatabase():
global player_database
str_path = ADDON_PATH.join('dnd') + '/player_database.db'
if os.path.isfile(str_path):
file_users = open(str_path, 'rb')
player_database = pickle.load(file_users)
file_users.close()
msg('Levels loaded')

def saveDatabase():
global player_database
str_path = open(ADDON_PATH.join('dnd') + '/player_database.db', 'wb')
pickle.dump(player_database, str_path)
str_path.close()
msg('Levels saved')

Posted: Wed Sep 04, 2013 4:38 am
by satoon101
You should only ever load the data once, on script load:

Syntax: Select all

# Store the data as an empty dictionary in case the database has never been stored
player_database = {}

# Get the Path instance to the database file
data_path = ADDON_PATH.joinpath('dnd', 'player_database.db')

# Does the database file exist?
if data_path.isfile():

# Open/close the database file in read/byte
with data_path.open('rb') as file_users:

# Load the database into the global variable
player_database = pickle.load(file_users)


@Event
def round_end(game_event):
saveDatabase()


def saveDatabase():
# Open/close the database file in write/byte
with data_path.open('wb') as file_users:

# Dump the database
pickle.dump(player_database, file_users)
Satoon

Posted: Wed Sep 04, 2013 4:42 am
by arawra
satoon101 wrote:You should only ever load the data once, on script load
Satoon


Whats the reasoning behind this? If the data is being saved, then loaded from that saved space, why is it being overwritten with blank information?

Posted: Wed Sep 04, 2013 4:57 am
by satoon101
I'm not sure why it would be blank information. However, as I said, you should only load the data once. Loading it again is just loading the same data that you already have loaded. This is pointless.

In order to answer your question, I would probably need to see a lot more of your code. Specifically the dndPlayer function and any other function having to do with player_database.

Satoon