From 96613ed3a020eb52575ac32c9e53f41a9ed34af8 Mon Sep 17 00:00:00 2001 From: Lorenc Pekaj Date: Tue, 25 Sep 2018 02:06:53 +1000 Subject: [PATCH] you can now buy homes directly off /houselistings ... and set a waypoint to the home that is of interest --- .../cnr/features/houses/house.pwn | 10 +- .../cnr/features/houses/realestate.pwn | 191 +++++++++++++++++- 2 files changed, 195 insertions(+), 6 deletions(-) diff --git a/gamemodes/irresistible/cnr/features/houses/house.pwn b/gamemodes/irresistible/cnr/features/houses/house.pwn index 148acd3..c6a5c7a 100644 --- a/gamemodes/irresistible/cnr/features/houses/house.pwn +++ b/gamemodes/irresistible/cnr/features/houses/house.pwn @@ -534,8 +534,7 @@ CMD:h( playerid, params[ ] ) p_OwnedHouses[ playerid ] ++; // destroyAllFurniture( houseid ); - mysql_single_query( sprintf( "UPDATE `FURNITURE` SET `OWNER`=%d WHERE `HOUSE_ID`=%d", p_AccountID[ playerid ], houseid ) ); - SetHouseOwner( houseid, ReturnPlayerName( playerid ) ); + SetHouseOwner( houseid, ReturnPlayerName( playerid ), .buyerid = playerid ); GivePlayerCash( playerid, -sellingprice ); GivePlayerCash( sellerid, sellingprice ); @@ -758,7 +757,7 @@ stock SetHouseForAuction( ID ) return 1; } -stock SetHouseOwner( houseid, szOwner[ MAX_PLAYER_NAME ] ) +stock SetHouseOwner( houseid, szOwner[ MAX_PLAYER_NAME ], buyerid = INVALID_PLAYER_ID ) { if ( ! Iter_Contains( houses, houseid ) || isnull( szOwner ) ) return 0; @@ -771,6 +770,11 @@ stock SetHouseOwner( houseid, szOwner[ MAX_PLAYER_NAME ] ) format( query, sizeof( query ), "UPDATE HOUSES SET OWNER='%s' WHERE ID=%d", mysql_escape( szOwner ), houseid ); mysql_single_query( query ); + // transfer furniture to account + if ( buyerid != INVALID_PLAYER_ID ) { + mysql_single_query( sprintf( "UPDATE `FURNITURE` SET `OWNER`=%d WHERE `HOUSE_ID`=%d", p_AccountID[ buyerid ], houseid ) ); + } + DestroyDynamicMapIcon( g_houseData[ houseid ] [ E_MAP_ICON ] ); format( szBigString, sizeof( szBigString ), ""COL_GOLD"House:"COL_WHITE" Home(%d)\n"COL_GOLD"Owner:"COL_WHITE" %s\n"COL_GOLD"Price:"COL_WHITE" %s", houseid, g_houseData[ houseid ] [ E_OWNER ], cash_format( g_houseData[ houseid ] [ E_COST ] ) ); UpdateDynamic3DTextLabelText( g_houseData[ houseid ] [ E_LABEL ] [ 0 ], COLOR_WHITE, szBigString); diff --git a/gamemodes/irresistible/cnr/features/houses/realestate.pwn b/gamemodes/irresistible/cnr/features/houses/realestate.pwn index 4870c98..449dd27 100644 --- a/gamemodes/irresistible/cnr/features/houses/realestate.pwn +++ b/gamemodes/irresistible/cnr/features/houses/realestate.pwn @@ -9,16 +9,73 @@ #include < YSI\y_hooks > /* ** Definitions ** */ +#define DIALOG_HOUSE_LISTINGS 5838 +#define DIALOG_HOUSE_LIST_VIEW 5839 + +/* ** Macros ** */ +#define ShowPlayerHomeListings(%0) \ + 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", %0 ) + +#define ShowPlayerHomeListing(%0,%1) \ + mysql_tquery( dbHandle, sprintf( "SELECT * FROM `HOUSE_LISTINGS` WHERE `ID` = %d", %1 ), "HouseListing_OnShowHome", "dd", %0, %1 ) /* ** Variables ** */ +static stock + p_CurrentListings [ MAX_PLAYERS ] [ 20 ]; /* ** 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 + listingid = GetPVarInt( playerid, "house_listing_viewid" ); + + switch ( listitem ) + { + case 0: mysql_tquery( dbHandle, sprintf( "SELECT * FROM `HOUSE_LISTINGS` WHERE `ID` = %d", listingid ), "HouseListing_OnBuyHome", "dd", playerid, listingid ); + case 1: + { + if ( IsPlayerInAnyVehicle( playerid ) ) + { + new houseid = GetPVarInt( playerid, "house_listing_houseid" ); + 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; + } + } + } + } return 1; } /* ** Commands ** */ +CMD:homelistings( playerid, params[ ] ) +{ + ShowPlayerHomeListings( playerid ); + return 1; +} + CMD:listhome( playerid, params[ ] ) { new Float: coins; @@ -39,18 +96,146 @@ CMD:listhome( playerid, params[ ] ) return 1; } -/* ** Functions ** */ +/* ** SQL Threads ** */ +thread HouseListing_OnShowHomes( playerid ) +{ + new + rows = cache_get_row_count( ); + + if ( rows ) + { + static + location[ MAX_ZONE_NAME ], + city[ MAX_ZONE_NAME ], + house_name[ 32 ], + owner[ 24 ]; + + // set headers + szLargeString = ""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" ); + + Get2DCity( city, g_houseData[ houseid ] [ E_EX ], g_houseData[ houseid ] [ E_EX ] ); + GetZoneFromCoordinates( location, g_houseData[ houseid ] [ E_EX ], g_houseData[ houseid ] [ E_EX ] ); + + new Float: coins = cache_get_field_content_float( row, "ASK" ); + + format( szLargeString, sizeof( szLargeString ), "%s%s\t%s\t%s, %s\t"COL_GREEN"%0.2f IC\n", szLargeString, house_name, owner, city, location, coins ); + } + else + { + p_CurrentListings[ playerid ] [ row ] = -1; + } + } + return ShowPlayerDialog( playerid, DIALOG_HOUSE_LISTINGS, DIALOG_STYLE_TABLIST_HEADERS, "{FFFFFF}House Real Estate", szLargeString, "Select", "Back" ); + } + 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, + "{FFFFFF}House Real Estate", + sprintf( "Purchase Home\t"COL_GREEN"%0.2f IC\nSet GPS Waypoint\t"COL_GREY">>>", coins ), + "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 ); + return SendError( playerid, "You do not have enough Irresistible coins for this home (%0.2f IC).", ask_price ); + } + + // + // TODO: check if sale elapsed + // + + new + owner_account_id = cache_get_field_content_int( 0, "USER_ID" ), + sellerid; + + foreach ( sellerid : Player ) if ( GetPlayerAccountID( sellerid ) == owner_account_id ) { + break; + } + + // credit seller if they are on/offline + if ( 0 <= sellerid < MAX_PLAYERS && Iter_Contains( Player, sellerid ) ) { + p_OwnedHouses[ sellerid ] --; + GivePlayerIrresistibleCoins( sellerid, ask_price ); + SendServerMessage( sellerid, "You have successfully sold your house for "COL_GOLD"%s IC"COL_WHITE" to %s(%d)!", number_format( ask_price, .decimals = 2 ), ReturnPlayerName( playerid ), playerid ); + } else { + mysql_single_query( sprintf( "UPDATE `USERS` SET `COINS` = `COINS` + %0.2f WHERE `ID` = %d", ask_price, owner_account_id ) ); + } + + new + houseid = cache_get_field_content_int( 0, "HOUSE_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 ) ); + SetHouseOwner( houseid, ReturnPlayerName( playerid ), .buyerid = playerid ); + GivePlayerIrresistibleCoins( playerid, -ask_price ); + return 1; + } + else + { + return SendError( playerid, "An error has occurred, please try again." ); + } +} /* ** Migrations ** */ /* DROP TABLE HOUSE_LISTINGS; CREATE TABLE IF NOT EXISTS HOUSE_LISTINGS ( - ID int(11), + ID int(11) AUTO_INCREMENT PRIMARY KEY, HOUSE_ID int(11), USER_ID int(11), ASK float, + LISTING_DATE DATETIME default CURRENT_TIMESTAMP, SALE_DATE DATETIME default null, - PRIMARY KEY (ID), UNIQUE KEY (HOUSE_ID), FOREIGN KEY (HOUSE_ID) REFERENCES HOUSES (ID) ON DELETE CASCADE );