Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a_samp>
- #include <zcmd>
- #include <strlib>
- #include <sscanf2>
- #define MAX_AUCTIONS (10)
- enum E_AUCTION {
- bool: a_used,
- a_player,
- a_offeredPrice,
- a_startPrice,
- a_offerDelay,
- a_delayTimer
- }
- new g_AuctionData[MAX_AUCTIONS][E_AUCTION];
- stock CreateAuction(price, offerDelay = 10000)
- {
- new id = FindFreeAuctionID();
- if (id == -1) return 0;
- g_AuctionData[id][a_used] = true;
- g_AuctionData[id][a_player] = INVALID_PLAYER_ID;
- g_AuctionData[id][a_offeredPrice] = 0;
- g_AuctionData[id][a_startPrice] = price < 1 ? 1 : price;
- g_AuctionData[id][a_offerDelay] = offerDelay;
- printf("Auction %d has started");
- SendClientMessageToAll(-1, sprintf("Auction %d has started, with start price of %d$", id, price));
- return 1;
- }
- stock DestroyAuction(id)
- {
- if (!g_AuctionData[id][a_used]) return 0; // Nema praznog slota za novu aukciju
- g_AuctionData[id][a_used] = false;
- return 1;
- }
- static stock FindFreeAuctionID()
- {
- for (new i = 0; i < MAX_AUCTIONS; i++) if (!g_AuctionData[i][a_used]) {
- return i;
- }
- return -1;
- }
- stock OfferAuctionPrice(playerid, auctionId, price)
- {
- if (!g_AuctionData[auctionId][a_used]) return 0; // Aukcija nije aktivna vise
- if (price < g_AuctionData[auctionId][a_startPrice]) return 0; // Ne mozes ponuditi manje od pocetne ponude
- if (price <= g_AuctionData[auctionId][a_offeredPrice]) return 0; // Ne mozes ponuditi manje od zadnje ponude
- if (playerid == g_AuctionData[auctionId][a_player]) return 0; // Ne mozes ponuditi 2 ponude za redom
- if (GetPlayerMoney(playerid) < price) return 0; // Nema dovoljno novca
- g_AuctionData[auctionId][a_player] = playerid;
- g_AuctionData[auctionId][a_offeredPrice] = price;
- new name[MAX_PLAYER_NAME];
- GetPlayerName(playerid, name, MAX_PLAYER_NAME);
- SendClientMessageToAll(-1, sprintf("Player %s offered %d$ on auction %d, you have %d seconds to put new offer",
- name, price, auctionId, g_AuctionData[auctionId][a_offerDelay] / 1000));
- KillTimer(g_AuctionData[auctionId][a_delayTimer]);
- g_AuctionData[auctionId][a_delayTimer] = SetTimerEx("AuctionDelay", g_AuctionData[auctionId][a_offerDelay], false, "i", auctionId);
- return 1;
- }
- // Handle timer
- forward AuctionDelay(id);
- public AuctionDelay(id)
- {
- if (g_AuctionData[id][a_player] == INVALID_PLAYER_ID) {
- printf("Auction (%d) has been closed, noone offered", id);
- return 0;
- }
- if (!IsPlayerConnected(g_AuctionData[id][a_player])) {
- printf("Auction (%d) has been closed, player who offered last offer has disconnected", id);
- return 0;
- }
- GivePlayerMoney(g_AuctionData[id][a_player], -g_AuctionData[id][a_offeredPrice]);
- g_AuctionData[id][a_used] = false;
- new buffer[128], name[MAX_PLAYER_NAME];
- GetPlayerName(g_AuctionData[id][a_player], name, MAX_PLAYER_NAME);
- format (buffer, sizeof buffer, "Player %s got auction %d for %d$", name, id, g_AuctionData[id][a_offeredPrice]);
- SendClientMessageToAll(-1, buffer);
- /*
- Das igracu sto je dobio, a njegov id je
- a_AuctionData[id][a_player]
- */
- return 1;
- }
- // Test
- CMD:createauction(playerid, arg[])
- {
- if (!CreateAuction(strval(arg))) {
- SendClientMessage(playerid, -1, "There's not free slots for new auction");
- }
- return 1;
- }
- CMD:offer(playerid, arg[])
- {
- new auction, offer;
- if (sscanf(arg, "ii", auction, offer)) return SendClientMessage(playerid, -1, "usage: /offer <auction> <offer>");
- OfferAuctionPrice(playerid, auction, offer);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement