Persistent Coins (#1405)

On Coin Insert, a file Save/Coin.ini is created if it does not exist, which stores the number of coins currrently in m_iCoins. Whenever m_iCoins is update, the Coin.ini is updated.

On boot, Coin.ini is read and m_iCoins is set to that value. If coins in coin.ini file is greater then MAX_NUM_CREDITS then reset the value to 0.
This commit is contained in:
Tyler Brekke
2017-02-17 08:21:38 -08:00
committed by Colby Klein
parent 4b3507f195
commit 8f332653e1
7 changed files with 49 additions and 1 deletions
+27
View File
@@ -4,10 +4,12 @@
#include "RageLog.h" #include "RageLog.h"
#include "IniFile.h" #include "IniFile.h"
#include "GameConstantsAndTypes.h" #include "GameConstantsAndTypes.h"
#include "GameState.h"
#include "SongManager.h" #include "SongManager.h"
#include "RageFile.h" #include "RageFile.h"
#include "XmlFile.h" #include "XmlFile.h"
#include "XmlFileUtil.h" #include "XmlFileUtil.h"
#include "SpecialFiles.h"
#include <ctime> #include <ctime>
Bookkeeper* BOOKKEEPER = NULL; // global and accessible from anywhere in our program Bookkeeper* BOOKKEEPER = NULL; // global and accessible from anywhere in our program
@@ -120,6 +122,17 @@ void Bookkeeper::ReadFromDisk()
if( !XmlFileUtil::LoadFromFileShowErrors(xml, COINS_DAT) ) if( !XmlFileUtil::LoadFromFileShowErrors(xml, COINS_DAT) )
return; return;
int numCoins = 0;
ReadCoinsFile(numCoins);
if ( numCoins < 0 )
numCoins = 0;
else if ( numCoins / PREFSMAN->m_iCoinsPerCredit > MAX_NUM_CREDITS )
numCoins = 0;
LOG->Trace("Number of Coins to Load on boot: %i", numCoins);
GAMESTATE->m_iCoins.Set(numCoins);
LoadFromNode( &xml ); LoadFromNode( &xml );
} }
@@ -145,6 +158,20 @@ void Bookkeeper::CoinInserted()
++m_mapCoinsForHour[d]; ++m_mapCoinsForHour[d];
} }
void Bookkeeper::WriteCoinsFile( int coins )
{
IniFile ini;
ini.SetValue( "Bookkeeping", "Coins", coins);
ini.WriteFile( SpecialFiles::COINS_INI );
}
void Bookkeeper::ReadCoinsFile( int &coins )
{
IniFile ini;
ini.ReadFile( SpecialFiles::COINS_INI );
ini.GetValue( "Bookkeeping", "Coins", coins);
}
// Return the number of coins between [beginning,ending). // Return the number of coins between [beginning,ending).
int Bookkeeper::GetNumCoinsInRange( map<Date,int>::const_iterator begin, map<Date,int>::const_iterator end ) const int Bookkeeper::GetNumCoinsInRange( map<Date,int>::const_iterator begin, map<Date,int>::const_iterator end ) const
{ {
+3
View File
@@ -21,6 +21,9 @@ public:
void GetCoinsLastWeeks( int coins[NUM_LAST_WEEKS] ) const; void GetCoinsLastWeeks( int coins[NUM_LAST_WEEKS] ) const;
void GetCoinsByDayOfWeek( int coins[DAYS_IN_WEEK] ) const; void GetCoinsByDayOfWeek( int coins[DAYS_IN_WEEK] ) const;
void GetCoinsByHour( int coins[HOURS_IN_DAY] ) const; void GetCoinsByHour( int coins[HOURS_IN_DAY] ) const;
void WriteCoinsFile( int coins ) ;
void ReadCoinsFile( int &coins ) ;
void LoadFromNode( const XNode *pNode ); void LoadFromNode( const XNode *pNode );
XNode* CreateNode() const; XNode* CreateNode() const;
+4
View File
@@ -5,6 +5,7 @@
#include "GameManager.h" #include "GameManager.h"
#include "GameState.h" #include "GameState.h"
#include "AnnouncerManager.h" #include "AnnouncerManager.h"
#include "Bookkeeper.h"
#include "PlayerOptions.h" #include "PlayerOptions.h"
#include "ProfileManager.h" #include "ProfileManager.h"
#include "Profile.h" #include "Profile.h"
@@ -718,6 +719,9 @@ void GameCommand::ApplySelf( const vector<PlayerNumber> &vpns ) const
GAMESTATE->m_iCoins.Set( GAMESTATE->m_iCoins - iNumCreditsOwed * PREFSMAN->m_iCoinsPerCredit ); GAMESTATE->m_iCoins.Set( GAMESTATE->m_iCoins - iNumCreditsOwed * PREFSMAN->m_iCoinsPerCredit );
LOG->Trace( "Deducted %i coins, %i remaining", LOG->Trace( "Deducted %i coins, %i remaining",
iNumCreditsOwed * PREFSMAN->m_iCoinsPerCredit, GAMESTATE->m_iCoins.Get() ); iNumCreditsOwed * PREFSMAN->m_iCoinsPerCredit, GAMESTATE->m_iCoins.Get() );
//Credit Used, make sure to update CoinsFile
BOOKKEEPER->WriteCoinsFile(GAMESTATE->m_iCoins.Get());
} }
// If only one side is joined and we picked a style that requires both // If only one side is joined and we picked a style that requires both
+3
View File
@@ -514,6 +514,9 @@ namespace
GAMESTATE->JoinPlayer( pn ); GAMESTATE->JoinPlayer( pn );
// On Join, make sure to update Coins File
BOOKKEEPER->WriteCoinsFile(GAMESTATE->m_iCoins.Get());
return true; return true;
} }
}; };
+2
View File
@@ -19,6 +19,8 @@ const RString SpecialFiles::TYPE_TXT_FILE = "Data/Type.txt";
const RString SpecialFiles::SONGS_DIR = "Songs/"; const RString SpecialFiles::SONGS_DIR = "Songs/";
const RString SpecialFiles::COURSES_DIR = "Courses/"; const RString SpecialFiles::COURSES_DIR = "Courses/";
const RString SpecialFiles::NOTESKINS_DIR = "NoteSkins/"; const RString SpecialFiles::NOTESKINS_DIR = "NoteSkins/";
const RString SpecialFiles::COINS_INI = "Save/Coin.ini";
/* /*
+3
View File
@@ -39,6 +39,9 @@ namespace SpecialFiles
extern const RString COURSES_DIR; extern const RString COURSES_DIR;
/** @brief The default noteskins directory. */ /** @brief The default noteskins directory. */
extern const RString NOTESKINS_DIR; extern const RString NOTESKINS_DIR;
extern const RString COINS_INI;
} }
#endif #endif
+6
View File
@@ -1284,6 +1284,9 @@ void StepMania::InsertCoin( int iNum, bool bCountInBookkeeping )
LOG->Trace("%i coins inserted, %i needed to play", GAMESTATE->m_iCoins.Get(), PREFSMAN->m_iCoinsPerCredit.Get() ); LOG->Trace("%i coins inserted, %i needed to play", GAMESTATE->m_iCoins.Get(), PREFSMAN->m_iCoinsPerCredit.Get() );
// On InsertCoin, make sure to update Coins file
BOOKKEEPER->WriteCoinsFile(GAMESTATE->m_iCoins.Get());
// If inserting coins, play an appropriate sound; if deducting coins, don't play anything. // If inserting coins, play an appropriate sound; if deducting coins, don't play anything.
if (iNum > 0) if (iNum > 0)
{ {
@@ -1323,6 +1326,9 @@ void StepMania::ClearCredits()
GAMESTATE->m_iCoins.Set( 0 ); GAMESTATE->m_iCoins.Set( 0 );
SCREENMAN->PlayInvalidSound(); SCREENMAN->PlayInvalidSound();
// Update Coins file to make sure credits are cleared.
BOOKKEEPER->WriteCoinsFile(GAMESTATE->m_iCoins.Get());
// TODO: remove this redundant message and things that depend on it // TODO: remove this redundant message and things that depend on it
Message msg( "CoinInserted" ); Message msg( "CoinInserted" );
// below params are unused // below params are unused