/** 
 * model.js
 * Hiper OchinchinTime Simulator 
 * Author: at-akada
 * nightly[atmark]at-akada.org
 * 2008.02.28-
 *
 */


var Agent = new Class();
Class.extend(Agent.prototype,{
    'isAnti': false,
    'frequency': 1,
    'initialize': function(isAnti,interval,flag){
        this.interval = interval;
        this.isFrequency = flag;
        this.simulator = Simulator.instance;
        this.bbs = this.simulator.bbs;
        this.controller = this.simulator.controller;
        this.id = BBS.make2chId();
        this.isAnti = isAnti;
        this.ready = false;
        this.probability = Config.appendProbability > 0 ? 
            Math.round(10 / Config.appendProbability) : 0;
    },
    'step': function(){
        if(Math.floor(Math.random()*this.probability) == 0){
            this.nextChar = 
                this.isAnti ? this.controller.nonNextChar() : this.controller.nextChar();
            this.ready = true;
            TaskManager.push(Function.bind(function(){
                this.controller.append(this.nextChar,this);
            },this));
            /* next turn, agent append message */
        }
    }
});


var BBS = new Class();
Class.extend(BBS,{
    'makeHeader': function(agent,bbs,date){
        if(!bbs){
            var bbs = {length:0};
        }
        if(!date) date = new Date();

        var resBan = bbs.messages.length+1,
            nanashi = "以下、名無しにかわりましてエージェントがお送りします。",
            date = this.getJapaneseDate(date),
            id = agent.id;
        return {"count":resBan,"name":nanashi,"date":date,"id":id};
    },
    'getJapaneseDate': function(date){
        var youbi = ["日","月","火","水","木","金","土"];
        var to2d = function(n){
            var n = n.toString();
            if(n.length < 2)
                n = "0" + n;
            return n;
        };

        var day = [date.getFullYear(),to2d(date.getMonth()+1),to2d(date.getDate())].join("/");
        var jDate = day + "(" + youbi[date.getDay()] + ")";
        var jTime = [to2d(date.getHours()),to2d(date.getMinutes()),to2d(date.getSeconds())].join(":")
            +"."+to2d(date.getMilliseconds()).toString().slice(0,2);
        return jDate + " " + jTime;
    },
    'make2chId': function(isKeitai){
        /* 0x30-0x39(48-57): "0-9"  :0-9 
         * 0x41-0x5A(65-90): "A-Z"  :10-35
         * 0x61-0x7A(97-122): "a-z"  :36-61
         */
        var makeRandChar = function(){
            var n = Math.floor(Math.random() * 62);
            if(n <= 9)return String.fromCharCode("0x"+Number(n+48).toString(16));
            else if(10 <= n && n <= 35) return String.fromCharCode("0x"+Number(n+55).toString(16));
            else return String.fromCharCode("0x"+Number(n+61).toString(16));
        }
        var id = "";
        for(var i=0;i<8;i++)
            id += makeRandChar();
        id += 
            isKeitai ? "O" : "0";
        return id;
    }
});
Class.extend(BBS.prototype,{
    'initialize': function(){
        this.clear();
    },
    'clear': function(){
        this.messages = [];
        this.words = "";
    },
    'append': function(chr,agent){
        this.messages.push(
            new Message(agent,BBS.makeHeader(agent,this,new Date()),chr)
        );
        this.words += chr;
    },
    'getWords': function(){
        return this.words;
    }
});

var Message = new Class();
Class.extend(Message.prototype,{
    'initialize': function(agent,header,chr){
        this.author = agent;
        this.header = header;
        this.body = chr;
    }
});