Transistor 發表於 2017-3-21 00:49:54

[開發中]NPC自動重生

npcdir是存放npc資料的

寫在sea.cgi

# Sub NPC Update
sub NPC_update{


        my@NPC_uid = ("00002");        # NPC ID
        my$NPCdir        = 'npcdir';                # NPC帳戶資料存放目錄(※可更改 不可與usrdir相同)
        my        $NPC_cycle = 3;                        # NPC重生週期

       
        &get_date(time);
       
        if($day % $NPC_cycle){
                my $NPC_upflag = 1;
                return;
        }
               

        if((!($day % $NPC_cycle) || $day == 1) && $NPC_upflag){
       
                foreach (@NPC_uid) {
                        my        $NPC_load_file = new Nfile("$NPCdir\/$_\.dat",'read');
                        @npc_ilines = $NPC_load_file->read;
                        if (!@npc_ilines) { &error("NPC資料讀取錯誤"); }
                       
                        open(write_file,">$usrdir\/$_\.dat") or "open file error!";
                        print write_file @npc_ilines;
                        close(write_file);
                }
                undef $NPC_upflag;
        }
       
}



並把原本的play函式

sub play {
        &get_me($F{'id'});
        &set_cookie if $F{'mode'} eq 'play';
        &get_host;
        &get_port($area,$port) if $port;
        &get_port($area,$area) if !$port;
        &ship_data;
        $last = time;
        &t_check;
        &sink;



改為這個(其實只是多呼叫一個函式XD)

sub play {
        &get_me($F{'id'});
        &set_cookie if $F{'mode'} eq 'play';
        &get_host;
        &get_port($area,$port) if $port;
        &get_port($area,$area) if !$port;
        &ship_data;
        &NPC_update;
        $last = time;
        &t_check;
        &sink;



Transistor 發表於 2017-3-21 01:02:56

這個方法有個缺點

如果重生那一整天都沒人上線就會跳過重生(人氣應該不會這麼慘吧:L)

Transistor 發表於 2017-3-21 01:26:43

原來NFILE要先讀才能寫

優化一下寫法~

        if((!($day % $NPC_cycle) || $day == 1)){
       
                foreach (@NPC_uid) {
                        my        $NPC_load_file = new Nfile("$NPCdir\/$_\.dat",'read');
                        @npc_ilines = $NPC_load_file->read;
                        if (!@npc_ilines) { &error("NPC資料讀取錯誤"); }
                        my        $NPC_save_file = new Nfile("$usrdir\/$_\.dat",'save');
                        $NPC_save_file->read;
                        $NPC_save_file->write(@npc_ilines); #(bug)
                }
                undef $NPC_upflag;
        }


Transistor 發表於 2017-3-21 01:42:06

新增一下讀取確認 防止讀取錯誤整個當機:Q




        if((!($day % $NPC_cycle) || $day == 1)){
       
                foreach (@NPC_uid) {
                        my        $NPC_load_file = new Nfile("$NPCdir\/$_\.dat",'read');
                        my@npc_ilines = $NPC_load_file->read;
                        if (!@npc_ilines) { &error("NPC資料讀取錯誤"); }
                        if ($npc_ilines eq "Read Error") {&msg("<font color=\"#FF0000\">NPC檔案不存在!</font>");return;}

                       
                       
                       
                        my        $NPC_save_file = new Nfile("$usrdir\/$_\.dat",'save');
                        my@npc_update = $NPC_save_file->read;
                        if (!@npc_update) { &error("NPC資料讀取錯誤"); }
                        if ($npc_update eq "Read Error") {&msg("<font color=\"#FF0000\">NPC檔案不存在!</font>");return;}
                       
       
                       
                        $NPC_save_file->write(@npc_ilines);
                }
                undef $NPC_upflag;
        }




dog29329584 發表於 2017-3-21 16:20:45

Transistor 發表於 2017-3-21 01:02 static/image/common/back.gif
這個方法有個缺點

如果重生那一整天都沒人上線就會跳過重生(人氣應該不會這麼慘吧) ...

那是否只要有人登入 就會重生?

teng 發表於 2017-3-21 16:47:41

dog29329584 發表於 2017-3-21 16:20 static/image/common/back.gif
那是否只要有人登入 就會重生?

以他做法是的0 0
頁: [1]
查看完整版本: [開發中]NPC自動重生