functions.php
```
<?php

### player id ####

find_local_id(<id>)                                                     // - searches for the local id slot. it returns the slot if it is found, else return 0.
get_player_ip()                                                         // - return the connected player ip
get_unique_id_by_username(<username>)           // - return unique id of the specified username
get_unique_id_by_local(<local_id>)                      // - return unique id of the specified local id
get_player_username(<unique_id>)                        // - return <unique_id> username
get_local_id_by_ip([ip])                                        // - if [ip] is specified, returns local id of that ip, else returns local id of the current connected ip
get_local_id_by_unique(<unique_id>)                     // - return local id of the <unique_id>

## player ####

get_active(<unique_id>)                                          // - return the unique id activity (1 if is active, 0 if isn't)
get_position(<unique_id>)                                        // - return player position (x,y,rotation)
get_gold(<unique_id>)                                            // - return player's gold
get_last_time_active(<unique_id>)                        // - return last time when the player was active
set_last_time_active(<unique_id>)                        // - set the current time into last_time_active.txt
set_position(<unique_id>,<x>,<y>,<rot>)          // - set the specified position into position.txt
set_gold(<unique_id>,<gold>)                             // - set the specified gold into gold.txt
set_active(<unique_id>,<state>)                          // - set the specified state (0 or 1)

inventory_get_slot(<unique_id>,<slot>)
inventory_set_slot(<unique_id>,<slot>,<item_id>)

player_get_troop_id(<unique_id> , <troop_slot>)                 //- W.I.P.
player_set_troop_slot(<unique_id>,<slot>,<troop_id>)   //- W.I.P.

send_initial_players(<unique_id>)                       // - sends all active players data
save_to_log(<string>,<dev>)                                     // - if <dev> is not 1, then the string gets saved into server log, else the string get saved into dev log

deconnect_player(<unique_id>)                            // - deconnect a player by deleting/setting 0 some files


send_data_to_player(<unique_id>,<arguments_number>,<data_array>,[strings_number]) // - sends the <data_array> to <unique_id>. data_array[0] = event number .

                                                  //    arguments number = int number + string number (without the event)


## files ##

read_file(<file_path>)                                                                            //    -returns file contents from the specified path
write_file(<file_path>,<contents>)                                                        //    -writes <contents> to the specified path
move_file_contents(<source_file>,<destination_file>,[delete]) //        - copy all contents from <source_file> to <destination file>,

          //    if [delete] is 1, then the source file becomes empty(0)
init_player(<unique_id>)                                                                          //    - creates all the files and folders for the specified id
make_dir(<path>)                                                                                          //    - creates a directory at the specified path


## other ##

check_string(<string>)                  // - return 1 if the <string> contains only alphanumeric and _-*[]~ characters, else return 0



?>
            ```



code.php
```
<?php
function main_code(unique_id,event,nr_args,args)
{
        event=args[0];
        if(unique_id==0&&event!=1&&event!=2)
        {
                send_data_to_player(unique_id,[2,0]);//force log in
        }else{

        if(event==1)
                        {
                                //REGISTER ACCOUNT
                                //INPUT:arg1-username  arg2-password
                                //OUTPUT:arg1- state  arg2 -unique id
                                if(isset(args[1])&&isset(args[2]))
                                {
                                        username=args[1];
                                        password=args[2];
                                        if(check_string(username) and check_string(password) and !(is_numeric(password)) and !(is_numeric(username)))
                                        {
                                                if(file_exists("accounts/".username.".txt"))
                                                {
                                                        send_data_to_player(unique_id,[1,0]);//the account already exists
                                                }else{
                                                        last_unique_id=read_file("server_vars/player.txt") + 1;
                                                        write_file("server_vars/player.txt",last_unique_id);

                                                        write_file("username_id/".username.".txt",last_unique_id);
                                                        write_file("accounts/".username.".txt",password);

                                                        make_dir('players/'.last_unique_id.'/');//create the id directory
                                                        init_player(last_unique_id,username);

                                                        send_data_to_player(unique_id,[1,1,last_unique_id]);//succesfull created account
                                                }
                                        }else{
                                                send_data_to_player(unique_id,[1,4]);//invalid characters used
                                        }
                                }
                        }
                else if(event==2)
                        {
                        //LOG IN
                        //INPUT:arg1-username  arg2-password
                        //OUTPUT:arg1- state  arg2 -unique id arg3- local id
                                if(isset(args[1])&&isset(args[2]))
                                {
                                        username=args[1];
                                        password=args[2];
                                        if(check_string(username) and check_string(password) and !(is_numeric(password)) and !(is_numeric(username)))
                                        {
                                                if(file_exists("accounts/".username.".txt"))
                                                {
                                                        real_password=read_file("accounts/".username.".txt");
                                                        if(real_password==password)
                                                        {
                                                                local_id_slot=find_local_id(0);
                                                                if(local_id_slot!=0)
                                                                {
                                                                        if(file_exists("ip_login/".get_player_ip().".txt"))
                                                                        {
                                                                                unique_id_real = get_unique_id_by_username(username);
                                                                                send_data_to_player(unique_id,[2,2,unique_id_real,get_local_id_by_ip()]);//succesfull log in
                                                                                send_initial_players(unique_id_real);
                                                                        }else{
                                                                                unique_id_real =get_unique_id_by_username(username);
                                                                                write_file("ip_login/".get_player_ip().".txt",local_id_slot);
                                                                                write_file("local_id/".local_id_slot.".txt",unique_id_real);
                                                                                write_file("players/".unique_id_real."/active.txt",1);
                                                                                write_file("players/".unique_id_real."/last_time_active.txt",time());
                                                                                write_file("players/".unique_id_real."/ip.txt",get_player_ip());
                                                                                write_file("players/".unique_id_real."/local_id.txt",local_id_slot);
                                                                                write_file("players/".unique_id_real."/ping.txt",0);
                                                                                write_file("players/".unique_id_real."/ping_var.txt",0);
                                                                                send_data_to_player(unique_id,[2,2,unique_id_real,local_id_slot]);//succesfull log in
                                                                                send_initial_players(unique_id);

                                                                                ti_on_player_connect(unique_id_real);

                                                                                }
                                                                }else{
                                                                        send_data_to_player(unique_id,[2,3]);//the server is full
                                                                }
                                                        }else{
                                                                send_data_to_player(unique_id,[2,1]);//invalid user or pass
                                                        }
                                                }else{
                                                        send_data_to_player(unique_id,[2,1]);//invalid user or pass
                                                }
                                        }else{
                                                send_data_to_player(unique_id,[2,4]);//invalid characters used
                                        }
                                }
                        }
                else if(event==3)
                        {
                                //CHAT
                                //Input arg1 - message
                                if(isset(args[1]))
                                {
                                        message = args[1];
                                        if(message=='')
                                        {
                                        }else{
                                                if(is_numeric(message))
                                                {
                                                        message = message.' ';
                                                }
                                                username=get_player_username(unique_id);
                                                for(i=1;i<=10;i++)
                                                {
                                                        u_id = get_unique_id_by_local(i);
                                                        if(u_id!=0)
                                                        {
                                                                send_data_to_player(u_id,[3,message,username],2);
                                                        }
                                                }
                                        }

                                }
                        }
                else if(event==4)
                        {
                                //SAVE PLAYER POSITION
                                //Input: arg1-x arg2-y arg3-rotation
                                //output:none
                                if(isset(args[1]) and isset(args[2]) and isset(args[3]))
                                {
                                        x=args[1];
                                        y=args[2];
                                        rot=args[3];
                                        global allow_teleport;

                                        if(allow_teleport)
                                        {
                                                set_position(unique_id,x,y,rot);
                                        }else{
                                                position=get_position(unique_id);
                                                old_x=position[0];
                                                old_y=position[1];
                                                old_rot=position[2];
                                                distance=sqrt( pow(old_x - x , 2)  +  pow(old_y - y , 2) );
                                                if(distance < 1000)
                                                        {
                                                                set_position(unique_id,x,y,rot);
                                                        }
                                                else
                                                        {
                                                                to_send[0]=5;
                                                                to_send[1]=old_x;
                                                                to_send[2]=old_y;
                                                                to_send[3]=old_rot;

                                                                send_data_to_player(unique_id,to_send);
                                                                // send_data_to_player(unique_id,[15," ".distance,0xFF0000],1);
                                                        }
                                        }
                                }
                        }
                else if(event==6)
                {
                                //SEND PLAYERS POSITION
                                //Input:none
                                //Output:arg1 - number of players  arg2 - local player id arg3 - x arg4- y arg5 - rot arg6 -local player id ....
                                number_of_players=0;
                                to_send[0]=6;
                                c=2;
                                for(i=1;i<=10;i++)
                                {
                                        u_id=get_unique_id_by_local(i);
                                        if(u_id!=0 and u_id!=unique_id)
                                        {
                                                number_of_players++;
                                                to_send[c]=i;
                                                c++;

                                                position=get_position(u_id);
                                                x=position[0];
                                                y=position[1];
                                                rot=position[2];

                                                to_send[c]=x;
                                                c++;
                                                to_send[c]=y;
                                                c++;
                                                to_send[c]=rot;
                                                c++;
                                        }
                                }
                                c--;
                                to_send[1]=number_of_players;

                                send_data_to_player(unique_id,to_send);
                }
                else if(event==9)
                        {
                                //PING
                                if(isset(args[1]))
                                {
                                        if(args[1]==0)
                                        {
                                                write_file("players/".unique_id."/ping_var.txt",round(microtime_float(), 2));
                                                send_data_to_player(unique_id,[9,1]);
                                        }else{
                                                time=read_file("players/".unique_id."/ping_var.txt");
                                                ping=round(round(round(microtime_float(), 2) - round(time,2),2)*100);
                                                write_file("players/".unique_id."/ping.txt",ping);
                                                write_file("players/".unique_id."/ping_var.txt",0);

                                                c=2;
                                                data[0]=9;
                                                data[1]=0;
                                                for(i=1;i<=10;i++)
                                                {
                                                        u_id=get_unique_id_by_local(i);
                                                        if(u_id!=0)
                                                        {
                                                                data[c]=read_file("players/".u_id."/ping.txt");
                                                                c++;
                                                        }else{
                                                                data[c]=0;
                                                                c++;
                                                        }
                                                }

                                                send_data_to_player(unique_id,data);
                                        }
                                }
                        }
                else if(event==10)
                        {
                                //SEND PLAYER INVENTORY
                                inv=read_file("players/".unique_id."/inventory.txt");
                                inv=explode("|",inv);
                                inv[0]=10;
                                send_data_to_player(unique_id,inv);
                        }
                else if(event==11)
                        {
                                //SEND PLAYER GOLD
                                send_data_to_player(unique_id,[11,get_gold(unique_id)]);
                        }
                else if(event==14)
                        {
                                //SEND PLAYER TROOPS
                                troops=read_file("players/".unique_id."/troops.txt");
                                troops=explode("|",troops);
                                nr=0;
                                foreach (a as troops)
                                {
                                        if(a!=-1)
                                                nr++;
                                }
                                troops[0]=14;
                                troops[1]=nr+2;//incrementing here, so we will not have to increment in the game
                                send_data_to_player(unique_id,troops);
                        }
                }
        }

?>
            ```



multiplayer.php
```
<?php
header_remove();
                function set_content_length(output)
                {
                        header("Content-Length: ".strlen(output));
                        return output;
                }
                ob_start("set_content_length");


include'functions/functions.php';
include'code.php';
 counter=1;

 //checking for active players
 last_time_check=read_file("server_vars/check_time.txt");
 if((time()-last_time_check) >=1)
 {
        write_file("server_vars/check_time.txt",time());
        check_players_activity();
 }
 //check end


 player_ip=get_player_ip();
 if(file_exists("ip_login/".player_ip.".txt"))
 {
        local_id=read_file("ip_login/".player_ip.".txt");
        unique_id=read_file("local_id/".local_id.".txt");
 }else{
        unique_id=0;
 }
 if(isset(_GET["event".counter]))
 {
 }elseif(unique_id==0){
        send_data_to_player(unique_id,[2,0]);
 }
 if(unique_id!=0)
        {
                write_file("players/".unique_id."/last_time_active.txt",time());
        }

 while((isset(_GET["event".counter])))
{
        data_args[0]=_GET["event".counter];//initialize data array
        if(isset(_GET["nrarg".counter]))
        {
                number_of_arguments=_GET["nrarg".counter];
                number_of_arguments--;//decrement 1, because we used the event arg

                for(i=1;i<=number_of_arguments;i++)//saving all args in the data array
                {
                        if(isset(_GET["arg".counter.'|'.i]))
                        {
                                data_args[i]=_GET["arg".counter.'|'.i];
                        }
                }
                event = _GET["event".counter];
                main_code(unique_id,event,number_of_arguments,data_args);//calling the main code(programable)
                counter++;
        }else{
                break;
        }
}
        send_data(unique_id);
?>
```






server_config.php
```
<?php
dev_log = 1; //it holds all the data(every single packet). it is for devs or for errors reporting. 1- enable 0 - disable
dev_log_path= 'logs/dev_log.txt';

server_log = 1;//enable log save
server_log_path= 'logs/server_log.txt';

allow_teleport = 1; //1 - allow teleport on the map; 0 - does not allow . If set to 0, it is possible to crash the game when you try to teleport.

starting_gold=2000;
?>
```



test.php
```
<?php
echo "All setups are fine!";
?>
            ```



functions/check_players_activity.php
```
<?php
function check_players_activity()
{
        for(i=1;i<=10;i++)
        {
                unique_id=read_file("local_id/".i.".txt");
                if(unique_id!=0)
                {
                        last_time_active=read_file("players/".unique_id."/last_time_active.txt");
                        if(time()-last_time_active > 3)//if more than 3 seconds passed since last active
                        {
                                deconnect_player(unique_id);

                        }
                }
        }
}
?>
            ```



functions/deconnect_player.php
```
<?php
        function deconnect_player(unique_id)
        {
                player_ip=read_file("players/".unique_id."/ip.txt");
                local_id=get_local_id_by_unique(unique_id);
                if(file_exists("ip_login/".player_ip.".txt"))
                {
                        unlink("ip_login/".player_ip.".txt");//deletes the file
                }
                write_file("local_id/".get_local_id_by_unique(unique_id).".txt",0);
                write_file("players/".unique_id."/active.txt",0);
                write_file("players/".unique_id."/ip.txt","0.0.0.0");
                write_file("players/".unique_id."/local_id.txt",0);

                ti_on_player_deconnect(unique_id,local_id);
        }
?>
            ```



functions/files_functions.php
```
<?php
function make_dir(path)
        {
                mkdir(path, 0700);
        }

function move_file_contents(file_source,file_output,delete=0)
        {
                contents=read_file(file_source);
                write_file(file_output,contents);
                if(delete==1)
                {
                        write_file(file_source,0);
                }
        }

function read_file(_FILE_PATH)
{
        if(file_exists(_FILE_PATH))
        {
                _CONTENTS = file(_FILE_PATH);
                _RESULT = implode(_CONTENTS);
                return _RESULT;
        }else{
                return 0;
        }
}

function write_file(_FILE_PATH,_CONTENTS)
{
        _FO = fopen(_FILE_PATH, "w");
        fwrite(_FO, _CONTENTS);
        fclose(_FO);
}
?>
            ```



functions/functions.php
```
<?php
include './server_config.php';

//functions
include 'functions/hardwired_functions.php';
include 'functions/init_player.php';
include 'functions/deconnect_player.php';
include 'functions/troops_functions.php';
include 'functions/player_id_functions.php';
include 'functions/player_functions.php';
include 'functions/files_functions.php';
include 'functions/other_functions.php';

//triggers
include './triggers/on_connect.php';
include './triggers/on_deconnect.php';
?>
            ```



functions/hardwired_functions.php
```
<?php

                function send_data(unique_id)
                {
                        global dev_log;

                        slots=[];
                        //decide wich slots to send, because there is a max of 100 args to be sended
                        total_lenght=0;

                        output='';
                        for(i=1;i<=10;i++)
                        {
                                args=read_file('players/'.unique_id.'/to_send/'.i.'.txt');
                                args=explode("|",args);
                                nr_str=read_file('players/'.unique_id.'/to_send/nr_string_'.i.'.txt');
                                nr_arg=count(args) - 1;


                                if( ( (total_lenght+nr_arg) <100) and (nr_arg!=0) and (read_file('players/'.unique_id.'/to_send/'.i.'.txt') != 0) )//max 100 args; will be tweaked later
                                {
                                        total_lenght+=nr_arg;
                                        output.=nr_arg.'|'.nr_str.'|'.implode("|",args).'|';

                                         //deleting the data
                                        write_file('players/'.unique_id.'/to_send/'.i.'.txt',0);
                                        write_file('players/'.unique_id.'/to_send/nr_string_'.i.'.txt',0);
                                        //deleting the data
                                }

                        }


                        //increasing the priority of the remaining data
                        j=1;
                        for(i=1;i<=10;i++)
                        {
                                nr_arg=read_file('players/'.unique_id.'/to_send/nr_'.i.'.txt');
                                if(nr_arg!=0)
                                {
                                        move_file_contents('players/'.unique_id.'/to_send/'.i.'.txt','players/'.unique_id.'/to_send/'.j.'.txt',1);
                                        move_file_contents('players/'.unique_id.'/to_send/nr_string_'.i.'.txt','players/'.unique_id.'/to_send/nr_string_'.j.'.txt',1);

                                }
                        }



                        echo output;

                        //adding to server log
                        if(dev_log)
                        {
                                save_to_log(output,1);
                        }

                }




function send_data_to_player(player_id,data,nr_strings=0)
{               //data[0]=event; data[1]=arg1
                //numer of args is without the event
         // player_ip=get_player_ip();
         //finding a slot to save the output
        slot=10;
        for(i=1;i<=10;i++)
        {
                args=read_file('players/'.player_id.'/to_send/'.i.'.txt');
                args=explode("|",args);
                nr_arg=count(args) - 1;
                if(nr_arg==0)
                {
                        slot=i;
                        break;
                }
        }
         //slot found
         nr_args=count(data);
         nr_args--;
         output=data[0];//data[0];
         for(i=1;i<=nr_args;i++)
         {
                output.='|'.data[i];
         }
         write_file('players/'.player_id.'/to_send/'.slot.'.txt',output);
         write_file('players/'.player_id.'/to_send/nr_string_'.slot.'.txt',nr_strings);


}
?>
            ```



functions/init_player.php
```
<?php

function init_player(unique_id,username)
{
        global starting_gold;
        player_root="players/".unique_id.'/';
        make_dir(player_root."to_send");
        for(i=1;i<=10;i++)
        {
                write_file(player_root."to_send/".i.".txt",0);
                write_file(player_root."to_send/nr_string_".i.".txt",0);
        }
        write_file(player_root."position.txt","0|0|0");
        write_file(player_root."username.txt",username);
        write_file(player_root."local_id.txt",0);
        write_file(player_root."ip.txt",0);
        write_file(player_root."active.txt",0);
        write_file(player_root."last_time_active.txt",0);
        write_file(player_root."ping.txt",0);
        write_file(player_root."ping_var.txt",0);
        write_file(player_root."gold.txt",starting_gold);

        inv=-1;
        for(i=1;i<=91;i++)
        {
                inv.='|-1';
        }
        write_file(player_root."inventory.txt",inv);

        troops='-1|-1|0';
        for(i=1;i<=39;i++)
        {
                troops.='|-1';
        }
        write_file(player_root."troops.txt",troops);
}
?>
            ```



functions/other_functions.php
```
<?php
function check_players_activity()
{
        for(i=1;i<=10;i++)
        {
                unique_id=read_file("local_id/".i.".txt");
                if(unique_id!=0)
                {
                        last_time_active=read_file("players/".unique_id."/last_time_active.txt");
                        if(time()-last_time_active > 3)//if more than 3 seconds passed since last active
                        {
                                deconnect_player(unique_id);

                        }
                }
        }
}

function microtime_float()
{
    list(usec, sec) = explode(" ", microtime());
    return ((float)usec + (float)sec);
}

function check_string(string)
{
        if(preg_match("/^[a-zA-Z0-9\_\-\[\]\~]+/", string))
        {
                return 1;
        }else{
                return 0;
        }
}

function send_initial_players(unique_id)
        {
                nr_players=0;
                to_send[0]=8;
                c=1;
                for(i=1;i<=10;i++)
                        {
                                u_id=get_unique_id_by_local(i);
                                if(u_id!=0)
                                {
                                        position=get_position(u_id);
                                        x=position[0];
                                        y=position[1];
                                        rot=position[2];

                                        to_send[c]=x;
                                        c++;
                                        to_send[c]=y;
                                        c++;
                                        to_send[c]=rot;
                                        c++;
                                        to_send[c]=get_player_username(u_id);
                                        c++;
                                        to_send[c]=get_active(u_id);
                                        c++;
                                }else{
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]="Offline";
                                        c++;
                                        to_send[c]=0;
                                        c++;

                                }
                        }
                send_data_to_player(unique_id,to_send,10);
        }

function save_to_log(string,dev=0)
{
        if(dev)
        {
                global dev_log_path;

                fh = fopen(dev_log_path, 'a');
                fwrite(fh , _SERVER['REMOTE_ADDR']."--"._SERVER["REQUEST_URI"]." --- ".string."\r\n");
                fclose(fh);
        }else{
                global server_log;
                global server_log_path;

                if(server_log)
                {
                        fh = fopen(server_log_path, 'a');
                        fwrite(fh, string."\r\n");
                        fclose(fh);
                }
        }
}
?>
            ```



functions/player_functions.php
```
<?php


//get functions
function get_active(unique_id)
{
        return read_file('players/'.unique_id.'/active.txt');
}

function get_position(unique_id)
{
        position= read_file('players/'.unique_id.'/position.txt');
        array=explode("|",position);
        return array;
}

function get_gold(unique_id)
{
        return read_file('players/'.unique_id.'/gold.txt');
}

function get_last_time_active(unique_id)
{
        return read_file('players/'.unique_id.'/last_time.txt');
}

function inventory_get_slot(unique_id,slot)
        {
                inv=read_file("players/".unique_id."/inventory.txt");
                inv_array=explode("|",inv);
                return inv_array[slot];
        }

//set functions

function inventory_set_slot(unique_id,slot,value)
{
        inv=read_file("players/".unique_id."/inventory.txt");
        inv_array=explode("|",inv);
        inv_array[slot]=value;
        to_write=implode("|",inv_array);
        write_file("players/".unique_id."/inventory.txt",to_write);
}

function set_last_time_active(unique_id)
{
        write_file('players/'.unique_id.'/last_time.txt',time());
}

function set_position(unique_id,x,y,rot)
{
        write_file('players/'.unique_id.'/position.txt',x.'|'.y.'|'.rot);
}

function set_gold(unique_id,gold)
{
        write_file('players/'.unique_id.'/gold.txt',gold);
}

function set_active(unique_id,state)
{
        write_file('players/'.unique_id.'/active.txt',state);
}
?>










            ```



functions/player_id_functions.php
```
<?php

function find_local_id(id)
{
        for(i=1;i<=10;i++)
        {
                file_id=read_file("local_id/".i.".txt");
                if(file_id==id)
                {
                        return i;
                }
        }
        return 0;
}

function get_player_ip()
        {
                return _SERVER['REMOTE_ADDR'];
        }

function get_unique_id_by_username(username)
        {
                return read_file("username_id/".username.".txt");
        }

function get_unique_id_by_local(local)
        {
                return read_file("local_id/".local.".txt");
        }

function get_player_username(unique_id)
        {
                return read_file("players/".unique_id."/username.txt");
        }

function get_local_id_by_ip(ip=0)
        {
                if(ip==0)
                {
                        return read_file("ip_login/"._SERVER['REMOTE_ADDR'].".txt");
                }else{
                        return read_file("ip_login/".ip.".txt");
                }
        }

function get_local_id_by_unique(unique_id)
        {
                return read_file("players/".unique_id."/local_id.txt");
        }


?>
            ```



functions/send_initial_players.php
```
<?php

function send_initial_players(unique_id)
        {
                nr_players=0;
                to_send[0]=8;
                c=1;
                for(i=1;i<=10;i++)
                        {
                                u_id=get_unique_id_by_local(i);
                                if(u_id!=0)
                                {
                                        position=get_position(u_id);
                                        x=position[0];
                                        y=position[1];
                                        rot=position[2];

                                        to_send[c]=x;
                                        c++;
                                        to_send[c]=y;
                                        c++;
                                        to_send[c]=rot;
                                        c++;
                                        to_send[c]=get_player_username(u_id);
                                        c++;
                                        to_send[c]=get_active(u_id);
                                        c++;
                                }else{
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]=0;
                                        c++;
                                        to_send[c]="Offline";
                                        c++;
                                        to_send[c]=0;
                                        c++;

                                }
                        }
                send_data_to_player(unique_id,to_send,10);
        }
?>
            ```



functions/troops_functions.php
```
<?php

function player_get_troop_id(unique_id,troop_slot)
        {
                troop=read_file("players/".unique_id."/troops.txt");
                array=explode("|",troop);
                return array[slot];
        }

function player_set_troop_slot(unique_id,slot,troop_id)
{
        troop=read_file("players/".unique_id."/troops.txt");
        array=explode("|",troop);
        array[slot]=troop_id;
        to_write=implode("|",array);
        write_file("players/".unique_id."/troops.txt",to_write);
}
?>
            ```



server_vars/check_time.txt
```
1380653098
            ```



server_vars/player.txt
```
0
            ```



triggers/on_connect.php
```
<?php

function ti_on_player_connect(unique_id)
        {

                username=get_player_username(unique_id);
                local_id=get_local_id_by_unique(unique_id);

                //send player log in to all active players
                for(i=1;i<=10;i++)
                {
                        u_id=get_unique_id_by_local(i);
                        if(u_id!=0)
                        {
                                send_data_to_player(u_id,[7,1,local_id,username],1);//player log in!
                        }
                }

                position=get_position(unique_id);
                x=position[0];
                y=position[1];
                rot=position[2];

                to_send[0]=5;
                to_send[1]=x;
                to_send[2]=y;
                to_send[3]=rot;

                send_data_to_player(unique_id,to_send);

                save_to_log(username . ' connected with ip ' . get_player_ip());

                send_data_to_player(unique_id,[15,"Hello ".username."!^Welcome to my test server!",0xA0EDF7],1);
                send_data_to_player(unique_id,[15,"R^O",0xFF0000],1);
                send_data_to_player(unique_id,[15,"M^A",0xF7FF05],1);
                send_data_to_player(unique_id,[15,"N^I^A",0xA0E3F7],1);

        }
?>
            ```



triggers/on_deconnect.php
```
<?php

function ti_on_player_deconnect(unique_id,local_id)
        {
                username=get_player_username(unique_id);

                for(j=1;j<=10;j++)//send player disconnect to all active players
                {
                        u_id=get_unique_id_by_local(j);
                        if(u_id!=0 and u_id!=unique_id)
                        {
                                send_data_to_player(u_id,[7,2,local_id,username],1);//player deconnect
                        }
                }
                save_to_log(username . ' lost connection/disconnect');
        }
?>
            ```

Rewrite the code using Python and use Flask for web access