Storing player information

Please post any questions about developing your plugin here. Please use the search function before posting!
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Storing player information

Postby arawra » Fri Aug 30, 2013 4:36 am

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?
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Fri Aug 30, 2013 6:41 am

Using pickle.
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Fri Aug 30, 2013 7:51 am

-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Fri Aug 30, 2013 3:26 pm

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
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Wed Sep 04, 2013 4:17 am

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')
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Sep 04, 2013 4:38 am

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
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Wed Sep 04, 2013 4:42 am

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?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Sep 04, 2013 4:57 am

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

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 35 guests