• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Help] command Log system

Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
Hi.

Just wanted to ask - which way is better to code a mini log system that saves the command's admin used.
A: A mini log system that works by its own and updates the sql constantly (everytime a gm uses a command).
B: A mini log system that saves the data
through the saveToDB() method in MapleCharacter

With option A i can update only the log instead of loading all player info all the time which can cause problems. But in the other hand maybe this is too much and it will be better to wait till the function saveToDB() will work and save it.

Please don't flame and such.. I'm trying to learn some basics and to improve my codes.


 
Last edited:
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
i dont understand how you can do it with savetodb?
 
Upvote 0
Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
i dont understand how you can do it with savetodb?
In MapleCharacter:

private ArrayList<String> commands = new ArrayList<String>();

public void addCommandToList(String command) {
commands.add(command);
}

and this specific in saveToDB which in MapleCharacter:
for (String com : commands) {
ps.setString(2, com);
ps.addBatch();
}

This is one way. The other way is to use 4-5 sql lines that opens the connection to the sql, inserts the String that includes the command to the correct query and saves it.
But again - The dilema which way is better..
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
If the player does not get saved the commands wont be saved, but the savetodb is the least heaviest
 
Upvote 0
Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
If the player does not get saved the commands wont be saved, but the savetodb is the least heaviest
I think u miss understood me. I didn't mean to remove the saveToDB().
I just wonderd which way is better to save the commands log into the sql - through the saveToDB() which means that the log will be updated only when the saveToDB works or maybe to make a function that saves the log itself and it doesn't need to wait to the saveToDB() cause it works constantly(everytime a gm uses a command.)
I know the cons and pros of each way but still don't know to say which way is better and that why im here - asking u people.
Would love to get some help
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
i didnt misunderstand you, if the player does not get saved, which can happen, the commands wont be saved either
 
Upvote 0
Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
i didnt misunderstand you, if the player does not get saved, which can happen, the commands wont be saved either
Sorry for thinking that way. So you say option B is better ah? Saving the log by itself?
Isn't it too much? I mean - it will update the log in the sql every minute and it can cause some memory leaks and such besides the fact it can make it difficult for the server that has enough anyway
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
savetodb isnt used every minute, you can use a
if (commands.isEmpty) {
// dont do poop
} else {
updateSQL
}
 
Upvote 0
Skilled Illusionist
Joined
Apr 17, 2008
Messages
316
Reaction score
1
Take a look at LeaderMS v62 . It creates a text document that logs all commands used. Shouldn't be too difficult to figure out how it works.
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
he said he didn't have problems understanding how it works, opening a file stream can be as heavy as quickly opening a database stream.
 
Upvote 0
Custom Title Activated
Loyal Member
Joined
Jan 18, 2010
Messages
3,109
Reaction score
1,139
You are doing the same thing either way (actually inserting data, you're not 'updating').

If you did stick with option A, and for any chance your players have a client with Spam and chat client addy hacks, they can spam commands over and over and over, resulting in a massive load of newly generated instances of DatabaseConnections which would be bad. The safer way to go would to be saving them all together. However, I do not recommend your suggestion of an ArrayList.

It would be more ideal to have a CommandLog class that has a Map of a String or Integer key for the users' CharacterID/Name, and a String which is the command (and arguments if necessary). Make the CommandLog class log ALL the commands instead of each individual user. This means if a user disconnects, it won't lose their command data; it will only wipe if you shutdown the server by force. Run a timer to save this CommandLogs' commands after maybe every hour to a log table not relating to the characters table of your database, and this would be the best way to go.

But if you don't wish to do that, then either way A or B you suggested is quite poor and could be abused to stress your database.
 
Upvote 0
Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
You are doing the same thing either way (actually inserting data, you're not 'updating').

If you did stick with option A, and for any chance your players have a client with Spam and chat client addy hacks, they can spam commands over and over and over, resulting in a massive load of newly generated instances of DatabaseConnections which would be bad. The safer way to go would to be saving them all together. However, I do not recommend your suggestion of an ArrayList.

It would be more ideal to have a CommandLog class that has a Map of a String or Integer key for the users' CharacterID/Name, and a String which is the command (and arguments if necessary). Make the CommandLog class log ALL the commands instead of each individual user. This means if a user disconnects, it won't lose their command data; it will only wipe if you shutdown the server by force. Run a timer to save this CommandLogs' commands after maybe every hour to a log table not relating to the characters table of your database, and this would be the best way to go.

But if you don't wish to do that, then either way A or B you suggested is quite poor and could be abused to stress your database.
Thank you, it helped alot .!
To be honest, i understand the idea of what u said but didn't really understand how should it look(the code) and how to do it..
has a Map of a String or Integer key for the users
and a String which is the command
Make the CommandLog class log ALL the commands instead of each individual user
Basically what u say is to make a commandLog class that has a String that gathers the info and to make a method that inserts the data from the string then make another mini-function that every hour(+-) runs the method?
If so - where should i put the method that runs it every X time and how should it look like? how i make it run the method every X time?
If i didn't understand you can you try to explain it to me again? im kinda new with java and trying to learn the basics plus the better way to everything.
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
Code:
public void saveCommandLog(String text) {
        TimerManager.getInstance().schedule(new Runnable() {
            @Override
            public void run() {
                saveCommands(); // ur method to save all the commands to the db
            }
        }, 600000); // not sure if 10 min
    }
 
Upvote 0
Newbie Spellweaver
Joined
Sep 28, 2014
Messages
11
Reaction score
0
Code:
public void saveCommandLog(String text) {
        TimerManager.getInstance().schedule(new Runnable() {
            @Override
            public void run() {
                saveCommands(); // ur method to save all the commands to the db
            }
        }, 600000); // not sure if 10 min
    }
Forgot to thank you about the help too ;)
Thank you for showing me how the timer works. Is it runs itself that way?
about the time i will search for in i should learn it anyway.
 
Upvote 0
Experienced Elementalist
Joined
Jul 3, 2012
Messages
202
Reaction score
32
No you should add this method to the server start method, and that code only runs once. my bad, its better to use this:
Code:
public void saveCommandLog(String text) {
        TimerManager.getInstance().register(new Runnable() { // register = multiple times
            @Override
            public void run() {
                saveCommands(); // ur method to save all the commands to the db
            }
        }, 600000, 600000); // repeat timer - delay
    }

and if you ever want to kill the command log saving, you can do this:

Code:
private ScheduledFuture<?> commandLogTask;

public void saveCommandLog() {
    if (commandLogTask == null) {
        commandLogTask = TimerManager.getInstance().register(new Runnable() { // register = multiple times
            @Override
            public void run() {
                saveCommands(); // ur method to save all the commands to the db
        }, 600000, 600000); // repeat timer - delay
    }
}

public void stopCommandLogSaving() {
    if (commandLogTask != null) {
        commandLogTask.cancel(false);
        commandLogTask = null;
    }
}

ps: there is a like button for that reason ;)
 
Upvote 0
Back
Top