|
acaykath Wanderer
Joined: 03 Aug 2005 Posts: 84
|
Posted: Wed Mar 19, 2008 6:14 am
Database Help |
I am writing a class for an immortal to help him discover multi-players and quick-loggers. This would involve comparing known alts, login/logoff times, and ip addresses.
I have a database with fields userID, character, IP, login, logout, but I cannot figure out a simple way to determine if a character is already in the database. (I really hope that databases are a lot easier to use once zugg updates them in cMud...)
can someone please show me how to do something along the lines of (sorry if i suddenly start resembling sql rather than zScript)
Code: |
#DBOPEN alts
#If (alts contains character) (set database record values) (insert new database record)
userid = new/updated &userid
ip = &ip
#Forall (where (&userID = @userid or &ip = @ip) and (&logout = 0 or timestamp-&logout < 300) {do actions )
#DBCLOSE alts |
While this is not the only database script I need, I am hopeful I'll be able to derive the rest from what I learn from your help. |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Thu Mar 20, 2008 5:46 am |
This is untested and probably incomplete but it's my take on how you might go about it.
first we define a function that generates unique GUIDs to use as a UserID
Code: |
#FUNC GenGuid {%replace(%leftback(%comget(%comcreate("Scriptlet.TypeLib"),"Guid"),2),"-")} |
Code: |
#var timestamp %mss("new Date().getTime()","JScript")
#var rec %query(&Character=@name)
#IF (not %null(@rec)) {
#DBPUT @rec {&Character=@name|&IP=@IP|&Login=@login|&Logout=@logout}
} {
#NEW {&UserID=@GenGuid()|&Character=@name|&IP=@IP|&Login=@login|&Logout=@logout}
}
#if (!%null(@rec)) {#DBGET @rec} {}
#var UID %db(%rec,UserID)
#var IP %db(%rec,IP)
#for %query( ((&userID=@UID) or (&IP=@IP)) and ((&Logout = 0) or (%eval(@timestamp - &Logout) < 300)) ) {
do something
} |
Goodluck and let me know if ya need further assistance. |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
acaykath Wanderer
Joined: 03 Aug 2005 Posts: 84
|
Posted: Thu Mar 20, 2008 1:24 pm |
Thanks for the help. I haven't had a chance to try it yet, but it looks like your code has examples of everything I need. (Probably never would have figured out the %null thing on my own.)
|
|
|
|
acaykath Wanderer
Joined: 03 Aug 2005 Posts: 84
|
Posted: Fri Mar 21, 2008 1:49 am |
Is there any way to get Database commands to work when the DB window is not open? Otherwise, it doesn't seem to do anything...
(Thanks for all the help again Dharkael, though for future readers of this thread, #NEW and #DBPUT should not have & in front of the field names.) |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Mar 21, 2008 2:06 am |
Nope, it has to be open to use it.
|
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Fri Mar 21, 2008 6:16 am |
Yeah I definitely made a few mistakes... including the timestamp.
It should probably be measuring seconds instead of milliseconds.
Code: |
#var timestamp {%mss("var _now = new Date().getTime();Math.round(_now/1000)","JScript")} |
I was pretty stoked with the GenGUID Func though, I'm sure I'll find a use for that again
Although I didn't realize it was returning the enclosing braces {} I guess thats not really a problem still its still unique.
but It might be better if it was just left with Hex Characters.
Here's an updated version
Code: |
#FUNC GenGuid {%subregex(%left(%comget(%comcreate("Scriptlet.TypeLib"),"Guid"),38),"[^A-F0-9]")} |
|
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
acaykath Wanderer
Joined: 03 Aug 2005 Posts: 84
|
Posted: Sat Mar 22, 2008 1:35 am |
I was wondering if it is possible to open the DB window with code so it doesn't have to be manually started...
Also, this is what my code currently looks like, though there are still a few bugs to be worked out...
Code: |
#TRIGGER {--> %w (%w) (%d) (%d):(%d):(%d) (%d) :: (%w)@(%x) has connected.} {
#NOOP Make Timestamp Integer
year = %6
#IF (%1="Jan") {month="01"}
#IF (%1="Feb") {month="02"}
#IF (%1="Mar") {month="03"}
#IF (%1="Apr") {month="04"}
#IF (%1="May") {month="05"}
#IF (%1="Jun") {month="06"}
#IF (%1="Jul") {month="07"}
#IF (%1="Aug") {month="08"}
#IF (%1="Sep") {month="09"}
#IF (%1="Oct") {month="10"}
#IF (%1="Nov") {month="11"}
#IF (%1="Dec") {month="12"}
day = %2
hour = %3
minute = %4
second = %5
timestamp = %concat( @year, @month, @day, @hour, @minute, @second)
#NOOP Set Character and IP
character = %7
IP = %8
#NOOP Check for Database Entry
record = %query( &character = @character, Alts)
#IF (!%null( @record)) {
#NOOP found character
#DBPUT @record {IP=@IP|Login=@timestamp|Logout=0}
@userID = %db( @record, UserID)
} {
#NOOP character not found
#ADD lastID 1
userID = @lastID
#NEW @record {UserID=@userID|Character=@character|IP=@IP|Login=@timestamp|Logout=0}
}
record = %query( ((&UserID=@UserID)OR(&IP=@IP))AND(&Character!=@Character)AND((&Logout=0)OR(@timestamp-&Logout<300)), Alts)
#FORALL @record {
#IF ((%db( %i, IP)=@IP)AND(%db( %i, Logout)!=0)) {
#NOOP Character's IP matches the Logging character's.
#ECHO @Character Shares and IP with %db( %i, Character) They might be multiplaying.
}
#IF ((%db( %i, UserID)=@userID)) {
#NOOP Characters User matches the Logging Characters
#ECHO @Character is multiplaying with %db( %i, Character)
}
#IF ((@timestamp-%db( %i, Logout)<300)AND(%db( %i, IP)=@IP)AND(%db( %i, Logout)!=0)) {
#NOOP Character's Login is less than 3 minutes after someone from the same IP adress
#ECHO @Character logged in less than 3 minutes after %db( %i, Character) from the same IP. Possible quicklog.
}
#IF ((@timestamp-%db( %i, Logout)<300)AND(%db( %i, UserID)=@userID)AND(%db( %i, Logout)!=0)) {
#NOOP Character's Login is less than 3 minutes after an alt's
#ECHO @Character logged in less than 3 minutes after %db( %i, Character). He quicklogged.
}
}
#NOOP Check to see if user has logged from this IP before.
record=%query( (UserID=@UserID)AND(Address=@IP), IP)
#IF (%null( @record)) {
#NOOP New IP for user, log the IP
#NEW @record {UserID=@userID|Address=IP}
}
}
|
|
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|