*Introduction:
NOTE! This script allows you to bind ONE function to listen packet with specific id.
NOTE! You have to specify packet id(s) by yourself in messageid.nut file
NOTE! Default packet Id type is Uint16, but you can change this easily by editing packetListener.nut script
NOTE! If you decide to test this script, make sure that you don't have any function(s) bind to onPacket event
Info: This script is stable and propably doesn't have any bugs.
Hi, as you know G2O allows you to communicate with client or server using packets, but if you want to split functions which are reading packets,
you still must have ONLY one onPacket event, in which you are reading packet id. If you have more complex scripts, it's not good to read a code
from file to file, so i've made very simple handler, which allows you to bind ONE function to specific packet id. Feel free to use it.
License
Download
*List of functions:
void addPacketListener(mixed packetId, func callback)
void removePacketListener(mixed packetId)
*Function (addPacketListener, removePacketListener) parameters description:
Info: packetId is "mixed" because it's up to you what packet id type will be (default is Uint16).
// Required parameters for addPacketListener
mixed packetId // The packetId, (it's recommended to specify packet Id's in file messageid.nut)
void callback // The function which will be called, when packet arrived to specific site.
// Required parameters for removePacketListener
mixed packetId // The packetId
*Installation:
1.Download zip archive
2.Unpack archive and place network folder in your G2O server directory wherever you want.
3.Add this line to parsed xml file, (for example: config.xml)
4.After that, you've loaded both scripts: (packetListener.nut and messageid.nut) to client-side and server-side.
*Examples:
*Sending client-side statistics to server, which can be later used for building an anti-cheat:
*Client-side:
*Server-side:
*Sending server-side custom player class to client
*Server-side:
*Client-side:
*Examples comment
Example nr 2:
This is only a dummy example, to illustrate how addPacketListener function works.
If you really want to create a script which give some class to player, i recommend you to
giving statistics on server-side, because this example is not safe.
NOTE! This script allows you to bind ONE function to listen packet with specific id.
NOTE! You have to specify packet id(s) by yourself in messageid.nut file
NOTE! Default packet Id type is Uint16, but you can change this easily by editing packetListener.nut script
NOTE! If you decide to test this script, make sure that you don't have any function(s) bind to onPacket event
Info: This script is stable and propably doesn't have any bugs.
Hi, as you know G2O allows you to communicate with client or server using packets, but if you want to split functions which are reading packets,
you still must have ONLY one onPacket event, in which you are reading packet id. If you have more complex scripts, it's not good to read a code
from file to file, so i've made very simple handler, which allows you to bind ONE function to specific packet id. Feel free to use it.
License
Download
*List of functions:
void addPacketListener(mixed packetId, func callback)
void removePacketListener(mixed packetId)
*Function (addPacketListener, removePacketListener) parameters description:
Info: packetId is "mixed" because it's up to you what packet id type will be (default is Uint16).
// Required parameters for addPacketListener
mixed packetId // The packetId, (it's recommended to specify packet Id's in file messageid.nut)
void callback // The function which will be called, when packet arrived to specific site.
// Required parameters for removePacketListener
mixed packetId // The packetId
*Installation:
1.Download zip archive
2.Unpack archive and place network folder in your G2O server directory wherever you want.
3.Add this line to parsed xml file, (for example: config.xml)
PHP
- <import src="network/network.xml" />
4.After that, you've loaded both scripts: (packetListener.nut and messageid.nut) to client-side and server-side.
*Examples:
*Sending client-side statistics to server, which can be later used for building an anti-cheat:
*Client-side:
Squirrel Script
- // creating infinite timer which will be called with one second delay
- setTimer(function()
- {
- local packet = Packet() // creating packet
-
- packet.writeUint16(PACKET.ID) // writing some id from messageid.nut, you have to define id yourself, it's only a dummy example
- packet.writeInt32(getPlayerStrength(heroId)) // writing player strength
- packet.writeInt32(getPlayerDexterity(heroId)) // writing player dexterity
-
- packet.send(RELIABLE_ORDERED) // sending packet to server
-
- }, 1000, -1)
*Server-side:
Squirrel Script
- addPacketListener(PACKET.ID, function(pid, packet) // binding function to packet with id PACKET.ID
- {
- local strength = packet.readInt32() // reading player strength
- local dexterity = packet.readInt32() // reading player dexterity
-
- if (strength != getPlayerStrength(pid)) // comparing client-side strength with server-side strength
- {
- ban(pid, -1, "Cheating strength") // givining permanent ban for cheating
- }
- else if (dexterity != getPlayerDexterity(pid)) // comparing client-side dexterity with server-side dexterity
- {
- ban(pid, -1, "Cheating dexterity") // givining permanent ban for cheating
- }
- })
*Sending server-side custom player class to client
*Server-side:
Squirrel Script
- local heroClass =
- {
- health = 1000,
- mana = 100,
-
- strength = 200,
- dexterity = 200
- }
-
- addEventHandler("onCommand", function(pid, cmd, params)
- {
- if (cmd == "giveclass") // if player type /giveclass command, then..
- {
- local packet = Packet() // creating packet
-
- packet.writeUint16(PACKET.CLASS) // writing some id from messageid.nut, you have to define id yourself, it's only a dummy example
- packet.writeInt32(heroClass.health) // writing class health
- packet.writeInt32(heroClass.mana) // writing class mana
- packet.writeInt32(heroClass.strength) // writing class strength
- packet.writeInt32(heroClass.dexterity) // writing class dexterity
-
- packet.send(pid, RELIABLE_ORDERED) // sending packet to client
- }
- })
*Client-side:
Squirrel Script
- addPacketListener(PACKET.ID, function(packet) // binding function to packet with id PACKET.ID
- {
- local health = packet.readInt32() // reading class health
- local mana = packet.readInt32() // reading class mana
- local strength = packet.readInt32() // reading class strength
- local dexterity = packet.readInt32() // reading class dexterity
-
- setPlayerHealth(heroId, health)
- setPlayerMaxHealth(heroId, health)
-
- setPlayerMana(heroId, mana)
- setPlayerMaxMana(heroId, mana)
-
- setPlayerStrength(heroId, strength)
- setPlayerDexterity(heroId, dexterity)
- })
*Examples comment
Example nr 2:
This is only a dummy example, to illustrate how addPacketListener function works.
If you really want to create a script which give some class to player, i recommend you to
giving statistics on server-side, because this example is not safe.