Advertisement
Vendily

Mystery Gift Gen 2 RecordMixer

Aug 21st, 2023 (edited)
4,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.92 KB | None | 0 0
  1. if defined?(RecordMixer)
  2.   class Mystery_Gift_Gen2
  3.     MAX_GIFTS_DAILY = 5
  4.     MAX_PER_PERSON_DAILY = 1
  5.    
  6.     COMMON_GIFTS = [
  7.       [:CHESTOBERRY,:RAWSTBERRY],
  8.       [:ASPEARBERRY,:PECHABERRY],
  9.       [:GUARDSPEC,:XDEFENSE],
  10.       [:XATTACK,:PERSIMBERRY],
  11.       [:DIREHIT,:XSPATK],
  12.       [:XACCURACY,:GRASSMAIL],
  13.       [:FLAMEMAIL,:BUBBLEMAIL],
  14.       [:ORANBERRY,:CHERIBERRY]
  15.     ]
  16.     UNCOMMON_GIFTS = [
  17.       [:LUMBERRY,:SITRUSBERRY],
  18.       [:REVIVE,:GREATBALL],
  19.       [:SUPERREPEL,:MAXREPEL],
  20.       [:ELIXIR,:ETHER]
  21.     ]
  22.     RARE_GIFTS = [
  23.       :WATERSTONE,
  24.       :FIRESTONE,
  25.       :LEAFSTONE,
  26.       :THUNDERSTONE,
  27.       :MAXETHER,
  28.       :MAXELIXIR,
  29.       :MAXREVIVE,
  30.       :SCOPELENS
  31.     ]
  32.     VERY_RARE_GIFTS = [:HPUP,:PPUP]
  33.    
  34.     attr_reader :daily_timer
  35.     attr_reader :gifts_received_today
  36.     attr_reader :current_gift
  37.     attr_reader :current_error
  38.    
  39.     def initialize
  40.       @daily_timer = Time.now.to_i
  41.       @gifts_received_today = []
  42.       @current_error = 0
  43.     end
  44.    
  45.     def reset_daily_gifts
  46.       @current_gift = nil
  47.       days = 1
  48.       return if !@daily_timer
  49.       now = Time.now
  50.       elapsed = (now.to_i - @daily_timer) / 86_400
  51.       elapsed += 1 if (now.to_i - @daily_timer) % 86_400 > ((now.hour * 3600) + (now.min * 60) + now.sec)
  52.       return unless elapsed >= days
  53.       @daily_timer = now
  54.       @gifts_received_today = []
  55.     end
  56.    
  57.     def receive_gift(player_name,player_id,gift)
  58.       @current_gift = [player_name, gift]
  59.       @gifts_received_today.push(sprintf("%s-%d",player_name,player_id))
  60.     end
  61.    
  62.     def can_receive_gift?(player_name,player_id)
  63.       player_key = sprintf("%s-%d",player_name,player_id)
  64.       @current_error = 0
  65.       if @gifts_received_today.length>=MAX_GIFTS_DAILY
  66.         @current_error = 1
  67.         return false
  68.       elsif @gifts_received_today.select {|g| g == player_key}.length>=MAX_PER_PERSON_DAILY
  69.         @current_error = 2
  70.         return false
  71.       end
  72.       return true
  73.     end
  74.    
  75.     def self.generate_gift(id)
  76.       r = rand(256)
  77.       if r>=26
  78.         r = rand(8)
  79.         gift = COMMON_GIFTS[r][(id & (1 << r) >> r)]
  80.         return gift
  81.       end
  82.       r = rand(256)
  83.       if r>=51
  84.         r = rand(4)
  85.         bit = [15,8,9,10][r]
  86.         gift = UNCOMMON_GIFTS[r][(id & (1 << bit) >> bit)]
  87.         return gift
  88.       end
  89.       r = rand(256)
  90.       if r>=51
  91.         bit =
  92.         gift = RARE_GIFTS[(id & (0b111<< 12) >> 12)]
  93.         return gift
  94.       end
  95.       gift = VERY_RARE_GIFTS[(id & (1 << 15) >> 15)]
  96.       return gift || :GREATBALL
  97.     end
  98.   end
  99.  
  100.   class PokemonGlobalMetadata
  101.     def mystery_gift_gen2
  102.       @mystery_gift_gen2 = Mystery_Gift_Gen2.new unless @mystery_gift_gen2
  103.       return @mystery_gift_gen2
  104.     end
  105.   end
  106.  
  107.   RecordMixer.register(:mystery_gift_gen2,{
  108.     "name" => proc { _INTL("Record Mixer Bonus Item")},
  109.     "prepareData" => proc {
  110.         $PokemonGlobal.mystery_gift_gen2.reset_daily_gifts
  111.         if !$PokemonGlobal.pcItemStorage
  112.           $PokemonGlobal.pcItemStorage = PCItemStorage.new
  113.         end
  114.      },
  115.     "writeData" => proc {|writer|
  116.       player_var = ($player.nil?) ? $Trainer : $player
  117.       writer.str(player_var.name)
  118.       writer.int(player_var.id)
  119.     },
  120.     "parseData" => proc {|record|
  121.       player_name = record.str
  122.       player_id = record.int
  123.       gift = nil
  124.       if $PokemonGlobal.mystery_gift_gen2.can_receive_gift?(player_name,player_id)
  125.         gift = Mystery_Gift_Gen2.generate_gift(player_id)
  126.       end
  127.       $PokemonGlobal.mystery_gift_gen2.receive_gift(player_name,player_id,gift)
  128.     },
  129.     "finalizeData" => proc {
  130.       player_name,gift = $PokemonGlobal.mystery_gift_gen2.current_gift
  131.       case $PokemonGlobal.mystery_gift_gen2.current_error
  132.       when 1
  133.         Kernel.pbMessage(_INTL("Sorry, only {1} gifts a day.\\1",Mystery_Gift_Gen2::MAX_GIFTS_DAILY))
  134.       when 2
  135.         Kernel.pbMessage(_INTL("Sorry, you got all the gifts from {1} for today.\\1",player_name))
  136.       else
  137.         if defined?(GameData)
  138.           gift_name = GameData::Item.get(gift).name
  139.         else
  140.           gift = getID(PBItems,gift)
  141.           gift_name = PBItems.getName(gift)
  142.         end
  143.         Kernel.pbMessage(_INTL("{1} sent {2}.\\1",player_name,gift_name))
  144.         if $PokemonGlobal.pcItemStorage.respond_to?(:can_add?)
  145.           if $PokemonGlobal.pcItemStorage.can_add?(gift)
  146.             $PokemonGlobal.pcItemStorage.add(gift)
  147.             Kernel.pbMessage(_INTL("It was sent to your PC.\\1"))
  148.           else
  149.             Kernel.pbMessage(_INTL("But you have no room for it...\\1"))
  150.           end
  151.         else
  152.           if $PokemonGlobal.pcItemStorage.pbCanStore?(gift)
  153.             $PokemonGlobal.pcItemStorage.pbStoreItem(gift)
  154.             Kernel.pbMessage(_INTL("It was sent to your PC.\\1"))
  155.           else
  156.             Kernel.pbMessage(_INTL("But you have no room for it...\\1"))
  157.           end
  158.         end
  159.       end
  160.     }
  161.   })
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement