Jump to content
Sign in to follow this  
eleria

[Help] What could be improved in this little script?

Recommended Posts

eleria

Hi i'm quite new to autoit (in fact it's the first time I really tried to write a real code)

This was done with my little logic and the help file.

I'm co-directing a very new project l2csp(lineage 2 community spawn project) which basicly provides a spawn table which aims to be very organized (split by geo-regions) and very close to the official lineage2 ones.

That's why I am writing a tool tohelp me not lose time in dozens of other programs to manipulate my data!

I'd like to know which parts could be improved and how.

Thank you in advance ^^

CODE
#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

Dim $auth, $geo, $desc, $file, $x1, $y1, $x2, $y2,$out, $quer, $ver

Dim $x11, $y11, $x21, $y21

$read = Fileread(".\header.txt")

$ver = @YEAR & "." & @MON & "." & @MDAY

$gui = GUICreate("Eleria's SwissKnife", 426, 241, 330, 229)

$group1 = GUICtrlCreateGroup("SQL_Creator", 8, 8, 409, 137)

$x1_ = GUICtrlCreateInput("x1", 16, 32, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$x2_ = GUICtrlCreateInput("x2", 16, 56, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$y1_ = GUICtrlCreateInput("y1", 80, 32, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$y2_ = GUICtrlCreateInput("y2", 80, 56, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$auth_ = GUICtrlCreateInput("Author", 344, 32, 65, 21)

$quer_ = GUICtrlCreateEdit("Querries", 144, 32, 193, 65, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))

$create = GUICtrlCreateButton("Create", 216, 104, 41, 25, 0)

$geo_ = GUICtrlCreateInput("Geo-Region", 344, 56, 65, 21)

$desc_ = GUICtrlCreateInput("Description", 344, 80, 65, 21)

$out_ = GUICtrlCreateInput("Filename", 48, 112, 57, 21)

$group2 = GUICtrlCreateGroup("Geo_Region_Coordinates", 8, 152, 409, 81)

$x11_ = GUICtrlCreateInput("x1", 224, 168, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y11_ = GUICtrlCreateInput("y1", 320, 168, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$x21_ = GUICtrlCreateInput("x2", 224, 200, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y21_ = GUICtrlCreateInput("y2", 320, 200, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$geox_ = GUICtrlCreateInput("Geo_y", 24, 184, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$geoy_ = GUICtrlCreateInput("Geo_x", 96, 184, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

GUISetState(@SW_SHOW)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $create

$auth = GuiCtrlRead ($auth_)

$geo = GuiCtrlRead ($geo_)

$x1 = GuiCtrlRead ($x1_)

$y1 = GuiCtrlRead ($y1_)

$x2 = GuiCtrlRead ($x2_)

$y2 = GuiCtrlRead ($y2_)

$out = GuiCtrlRead ($out_)

$desc = GuiCtrlRead ($desc_)

$quer = GuiCtrlRead ($quer_)

$read = StringReplace($read, "%auth%", $auth)

$read = StringReplace($read, "%geo%", $geo)

$read = StringReplace($read, "%x1%", $x1)

$read = StringReplace($read, "%y1%", $y1)

$read = StringReplace($read, "%x2%", $x2)

$read = StringReplace($read, "%y2%", $y2)

$read = StringReplace($read, "%out%", $out)

$read = StringReplace($read, "%desc%", $desc)

$read = StringReplace($read, "%quer%", $quer)

$read = StringReplace($read, "%ver%", $ver)

If FileExists($out) Then

FileDelete ($out)

EndIf

FileWrite ($out & ".sql", $read)

Case $geox_

$geox = GuiCtrlRead ($geox_)

$x11 = ($geox - 20) * 32768

$x21 = ($geox - 19) * 32768

GUICtrlSetData ($x11_, $x11)

GUICtrlSetData ($x21_, $x21)

Case $geoy_

$geoy = GuiCtrlRead ($geoy_)

$y11 = ($geoy - 18) * 32768

$y21 = ($geoy - 17) * 32768

GUICtrlSetData ($y21_, $y21)

GUICtrlSetData ($y11_, $y11)

EndSwitch

WEnd

Also this is what the header.txt file looks like:

CODE
-- ----------------------------------------------------------------------------

-- -- DB Patch file for region %geo% -- version %ver% -- By %auth% --

-- ----------------------------------------------------------------------------

-- -- Notes:

-- -- a) This file contains *ONLY* 'L2Monster' type spawn data!

-- -- :D This file *DOES NOT CREATE* a 'spawnlist' table.

-- -- c) You *MUST* use it on an existing 'spawnlist' table. Preferably,

-- -- from a clean table generated from the L2JFree SVN 'spawnlist.sql'

-- -- CT1 DataPack script. A table built using my latest spawn list

-- -- "header" script is also OK. But be warned that, such method will only

-- -- load the non-'L2Monster' spawns for the world + each patch file you

-- -- run. I highly recomend using the script versions designed for clean

-- -- install of my spawn data instead. They are much faster, because

-- -- they do not scan the table for removal of, all the old 'L2Monster'

-- -- type spawns in the region.

-- ----------------------------------------------------------------------------

-- ----------------------------------------------------------------------------

-- %desc% -- Geo: %geo% --

-- ----------------------------------------------------------------------------

-- Remove 'L2Monster' type spawns from region %geo%

DELETE FROM `spawnlist` WHERE

spawnlist.locx >= %x1% AND

spawnlist.locy >= %y1% AND

spawnlist.locx < %x2% AND

spawnlist.locy < %y2% AND

EXISTS (

SELECT *

FROM npc n

WHERE n.type = "L2Monster"

AND spawnlist.npc_templateid = n.idTemplate );

-- Add the new 'L2Monster' type spawns to region %geo%

INSERT INTO `spawnlist` ( location, count, npc_templateid, locx, locy, locz, randomx, randomy, heading, respawn_delay, loc_id, periodOfDay ) VALUES

%quer%

What it does : One part is for replacing values from a header file and adding the sql querries, then the other part is for calculating geo-region boundaries used in the SQL DELETE function...

Edited by eleria

Share this post


Link to post
Share on other sites
martin

Hi i'm quite new to autoit (in fact it's the first time I really tried to write a real code)

This was done with my little logic and the help file.

I'm co-directing a very new project l2csp(lineage 2 community spawn project) which basicly provides a spawn table which aims to be very organized (split by geo-regions) and very close to the official lineage2 ones.

That's why I am writing a tool tohelp me not lose time in dozens of other programs to manipulate my data!

I'd like to know which parts could be improved and how.

Thank you in advance ^^

CODE
#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

Dim $auth, $geo, $desc, $file, $x1, $y1, $x2, $y2,$out, $quer, $ver

Dim $x11, $y11, $x21, $y21

$read = Fileread(".\header.txt")

$ver = @YEAR & "." & @MON & "." & @MDAY

$gui = GUICreate("Eleria's SwissKnife", 426, 241, 330, 229)

$group1 = GUICtrlCreateGroup("SQL_Creator", 8, 8, 409, 137)

$x1_ = GUICtrlCreateInput("x1", 16, 32, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$x2_ = GUICtrlCreateInput("x2", 16, 56, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$y1_ = GUICtrlCreateInput("y1", 80, 32, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$y2_ = GUICtrlCreateInput("y2", 80, 56, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

$auth_ = GUICtrlCreateInput("Author", 344, 32, 65, 21)

$quer_ = GUICtrlCreateEdit("Querries", 144, 32, 193, 65, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))

$create = GUICtrlCreateButton("Create", 216, 104, 41, 25, 0)

$geo_ = GUICtrlCreateInput("Geo-Region", 344, 56, 65, 21)

$desc_ = GUICtrlCreateInput("Description", 344, 80, 65, 21)

$out_ = GUICtrlCreateInput("Filename", 48, 112, 57, 21)

$group2 = GUICtrlCreateGroup("Geo_Region_Coordinates", 8, 152, 409, 81)

$x11_ = GUICtrlCreateInput("x1", 224, 168, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y11_ = GUICtrlCreateInput("y1", 320, 168, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$x21_ = GUICtrlCreateInput("x2", 224, 200, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y21_ = GUICtrlCreateInput("y2", 320, 200, 89, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$geox_ = GUICtrlCreateInput("Geo_y", 24, 184, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$geoy_ = GUICtrlCreateInput("Geo_x", 96, 184, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

GUISetState(@SW_SHOW)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $create

$auth = GuiCtrlRead ($auth_)

$geo = GuiCtrlRead ($geo_)

$x1 = GuiCtrlRead ($x1_)

$y1 = GuiCtrlRead ($y1_)

$x2 = GuiCtrlRead ($x2_)

$y2 = GuiCtrlRead ($y2_)

$out = GuiCtrlRead ($out_)

$desc = GuiCtrlRead ($desc_)

$quer = GuiCtrlRead ($quer_)

$read = StringReplace($read, "%auth%", $auth)

$read = StringReplace($read, "%geo%", $geo)

$read = StringReplace($read, "%x1%", $x1)

$read = StringReplace($read, "%y1%", $y1)

$read = StringReplace($read, "%x2%", $x2)

$read = StringReplace($read, "%y2%", $y2)

$read = StringReplace($read, "%out%", $out)

$read = StringReplace($read, "%desc%", $desc)

$read = StringReplace($read, "%quer%", $quer)

$read = StringReplace($read, "%ver%", $ver)

If FileExists($out) Then

FileDelete ($out)

EndIf

FileWrite ($out & ".sql", $read)

Case $geox_

$geox = GuiCtrlRead ($geox_)

$x11 = ($geox - 20) * 32768

$x21 = ($geox - 19) * 32768

GUICtrlSetData ($x11_, $x11)

GUICtrlSetData ($x21_, $x21)

Case $geoy_

$geoy = GuiCtrlRead ($geoy_)

$y11 = ($geoy - 18) * 32768

$y21 = ($geoy - 17) * 32768

GUICtrlSetData ($y21_, $y21)

GUICtrlSetData ($y11_, $y11)

EndSwitch

WEnd

Also this is what the header.txt file looks like:

CODE
-- ----------------------------------------------------------------------------

-- -- DB Patch file for region %geo% -- version %ver% -- By %auth% --

-- ----------------------------------------------------------------------------

-- -- Notes:

-- -- a) This file contains *ONLY* 'L2Monster' type spawn data!

-- -- :D This file *DOES NOT CREATE* a 'spawnlist' table.

-- -- c) You *MUST* use it on an existing 'spawnlist' table. Preferably,

-- -- from a clean table generated from the L2JFree SVN 'spawnlist.sql'

-- -- CT1 DataPack script. A table built using my latest spawn list

-- -- "header" script is also OK. But be warned that, such method will only

-- -- load the non-'L2Monster' spawns for the world + each patch file you

-- -- run. I highly recomend using the script versions designed for clean

-- -- install of my spawn data instead. They are much faster, because

-- -- they do not scan the table for removal of, all the old 'L2Monster'

-- -- type spawns in the region.

-- ----------------------------------------------------------------------------

-- ----------------------------------------------------------------------------

-- %desc% -- Geo: %geo% --

-- ----------------------------------------------------------------------------

-- Remove 'L2Monster' type spawns from region %geo%

DELETE FROM `spawnlist` WHERE

spawnlist.locx >= %x1% AND

spawnlist.locy >= %y1% AND

spawnlist.locx < %x2% AND

spawnlist.locy < %y2% AND

EXISTS (

SELECT *

FROM npc n

WHERE n.type = "L2Monster"

AND spawnlist.npc_templateid = n.idTemplate );

-- Add the new 'L2Monster' type spawns to region %geo%

INSERT INTO `spawnlist` ( location, count, npc_templateid, locx, locy, locz, randomx, randomy, heading, respawn_delay, loc_id, periodOfDay ) VALUES

%quer%

What it does : One part is for replacing values from a header file and adding the sql querries, then the other part is for calculating geo-region boundaries used in the SQL DELETE function...

I would recommend using labels for your input boxes rather than indicating their function with an initial string. Once data has been entered you don't know which box to use without the labels.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
zorphnog

You don't necessarily have to have a header.txt. You could write the entire file from within the script. Although, if you are constantly changing/updating the header file then your way is fine. I'd also use some error checking on your file operations (i.e. make sure FileRead was successful otherwise you'll end up creating a blank file).

Share this post


Link to post
Share on other sites
eleria

Alright

New version :

CODE
#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

Dim $read, $auth, $geo, $desc, $file, $x1, $y1, $x2, $y2,$out, $quer, $ver

Dim $x11, $y11, $x21, $y21

$read = FileRead(".\header")

If (@error = 1) then

Msgbox (0,"ERROR", "Could not open header file!")

Exit

Endif

$ver = @YEAR & "." & @MON & "." & @MDAY

$gui = GUICreate("Eleria's SwissKnife", 426, 306, 330, 229)

$Group1 = GUICtrlCreateGroup("SQL_Creator", 8, 0, 409, 177)

$x1_ = GUICtrlCreateInput("", 16, 40, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$x2_ = GUICtrlCreateInput("", 16, 88, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$y1_ = GUICtrlCreateInput("", 80, 40, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$y2_ = GUICtrlCreateInput("", 80, 88, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$auth_ = GUICtrlCreateInput("", 344, 40, 65, 21)

$quer_ = GUICtrlCreateEdit("", 144, 40, 193, 65, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))

$create = GUICtrlCreateButton("create", 204, 120, 57, 33, 0)

$geo_ = GUICtrlCreateInput("", 344, 88, 65, 21)

$desc_ = GUICtrlCreateInput("", 344, 136, 65, 21)

$out_ = GUICtrlCreateInput("", 48, 136, 57, 21)

$Label1 = GUICtrlCreateLabel("Author", 360, 20, 35, 17)

$Label2 = GUICtrlCreateLabel("Region", 360, 68, 38, 17)

$Label3 = GUICtrlCreateLabel("Details", 360, 116, 36, 17, $SS_CENTER)

$Label4 = GUICtrlCreateLabel("Filename", 52, 116, 50, 17, $SS_CENTER)

$Label5 = GUICtrlCreateLabel("x1", 40, 20, 15, 17)

$Label6 = GUICtrlCreateLabel("y1", 104, 20, 15, 17)

$Label7 = GUICtrlCreateLabel("y2", 104, 68, 15, 17)

$Label8 = GUICtrlCreateLabel("x2", 40, 68, 15, 17)

$Label9 = GUICtrlCreateLabel("SQL querries", 200, 20, 65, 17)

$Group2 = GUICtrlCreateGroup("Geo_Region_Coordinates", 8, 184, 289, 113)

$x11_ = GUICtrlCreateInput("", 156, 216, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y11_ = GUICtrlCreateInput("", 228, 216, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$x21_ = GUICtrlCreateInput("", 156, 264, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$y21_ = GUICtrlCreateInput("", 228, 264, 57, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_READONLY))

$geox_ = GUICtrlCreateInput("", 72, 216, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$geoy_ = GUICtrlCreateInput("", 72, 264, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))

$Label43 = GUICtrlCreateLabel("Geo_x", 32, 216, 35, 17)

$Label12 = GUICtrlCreateLabel("x2", 176, 244, 15, 17)

$Label13 = GUICtrlCreateLabel("x1", 176, 196, 15, 17)

$Label14 = GUICtrlCreateLabel("y1", 248, 196, 15, 17)

$Label15 = GUICtrlCreateLabel("y2", 248, 244, 15, 17)

$Label10 = GUICtrlCreateLabel("Geo_y", 32, 264, 35, 17)

GUISetState(@SW_SHOW)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $create

$read = FileRead(".\header")

$auth = GuiCtrlRead ($auth_)

$geo = GuiCtrlRead ($geo_)

$x1 = GuiCtrlRead ($x1_)

$y1 = GuiCtrlRead ($y1_)

$x2 = GuiCtrlRead ($x2_)

$y2 = GuiCtrlRead ($y2_)

$out = GuiCtrlRead ($out_)

$desc = GuiCtrlRead ($desc_)

$quer = GuiCtrlRead ($quer_)

$read = StringReplace($read, "%auth%", $auth)

$read = StringReplace($read, "%geo%", $geo)

$read = StringReplace($read, "%x1%", $x1)

$read = StringReplace($read, "%y1%", $y1)

$read = StringReplace($read, "%x2%", $x2)

$read = StringReplace($read, "%y2%", $y2)

$read = StringReplace($read, "%out%", $out)

$read = StringReplace($read, "%desc%", $desc)

$read = StringReplace($read, "%quer%", $quer)

$read = StringReplace($read, "%ver%", $ver)

If FileExists($out & ".sql") Then

FileDelete ($out & ".sql")

EndIf

FileWrite ($out & ".sql", $read)

Case $geox_

$geox = GuiCtrlRead ($geox_)

$x11 = ($geox - 20) * 32768

$x21 = ($geox - 19) * 32768

GUICtrlSetData ($x11_, $x11)

GUICtrlSetData ($x21_, $x21)

Case $geoy_

$geoy = GuiCtrlRead ($geoy_)

$y11 = ($geoy - 18) * 32768

$y21 = ($geoy - 17) * 32768

GUICtrlSetData ($y21_, $y21)

GUICtrlSetData ($y11_, $y11)

EndSwitch

WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×