[PHP] MuEngine: PHP Library for MuOnline
Hi everyone,
I have made these open-source PHP classes so that you can manage your MuOnline database very easily. You won't even have to manipulate SQL queries anymore, the code behind the classes does it all for you.
MuEngine is made up of 6 classes: MuDatabase, MuCharacter, MuAccount, MuGuild, MuAccountCreator, MuQueryResult
Here is a short preview to each of them:
MuDatabase: Represents a connection to your database. You can execute raw queries from there if you want, but it is WAY simpler calling one of the functions such as account(), character(), guild(), etc.
MuCharacter: Represents a MuOnline character.
MuAccount: Represents a MuOnline account.
MuGuild: Represents a MuOnline guild.
MuAccountCreator: Represents an account creator.
MuQueryResult: Represents the result to a query executed through MuDatabase::execute() or MuDatabase::query().
Unfortunately, MuEngine is NOT documented yet, BUT the function names are pretty self-explanatory, besides I will give a few examples of how to use MuEngine below.
Personally, I am almost done with a website, that uses MuEngine without having to write raw SQL queries about 99% of the time, proof that you can do A LOT with it.
MUENGINE CLASS METHOD LIST :
Code:
<return_type> <class>::<function_name> (<args>)
===== MuDatabase CLASS ====
const MuDatabase::CONNTYPE_MSSQL
const MuDatabase::CONNTYPE_ODBC
MuDatabase MuDatabase::setUsername(string $username)
MuDatabase MuDatabase::setPassword(string $password)
MuDatabase MuDatabase::setHost(string $host)
MuDatabase MuDatabase::setDSN(string $dsn)
MuDatabase MuDatabase::setConnType(int $conntype)
MuDatabase MuDatabase::useResetSystem()
MuDatabase MuDatabase::connect()
MuDatabase MuDatabase::useMD5()
MuDatabase MuDatabase::lastError(bool $forceShowEvenIfNotInDebugMode=false)
bool MuDatabase::connected()
MuDatabase MuDatabase::useDebugMode()
MuQueryResult MuDatabase::execute(string $queryFormat, string $arg1, string $arg2, ...)
MuQueryResult MuDatabase::query(string $query)
MuAccount MuDatabase::account(string $accountname)
MuCharacter MuDatabase::character(string $charactername)
MuDatabase MuDatabase::enableLogging(string $fileName, string $format)
MuDatabase MuDatabase::use64KStatSupport()
bool MuDatabase::accountExists(string $accountName)
bool MuDatabase::emailExists(string $emailAddress)
MuAccountCreator MuDatabase::accountCreator()
array MuDatabase::rankings(int $max, bool $showGM) [array<MuCharacter>]
MuGuild MuDatabase::guild(string $guildName)
====MuCharacter CLASS====
string MuCharacter::getName()
int MuCharacter::getStrength()
int MuCharacter::getAgility()
int MuCharacter::getVitality()
int MuCharacter::getEnergy()
int MuCharacter::getCommand()
int MuCharacter::getFreePoints()
MuCharacter MuCharacter::setFreePoints(int $newVal)
MuCharacter MuCharacter::addFreePoints(int $addVal)
MuCharacter MuCharacter::remFreePoints(int $remVal)
MuCharacter MuCharacter::setStrength(int $newVal)
MuCharacter MuCharacter::setAgility(int $newVal)
MuCharacter MuCharacter::setVitality(int $newVal)
MuCharacter MuCharacter::setEnergy(int $newVal)
MuCharacter MuCharacter::setCommand(int $newVal)
MuCharacter MuCharacter::setPK(int $newVal)
string MuCharacter::getClass()
int MuCharacter::getClassCode()
string MuCharacter::getRawClass()
MuCharacter MuCharacter::setClass(mixed $newVal)
MuCharacter MuCharacter::addStrength(int $addVal)
MuCharacter MuCharacter::addAgility(int $addVal)
MuCharacter MuCharacter::addVitality(int $addVal)
MuCharacter MuCharacter::addEnergy(int $addVal)
MuCharacter MuCharacter::addCommand(int $addVal)
MuCharacter MuCharacter::remStrength(int $remVal)
MuCharacter MuCharacter::remAgility(int $remVal)
MuCharacter MuCharacter::remVitality(int $remVal)
MuCharacter MuCharacter::remEnergy(int $remVal)
MuCharacter MuCharacter::remCommand(int $remVal)
int MuCharacter::getLevel()
int MuCharacter::setLevel(int $newVal)
int MuCharacter::getExp(int $newVal)
MuCharacter MuCharacter::addLevel(int $addVal)
MuCharacter MuCharacter::remLevel(int $remVal)
bool MuCharacter::isBlocked()
int MuCharacter::getMuteTime()
MuCharacter MuCharacter::mute(int $minutes)
MuCharacter MuCharacter::unmute()
MuCharacter MuCharacter::block()
MuCharacter MuCharacter::unblock()
bool MuCharacter::isGM()
int MuCharacter::getMapID()
string MuCharacter::getMap()
MuCharacter MuCharacter::getGM(int $gmCode=8)
MuCharacter MuCharacter::unmakeGM()
MuCharacter MuCharacter::move(mixed $map, int $x, int $y)
MuCharacter MuCharacter::warp(mixed $map)
MuCharacter MuCharacter::setX(int $x)
MuCharacter MuCharacter::setY(int $y)
int MuCharacter::getResets()
MuCharacter MuCharacter::addResets(int $addVal)
MuCharacter MuCharacter::setResets(int $newVal)
MuCharacter MuCharacter::remResets(int $remVal)
MuAccount MuCharacter::account()
MuCharacter MuCharacter::setNumArray(array $vals)
array MuCharacter::getInfo() [array<string=>mixed>]
MuGuild MuCharacter::guild()
====MuAccount CLASS====
string MuAccount::getPassword()
string MuAccount::getUsername()
string MuAccount::getName()
MuAccount MuAccount::setName(string $newVal)
bool MuAccount::isBlocked()
string MuAccount::getSecretQuestion()
MuAccount MuAccount::setSecretQuestion(string $newVal)
string MuAccount::getSecretAnswer()
MuAccount MuAccount::setSecretAnswer(string $newVal)
MuAccount MuAccount::block()
MuAccount MuAccount::unblock()
bool MuAccount::hasPassword(string $password)
MuAccount MuAccount::setPassword(string $newVal)
MuAccount MuAccount::setPersonalID(string $newID)
string MuAccount::getPersonalID()
MuAccount MuAccount::setEmail($newVal)
string MuAccount::getEmail()
bool MuAccount::isOnline()
int MuAccount::onlineHours()
MuCharacter MuAccount::getLastUsedChar()
string MuAccount::getLastUsedServer()
array MuAccount::characters() [array<MuCharacter>]
array MuAccount::getInfo() [array<string=>mixed>]
string MuAccount::get(string $columnName)
MuAccount MuAccount::set(string $columnName, string $newVal)
====MuAccountCreator CLASS====
MuAccountCreator MuAccountCreator::setUsername(string $val)
MuAccountCreator MuAccountCreator::setPassword(string $val)
MuAccountCreator MuAccountCreator::setSecretQuestion(string $val)
MuAccountCreator MuAccountCreator::setSecretAnswer(string $val)
MuAccountCreator MuAccountCreator::setName(string $val)
MuAccountCreator MuAccountCreator::setEmail(string $val)
MuAccountCreator MuAccountCreator::setPersonalID(string $val)
MuAccountCreator MuAccountCreator::set(string $clumnName, string $to)
MuAccountCreator MuAccountCreator::save()
====MuGuild CLASS====
string MuGuild::getName()
MuCharacter MuGuild::getGM()
MuCharacter MuGuild::getAGM()
array MuGuild::getBMs() [array<MuCharacter>]
array MuGuild::getChars()
array MuGuild::getMembers()
string MuGuild::getGMC()
====MuQueryResult CLASS====
array MuQueryResult::fetchArray()
int MuQueryResult::affectedRows()
void MuQueryResult::free()
array MuQueryResult::fetchRow()
object(stdclass) MuQueryResult::fetchObject()
MuQueryResult MuQueryResult::nextResult()
EXAMPLES :
Example 1. Including MuEngine
PHP Code:
<?php
include 'muengine/all';
?>
Example 2. Establishing a connection to your database
PHP Code:
<?php
include 'muengine/all';
$db = new MuDatabase(); // Note that MuDatabase is the ONLY class we have to instanciate this way.
$db->setConnType(CONNTYPE_MSSQL);
$db->setHost('127.0.0.1');
$db->setUsername('sa');
$db->setPassword('sqlpassword');
$db->connect() or die('Could not connect to SQL database');
/* Note that we also could have done
$db->setConnType(CONNTYPE_MSSQL)->setHost('127.0.0.1')->setUsername('sa')->setPassword('sqlpassword')->connect()
Since all setters return the current object. */
?>
IN THE FOLLOWING EXAMPLES I WILL NOT REWRITE THE CONNECTION CODE. I ASSUME $db IS A VALID DATABASE CONNECTION.
Example 2. Getting a character's account name
PHP Code:
<?php
echo $db->character('MyCharacter')->account()->getUsername();
?>
Example 2. Getting all of an account's characters
PHP Code:
<?php
foreach($db->account('myaccount')->characters() as $character)
echo $character->getName();
?>
Example 3. Setting all of an account's character stats to 32767
PHP Code:
<?php
foreach($db->account('myaccount')->characters() as $character) {
$character->setStrength(32767)->setAgility(32767)->setVitality(32767)->setEnergy(32767);
if($character->getRawClass() == 'dl') {
$character->setCommand(32767);
}
}
/* However, this will run 4 different queries per character.
Performance-wise, it would have been better to use setNumArray(), but the above way is the easiest to read */
?>
Example 4. Blocking the assistant guild master of a specific guild
PHP Code:
<?php
$agm = $db->guild('myguild')->getAGM();
$agm->block();
?>
Example 5. Creating an account
PHP Code:
<?php
$accountcreator = $db->accountCreator();
$accountcreator->setUsername('mynewacc');
$accountcreator->setPassword('mynewpwd');
$accountcreator->setSecretQuestion('lol');
$accountcreator->setSecretAnswer('yeah');
$accountcreator->setName('mynewacc');
$accountcreator->setPersonalID(111111);
$accountcreator->save();
?>
Example 6. Displaying all of a guild's characters, as well as if they are online
PHP Code:
<?php
foreach($db->guild('myguild')->getChars() as $character) {
echo $character->getName();
if($character->account()->isOnline())
echo ' - ONLINE';
echo '<br />';
}
Example 7. Running an SQL query <-- even though you shouldn't need it
PHP Code:
<?php
/* MuDatabase::execute() escapes its arguments after the first one AUTOMATICALLY, THEREFORE we need to remove the magic quote effect
(it is also good to do it preventively, as PHP6 will have magic quotes removed) */
if(@get_magic_quotes_gpc()) {
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
}
/* SAFE WAY to run a query with user input
You can use any format that printf() uses */
$db->execute("SELECT * FROM Character WHERE Name = '%s'", $_POST['character']);
/* UNSAFE WAY: NEVER DO THIS!! */
$db->execute("SELECT * FROM Character WHERE Name = '" . $_POST['name'] . "'");
?>
DOWNLOADS
Download the latest version at https://sourceforge.net/projects/muengine/files/
IF YOU WANT TO CONTRIBUTE TO THIS PROJECT, PLEASE SEND ME A FORUM PM ON RAGEZONE AT LifeTaker.
OF COURSE YOUR WORK WILL BE CREDITED.
TODO
- Documentation
- MuItem class
- Make MuAccountCreator more convenient
- Make sure MuEngine supports all server databases
Re: [PHP] MuEngine: PHP Library for MuOnline
Re: [PHP] MuEngine: PHP Library for MuOnline
1 idea: remove stripslashes use htmlspecialchars
Re: [PHP] MuEngine: PHP Library for MuOnline
What we really need
is
MuItem convert to/from DB format,
magiclist, inventory, warehouse functions
(harmony, 380 lvl, socket option support, season 4-5 item support).
All i saw in public about that- have very bad design.
We need fast(without useless customs, logs, checks and so on), stable and easy to use.
All i want to say- to concentrate brain on hard work on things like what i say in start(MuItem and so on).
Do this- and it will be really great.
Re: [PHP] MuEngine: PHP Library for MuOnline
Re: [PHP] MuEngine: PHP Library for MuOnline
@Stoff: I agree with you, but I must admit that this release is great aswell.
@Shagoon: Still doing your thing :D
Re: [PHP] MuEngine: PHP Library for MuOnline
Quote:
Originally Posted by
diablo71
1 idea: remove stripslashes use htmlspecialchars
Stripslashes = remove backslashes in front of ", ', and \x00.
Htmlspecialchars = convert < > to < > respectively.
So I don't really see a relation =/
Quote:
Originally Posted by
Stoff
What we really need
is
MuItem convert to/from DB format,
magiclist, inventory, warehouse functions
(harmony, 380 lvl, socket option support, season 4-5 item support).
All i saw in public about that- have very bad design.
We need fast(without useless customs, logs, checks and so on), stable and easy to use.
All i want to say- to concentrate brain on hard work on things like what i say in start(MuItem and so on).
Do this- and it will be really great.
Yeap, it is currently on the To Do list. I am a bit busy with school atm but I'll soon get to work on it. If you can help with anything, please send me a PM =)
Re: [PHP] MuEngine: PHP Library for MuOnline
very usgfull thing but something is missing and i cant understand what
oh maybe the top200 chars script and add some explannation about the arrays in php and one more thing add exampales for session_start();
and maybe a hall php about index could be nice
Re: [PHP] MuEngine: PHP Library for MuOnline
Quote:
Originally Posted by
Y2J
very usgfull thing but something is missing and i cant understand what
oh maybe the top200 chars script and add some explannation about the arrays in php and one more thing add exampales for session_start();
and maybe a hall php about index could be nice
Ok, I have added the exact return types for methods that return arrays.
Top 200? This would be an example of how to use it
PHP Code:
<?php
$count = 0;
foreach($db->rankings(200, true) as $char)
echo ++$count, ' - ', htmlspecialchars($char->getName());
?>
What is the problem with session_start() exactly?
Re: [PHP] MuEngine: PHP Library for MuOnline
bro give me your skype or msn if you have
Re: [PHP] MuEngine: PHP Library for MuOnline
Quote:
Originally Posted by
LifeTaker
Yeap, it is currently on the To Do list. I am a bit busy with school atm but I'll soon get to work on it. If you can help with anything, please send me a PM =)
How can I help?
As I understand it - it's not work for the team, it is enough a person.
When many people are working on things that requires the attention of one person, it creates unnecessary confusion. Correct me if I'm wrong.
If you want, I can post some information, which describe in some degree work with MuItem database format and similar. But I think you already have this.
Re: [PHP] MuEngine: PHP Library for MuOnline
Quote:
Originally Posted by
Stoff
If you want, I can post some information, which describe in some degree work with MuItem database format and similar. But I think you already have this.
Not really, I was looking around and I found no info whatsoever on how items are structured in the db. The best thing I could find was a code from Savoy, but I still don't know how sockets, JOH and so on are transcribed in the db. Then I'd have to experiment with MuMaker to see which codes it generates, but I really have no time for that.
Quote:
Originally Posted by
Stoff
How can I help?
As I understand it - it's not work for the team, it is enough a person.
When many people are working on things that requires the attention of one person, it creates unnecessary confusion. Correct me if I'm wrong.
You're correct, I wasn't talking about a team, but helping even without coding anything (just like you mentionned below).
Re: [PHP] MuEngine: PHP Library for MuOnline
Great work man ^^
NOw really easy create musite :)
Re: [PHP] MuEngine: PHP Library for MuOnline
Quote:
Originally Posted by
Stoff
What we really need
is
MuItem convert to/from DB format,
magiclist, inventory, warehouse functions
(harmony, 380 lvl, socket option support, season 4-5 item support).
All i saw in public about that- have very bad design.
We need fast(without useless customs, logs, checks and so on), stable and easy to use.
All i want to say- to concentrate brain on hard work on things like what i say in start(MuItem and so on).
Do this- and it will be really great.
This is easy to do...
will be something like this
PHP Code:
function ItemToHex($Group, $ID, $Level, $Skill, $Luck, $Life, $Exc, $Anc, $_380Opt, $JOH, $Sck1, $Sck2, $Sck3, $Sck4, $Sck5)
{
$ItemID = $Group*512+$ID;
$buf1 = $ItemID % 256;
$buf2 = $Level * 8;
$buf2 |= $Skill * 128;
$buf2 |= $Luck * 4;
$buf2 |= $Life & 3;
$buf3 = 255;
$buf4 = 0;
$buf5 = 0;
$buf6 = 0;
$buf7 = 0;
$buf8 = (($ItemID & 256) >> 1);
if($Life > 3)
{
$buf8 |= 64;
}
$buf8 |= $Exc;
$buf9 = $Anc;
$buf10 = (($ItemID & 7680 ) >> 5);
$buf10 |= (($_380Opt & 128 ) >> 4);
if($JOH >= 255)
{
$JOH = 0;
}
$buf11 = $JOH;
$buf12 = $Sck1;
$buf13 = $Sck2;
$buf14 = $Sck3;
$buf15 = $Sck4;
$buf16 = $Sck5;
$output = dechex($buf1);
$output .= dechex($buf2);
$output .= dechex($buf3);
$output .= dechex($buf4);
$output .= dechex($buf5);
$output .= dechex($buf6);
$output .= dechex($buf7);
$output .= dechex($buf8);
$output .= dechex($buf9);
$output .= dechex($buf10);
$output .= dechex($buf11);
$output .= dechex($buf12);
$output .= dechex($buf13);
$output .= dechex($buf14);
$output .= dechex($buf15);
$output .= dechex($buf16);
$output = strtoupper($output);
echo "ITEM HEX: 0x$output";
}
Then Call:
PHP Code:
ItemToHex(0,22,15,1,1,7,63,0,0,0,0,0,0,0,0);
// This will make a Bone Blade +15+7+Skill+Luck FULL EXC;
ItemToHex(7,1,8,0,1,2,0,10,0,0,0,0,0,0,0);
// This will make a bronze Helm +8+2+Luck ANC;
Tip: This functions prints Item HexCode
Tip2: All Item have 255 durability
Credits: zemattana
Re: [PHP] MuEngine: PHP Library for MuOnline
zemattana - thanks for this function, i'll do function to build itemcode from hex, and upload it.
also i want to make class, fro view inventory and warehouse ^^)
I have problem with $db->ranking
when i call it, my fast-CGI die with self error(
any suggestions?