sfcnr/gamemodes/irresistible/cnr/features/houses/realestate.pwn

324 lines
11 KiB
Plaintext
Raw Normal View History

2018-09-24 14:35:46 +00:00
/*
* Irresistible Gaming (c) 2018
* Developed by Lorenc Pekaj
* Module: cnr\features\home\realestate.pwn
* Purpose: home listings for player homes
*/
/* ** Includes ** */
#include < YSI\y_hooks >
/* ** Definitions ** */
#define DIALOG_HOUSE_LISTINGS 5838
#define DIALOG_HOUSE_LIST_VIEW 5839
/* ** Macros ** */
#define ShowPlayerHomeListing(%0,%1) \
mysql_tquery( dbHandle, sprintf( "SELECT * FROM `HOUSE_LISTINGS` WHERE `ID` = %d", %1 ), "HouseListing_OnShowHome", "dd", %0, %1 )
2018-09-24 14:35:46 +00:00
/* ** Constants ** */
static const
2018-09-25 02:42:20 +00:00
HOUSE_LISTING_FEE = 75000;
2018-09-24 14:35:46 +00:00
/* ** Variables ** */
static stock
2018-09-25 02:42:20 +00:00
p_CurrentListings [ MAX_PLAYERS ] [ 50 ]; // TODO: add pagination
2018-09-24 14:35:46 +00:00
/* ** Hooks ** */
hook OnDialogResponse( playerid, dialogid, response, listitem, inputtext[ ] )
{
if ( dialogid == DIALOG_HOUSE_LISTINGS && response )
{
for ( new i = 0, x = 0; i < sizeof ( p_CurrentListings ); i ++ ) if ( p_CurrentListings[ playerid ] [ i ] != -1 )
{
if ( x == listitem )
{
ShowPlayerHomeListing( playerid, p_CurrentListings[ playerid ] [ i ] );
break;
}
x ++;
}
}
else if ( dialogid == DIALOG_HOUSE_LIST_VIEW )
{
if ( ! response )
return ShowPlayerHomeListings( playerid );
new houseid = GetPVarInt( playerid, "house_listing_houseid" );
new listingid = GetPVarInt( playerid, "house_listing_viewid" );
switch ( listitem )
{
case 0: mysql_tquery( dbHandle, sprintf( "SELECT *, UNIX_TIMESTAMP(`SALE_DATE`) as `SALE_DATE_TS` FROM `HOUSE_LISTINGS` WHERE `ID` = %d", listingid ), "HouseListing_OnBuyHome", "dd", playerid, listingid );
case 1:
{
if ( IsPlayerInAnyVehicle( playerid ) )
{
GPS_SetPlayerWaypoint( playerid, g_houseData[ houseid ] [ E_HOUSE_NAME ], g_houseData[ houseid ] [ E_EX ], g_houseData[ houseid ] [ E_EY ], g_houseData[ houseid ] [ E_EZ ] );
SendClientMessageFormatted( playerid, -1, ""COL_GREY"[GPS]"COL_WHITE" You have set your destination to %s. Follow the arrow to reach your destination.", g_houseData[ houseid ] [ E_HOUSE_NAME ] );
}
else
{
ShowPlayerHomeListing( playerid, listingid );
SendError( playerid, "You need to be in a vehicle to set a GPS waypoint." );
return 1;
}
}
}
}
2018-09-24 14:35:46 +00:00
return 1;
}
2018-09-24 17:46:21 +00:00
hook OnHouseOwnerChange( houseid, owner )
{
mysql_single_query( sprintf( "DELETE FROM `HOUSE_LISTINGS` WHERE `HOUSE_ID` = %d AND `SALE_DATE` IS NULL", houseid ) );
return 1;
}
2018-09-24 14:35:46 +00:00
/* ** Commands ** */
2018-09-25 02:42:20 +00:00
CMD:realestate( playerid, params[ ] ) return cmd_estate( playerid, params );
CMD:estate( playerid, params[ ] )
2018-09-24 14:35:46 +00:00
{
new
houseid = p_InHouse[ playerid ];
if ( strmatch( params, "list cancel" ) )
{
if ( ! Iter_Contains( houses, houseid ) ) return SendError( playerid, "You are not inside of any home." );
else if ( ! IsPlayerHomeOwner( playerid, houseid ) ) return SendError( playerid, "You are not the owner of this home." );
{
mysql_tquery( dbHandle, sprintf( "DELETE FROM `HOUSE_LISTINGS` WHERE `HOUSE_ID` = %d AND `SALE_DATE` IS NULL", houseid ), "HouseListing_OnDeleteListing", "dd", playerid, houseid );
}
2018-09-25 02:42:20 +00:00
return 1;
}
else if ( !strcmp( params, "list", false, 4 ) )
{
new
Float: coins;
2018-09-25 02:42:20 +00:00
if ( sscanf( params[ 5 ], "f", coins ) ) return SendUsage( playerid, "/estate list [IC_ASK_PRICE/CANCEL]");
else if ( ! Iter_Contains( houses, houseid ) ) return SendError( playerid, "You are not inside of any home." );
else if ( g_houseData[ houseid ] [ E_COST ] > 2500 ) return SendError( playerid, "This home is not a V.I.P home." );
else if ( ! IsPlayerHomeOwner( playerid, houseid ) ) return SendError( playerid, "You are not the owner of this home." );
else if ( coins < 25.0 ) return SendError( playerid, "Please specify an ask price greater than 25.00 IC." );
else if ( coins > 25000.0 ) return SendError( playerid, "Please specify an ask price less than 25,000 IC." );
2018-09-25 02:42:20 +00:00
else if ( GetPlayerCash( playerid ) < HOUSE_LISTING_FEE && GetPlayerVIPLevel( playerid ) < VIP_GOLD ) return SendError( playerid, "You need at least %s to create a house listing.", cash_format( HOUSE_LISTING_FEE ) );
else
{
mysql_tquery( dbHandle, sprintf( "SELECT * FROM `HOUSE_LISTINGS` WHERE `HOUSE_ID` = %d AND `SALE_DATE` IS NULL", houseid ), "HouseListing_OnCreateListing", "ddf", playerid, houseid, coins );
}
2018-09-25 02:42:20 +00:00
return 1;
}
else
{
return ShowPlayerHomeListings( playerid );
}
2018-09-24 14:35:46 +00:00
}
/* ** SQL Threads ** */
thread HouseListing_OnShowHomes( playerid )
{
new
rows = cache_get_row_count( );
if ( rows )
{
static
location[ MAX_ZONE_NAME ],
2018-09-25 02:42:20 +00:00
city[ 3 ],
house_name[ 32 ],
owner[ 24 ];
// set headers
szHugeString = ""COL_GREY"House\t"COL_GREY"Owner\t"COL_GREY"Location\t"COL_GREY"Ask Price (IC)\n";
for ( new row = 0; row < sizeof ( p_CurrentListings[ ] ); row ++ )
{
if ( row < rows )
{
// store all the listing ids to the player
p_CurrentListings[ playerid ] [ row ] = cache_get_field_content_int( row, "ID" );
cache_get_field_content( row, "NAME", house_name );
cache_get_field_content( row, "OWNER", owner );
new houseid = cache_get_field_content_int( row, "HOUSE_ID" );
2018-09-25 02:42:20 +00:00
Get2DCityShort( city, g_houseData[ houseid ] [ E_EX ], g_houseData[ houseid ] [ E_EY ] );
GetZoneFromCoordinates( location, g_houseData[ houseid ] [ E_EX ], g_houseData[ houseid ] [ E_EY ], .placeholder = "Island" );
new Float: coins = cache_get_field_content_float( row, "ASK" );
format( szHugeString, sizeof( szHugeString ), "%s%s\t%s\t%s, %s\t"COL_GREEN"%s IC\n", szHugeString, house_name, owner, location, city, number_format( coins, .decimals = 2 ) );
}
else
{
p_CurrentListings[ playerid ] [ row ] = -1;
}
}
2018-09-25 02:42:20 +00:00
SendServerMessage( playerid, "You can list your own home using "COL_GREY"/estate list"COL_WHITE" for %s.", p_VIPLevel[ playerid ] < VIP_GOLD ? ( cash_format( HOUSE_LISTING_FEE ) ) : ( "FREE" ) );
return ShowPlayerDialog( playerid, DIALOG_HOUSE_LISTINGS, DIALOG_STYLE_TABLIST_HEADERS, ""COL_GOLD"Irresistible Coin - "COL_WHITE"Premium Home Estate", szHugeString, "Select", "Close" );
}
else
{
return SendError( playerid, "There are no available homes for sale at current." );
}
}
thread HouseListing_OnShowHome( playerid, house_listing_id )
{
new
rows = cache_get_row_count( );
if ( rows )
{
new house_id = cache_get_field_content_int( 0, "HOUSE_ID" );
new Float: coins = cache_get_field_content_float( 0, "ASK" );
SetPVarInt( playerid, "house_listing_viewid", house_listing_id );
SetPVarInt( playerid, "house_listing_houseid", house_id );
return ShowPlayerDialog(
playerid, DIALOG_HOUSE_LIST_VIEW, DIALOG_STYLE_TABLIST,
2018-09-25 02:42:20 +00:00
""COL_GOLD"Irresistible Coin - "COL_WHITE"Premium Home Estate",
sprintf( "Purchase Home\t"COL_GREEN"%s IC\nSet GPS Waypoint\t"COL_GREY">>>", number_format( coins, .decimals = 2 ) ),
"Select", "Back"
);
}
else
{
return SendError( playerid, "An error has occurred, please try again." );
}
}
thread HouseListing_OnBuyHome( playerid, house_listing_id )
{
new
rows = cache_get_row_count( );
if ( rows )
{
new
Float: ask_price = cache_get_field_content_float( 0, "ASK" );
// does the dude even have the coins
if ( GetPlayerIrresistibleCoins( playerid ) < ask_price ) {
ShowPlayerHomeListing( playerid, house_listing_id );
2018-09-25 02:42:20 +00:00
return SendError( playerid, "You do not have enough Irresistible Coins for this home (%s IC).", number_format( ask_price, .decimals = 2 ) );
}
// check if player is over the limit
if ( p_OwnedHouses[ playerid ] >= GetPlayerHouseSlots( playerid ) ) {
ShowPlayerHomeListing( playerid, house_listing_id );
return SendError( playerid, "You cannot purchase any more houses, you've reached the limit." );
}
// check if sale already completed
new
sale_date_ts = cache_get_field_content_int( 0, "SALE_DATE_TS" );
if ( sale_date_ts != 0 && GetServerTime( ) > sale_date_ts ) {
return SendError( playerid, "You can no longer buy this home as it has been sold." );
}
2018-09-25 02:42:20 +00:00
// check if buyer is the player himself
new
owner_account_id = cache_get_field_content_int( 0, "USER_ID" );
if ( GetPlayerAccountID( playerid ) == owner_account_id ) {
ShowPlayerHomeListing( playerid, house_listing_id );
return SendError( playerid, "You cannot buy your own home." );
}
// credit seller if they are on/offline
new
2018-09-25 13:28:31 +00:00
houseid = cache_get_field_content_int( 0, "HOUSE_ID" ),
sellerid;
foreach ( sellerid : Player ) if ( GetPlayerAccountID( sellerid ) == owner_account_id ) {
break;
}
// validate seller id
if ( 0 <= sellerid < MAX_PLAYERS && Iter_Contains( Player, sellerid ) ) {
p_OwnedHouses[ sellerid ] --;
GivePlayerIrresistibleCoins( sellerid, ask_price );
2018-09-25 13:12:41 +00:00
SendServerMessage( sellerid, "You have successfully sold your home (%s) for "COL_GOLD"%s IC"COL_WHITE" to %s(%d)!", g_houseData[ houseid ] [ E_HOUSE_NAME ], number_format( ask_price, .decimals = 2 ), ReturnPlayerName( playerid ), playerid );
} else {
mysql_single_query( sprintf( "UPDATE `USERS` SET `COINS` = `COINS` + %f WHERE `ID` = %d", ask_price, owner_account_id ) );
}
// show sellers name & house name
SendServerMessage( playerid, "You have successfully bought %s's home (%s"COL_WHITE") for "COL_GOLD"%s IC"COL_WHITE"!", g_houseData[ houseid ] [ E_OWNER ], g_houseData[ houseid ] [ E_HOUSE_NAME ], number_format( ask_price, .decimals = 2 ) );
// set listing as sold and transfer home
mysql_single_query( sprintf( "UPDATE `HOUSE_LISTINGS` SET `SALE_DATE` = CURRENT_TIMESTAMP WHERE `ID` = %d", house_listing_id ) );
2018-09-24 17:46:21 +00:00
SetHouseOwner( houseid, GetPlayerAccountID( playerid ), ReturnPlayerName( playerid ) );
GivePlayerIrresistibleCoins( playerid, -ask_price );
p_OwnedHouses[ playerid ] ++;
return 1;
}
else
{
return SendError( playerid, "An error has occurred, please try again." );
}
}
2018-09-24 14:35:46 +00:00
thread HouseListing_OnCreateListing( playerid, houseid, Float: ask_price )
{
new
rows = cache_get_row_count( );
if ( ! rows )
{
// debit user account
2018-09-25 02:42:20 +00:00
if ( GetPlayerVIPLevel( playerid ) < VIP_GOLD ) {
GivePlayerCash( playerid, -HOUSE_LISTING_FEE );
}
// insert into database and notify
mysql_single_query( sprintf( "INSERT INTO `HOUSE_LISTINGS` (`HOUSE_ID`, `USER_ID`, `ASK`) VALUES (%d, %d, %f)", houseid, GetPlayerAccountID( playerid ), ask_price ) );
2018-09-25 02:42:20 +00:00
return SendServerMessage( playerid, "You have listed your home. You can retract your listing using "COL_GREY"/estate list cancel"COL_WHITE"." );
}
else
{
2018-09-25 02:42:20 +00:00
return SendError( playerid, "This home is already listed. You can retract your listing using "COL_GREY"/estate list cancel"COL_WHITE"." );
}
}
thread HouseListing_OnDeleteListing( playerid, houseid )
{
new
deleted_rows = cache_affected_rows( );
if ( deleted_rows )
{
2018-09-25 02:42:20 +00:00
return SendServerMessage( playerid, "You have delisted your home. You can create a new listing using "COL_GREY"/estate list"COL_WHITE"." );
}
else
{
return SendError( playerid, "This home is not listed on the premium realestate market." );
}
}
/* ** Functions ** */
stock ShowPlayerHomeListings( playerid ) {
return mysql_tquery( dbHandle, "SELECT HL.ID, HL.HOUSE_ID, H.NAME, U.NAME AS OWNER, HL.ASK FROM HOUSE_LISTINGS HL INNER JOIN HOUSES H ON H.ID = HL.HOUSE_ID INNER JOIN USERS U ON U.ID = HL.USER_ID WHERE SALE_DATE IS NULL", "HouseListing_OnShowHomes", "d", playerid );
}
2018-09-24 14:35:46 +00:00
/* ** Migrations ** */
/*
DROP TABLE HOUSE_LISTINGS;
CREATE TABLE IF NOT EXISTS HOUSE_LISTINGS (
ID int(11) AUTO_INCREMENT PRIMARY KEY,
2018-09-24 14:35:46 +00:00
HOUSE_ID int(11),
USER_ID int(11),
ASK float,
2018-09-25 02:42:20 +00:00
LISTING_DATE TIMESTAMP default CURRENT_TIMESTAMP,
SALE_DATE TIMESTAMP nullable default null,
2018-09-24 14:35:46 +00:00
FOREIGN KEY (HOUSE_ID) REFERENCES HOUSES (ID) ON DELETE CASCADE
2018-09-25 02:42:20 +00:00
)
2018-09-24 14:35:46 +00:00
*/