Door Rent Collection Script, tell me if you see obvious bugs
Posted: Sat Nov 23, 2002 1:55 am
I've written this script to put on doors of rooms people rent. It's designed to collect rent every 7 days from the time they've first opened the door with their key:
// Rent collection script that goes in the OnOpen for player's door
#include "datetime"
void main()
{
object oPC = GetLastOpenedBy();
object oKey = GetFirstItemInInventory(oPC);
int bAlreadyOpened = GetPersistantDynamicInt(oKey, "Rent Collection On");
string sNextPayment;
string sKeyTag;
string sDoorTagOwner = GetStringLeft(GetTag(OBJECT_SELF), 4);
struct DateTime sNow = getCurrentDateTime();
struct DateTime sRentCollectionTime =
addDateTime(GetPersistantDateTime(oKey, "Rent Start Time"), newDateTime(0,0,7,0));
// Check if the PC has the door's key
while (GetIsObjectValid(oKey))
{
// Check if sDoorTagOwner is the same as the first 4 letters of sKeyTag
// Key tag names are usually of the format <Character Name>Key, and door
// tag names for renters are usually of the format <Character Name>Door.
sKeyTag = GetStringLeft(GetTag(oKey),4);
if (TestStringAgainstPattern(sKeyTag, sDoorTagOwner))
{
// if this object is in fact the door key, check to see if this is
// the first time the door is being opened by the player. If it is,
// start the rent clock ticking. Collect rent every 7 days.
if (!bAlreadyOpened)
{
// Set key to Rent Collection Status ON
SetPersistantInt(oKey, "Rent Collection On", TRUE, 60, SCRIPTNAME_GENERAL);
// Set Rent Start time to Current Time
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the door has already been opened before, check the time on the key
// to see if 7 days have passed since the last Rent Start Time
// if more than 7 days have passed, collect the rent and reset the rent
// start time
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) >= 1000)
{
TakeGoldFromCreature(1000, oPC, TRUE);
SendMessageToPC(oPC, "1000 gold has been taken for your rent.");
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the player doesn't have enough gold after 7 days, destroy their key
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) <= 1000)
{
DestroyObject(oKey);
SendMessageToPC(oPC, "You do not have enough money to meet your rent!");
SendMessageToPC(oPC, "Your key has been destroyed. You will have to see a real estate agent.");
}
}
oKey = GetNextItemInInventory();
}
}
// Rent collection script that goes in the OnOpen for player's door
#include "datetime"
void main()
{
object oPC = GetLastOpenedBy();
object oKey = GetFirstItemInInventory(oPC);
int bAlreadyOpened = GetPersistantDynamicInt(oKey, "Rent Collection On");
string sNextPayment;
string sKeyTag;
string sDoorTagOwner = GetStringLeft(GetTag(OBJECT_SELF), 4);
struct DateTime sNow = getCurrentDateTime();
struct DateTime sRentCollectionTime =
addDateTime(GetPersistantDateTime(oKey, "Rent Start Time"), newDateTime(0,0,7,0));
// Check if the PC has the door's key
while (GetIsObjectValid(oKey))
{
// Check if sDoorTagOwner is the same as the first 4 letters of sKeyTag
// Key tag names are usually of the format <Character Name>Key, and door
// tag names for renters are usually of the format <Character Name>Door.
sKeyTag = GetStringLeft(GetTag(oKey),4);
if (TestStringAgainstPattern(sKeyTag, sDoorTagOwner))
{
// if this object is in fact the door key, check to see if this is
// the first time the door is being opened by the player. If it is,
// start the rent clock ticking. Collect rent every 7 days.
if (!bAlreadyOpened)
{
// Set key to Rent Collection Status ON
SetPersistantInt(oKey, "Rent Collection On", TRUE, 60, SCRIPTNAME_GENERAL);
// Set Rent Start time to Current Time
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the door has already been opened before, check the time on the key
// to see if 7 days have passed since the last Rent Start Time
// if more than 7 days have passed, collect the rent and reset the rent
// start time
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) >= 1000)
{
TakeGoldFromCreature(1000, oPC, TRUE);
SendMessageToPC(oPC, "1000 gold has been taken for your rent.");
SetPersistantDateTime(oKey, "Rent Start Time", sNow);
}
// if the player doesn't have enough gold after 7 days, destroy their key
else if (bAlreadyOpened
&& compareDateTime(sNow, sRentCollectionTime) >= 7
&& GetGold(oPC) <= 1000)
{
DestroyObject(oKey);
SendMessageToPC(oPC, "You do not have enough money to meet your rent!");
SendMessageToPC(oPC, "Your key has been destroyed. You will have to see a real estate agent.");
}
}
oKey = GetNextItemInInventory();
}
}