Thread: [Essentials Script] My Script Library
View Single Post
  #11    
Old October 6th, 2012 (9:04 AM).
Umbra's Avatar
Umbra Umbra is offline
Platinum Tier
 
Join Date: Aug 2009
Location: Nearby my feet.
Age: 19
Gender: Female
Nature: Calm
Posts: 1,729

Alright today I got something new, a socket handler!

It's easy to use but needs some serious updates to actually work properly:

Code:
################################################################################
# Sockets Extension
# By Hansiec
# Uses:
# Connections though networking Includes basic functions
# Comes alongside with a few basic functions that could be helpful to some people
# Features:
#  *Connect to any server
#  *Choose a "Host" which is a player not a server (which means you do not need
#  to handle your servers as people will need to setup their own)
#  *Easy to use data sharing (by using the send_data(receiver,data) function)
#  *Comes with a few functions built it such as item handling
#  *Specify this player only messages.
#  *Easily add data (by using the add_data(data) function)
#  *Easily specify a player to send data to (by using the specify_player(player,
#   data) function)
# TODO:
#  * Add usernames
#  * Add passwords
#  * Add permanent user ids
#  * Add functions to get a player's id
################################################################################

class SocketHandler attr_accessor :socket attr_accessor :data attr_accessor :connection_handle attr_accessor :host attr_accessor :new_data

# Startup the system def initialize(host, port, host2) @socket=TCPSocket.new(host, port) @data = [] @connection_handle = rand(9999999) @host = host2 @new_data = "" end

# so you want to send some data? specify a user and data (-1 is for everyone) def send_data(receiver,data) @socket.send(data) end

# Update the system def update [email protected](0xfff).split("\n") parse_data(data) if @host send_data(-1, @new_data) reset_data end end

def parse_data(data) for line in data # First we must check if we may access the data if line.include?("p=#{get_handle}") || line.include?("p=-1") if line.include?("#{get_handle}") line=line.split("p=#{get_handle}")[1] else line=line.split("p=-1")[1] end if !edit_variable(line) if !edit_items(line) if !edit_pokemon(line) edit_misc(line) end end end # End of checks end end end

# Handles variable/switch data def edit_variable(line) if line.include?("var") line=line.split("var")[1] $game_variables[line.replace("var").split(",")[0].to_i]=line.split(",")[1].to_i return true elsif line.include?("swt") line=line.split("swt")[1] $game_switches[line.replace("swt").split(",")[0].to_i]=split(",")[1].to_i return true end return false end

# Handles item data def edit_items(line) if line.include?("give_item") line=line.split("give_item")[1] eval("pbReceiveItem("+line+")") return true elsif line.include?("delete_item") line=line.split("delete_item")[1] eval("$PokemonBag.pbDeleteItem("+line+")") return true end return false end

# Handles pokemon data def edit_pokemon(line) if line.include?("give_pokemon") line=line.replace("give_pokemon") if line.include?("silent") line=line.split("give_pokemon")[1] pbAddPokemonSilent(line.split(",")[0], line.split(",")[1]) else pbAddPokemon(line.split(",")[0], line.split(",")[1]) end return true elsif line.include?("take_pokemon") line=line.split("take_pokemon")[1] pbRemovePokemonAt(line.replace("take_pokemon").to_i) return true end return false end

# Handles un-classed data def edit_misc(line) if line.include?("message") Kernel.pbMessage(_INTL(line.split("message ")[1])) return true end return false end

# resets the data to the default settings def reset_data @new_data = "" end

# adds data to send def add_data(data) @new_data+="\n"+data end

# adds player specifics on the data (use once per data) def specify_player(player,data) return "p=#{player} data" end

# Returns the player's connection handle def get_handle return @connection_handle end

end

# You may delete these lines below! REALLY DO SO! $s=SocketHandler.new("localhost", 3306, true) $s.parse_data(["p=-1 message This is a test socket!", "p=#{$s.get_handle} message Testing specific player only!"])

the update function is rather bare for now, I hope you all enjoy this one!

Reply With Quote