Dynamic MOTD?
#1

Coming back from a lengthy break from coding, and I forgot a good portion of Y_Ini.

How would one create a dynamic MOTD for his server? I'll add the code I have managed to put together below.

Also, how would you use ini parsefile in this type of scenario when loading from a file?

pawn Код:
#define ServerPath "Server/%s.ini"
enum ServerInfo
{
    sServerMessage[100]
};

new sInfo[ServerInfo];



//This is a very basic command I have created to create the actual file.
CMD:motdtest(playerid, params[])
{
    new path[128];
    format(path,sizeof(path), ServerPath);
    if(fexist(path))
    {
        INI_ParseFile(path(server),"LoadServer_data", .bExtra = true, .extra = ?);
    }
    else
    {
        new INI:file = INI_Open(path);
        INI_SetTag(file, "Server");
        INI_WriteString(file, "ServerMessage", "Change me via /servermessage.");
        INI_Close(file);
    }
    return 1;
}



public LoadServer_data(name[], value[])
{
    INI_String("ServerMessage", sInfo[sServerMessage], 100);
    return 1;
}
Thank you for any help.
Reply
#2

pawn Код:
CMD:servermessage(playerid, params[])
{
    new string[100];
    if(sscanf(params, "s[100]", params))
    {
        SendClientMessage(playerid, COLOR_WHITE, "USAGE: /servermessage [message]");
        return 1;
    }
    format(sInfo[sServerMessage], sizeof(sInfo[sServerMessage]), "%s", params);
    return 1;
}
pawn Код:
public OnPlayerSpawn(playerid)
{
    new string[100];
    if(strlen(sInfo[sServerMessage]) > 0)
    {
        format(string, sizeof(string), "Server Message: %s", sInfo[sServerMessage]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
Reply
#3

How would that save it to a file and make it load?
Reply
#4

You can probably use normal file functions. In that case,
pawn Код:
stock SaveMOTD()
{
      if(fexists("motd.ini"))
      {
            new File:motd = fopen("motd.ini", io_write);
            fwrite(motd, serverMOTD);
            fclose(motd);
            return 1;
      }
      return true;
}

stock LoadMOTD()
{
    if(fexists("motd.ini"))
    {
          new File:motd = fopen("motd.ini", io_read);
          fread(file, serverMOTD);
          fclose(file);
          return 1;
    }
    return true;
}
And then simply,
pawn Код:
public OnPlayerConnect(playerid)
{
      new string[64];
      format(string, sizeof(string), "MOTD: %s", serverMOTD);
      SendClientMessage(playerid, -1, string);
      return 1;
}
Reply
#5

pawn Код:
stock INI_Get(filename[],key[])
{
    new File:F,string[128];
    new sname[24],sval[24];
    F = fopen(filename,io_read);
    if(!F) return sname;    while(fread(F,string))
    {
        sscanf(string,"p<=>s[24]s[24]",sname,sval);
        if(!strcmp(sname,key))
        {
            sval[strlen(sval)-2] = 0;
            fclose(F);
            return sval;
        }
    }
    fclose(F);
    sname[0] = '\0';
    return sname;
}

stock LoadMessage()
{
    new path[128];    
    format(path,sizeof(path), ServerPath);
    new INI:file = INI_Open(path);
    INI_SetTag(file, "Server");
    INI_WriteString(file, "ServerMessage", sInfo[sServerMessage]);
    INI_Close(file);
    return 1;
}

stock SaveMessage()
{  
    new path[128];    
    format(path,sizeof(path), ServerPath);
    new INI:file = INI_Open(path);
    INI_SetTag(file, "Server");
    INI_Get(file, "ServerMessage");
    INI_Close(file);
    return 1;
}
I hate y_ini .
Reply
#6

Still having issues. Can anyone help me out? I'd like to know how to do it with yini.
Reply
#7

Hi.
i will explain the code with comment in the script.


pawn Код:
#include <YSI\y_ini>
//>Including y_ini from YSI Library

#include <zcmd>
//> i'll use ZCMD to make command

#include <sscanf2>
//>For command

#define ServerFile "Server/Info.ini" //>Definind .ini file address in > scriptfiles/Server/"Ini file"
//>You have to create "Server" Folder in scriptfile folder in your server files.

enum sInfo //>Creating enum for variables that i want to load my ini file data into it!
{
 LoginMSG[128] //> String
}
new ServerInfo[sInfo]; //>My Variable(s)

//>Creating a callback that i will use to load my ini file into variables With "INI_ParseFile" function.
forward LoadServerInfo(name[], value[]);
public LoadServerInfo(name[], value[])
{
    INI_String("LoginMSG",ServerInfo[LoginMSG],128); //> i use "INI_String" to load a string into "ServerInfo[LoginMSG]" Variable
    return 1;
}

public OnGameModeInit() //>Load Data from file when server start
{
  INI_ParseFile(ServerFile, "LoadServerInfo", .bExtra = false, .extra = 0); //>Load "ServerFile" path with "LoadServerInfo" Function that i created.
  return 1;
}

public OnPlayerConnect(playerid) //>Sending ServerMSG with formating "ServerInfo[LoginMSG]" Variable...
{
//>My Server Message Loaded in "ServerInfo[LoginMSG]" Variable with "LoadServerInfo" Callback
 new string[128];
 format(string,sizeof(string),"%s",ServerInfo[LoginMSG]);
 SendClientMessage(playerid,-1,string);
 return 1;
}

//>Creatind command for creating or updating "Info.ini" File !!
CMD:servermsg(playerid, params[])
{
  new servermsg[128];
  if(sscanf(params,"s[128]",servermsg)) return SendClientMessage(playerid,-1,"Usage: /servermsg [Your Login Message]");
  new INI:File = INI_Open("Server/Info.ini"); //>Open Or Create Info.ini File in /scriptfiles/Server/Info.ini
  INI_SetTag(File,"sdata"); //>Set "[sdata]" tag in ini file for assortment data
  INI_WriteString(File,"LoginMSG",servermsg); //>wreite "servermsg" that i used in sscanf (my msg)
  INI_Close(File); //>Closing File that i opened for writing
  SendClientMessage(playerid,-1,"Your Srver Message Successfully Created."); //> :D :D
  return 1;
}
hope i helped...

Good Luck.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)