15.10.2011, 11:02
(
Последний раз редактировалось SmiT; 29.10.2011 в 21:02.
)
Introduction
Credits
- I'll show you how to create a simple registration and login system with dialogs, using DOF2.
- Double-O-Files 2 aka DOF2 it's a fast file reader and writer created by Double-O-Seven.
- Download Double-O-Files 2.
- Once downloaded, Copy/Cut "Double-O-Files_2.inc" in your ../pawno/include folder.
- We need to include the file, so at the top of our script: pawn Код:#include <Double-O-Files_2>
- Let's also define some Dialog ID's and colors:
pawn Код:#define DIALOG_REGISTER 1
#define DIALOG_LOGIN 2
#define WHITE "{FFFFFF}"
#define RED "{F81414}"
#define GREEN "{00FF22}"
- Now, we need to store our variables in an enum.
- Enumerations are a very useful system for representing large groups of data and modifying constants quickly. There are a few main uses - replacing large sets of define statements, symbolically representing array slots (these are actually the same thing but they look different) and creating new tags.
pawn Код:enum P_ENUM
{
pKills,
pDeaths,
pMoney,
pAdmin
}
new PlayerInfo[ MAX_PLAYERS ][ P_ENUM ];
- Let's create a simple stock function to return the path were our accounts will be saved, in this case: "../scriptfiles".
- You can change the accounts path by opening "Double-O-Files_2.inc" and changing pawn Код:#define USER_FILE_PATH "%s.ini"
pawn Код:stock USER_FILE(playerid)
{
new
STR[ 128 ],
P_NAME[ MAX_PLAYER_NAME ];
GetPlayerName( playerid, P_NAME, sizeof ( P_NAME ) );
format( STR, sizeof ( STR ), USER_FILE_PATH, P_NAME);
return
STR;
}
- Let's create a simple stock function to load the player stats.
pawn Код:stock Load_Player_Stats(playerid)
{
PlayerInfo[ playerid ][ pKills ] = DOF2_GetInt( USER_FILE( playerid ),"Kills");
PlayerInfo[ playerid ][ pDeaths ] = DOF2_GetInt( USER_FILE( playerid ),"Deaths");
PlayerInfo[ playerid ][ pMoney ] = DOF2_GetInt( USER_FILE( playerid ),"Money");
PlayerInfo[ playerid ][ pAdmin ] = DOF2_GetInt( USER_FILE( playerid ),"AdminLevel");
GivePlayerMoney(playerid, PlayerInfo[ playerid ][ pMoney ]);
}
- Let's scroll down to "OnPlayerConnect" callback, we must check when a player connects to the server, if he is registered or not:
pawn Код:public OnPlayerConnect(playerid)
{
if( DOF2_FileExists ( USER_FILE ( playerid ) ) )
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,"Welcome.Please log-in",""WHITE"Type your "GREEN"password "WHITE"here to log-in",#Log-in,#Quit);
}
else
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,"Please register!",""WHITE"Type your "GREEN"password "WHITE"here to register.",#Register,#Quit);
}
return true;
} - DOF2_FileExists will check if the player file exist. We use our stock as the parameter. If the file exist, the player will be promted to a dialog telling him to login. "Else" if the player file don't exist, he will be promted to another dialog telling him to register.
- Let's scroll down to "OnDialogResponse" callback.
pawn Код:public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch( dialogid )
{
case DIALOG_REGISTER:
{
if ( !response ) return Kick( playerid );
if( response )
{
if( !strlen ( inputtext ) ) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Welcome.Please log-in","You have entered an "RED"invalid"WHITE" password\n"WHITE"Type your "GREEN"password "WHITE"here to log-in",#Register,#Quit);
DOF2_CreateFile( USER_FILE ( playerid ), inputtext );
DOF2_SetInt( USER_FILE ( playerid ), "Kills", 0);
DOF2_SetInt( USER_FILE ( playerid ), "Deaths", 0);
DOF2_SetInt( USER_FILE ( playerid ), "Money", 1000);
DOF2_SetInt( USER_FILE ( playerid ), "AdminLevel", 0);
DOF2_SaveFile();
SetSpawnInfo( playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0 );
SpawnPlayer( playerid );
GivePlayerMoney(playerid, 1000);
}
}
case DIALOG_LOGIN:
{
if ( !response ) return Kick( playerid );
if( response )
{
if( DOF2_CheckLogin( USER_FILE( playerid ), inputtext ) )
{
Load_Player_Stats(playerid);
}
else
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login","You have entered an "RED"incorrect "WHITE"password.\n{FFFFFF}Type your "GREEN"password "WHITE"below to login.",#Log-in,#Quit);
}
return 1;
}
}
}
return 1;
} - Instead of using "if" statements to check the dialogs, I used cases as they are faster. We swtich through all our dialogs, we check if "DIALOG_REGISTER" appears, the " if ( !response ) " checks if the player press the second button (Quit) if so it will kick the player. We check if the player press the first button (Register) using " if ( response ) ". The " if ( !strlen ( inputtext ) ) " will check if NOTHING has been entered in the dialog (e.g player connects and just press "Register") if so, you will be prompted to another dialog telling you the password is INCORRECT.
- DOF2_CreateFile will create the file for the player, also create the password for him and hash it. Then we write into the file some integers using DOF2_SetInt, after that we save the file using DOF2_SaveFile(). We set the player spawn, spawn him and give him $1000.
- Same to DIALOG_LOGIN. DOF2_CheckLogin will check if the player enteres the correct password, if so it will load the player stats using our stock. "Else" if he enteres the wrong password, he will be promted to another dialog telling him the password is incorrect.
- After the player disconnects, we need to write and save the player stats, so let's scroll down to "OnPlayerDisconnect" callback.
pawn Код:public OnPlayerDisconnect(playerid, reason)
{
DOF2_SetInt( USER_FILE ( playerid ), "Kills", PlayerInfo[ playerid ][ pKills ]);
DOF2_SetInt( USER_FILE ( playerid ), "Deaths", PlayerInfo[ playerid ][ pDeaths ]);
DOF2_SetInt( USER_FILE ( playerid ), "Money", GetPlayerMoney( playerid ));
DOF2_SetInt( USER_FILE ( playerid ), "AdminLevel", PlayerInfo[ playerid ][ pAdmin ]);
DOF2_SaveFile();
return 1;
}
- We need to add values to the player "Kills" and "Deaths" so we scroll down to "OnPlayerDeath" callback.
pawn Код:public OnPlayerDeath(playerid, killerid, reason)
{
if( killerid != INVALID_PLAYER_ID )
{
PlayerInfo[ playerid ][ pKills ] ++;
}
PlayerInfo[ playerid ][ pDeaths ] ++;
return 1;
} - Killerid is the one who killed the person. Playerid is the one who dies. We check if killerid isn't equal to INVALID_PLAYER_ID. We increase the killerid kills, and we increase the playerid deaths.
- Add pawn Код:DOF2_Exit();
pawn Код:public OnFilterScriptExit()
{
DOF2_Exit();
return 1;
}Код:symbol is never used: "DOF2_Exit"
Credits