Lotto

Newbie Spellweaver
Joined
Jul 24, 2008
Messages
22
Reaction score
3
create a new package at net/sf/odinms/server and name it 'lottery'
add a new class and paste this in it:
Code:
package net.sf.odinms.server.lottery;
import net.sf.odinms.client.MapleCharacter;
/**
 *
 * @author AssafMEC
 */
 public class LottoCharacter {
     private MapleCharacter player;
     private int bet;
     private int mesosBet;
     public LottoCharacter(MapleCharacter chr, int bet, int mesosBet) {
        this.player = chr;
        this.bet = bet;
        this.mesosBet = mesosBet;:glare:        
     }
     public MapleCharacter getPlayer() {
         return this.player;
     }
     public int getBet() {
         return this.bet;
     }
     public int getMesosBet() {
         return this.mesosBet;
     }
}
name the class 'LottoCharacter'

add a new class and paste this in it:
Code:
package net.sf.odinms.server.lottery;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import net.sf.odinms.client.*;
import net.sf.odinms.net.channel.*;
import net.sf.odinms.tools.Pair;
/**
 *
 * @author AssafMEC
 */
public class LottoGenerator {
    private final int winningNumber;
     private List<LottoCharacter> registeredPlayers;
    private int winningMesos;
    private int[] itemsId = new int[] {1122000};
    public LottoGenerator() {
        Random gen = new Random();
        this.winningNumber = gen.nextInt(9999)+1;
        this.registeredPlayers = new LinkedList<LottoCharacter>();
        this.winningMesos = 1000000;
        SideLottoSettings.startTime = System.currentTimeMillis();
        dropEveryoneMessage("[Lotto] A new lotto round has been started! the current start bet is " + this.winningMesos+" to buy a ticked talk to Muhamad!");
        dropGmMessage("[Lotto] the winning number is: " + winningNumber);
    }
    public int getWinningMesos() {
            return winningMesos;
    }
    public int getRegistrationNumbers() {
        return this.registeredPlayers.size();
    }
    private void dropEveryoneMessage(String message) {
      for (ChannelServer cserv :ChannelServer.getAllInstances()) {
        for (MapleCharacter chr :cserv.getPlayerStorage().getAllCharacters()) {
            chr.dropMessage(message);
        }
      }
    }
     private void dropGmMessage(String message) {
      for (ChannelServer cserv :ChannelServer.getAllInstances()) {
        for (MapleCharacter chr :cserv.getPlayerStorage().getAllCharacters()) {
            if (chr.getGMLevel() > 4)
                chr.dropMessage(message);
        }
      }
    }
    public int getWinningNumber() {
            return this.winningNumber;
    }
    private void addToWinningMeso(int bet) {
        this.winningMesos += bet;
    }
     private String fromMilliToTime(long mili) {
        String ret = "";
            long hours = mili / 1000 / 60 / 60;
            long mins = (mili - hours * 1000 * 60 * 60) / 1000 / 60;
            long secs = (mili - hours * 1000 * 60 * 60 - mins * 1000 * 60) / 1000;
            if (hours > 0)
            {
                ret += hours + " hour(s) ";
            }
            if (mins > 0)
            {
                ret += mins + " minute(s) and";
            }
            if (secs > 0)
            {
                ret += secs + " second(s) ";
            }
        return ret;
    }
    public void addPlayer(LottoCharacter chr) {
            this.registeredPlayers.add(chr);
             addToWinningMeso(chr.getMesosBet());
             chr.getPlayer().dropMessage("[Lotto] Succesfuly bought a ticket [CASH]! come back in "+fromMilliToTime(SideLottoSettings.UntillWeFinish())+"to check if you are the winner!");
    }
     public void finishCurrentLotto() {
         dropEveryoneMessage("[Lotto] The lotto round has been finished! the winning number is: " + this.winningNumber + ". if you are a winner you may claim your prize at Muhamad!");
         int winners = 0;
         int size =  registeredPlayers.size();
         for (int i = 0; i < size; i++) {
             if (registeredPlayers.get(i).getBet()== this.winningNumber) {
                 winners++;
             }
         }
         if (winners > 0)   {
             int mesosAward = this.winningMesos / winners;
             for (LottoCharacter lc : this.registeredPlayers) {
                    if (lc.getBet() == this.winningNumber) {
                            lc.getPlayer().dropMessage("[Lotto] Congrats! you won the lottery! go talk to Muhamad to claim your price!");
                            LottoWinnersHolder.winners.add(new Pair<Integer,MapleCharacter>(mesosAward, lc.getPlayer()));
                 }
             }
         }
    }
}
name the class 'LottoGenerator'

add a new class and paste this in it:
Code:
package net.sf.odinms.server.lottery;
import net.sf.odinms.client.MapleCharacter;
import java.util.List;
import net.sf.odinms.tools.Pair;
/**
 *
 * @author AssafMEC
 */
public class LottoWinnersHolder {
    public static List<Pair<Integer,MapleCharacter>> winners ;
}
name the class 'LottoWinnersHolder'

add a new class and paste this in it:
Code:
package net.sf.odinms.server.lottery;
/**
 *
 * @author AssafMEC
 */
public class SideLottoSettings {
    public static long startTime = 0;
    public static long lottoRound = 1 * 60 * 60 * 1000; // 1 hour
    public static long afterFinish = 2 * 60 * 1000; // 2 minutes
    public static int tickedId = 5210002;

    public static long getPastTime() {
        return System.currentTimeMillis() - startTime;
    }
    public static long UntillWeFinish() {
        return lottoRound - getPastTime();
    }
}
name the class 'SideLottoSettings'

now go to this location:
net/sf/odinms/net/channel

open 'ChannelServer'
and paste this somewhere:

Code:
private static LottoGenerator lotto = null;
public LottoGenerator getLotto() {
            return lotto;
        }
     
        private static void finishLotto() {
        TimerManager.getInstance().schedule(new Runnable() {
               public void run() {
                        lotto.finishCurrentLotto();
                         startLotto();
               }
           }, SideLottoSettings.lottoRound);
       }

    private static void startLotto() {
        lotto = null;
        TimerManager.getInstance().schedule(new Runnable() {
               public void run() {
                   if (lotto == null) {
                        lotto = new LottoGenerator();
                         finishLotto();
                   }
               }
           }, SideLottoSettings.afterFinish);
       }
now in the same class add this:
Code:
LottoWinnersHolder.winners = new LinkedList<net.sf.odinms.tools.Pair<Integer,MapleCharacter>>();
        startLotto();

before this:
Code:
Runtime.getRuntime().addShutdownHook(new Thread() {

now go to this location:
net/sf/odinms/scripting/npc
open up NPCConversationManager
and paste this :
Code:
private Pair<Integer,MapleCharacter> lottoWinner ;

    public boolean lottoIsOn() {
        return getClient().getChannelServer().getLotto() != null;
    }
    public boolean isLottoWinner() {
        for (Pair<Integer,MapleCharacter> pair : LottoWinnersHolder.winners) {
            if (pair.getRight() == c.getPlayer()) {
                lottoWinner = pair;
                return true;
            }
        }
            return false;
    }
    public String getLottoStatus() {
                
        LottoGenerator lotto = c.getChannelServer().getLotto();
        String ret = "The current mesos you can win: "+lotto.getWinningMesos()+"";
        ret += "\r\n"+lotto.getRegistrationNumbers()+" tickets have been already bought!";
        ret+= "\r\nWinners that havent claimed their prices:"+LottoWinnersHolder.winners.size();
        // time left to the next lottery round
        ret += "\r\nTime left until the round is over:\r\n#e"+fromMilliToTime(SideLottoSettings.UntillWeFinish())+"#n";
        return ret;
    }
    private String fromMilliToTime(long mili) {
        String ret = "";
            long hours = mili / 1000 / 60 / 60;
            long mins = (mili - hours * 1000 * 60 * 60) / 1000 / 60;
            long secs = (mili - hours * 1000 * 60 * 60 - mins * 1000 * 60) / 1000;
            if (hours > 0)
            {
                ret += hours + " hour(s)\r\n";
            }
            if (mins > 0)
            {
                ret += mins + " minute(s)\r\n";
            }
            if (secs > 0)
            {
                ret += secs + " second(s)\r\n";
            }
        return ret;
    }

    public void takeLotteryItems() {
        if (needToBeTaken()) {
            this.gainItem(SideLottoSettings.tickedId,(byte)-this.itemQuantity(SideLottoSettings.tickedId));
        }
    }
    public boolean needToBeTaken() {
        return this.itemQuantity(SideLottoSettings.tickedId) > 0;
    }
    public void buyLottoTicket(int number, int price) {
       MapleInventoryManipulator.addById( c, SideLottoSettings.tickedId, (byte)1,Integer.toString(number),-1);
       c.getPlayer().gainMeso(-price);
       LottoGenerator lotto = c.getChannelServer().getLotto();
       lotto.addPlayer(new LottoCharacter(c.getPlayer(), number, price));
    }
    public int getWinningAmount() {
         return lottoWinner.getLeft();
    }
    public void giveLottoPrize() {
        c.getPlayer().gainMeso(lottoWinner.getLeft());
        LottoWinnersHolder.winners.remove(lottoWinner);
        this.gainItem(SideLottoSettings.tickedId,(byte)-1);
    }
now open up script/npc
add this new npc (id = 2100001)
and paste inside this:
Code:
/*
Made by AssafMEC
*/
var status = 0;
var price = 1000000;
 var text = 0;
function start() {
      status = -1;
      action(1, 0, 0);
}
function action(mode, type, selection) {
        if (mode == -1) {
             cm.dispose();
        } else {
             if (mode == 0) {
                 cm.dispose();
                 return;
             }
              if (mode == 1)
                  status++;
             else
                  status--;

              if (status == 0) {
                  cm.sendNext("Hello, my name is Muhamad, and I'm incharge of all the lottery bussiness around this server!");
              } else if (status == 1) {
                  if (cm.lottoIsOn()) {
                  var message = "So, what do you want from me?\r\n#L0#I'd like to buy a ticket!#l\r\n#L1#I'd like to see the current lotto status!#l\r\n#L3#I have plany of unused tickets! take them away!#l";
                  if (cm.isLottoWinner()) {
                            message += "\r\n#b#e#L2#OMFG!> I think I won! the lottery!!!#k#n";
                  }
                   cm.sendSimple(message);
                   } else {
                       if (cm.isLottoWinner() ){
                            cm.sendSimple("#b#e#L2#OMFG!> I think I won! the lotto!!!#k#n");
                       } else {
                            if (cm.needToBeTaken()) {
                               cm.sendSimple("#L3#I have plany of unused tickets! take them away!#l") ;
                            } else {
                               cm.sendOk("The lotto is not working right now!");
                               cm.dispose();
                            }
                       }
                    }
              } else if (status == 2) {
                  if (selection == 0) {//buy
                        cm.sendYesNo("A lotto ticked cost "+price+" mesos! are you sure you want to buy?");
                  } else if (selection == 1) {//status
                            cm.sendNext(cm.getLottoStatus());
                            status = 0;
                  } else if (selection == 2) {//winner
                      cm.sendNext("let me see if your the winner!\r\n#B100# OMFG! A 100% match!!!\r\n#b#eYou are the great winner!#k#n");
                      status = 5;
                  } else if (selection == 3) {
                      if (cm.isLottoWinner()) {
                          cm.sendNext("You are the winner! got claim your prize!");
                      } else {
                          cm.takeLotteryItems();
                          cm.sendNext("I took everything! have a nice day!");
                        }
                        cm.dispose();
                  }
              } else if (status == 3) {
                    cm.sendGetText("Please enter your number! (must be between 1~9999)");
              } else if (status == 4) {
                 text = cm.getText();
                  if (text < 0 || text > 9999) {
                      cm.sendNext("The number you entered is not vaild!  (must be between 1~9999)")
                      status = 2;
                  } else {
                      cm.sendYesNo("Are you sure you want to buy a lotto ticket (" + text + ") for " + price);
                  }
              } else if (status == 5) {
                  if (cm.getMeso() >= price) {
                    cm.sendNext("Thank you! have a nice day!");
                    cm.buyLottoTicket(text,price);
                  } else {
                      cm.sendNext("You don't have enought mesos.");
                  }
                    cm.dispose();
              } else if (status == 6) {
                  cm.sendYesNo("You won the exact amount of " + cm.getWinningAmount() + "!!\r\n would you like to take it?");
              } else if (status == 7) {
                  cm.giveLottoPrize();
                  cm.dispose();
              }
       }
}

to change the diffrence between the draws go to SideLottoSettings and change nextRound to the value you want
 
Code:
 String ret = "The current mesos you can win: "+lotto.getWinningMesos()+"";
        ret += "\r\n"+lotto.getRegistrationNumbers()+" tickets have been already bought!";
        ret+= "\r\nWinners that havent claimed their prices:"+LottoWinnersHolder.winners.size();
        // time left to the next lottery round
        ret += "\r\nTime left until the round is over:\r\n#e"+fromMilliToTime(SideLottoSettings.UntillWeFinish())+"#n";
        return ret;

why are you using += ?
 
Code:
 String ret = "The current mesos you can win: "+lotto.getWinningMesos()+"";
        ret += "\r\n"+lotto.getRegistrationNumbers()+" tickets have been already bought!";
        ret+= "\r\nWinners that havent claimed their prices:"+LottoWinnersHolder.winners.size();
        // time left to the next lottery round
        ret += "\r\nTime left until the round is over:\r\n#e"+fromMilliToTime(SideLottoSettings.UntillWeFinish())+"#n";
        return ret;

why are you using += ?

That's obvious, not to make a huge line. :closedeyes:
 
That's obvious, not to make a huge line. :closedeyes:

I know... but why not?

Code:
   String ret = "The current mesos you can win: "+lotto.getWinningMesos()
        + "\r\n"+lotto.getRegistrationNumbers()+" tickets have been already bought!"
        +"\r\nWinners that havent claimed their prices:"+LottoWinnersHolder.winners.size()
        // time left to the next lottery round
        +"\r\nTime left until the round is over:\r\n#e"+fromMilliToTime(SideLottoSettings.UntillWeFinish())+"#n";
        return ret;

There you go a "shorter" line for you..
 
Back