Lottery System


  • Corleone Staff Forum Admin

    Good Afternoon,

    To get familiar with the new functions available, I've designed a simple lottery system. Players can buy a ticket by using the command 'buy ticket' and once it has started it will automatically generate a winner in the next 5 minutes.

    function onPlayerTouchsMe() {
        player.showmessage("To play the lottery say 'buy ticket'");
    }
    
    function onPlayerSays() {
            //If the players chat isn't 'buy ticket' let's ignore it and not continue with the script
        if (player.chat.toLowerCase() !== "buy ticket") return;
    
            //If the player does say 'buy ticket' and the array exists, lets see if the player has already purchased a ticket
        if (this.tickets_accounts !== null) {
    
                //If their ID appears in the list it means they have already purchased a ticket so we shouldn't add them to the list again
            if (this.tickets_accounts.includes(player.id)) {
                player.showmessage("You've already purchased a ticket.");
                return;
            }
    
                //If they don't appear in the list, let's add them to the list
            this.tickets_accounts.push(player.id);
        } else {
                //If the array doesn't exist, let's create it and also start the count down
            this.tickets_accounts = [player.id];
            this.tickets_timer = (60*5); //Draw it in five minutes, change this time if you would like to make it longer
        }
    
            //Make the player aware they've purchased a ticket
        player.showmessage("You've purchased a ticket " + player.id);
    
        //Deduct their money here
    
    
            //Copy the players outfit so they know they've purchased it
        this.setani("player_idle");
        this.sethead(player.head);
        this.setbody(player.body);
        this.sethat(player.hat);
    
            //Store the variables every time somebody buys a ticket
        this.save();  
    
            //Start the count down
        this.scheduleevent(10, "updatetext", "");
    }
    
        //This is the main function which displays the text and also controls the prize draw
    function onUpdateText() {
    
            //Cancel all other events so it only runs the function once every 10 seconds
        this.cancelevents("updatetext");
    
            //Deduct 10 seconds from the countdown timer as we scheduled to run this function every 10 seconds
        this.tickets_timer -= 10;
    
            //If the seconds are less than 0, we randomly select a winning ID
        if ( this.tickets_timer < = 0) {
    
                //Find a random ID from the accounts list we stored when the player buys a ticket
            let winner = this.tickets_accounts[Math.floor(Math.random() * this.tickets_accounts.length)];
            this.chat = "AND THE WINNER IS.... " + winner;
    
            //Add your items here
    
                //Make the animation look happy
            this.setani("player_happy");
    
                //Reset the variables
            this.tickets_accounts = null;
            this.tickets_timer = null;
    
                //Stop the function and no longer run any timeouts or events as it is now over
            return;
        }
    
            //If there is still time remaining, show how many entries and the time until it the draw
        let time = toTimeString(this.tickets_timer);
        this.chat = "Total Entries: " + this.tickets_accounts.length + ". Drawing in: " + time;
    
            //If it's made it this far, we still have time remaining so let's call this function again in 10 seconds
        this.scheduleevent(10, "updatetext", "");
    }
    
        //Wake up the script if necessary and start the timeout to display the message
    function onUpdated() {
        if (this.tickets_accounts !== null)
            this.scheduleevent(10, "updatetext", "");
    }
    
        //Convert the seconds to an hh:mm:ss format
    function toTimeString(seconds) {
      return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
    }
    

    image_784.png

    image_785.png



  • Great job! Hopefully this is added to the server 🙂



  • I really hope this is added. Would bring a great concept, especially if trades come along because this would help increase the games economy. 🤙🏼


  • Corleone Staff

    It's awesomeee


Log in to reply