Guest User

Untitled

a guest
Aug 28th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 22.13 KB | None | 0 0
  1. ################################################################################
  2. #-------------------------------------------------------------------------------
  3. #Author: Alexandre
  4. #Handles Connection, Registration and Login
  5. #Modified by Deukhoofd for Insurgence
  6. #-------------------------------------------------------------------------------
  7. ################################################################################
  8. class Connect
  9.     #Initialization
  10.     def initialize(sbase=false)
  11.         @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  12.         @overlay=SpriteWrapper.new(@viewport)
  13.         @overlay.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  14.         @viewport.z = 99999
  15.         @username = ""
  16.         @password = ""
  17.         @email = ""
  18.         @base = sbase
  19.         @idle = true
  20.     GTSHandler.Connect = self
  21.     end
  22.     def main
  23.         Graphics.transition
  24.         Kernel.pbMessage("Connecting to server... (Press C)")
  25.         version = getversion()
  26.         begin
  27.             $network = DeukNetwork.new
  28.             $network.open
  29.             $network.send("<CON\tversion=#{version}>")
  30.         rescue
  31.             Kernel.pbMessage("Server is not online or your internet connection has a problem.")
  32.             $scene = Scene_Map.new
  33.             Graphics.freeze
  34.             $network.close
  35.             @viewport.dispose
  36.         end
  37.         loop do
  38.           break if $scene != self
  39.           update
  40.           if Input.trigger?(Input::B)
  41.             $network.send("<DSC>")
  42.             $scene=Scene_Map.new
  43.             break
  44.           end
  45.         end
  46.         Graphics.freeze
  47.         @viewport.dispose
  48.     end
  49.     def update
  50.         message = $network.listen
  51.         handle(message)
  52.         @viewport.update
  53.         @overlay.update
  54.         Graphics.update
  55.         Input.update
  56.     end
  57.     def handle(message)
  58.         Input.trigger?(Input::B)
  59.         if message.kind_of?(Array)
  60.             message = message[0]
  61.         end
  62.         case message
  63.             when /<CON result=(.*)>/ then check_connection($1.to_i)
  64.             when /<DSC>/ then disconnect()
  65.             when /<DSC reason=(.*)>/ then disconnect($1.to_s)
  66.             when /<REG result=(.*)>/ then check_registration($1.to_i)
  67.             when /<LOG result=(.*)>/ then check_login($1.to_i)
  68.             when /<VBASE user=(.*) result=(.*) base=(.*)>/ then check_basevisit($1.to_s,$2.to_i,$3.to_s)
  69.             when /<UBASE result=(.*)>/ then check_baseupload($1.to_i)
  70.             when /<TRA user=(.*) result=(.*)>/ then check_trade($1.to_s,$2.to_i)
  71.             when /<BAT user=(.*) result=(.*) trainer=(.*)>/ then check_battle($1.to_s,$2.to_i,$3)
  72.             when "" then nil
  73.        
  74.             #These handle Random Battle messages
  75.             when /<RANTIER tier=(.*)>/ then rndbattle_tier($1.to_s)
  76.             when /<RANDBAT user=(.*)>/ then rndbattle_check($1.to_s)
  77.             when /<RAND INC>/ then randbattlenotallowed
  78.            
  79.             when /<GLOBAL message=(.*)>/ then Kernel.pbMessage("#{$1.to_s}")
  80.  
  81.             #this handles GTS messages
  82.             when /<GTSCREATE result=(.*) index=(.*)>/ then GTSHandler.checkgtscreate($1.to_i, $2.to_i)
  83.             when /<GTSREQUEST trades=(.*)>/ then GTSHandler.displaytrades($1.to_s, false)
  84.       when /<GTSMINE trades=(.*)>/ then GTSHandler.displaytrades($1.to_s, true)
  85.       when /<GTSOFFER result=(.*) pkmn=(.*)>/ then GTSHandler.gts_offer_response($1.to_i, $2.to_s)
  86.       when /<GTSCANCEL result=(.*) pkmn=(.*)>/ then GTSHandler.gts_cancel_response($1.to_i, $2.to_s)
  87.       when /<GTSCOLLECT result=(.*) pkmn=(.*)>/ then GTSHandler.gts_collect_response($1.to_i, $2.to_s)
  88.        
  89.       when /<WTRESULT result=(.*) user=(.*) pkmn=(.*)>/ then wtresult($1.to_i, $2.to_s, $3.to_s)
  90.      
  91.             when /<PNG>/ then nil
  92.         end
  93.     end
  94.     def check_connection(result)
  95.         if result == 0
  96.             Kernel.pbMessage("Your version is outdated, please download the latest version of the game")
  97.             $scene = Scene_Map.new
  98.         elsif result == 1
  99.             Kernel.pbMessage("The server is full, please try again later.")
  100.             $scene = Scene_Map.new
  101.         else
  102.             Kernel.pbMessage("Connection Successful.")
  103.             registerorlogin
  104.         end
  105.     end
  106.     def registerorlogin
  107.         commands=[_INTL("Login"),_INTL("Register"),_INTL("Cancel")]
  108.         choice=Kernel.pbMessage(_INTL("What do you want to do?"),commands)
  109.         if choice==0
  110.             attempt_login
  111.         elsif choice==1
  112.             attempt_register
  113.         elsif choice==2
  114.             $network.send("<DSC>")
  115.             $scene=Scene_Map.new
  116.         end
  117.     end
  118.     def disconnect(reason)
  119.         Kernel.pbMessage("You have been disconnected: #{reason}")
  120.         $scene=Scene_Map.new
  121.     end
  122.     def attempt_register
  123.         Kernel.pbMessage("Please enter a username.")
  124.         loop do
  125.             @username = Kernel.pbMessageFreeText(_INTL("Username?"),"",false,32)
  126.             break if @username==""
  127.             if @username != ""
  128.                 Kernel.pbMessage("Please re-enter your username.")
  129.        
  130.                 username = Kernel.pbMessageFreeText(_INTL("Username?"),"",false,32)
  131.                 break if @username == username
  132.                 Kernel.pbMessage("The username you entered does not match, please try again.")
  133.             end
  134.         end
  135.         if @username==""
  136.             registerorlogin
  137.         else
  138.             Kernel.pbMessage("Please enter a password.")
  139.             loop do
  140.                 @password = Kernel.pbMessageFreeText(_INTL("Password?"),"",true,32)
  141.                 if @password != ""
  142.                     Kernel.pbMessage("Please re-enter your password.")
  143.                     password = Kernel.pbMessageFreeText(_INTL("Password?"),"",true,32)
  144.                     break if @password == password
  145.                     Kernel.pbMessage("The password you entered does not match, please try again.")
  146.                 end
  147.                 break if @password == ""
  148.             end
  149.             if @password==""
  150.                 registerorlogin
  151.             else
  152.                 @email=($Trainer.party[0].trainerID*100000+rand(100000)).to_s
  153.                 $network.send("<REG\tuser=#{@username}\tpass=#{encrypt_password(@password)}\temail=#{@email}>")
  154.             end
  155.         end
  156.     end
  157.     def encrypt_password(password)
  158.         encrypted = password.crypt("XS")
  159.         return encrypted[2, encrypted.size - 2]
  160.     end
  161.     def check_registration(result)
  162.         if result == 0
  163.             Kernel.pbMessage("The username is already taken, please try a different username.")
  164.             attempt_register
  165.         elsif result == 1
  166.             Kernel.pbMessage("The email you entered has already been used to register an account, you can only have one acount per email.")
  167.             attempt_register
  168.         elsif result == 2
  169.             Kernel.pbMessage("Registration was successful!")
  170.             registerorlogin
  171.         end
  172.     end
  173.     def attempt_login
  174.         Kernel.pbMessage("Please enter your username.")
  175.         tempuser=""
  176.         tempuser=$game_variables[109] if $game_variables[109] != 0
  177.         @username = Kernel.pbMessageFreeText(_INTL("Username?"),tempuser,false,32)
  178.         if @username==""
  179.             registerorlogin
  180.         else
  181.             temppass=""
  182.             temppass=$game_variables[110] if $game_variables[110] != 0
  183.  
  184.             Kernel.pbMessage("Please enter your password.")
  185.             @password = Kernel.pbMessageFreeText(_INTL("Password?"),temppass,true,32)
  186.             if @password==""
  187.                 registerorlogin
  188.             else
  189.                 Kernel.pbMessage("Logging in... (Press C)")  
  190.                 $network.send("<LOG\tuser=#{@username}\tpass=#{encrypt_password(@password)}>")
  191.             end    
  192.         end
  193.     end
  194.     def check_login(result)
  195.         if result == 0
  196.             Kernel.pbMessage("The username entered does not exist.")
  197.             registerorlogin
  198.         elsif result == 1
  199.             Kernel.pbMessage("The password entered is incorrect.")
  200.             registerorlogin
  201.         elsif result == 2
  202.             Kernel.pbMessage("This account has been banned.")
  203.             registerorlogin
  204.         elsif result == 3
  205.             Kernel.pbMessage("Your IP has been banned.")
  206.             $Scene=Scene_Map.new
  207.         elsif result == 4
  208.             Kernel.pbMessage("Login was successful!")
  209.             $network.loggedin=true
  210.             $network.username = @username
  211.             $game_variables[109]=@username
  212.             $game_variables[110]=@password
  213.             tradeorbattle
  214.         end
  215.     end
  216.     def tradeorbattle
  217.         if @base
  218.             commands=[_INTL("Visit Base"),_INTL("Upload Base"),_INTL("Cancel")]
  219.             choice=Kernel.pbMessage(_INTL("What do you want to do?"),commands)
  220.             if choice==0
  221.                 visitbase
  222.             elsif choice==1
  223.                 uploadbase
  224.             elsif choice==2
  225.                 $network.send("<DSC>")
  226.                 $scene=Scene_Map.new
  227.             end
  228.         else
  229.             if $game_switches[406]
  230.                 $Trainer.party=$game_variables[124]
  231.                 $game_switches[406]=false
  232.             end
  233.    
  234.             commands=[_INTL("Trade"),_INTL("Battle"), _INTL("Random Battle"),
  235.             _INTL("GTS"), _INTL("Wonder Trade"), _INTL("Cancel")]
  236.             choice=Kernel.pbMessage(_INTL("What do you want to do?"),commands)
  237.             if choice==0
  238.                 trade
  239.             elsif choice==1
  240.                 battle
  241.             elsif choice==2
  242.                 randombattle
  243.             elsif choice==3
  244.                 GTSHandler.handlegts
  245.       elsif choice==4
  246.         wtsend
  247.             else
  248.                 $network.send("<DSC>")
  249.                 $scene=Scene_Map.new
  250.             end
  251.         end
  252.     end
  253.     def visitbase
  254.         loop do
  255.             @player = Kernel.pbMessageFreeText(_INTL("Whose base would you like to visit?"),"",false,32)
  256.             Kernel.pbMessage("You cannot visit your own base.") if @player.downcase == $network.username.downcase
  257.             return tradeorbattle if @player == "" || @player.downcase == $network.username.downcase
  258.             break if @player !="" || @player !=$network.username      
  259.         end
  260.         $game_variables[5]=@player
  261.         $network.send("<VBASE\tuser=#{@player}>")    
  262.     end
  263.     def uploadbase
  264.         loop do
  265.             truefalse = Kernel.pbConfirmMessage(_INTL("Would you like to save your base and upload it?"))
  266.             if truefalse
  267.                 basevar = compileSecretBase
  268.                 $network.send("<UBASE\tuser=#{@player}\tbase=#{basevar}>")
  269.                 break
  270.             end
  271.         end
  272.     end
  273.  
  274.     def compileSecretBase
  275.         baseStr = ""
  276.         baseStr += $game_variables[81].to_s
  277.         baseStr += "g"
  278.         for i in $game_variables[76]
  279.             if i != nil && i[1] != nil
  280.                 tempStr = ""
  281.                 tempStr+=i[1].to_s
  282.                 tempStr+= "n"
  283.                 tempStr+=i[3].to_s
  284.                 tempStr+= "n"
  285.                 tempStr+=i[4].to_s
  286.                 tempStr+= "n"
  287.                 tempStr+= "f"
  288.                 baseStr+= tempStr
  289.             end
  290.         end
  291.         baseStr+= "g"
  292.         for i in $game_variables[77]
  293.             baseStr+= i.to_s
  294.             baseStr+= "f"
  295.         end
  296.         baseStr+="g"
  297.         return baseStr
  298.     end
  299.     def decompileSecretBase(string)
  300.         baseString=[]
  301.         string.each_line("g") {|s|
  302.             baseString.push(s)
  303.         }
  304.         $game_variables[85]=baseString[0].chomp("g").to_i
  305.    
  306.         $game_variables[84] = Array.new
  307.         eventsString=[]
  308.         baseString[1].each_line("f") {|s|
  309.             eventsString.push(s)
  310.             eventsString[eventsString.length-1]=Array.new
  311.         }
  312.         for i in eventsString
  313.             i = Array.new
  314.         end
  315.         eventNo=0
  316.         baseString[1].each_line("f") {|s|
  317.             s=s.chomp("f")
  318.             eventPart=1
  319.             s.each_line("n") {|s2|
  320.                 s2=s2.chomp("n").to_i
  321.                 eventsString[eventNo][quickRenderInt(eventPart)]=s2
  322.                 eventPart += 1
  323.             }
  324.             eventNo += 1
  325.         }
  326.         $game_variables[84]=eventsString
  327.         $game_variables[86]=Array.new
  328.         baseString[2].each_line("f") {|s|
  329.             s = s.chomp("f")
  330.             $game_variables[86].push(s.to_i)
  331.         }
  332.     end
  333.     def quickRenderInt(int)
  334.         return 0 if int == 0
  335.         int += 1
  336.         return int
  337.     end
  338.     def trade
  339.         loop do
  340.             @player = Kernel.pbMessageFreeText(_INTL("Who would you like to trade with?"),"",false,32)
  341.             Kernel.pbMessage("You cannot trade with yourself.") if @player == $network.username
  342.             return tradeorbattle if @player == "" || @player == $network.username
  343.             break if @player !="" || @player !=$network.username
  344.         end
  345.         $network.send("<TRA\tuser=#{@player}>")
  346.     end
  347.     def check_basevisit(player,result,basestring)
  348.         if result == 0
  349.             Kernel.pbMessage(_INTL("The user #{player} does not exist."))
  350.             tradeorbattle
  351.         elsif result == 1
  352.             Kernel.pbMessage(_INTL("The user #{player} has been banned."))
  353.             tradeorbattle
  354.         elsif result == 2
  355.             if basestring != nil && basestring != ""
  356.                 $scene=Scene_Map.new
  357.                 player=$game_variables[5]
  358.                 Kernel.pbMessage(_INTL("The door to #{player}'s base has opened!"))
  359.                 $game_variables[87]=player
  360.    
  361.                 decompileSecretBase(basestring)
  362.                 pbCommonEvent(7)
  363.             else
  364.                 Kernel.pbMessage("The user #{player} doesn't exist or hasn't created a base.")
  365.                 tradeorbattle
  366.             end
  367.         end
  368.     end
  369.     def check_baseupload(result)
  370.         if result == 0
  371.             Kernel.pbMessage(_INTL("The base did not upload successfully."))
  372.             tradeorbattle
  373.         elsif result == 1
  374.             Kernel.pbMessage(_INTL("The base was uploaded successfully!"))
  375.             tradeorbattle
  376.         end
  377.     end
  378.     def check_trade(player,result)
  379.         if result == 0
  380.             Kernel.pbMessage(_INTL("The user #{player} does not exist."))
  381.             tradeorbattle
  382.         elsif result == 1
  383.             Kernel.pbMessage(_INTL("The user #{player} has been banned."))
  384.             tradeorbattle
  385.         elsif result == 2
  386.             Kernel.pbMessage(_INTL("The user #{player} is not online."))
  387.             tradeorbattle
  388.         elsif result == 3
  389.             Kernel.pbMessage(_INTL("The user #{player} has declined or did not respond your trade request."))
  390.             tradeorbattle
  391.         elsif result == 4
  392.             Kernel.pbMessage(_INTL("The user #{player} has accepted your trade request."))
  393.             $scene = Scene_Trade.new(player)
  394.         end
  395.     end
  396.  
  397.  
  398.     def battle
  399.         if !$Trainer.party[0] || $Trainer.party[0].egg? || $Trainer.party[0].hp<1
  400.             Kernel.pbMessage("You need to be able to use the first Pokemon in your party.")
  401.             return tradeorbattle
  402.             break
  403.         end
  404.    
  405.         for i in 0..$Trainer.party.length-1
  406.             $Trainer.party[i].heal
  407.         end
  408.         $game_variables[124]=Array.new
  409.         for i in 0..$Trainer.party.length-1
  410.             $game_variables[124][i]=$Trainer.party[i].clone
  411.         end
  412.         $game_switches[371]=true    
  413.         $game_switches[406]=true
  414.         for i in $Trainer.party
  415.             i.level=50
  416.             i.calcStats
  417.         end
  418.    
  419.         partyTemp=[]
  420.         for poke in $Trainer.party
  421.             partyTemp.push(Marshal.dump(poke))
  422.         end
  423.         mons=MakeTeamString
  424.         trainerAry=[$Trainer.name,
  425.         $Trainer.id,
  426.         $Trainer.trainertype,
  427.         $Trainer.megaforme,
  428.         $Trainer.clothes.join("s"),
  429.         mons]
  430.         serialized=trainerAry.join("/g/")
  431.         loop do
  432.             @player = Kernel.pbMessageFreeText(_INTL("Who would you like to battle with?"),"",false,32)
  433.             Kernel.pbMessage("You cannot battle with yourself.") if @player == $network.username
  434.             return tradeorbattle if @player == "" || @player == $network.username
  435.             break
  436.         end
  437.         $network.send("<BAT\tuser=#{@player}\ttrainer=#{serialized}>")
  438.     end
  439.     def MakeTeamString
  440.         pokemonArray=[]
  441.         for poke in $Trainer.party
  442.             poke.abilityflag="nil" if !poke.abilityflag
  443.             if !poke.isShiny?
  444.                 shininess=0
  445.             else
  446.                 shininess=1
  447.             end
  448.             varArray=[poke.species,
  449.                 50,
  450.                 poke.iv[0],
  451.                 poke.iv[1],
  452.                 poke.iv[2],
  453.                 poke.iv[3],
  454.                 poke.iv[4],
  455.                 poke.iv[5],
  456.                 poke.ev[0],
  457.                 poke.ev[1],
  458.                 poke.ev[2],
  459.                 poke.ev[3],
  460.                 poke.ev[4],
  461.                 poke.ev[5],
  462.                 poke.personalID,
  463.                 poke.trainerID,
  464.                 poke.item,
  465.                 poke.name,
  466.                 poke.exp,
  467.                 poke.happiness,
  468.                 poke.moves[0].id,
  469.                 poke.moves[0].pp,
  470.                 poke.moves[1].id,
  471.                 poke.moves[1].pp,
  472.                 poke.moves[2].id,
  473.                 poke.moves[2].pp,
  474.                 poke.moves[3].id,
  475.                 poke.moves[3].pp,
  476.                 poke.form,
  477.                 poke.nature,
  478.                 poke.totalhp,
  479.                 poke.attack,
  480.                 poke.defense,
  481.                 poke.spatk,
  482.                 poke.spdef,
  483.                 poke.speed,
  484.                 poke.ballused,
  485.                 poke.ot,
  486.                 shininess,
  487.                 poke.abilityflag
  488.             ]
  489.             for var in pokemonArray
  490.                 var=var.to_s
  491.             end
  492.             pokemonArray.push(varArray.join("^%*"))
  493.         end
  494.         mons=pokemonArray.join("/u/")
  495.         return mons
  496.     end
  497.  
  498.     def check_battle(player,result,opponent)
  499.         if result == 0
  500.             Kernel.pbMessage(_INTL("The user #{player} does not exist."))
  501.             tradeorbattle
  502.         elsif result == 1
  503.             Kernel.pbMessage(_INTL("The user #{player} has been banned."))
  504.             tradeorbattle
  505.         elsif result == 2
  506.             Kernel.pbMessage(_INTL("The user #{player} is not online."))
  507.             tradeorbattle
  508.         elsif result == 3
  509.             Kernel.pbMessage(_INTL("The user #{player} has declined or did not respond your battle request."))
  510.             tradeorbattle
  511.         elsif result == 4
  512.             Kernel.pbMessage(_INTL("The user #{player} has accepted your battle request."))
  513.             Kernel.pbMessage(_INTL("Do not click outside the window during the battle!"))
  514.  
  515.             unpacked=opponent.split("/g/")
  516.             unpacked[4]=unpacked[4].split("s")
  517.             unpacked[5]=unpacked[5].split("/u/")
  518.  
  519.             pokeAry=[]
  520.             for prePoke in unpacked[5]
  521.                 longarray=prePoke.split("^%*")
  522.                 mon=PokeBattle_Pokemon.new(longarray[0].to_i,50)
  523.                 thing=0
  524.                 for v in 2..7
  525.                     mon.iv[thing]=longarray[v].to_i
  526.                     thing+=1
  527.                 end
  528.                 thing=0
  529.                 for v in 8..13
  530.                     mon.ev[thing]=longarray[v].to_i
  531.                     thing+=1
  532.                 end
  533.          
  534.                 mon.personalID=longarray[14].to_i
  535.                 mon.trainerID=longarray[15].to_i
  536.                 mon.item=longarray[16].to_i
  537.                 mon.name=longarray[17]
  538.                 mon.exp=longarray[18].to_i
  539.                 mon.happiness[19].to_i
  540.                 for i in 0..3
  541.                     mon.moves[i]=PBMove.new(longarray[20+(i*2)].to_i)
  542.                     mon.moves[i].pp=longarray[21+(i*2)].to_i
  543.                 end
  544.                 mon.form=longarray[28].to_i
  545.                 mon.setNature(longarray[29].to_i)
  546.  
  547.                 mon.totalhp=longarray[30].to_i
  548.                 mon.attack=longarray[31].to_i
  549.                 mon.defense=longarray[32].to_i
  550.                 mon.spatk=longarray[33].to_i
  551.                 mon.spdef=longarray[34].to_i
  552.                 mon.speed=longarray[35].to_i
  553.  
  554.                 mon. ballused=longarray[36].to_i
  555.                 mon.ot=longarray[37]
  556.  
  557.                 if longarray[38].to_i==0 && mon.isShiny?
  558.                     mon.makeNotShiny
  559.                 elsif !mon.isShiny?
  560.                     mon.makeShiny
  561.                 end
  562.        
  563.                 if longarray[38]!="nil"
  564.                     mon.abilityflag=longarray[38].to_i
  565.                 end
  566.                 pokeAry.push(mon)
  567.             end
  568.  
  569.             deserialized=PokeBattle_Trainer.new(unpacked[0],unpacked[2])
  570.             deserialized.id=unpacked[1]
  571.             deserialized.megaforme=unpacked[3]
  572.             deserialized.clothes=unpacked[4]
  573.             deserialized.party=pokeAry
  574.             return start_battle(deserialized)
  575.         end
  576.     end
  577.  
  578.     def start_battle(opponent)
  579.         scene=pbNewBattleScene      
  580.      
  581.         battle=PokeBattle_OnlineBattle.new(scene,$Trainer.party,opponent.party,$Trainer,opponent)
  582.         fullparty1 = true ? $Trainer.party.length == 6 : false
  583.         fullparty2 = true ? opponent.party.length == 6 : false
  584.         battle.fullparty1=fullparty1
  585.         battle.fullparty2=fullparty2
  586.         battle.endspeech=""
  587.         battle.internalbattle=false
  588.         $game_switches[393]=true
  589.         pbPrepareBattle(battle)
  590.         restorebgm=true
  591.         decision=0
  592.         decision=battle.pbStartBattle(true)
  593.  
  594.         $Trainer.party=$game_variables[124]
  595.         $game_switches[406]=false
  596.         $game_switches[371]=false
  597.         for i in 0..$Trainer.party.length-1
  598.             $Trainer.party[i].heal
  599.         end
  600.         $game_switches[393]=false
  601.         if decision==1
  602.             Kernel.pbMessage("You won the battle.")
  603.         else
  604.             Kernel.pbMessage("You lost the battle.")
  605.         end
  606.         tradeorbattle
  607.     end
  608.  
  609.     def randombattle
  610.         fullarray=[]
  611.         speciesArray=[]
  612.         itemsArray=[]
  613.         for poke in $Trainer.party
  614.             pokeform=poke.form
  615.             if poke.species==384
  616.                 for i in 0..3
  617.                     if poke.moves[i] && poke.moves[i].id==PBMoves::DRAGONSASCENT
  618.                         pokeform=1
  619.                     end
  620.                 end
  621.             end
  622.             species="#{poke.species}_#{pokeform}"
  623.             speciesArray.push(species)
  624.         end
  625.         speciesstring=speciesArray.join("^")
  626.         fullarray.push(speciesstring)
  627.         for poke in $Trainer.party
  628.             helditem = poke.item
  629.             itemsArray.push(helditem)
  630.         end
  631.         itemsstring=itemsArray.join("^")
  632.         fullarray.push(itemsstring)
  633.         fullstring = fullarray.join("/")
  634.         $network.send("<RAND\tbattle\tspecies=#{fullstring}>")
  635.     end
  636.  
  637.     def rndbattle_tier(tier)
  638.         commands=[_INTL("No Tier"),_INTL("Uber"),_INTL("OU"),_INTL("BL"),_INTL("UU"),_INTL("RU"),_INTL("NU"),_INTL("Cancel")]
  639.         choice=Kernel.pbMessage(_INTL("The lowest tier you can battle in is #{tier}, what tier do you want to use?"),commands)
  640.         if choice == 0
  641.             $network.send("<RANBAT\ttier=notier>")
  642.         elsif choice == 1
  643.             $network.send("<RANBAT\ttier=Uber>")
  644.         elsif choice == 2
  645.             $network.send("<RANBAT\ttier=OU>")
  646.         elsif choice == 3
  647.             $network.send("<RANBAT\ttier=BL>")
  648.         elsif choice == 4
  649.             $network.send("<RANBAT\ttier=UU>")
  650.         elsif choice == 5
  651.             $network.send("<RANBAT\ttier=RU>")
  652.         elsif choice == 6
  653.             $network.send("<RANBAT\ttier=NU>")
  654.         else
  655.             $network.send("<RANBAT\tcancel>")
  656.             return tradeorbattle
  657.         end
  658.     end
  659.  
  660.     def randbattlenotallowed
  661.         Kernel.pbMessage("That tier is beneath you.")
  662.         return tradeorbattle
  663.     end
  664.  
  665.     def rndbattle_check(player)
  666.         commands=[_INTL("Accept"),_INTL("Decline")]
  667.         choice=Kernel.pbMessage(_INTL("User #{player} sent you a random battle request, What do you want to do?"),commands)
  668.         if choice == 0
  669.             if !$Trainer.party[0] || $Trainer.party[0].egg? || $Trainer.party[0].hp<1
  670.                 Kernel.pbMessage("You need to be able to use the first Pokemon in your party.")
  671.                 return tradeorbattle
  672.                 break
  673.             end
  674.    
  675.             for i in 0..$Trainer.party.length-1
  676.                 $Trainer.party[i].heal
  677.             end
  678.             $game_variables[124]=Array.new
  679.             for i in 0..$Trainer.party.length-1
  680.                 $game_variables[124][i]=$Trainer.party[i].clone
  681.             end
  682.             $game_switches[371]=true    
  683.             $game_switches[406]=true
  684.             for i in $Trainer.party
  685.                 i.level=50
  686.                 i.calcStats
  687.             end
  688.    
  689.             partyTemp=[]
  690.             for poke in $Trainer.party
  691.                 partyTemp.push(Marshal.dump(poke))
  692.             end    
  693.        
  694.             mons=MakeTeamString
  695.             trainerAry=[$Trainer.name,
  696.                 $Trainer.id,
  697.                 $Trainer.trainertype,
  698.                 $Trainer.megaforme,
  699.                 $Trainer.clothes.join("s"),
  700.                 mons
  701.             ]
  702.    
  703.             serialized=trainerAry.join("/g/")
  704.             $network.send("<BAT\tuser=#{player}\ttrainer=#{serialized}>")
  705.         elsif choice == 1
  706.             $network.send("<RANBAT\tdecline\tuser=#{player}>")
  707.             return tradeorbattle
  708.         end
  709.     end
  710.  
  711.     def ranbattle_denied(user)
  712.         Kernel.pbMessage("#{user} has denied your random battle request")
  713.         $network.send("<RANBAT\tcancel>")
  714.         tradeorbattle
  715.     end
  716.  
  717.     def getversion
  718.         _version = "3.0"
  719.         _version = "6.84" if $DEBUG
  720.         return _version
  721.     end
  722.  
  723.   @WTIndex = 0
  724.   def wtsend
  725.     #TODO: select pokemon index with menu
  726.     #Set this index to @WTIndex
  727.     @WTIndex = 0
  728.     pokestring = [JSON.encode($Trainer.party[@WTIndex.to_i])].pack("m")
  729.     $network.send("<WTCREATE\tpkmn=#{pokestring}>")
  730.   end
  731.  
  732.   def wtresult(result, user, pkmn)
  733.     if result == 0
  734.       Kernel.pbMessage("You have been banned from the online server")
  735.       return tradeorbattle
  736.     elsif result == 1
  737.       Kernel.pbMessage("Your wonder trade has timed out")
  738.       return tradeorbattle
  739.     elsif result != 2
  740.       return tradeorbattle
  741.     end
  742.     battler = objecttobattler(JSON.decode(pkmn.unpack("m")[0]))
  743.     Kernel.pbMessage("You received a #{battler} from #{user}!")
  744.     #TODO: replace pokemon at @WTIndex with battler
  745.     #TODO: save
  746.   end
  747. end
  748.  
  749. class GTSRequestHolder
  750.   attr_accessor :MinLevel
  751.     @MinLevel = 1
  752. end
  753.  
  754. class DeukNetwork
  755.     attr_accessor :loggedin
  756.     attr_accessor :socket
  757.     attr_accessor :username
  758.  
  759.     def initialize
  760.         @loggedin=false
  761.         @socket = nil
  762.         @username = ""
  763.     end
  764.     def open
  765.         @socket=TCPSocket.new("127.0.0.1", 6420)
  766.     end
  767.     def close
  768.         @loggedin=false
  769.         @socket.send("<DSC>") if @socket != nil
  770.         @socket.close if @socket != nil
  771.     end
  772.     def listen
  773.         return "" if !@socket.ready?
  774.         buffer = @socket.recv(0xFFFF)
  775.         buffer = buffer.split("\n", -1)
  776.         if @previous_chunk != nil
  777.             buffer[0] = @previous_chunk + buffer[0]
  778.             @previous_chunk = nil
  779.         end
  780.         last_chunk = buffer.pop
  781.         @previous_chunk = last_chunk if last_chunk != ''
  782.         buffer.each {|message|
  783.             case message
  784.                 when /<PNG>/ then next
  785.             else
  786.                 return message
  787.             end
  788.         }
  789.     end
  790.     def send(message)    
  791.         @socket.send(message + "\n")
  792.     end
  793. end
Add Comment
Please, Sign In to add comment