Advertisement
Kalashnikov

Untitled

Oct 14th, 2011
2,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var DEBUG = false
  2.  
  3. var TIER_FACTORY  = "Battle Factory";
  4. var TIER_BACKYARD = "Main Hall";
  5.  
  6. var STATE_NO_TEAM   = 0;
  7. var STATE_INIT_TEAM = 1;
  8. var STATE_HAS_TEAM  = 2;
  9. var STATE_BATTLING  = 3;
  10. var STATE_SWAPING   = 4;
  11.  
  12. var MISSINGNO = new Pokemon({"item": [0], "ev": [[0, 0, 0, 0, 0, 0]], "ratio": 0, "ability": [0], "nature": [0], "move": [[0], [0], [0], [0]], "id": 0, "iv": [[0, 0, 0, 0, 0, 0]]});
  13.  
  14. /*
  15. if (DEBUG)
  16.     SESSION.identifyScriptAs(sys.rand(0, 0xFFFAAA));
  17. */
  18. function debug(message){
  19.     if (!DEBUG)
  20.         return;
  21.     sys.sendHtmlAll('!debugBot: '+message);
  22. }
  23.  
  24. function attendant(id, message) {
  25.     sys.sendHtmlMessage(id, '<table width="100%" style="margin: 0 auto; background: #222; border: 1px solid blue; border-top: 3px solid red;">\
  26.     <tr border="0">\
  27.         <td width="80"><img src="Themes/Classic/Trainer Sprites/'+ (215 + 5*(id%2))+ '.png"/></td>\
  28.         <td style="vertical-align: top; padding: 5px; color: white; font-family: \'Ubuntu\', \'Trebuchet MS\', sans-serif;"> <b>Factory Attendant</b>:<br>' +message+ '</td>\
  29.     </tr>\
  30.     </table>');
  31. }
  32.  
  33. function bot (id, message) {
  34.     sys.sendHtmlMessage(id, '<span style="font-family: lucida console, consolas, monospace; font-weight: 800; font-size: 16px; color: crimson">Bot</span>: ' + message);
  35. }
  36.  
  37. commands = {
  38.     "channel": {
  39.         "rules               ": "Shows the rules",
  40.         "commands [category] ": "Lists all commands, if optional category is specfied, list only commands from this category. Categories: channel, factory.",
  41.         "bug [message]       ": "Reports bug to administrator. Describe your bugs throughfully."
  42.     },
  43.     "factory": {
  44.         "wat                      ": "Suggests your next action.",
  45.         "team                     ": "Lists your current team.",
  46.         "streak [player]          ": "Shows player's winstreak, shows your own if player name is omitted.",
  47.         "choices                  ": "Lists all pokemon you can choose with /choose or /swap at the moment.",
  48.         "choose [number]          ": "Claims number's pokemon as your.",
  49.         "swap [number1] [number2] ": "Replaces number1'th pokemon in your team with number2'th from opponent's.",
  50.         "noswap                   ": "Lets you proceed without swaping any pokemon."
  51.     }
  52. }
  53.  
  54. sets = JSON.parse(sys.getFileContent('sets_subway.json'))
  55.  
  56. function Pokemon(set) {
  57.     for (var i in {'item':0, 'ev':0, 'ability':0, 'nature':0})
  58.         this[i] = set[i][sys.rand(0, set[i].length)];
  59.     this.id = set.id;
  60.     this.iv = set.iv;
  61.     this.moves = [];
  62.     for (var i=0; i<4; i++)
  63.         this.moves[i] = set.move[i][sys.rand(0, set.move[i].length)];
  64.    
  65.     if (set.ratio == -1)
  66.         this.gender = 0;
  67.     else
  68.         this.gender = (sys.rand(0, 9) < set.ratio? 2 : 1);
  69. }
  70.  
  71. Pokemon.prototype.toHtml = function(i, short) {
  72.     var s = '<table width="100%" style="margin: 0 auto; background: #333;"><tr>';
  73.     s += '<td width="90"><img src="pokemon:num=' +this.id+ '"/></td><td style="padding: 5px; color: #ddd; font-family: panic sans, consolas, monospace;">';
  74.     s += '<span style="font-size: 18px; font-family: ubuntu, trebuchet ms, sans-serif; color: white">' +(i? i+'.' : '')+ ' ' + sys.pokemon(this.id) + '</span> ' + ['', '♂', '♀'][this.gender]+ '<br>';
  75.     if (!short) {
  76.         s += 'Nature&nbsp; <b>' + sys.nature(this.nature) + '</b><br>';
  77.         s += 'Ability <b>' + sys.ability(this.ability) + '</b><br>';
  78.         s += 'Item&nbsp;&nbsp;&nbsp; <b>'+ sys.item(this.item) + '</b><br>';
  79.         s += 'Moves&nbsp;&nbsp;&nbsp;';
  80.         s += this.moves.map(function(m) {return '<b>'+sys.move(m)+'</b>';}).join(', ');
  81.     }
  82.     s += '</td></tr></table>';
  83. /*  if (DEBUG) {
  84.         s += '\nIVs: '       + this.iv;
  85.         s += '\nEVs: '       + this.ev;
  86.         //s += '\nMoves: '     + this.moves;
  87.         //s += '\nAbility: '   + this.abilities;
  88.         //s += '\nFemales: '   + this.f_ratio;
  89.     }*/
  90.     return s;
  91. };
  92.  
  93. debug('Random pokemon:' + (new Pokemon(sets[sys.rand(0, sets.length)]).toHtml()));
  94.  
  95. function Player(id) {
  96.     this.id = id;
  97.     this.winstreak = 0;
  98.     this.team = [];     // contains Pokemon instances
  99.     this.party = 0;     // number of pokemon in the party
  100.     this.choices = [];  // possible choices for swapping/initial team
  101.     for (var i=0; i<6; i++)
  102.         this.team[i] = MISSINGNO;
  103.     this.state = STATE_NO_TEAM;
  104.     for (var i=0; i<5; i++)
  105.         this.choices.push(new Pokemon(sets[sys.rand(0, sets.length)]));
  106.  
  107.     // called inb4Battle started, loads this.team into actual team
  108.     this.prepare = function() {
  109.         if (this.state != STATE_HAS_TEAM || this.party != 3) {
  110.             bot(this.id, 'An unknown error occured: You don\'t have a team!')
  111.             //sys.stopEvent();
  112.             return;
  113.         }
  114.         for (var i=0; i<3; i++) {
  115.             //debug('setting this pokemon:'+this.team[i].toHtml());
  116.             sys.changePokeNum      (this.id, i, this.team[i].id);
  117.             sys.changePokeItem     (this.id, i, this.team[i].item);
  118.             sys.changePokeNature   (this.id, i, this.team[i].nature);
  119.             sys.changePokeLevel    (this.id, i, 50);
  120.             sys.changePokeName     (this.id, i, sys.pokemon(this.team[i].id));
  121.             sys.changePokeAbility  (this.id, i, this.team[i].ability);
  122.             sys.changePokeGender   (this.id, i, this.team[i].gender);
  123.             sys.changePokeShine    (this.id, i, !sys.rand(0, 8096));
  124.             sys.changePokeHappiness(this.id, i, 255);
  125.             for (var j=0; j<4; j++)
  126.                 sys.changePokeMove    (this.id, i, j, this.team[i].moves[j])
  127.             for (var j=0; j<6; j++)
  128.                 sys.changeTeamPokeEV  (this.id, i, j, this.team[i].ev[j]);  // WARNING
  129.             for (var j=0; j<6; j++)
  130.                 sys.changeTeamPokeIV  (this.id, i, j, this.team[i].iv[j]);  // WARNING
  131.         }
  132.     }
  133.     //dropChoices() {}
  134. }
  135.  
  136. SESSION.registerUserFactory(Player);
  137.  
  138. ({
  139.     afterLogIn: function(src) {
  140.         sys.changeTier(src, TIER_BACKYARD);
  141.         attendant(src, 'Greetings, trainer, wellcome to the battle factory<br>\
  142. We research pokemon battles here.<br>\
  143. To get started, read the rules by typing "/rules"<br>\
  144. For list of commands type "/commands."');
  145.     },
  146.    
  147.     afterChangeTier: function(src, oldtier, newtier) {
  148.         if (newtier == TIER_FACTORY && SESSION.users(src).state == STATE_NO_TEAM) {
  149.             attendant(src, 'I\'ll take your pokemon for safekeeping<br>Choose 3 pokemon pokemon from this list:');
  150.             for (var i=0; i<5; i++) {
  151.                 pkm = SESSION.users(src).choices[i];
  152.                 if (pkm.id == 0)
  153.                     continue;
  154.                 sys.sendHtmlMessage(src, pkm.toHtml(i+1));
  155.             }
  156.             attendant(src, 'Type "/choose number" to choose a pokemon.');
  157.             SESSION.users(src).state = STATE_INIT_TEAM;
  158.         }
  159.     },
  160.    
  161.     beforeFindBattle: function(src) {   // WARNING
  162.         if (SESSION.users(src).state != STATE_HAS_TEAM) {
  163.             attendant(src, 'You can\'t battle yet.');
  164.             bot(src, 'stopping event');
  165.             sys.stopEvent();
  166.             return;
  167.         }
  168.     },
  169.    
  170.     beforeBattleMatchup: function(){
  171.         debug('bBM fired');
  172.         //sys.stopEvent();
  173.     },
  174.    
  175.     beforeChallengeIssued: function(src, tar, clauses) {
  176.         /*if (sys.tier(src) == TIER_BACKYARD) {
  177.             bot(src, 'You can\'t battle in '+TIER_BACKYARD);
  178.             sys.stopEvent();
  179.             return;
  180.         } */
  181.         if (sys.tier(src) != sys.tier(tar)) {
  182.             bot(src, 'Cross-tier battling is not possible.');
  183.             sys.stopEvent();
  184.             return;
  185.         }
  186.         if (SESSION.users(src).state != STATE_HAS_TEAM) {
  187.             attendant(src, 'You can\'t start battling yet.');
  188.             sys.stopEvent();
  189.             return;
  190.         }
  191.         if (SESSION.users(tar).state != STATE_HAS_TEAM) {
  192.             attendant(src, sys.name(tar) + ' is busy right now.');
  193.             sys.stopEvent();
  194.             return;
  195.         }
  196.         if (clauses) {
  197.             attendant(src, "You can only battle with all clauses diabled.");
  198.             // at least we need to get rid of WF clause
  199.             sys.stopEvent();
  200.         }
  201.         //sys.forceBattle(src, tar, 0, 0, false);
  202.     },
  203.  
  204.     beforeBattleStarted: function(p1, p2) {
  205.         //debug('bBS');
  206.         SESSION.users(p1).prepare();
  207.         SESSION.users(p2).prepare();
  208.         SESSION.users(p1).state = STATE_BATTLING;
  209.         SESSION.users(p2).state = STATE_BATTLING;
  210.     },
  211.    
  212.     afterBattleEnded: function(winner, looser, result) {
  213.         if (sys.tier(winner) != TIER_FACTORY)
  214.             return;
  215.         if (result == 'tie') {
  216.             //for (p in [winner, looser]) {
  217.                 attendant(winner, 'What? A tie? This is quite unexpected.');
  218.                 SESSION.users(winner).state = STATE_HAS_TEAM;
  219.                 attendant(looser, 'What? A tie? This is quite unexpected.');
  220.                 SESSION.users(looser).state = STATE_HAS_TEAM;
  221.             //}
  222.             return;
  223.         }
  224.         SESSION.users(winner).winstreak++;
  225.         SESSION.users(winner).state = STATE_SWAPING;
  226.         attendant(winner, 'Congratulations for your win, now you can swap one of your pokmon with one of defeated trainer\'s:');
  227.         for (var i=0; i< 3; i++)
  228.             sys.sendHtmlMessage(winner, (SESSION.users(winner).choices[i] = SESSION.users(looser).team[i]).toHtml(i+1, true));
  229.         attendant(winner, 'Type "/swap pokemon_to_swap pokemon_to_take".');
  230.         // is it possible to set rating to winstreak?
  231.        
  232.         //SESSION.users(looser) = new Player(looser);   // will it work?
  233.        
  234.         sys.changeTier(looser, TIER_BACKYARD);
  235.         SESSION.users(looser).winstreak = 0;
  236.         for (i=0; i< 3; i++)
  237.             SESSION.users(looser).team[i] = MISSINGNO;
  238.         SESSION.users(looser).state = STATE_NO_TEAM;
  239.         SESSION.users(looser).party = 0;
  240.        
  241.         for (var i=0; i<5; i++)
  242.             SESSION.users(looser).choices[i] = new Pokemon(sets[sys.rand(0, sets.length)]);
  243.         attendant(looser, 'Sorry, but you lost. You winstreak is reset to 0.');
  244.     },
  245.  
  246.     beforeChatMessage: function(src, msg) {
  247.         if (msg[0] == '/') {
  248.             sys.stopEvent();
  249.             var a = msg.split(' ');
  250.             command = a[0].substr(1).toLowerCase();
  251.             param   = a.slice(1).join(' ');
  252.            
  253.             switch (command){
  254.                 case 'rules':
  255.                     sys.sendHtmlMessage(src, '<table><tr> <td align="center" style="background: #eee; font-family: ubuntu, trebuchet ms, sans-serif; font-size: 18px; text-align: center;">The only rule is COMMON SENSE.<br>\
  256. That also means you can be punished for whatever reason.<br>\
  257. To start the game switch to "'+TIER_FACTORY+'" tier and follow the instructions.<br>\
  258. If you ever get lost, try <tt>/wat</tt> or <tt>/commands</tt>.</td></tr></table>');
  259.                     break;
  260.                 case 'commands':
  261.                     var head = '<span style="white-space: pre; font-family: lucida console, consolas, monospace; color: purple;">/';
  262.                     if (!param) {
  263.                         for (cat in commands)
  264.                             for (c in commands[cat])
  265.                                 bot(src, head+c+'</span>'+commands[cat][c]);
  266.                     }
  267.                     else if (param in commands) {
  268.                         for (c in commands[param])
  269.                             bot(src, head+c+'</span>'+commands[param][c]);
  270.                     }
  271.                     else
  272.                         bot(src, 'No such category "'+param+'".');
  273.                     break;
  274.                 case 'wat':
  275.                     var message;
  276.                     switch(SESSION.users(src).state){
  277.                         case STATE_NO_TEAM:
  278.                             message = 'You don\'t have a team yet, switch to "'+TIER_FACTORY+'" tier to start building it.';
  279.                             break;
  280.                         case STATE_INIT_TEAM:
  281.                             message = 'You have yet to choose pokemon for your starting team, use "/choices" command.';
  282.                             break;
  283.                         case STATE_HAS_TEAM:
  284.                             message = 'You can start battling. Click Find Battle, or make a challenge.';
  285.                             break;
  286.                         case STATE_BATTLING:
  287.                             message = 'What are doing here? <br> You have a battle ongoing, don\'t make your opponent wait.';
  288.                             break;
  289.                         case STATE_SWAPING:
  290.                             message = 'You have yet to swap pokemon, use "/choices" command to see possible choices.';
  291.                             break;
  292.                         default:
  293.                             message = 'SCIENCE!';
  294.                     }
  295.                     attendant(src, message);
  296.                     break;
  297.                 case 'choices':
  298.                     if (SESSION.users(src).state == STATE_INIT_TEAM) {
  299.                         left = 3 - SESSION.users(src).party;
  300.                         short = false;
  301.                         msg = 'Type "/swap number1 number2" to swap your number1\'th pokemon with number2\'th pokemon.<br>Type "/noswap" to keep your current team.';
  302.                     }
  303.                     else if (SESSION.users(src).state == STATE_SWAPING){
  304.                         left = 'one';
  305.                         short = true;
  306.                         msg = 'Type "/choose number" to choose a pokemon.';
  307.                     }
  308.                     else {
  309.                         attendant(src, 'You don\'t have any pokemon to choose.');
  310.                         break;
  311.                     }
  312.                     attendant(src, 'You may choose ' +left+ ' pokemon from this list:');
  313.                     for (var i=0; i<5; i++) {
  314.                         pkm = SESSION.users(src).choices[i];
  315.                         if (pkm.id == 0)
  316.                             continue;
  317.                         sys.sendHtmlMessage(src, pkm.toHtml(i+1, short));
  318.                     }
  319.                     attendant(src, msg);
  320.                     break;
  321.                 case 'choose':
  322.                     if (SESSION.users(src).state != STATE_INIT_TEAM) {
  323.                         attendant(src, 'This isn\'t the time to use that.');
  324.                         break;
  325.                     }
  326.                     if (SESSION.users(src).party >= 3) {
  327.                         attendant(src, 'Your team is full, proceed battling.');
  328.                         break;
  329.                     }
  330.                     if (!/^[1-5]$/.test(param)) {
  331.                         attendant(src, 'What is "' +param+ '"? Please, enter a number from 1 to 5.');
  332.                         break;
  333.                     }
  334.                     pkm = SESSION.users(src).choices[param-1];
  335.                     if (pkm.id == 0 ) {
  336.                         attendant(src, 'You took that pokemon already.');
  337.                         break;
  338.                     }
  339.                     SESSION.users(src).team[SESSION.users(src).party] = pkm;
  340.                     attendant(src, 'You took ' +sys.pokemon(pkm.id)+ '.');
  341.                     SESSION.users(src).choices[param-1] = MISSINGNO;
  342.                     if(++SESSION.users(src).party >= 3) {
  343.                         for (i=0; i<SESSION.users(src).choices.length; i++)
  344.                             SESSION.users(src).choices[i] = MISSINGNO;
  345.                         attendant(src, 'Your team is set, <br>You can start battling.');
  346.                         SESSION.users(src).state = STATE_HAS_TEAM;
  347.                     }
  348.                     break;
  349.                 case 'team':
  350.                     if (SESSION.users(src).state == STATE_NO_TEAM || !SESSION.users(src).party) {
  351.                         attendant(src, 'You don\'t have a team.');
  352.                         break;
  353.                     }
  354.                     attendant(src, 'Your current team:');
  355.                     for (i=0; i<SESSION.users(src).party; i++)
  356.                         sys.sendHtmlMessage(src, SESSION.users(src).team[i].toHtml());
  357.                     break;
  358.                 case 'swap':
  359.                     if (SESSION.users(src).state != STATE_SWAPING) {
  360.                         attendant(src, 'You can\'t swap pokemo right now.');
  361.                         break;
  362.                     }
  363.                     if (!/^[1-3] [1-3]$/.test(param)) {
  364.                         attendant(src, 'What do you mean by "' +param+ '"? Please, enter two numbers from 1 to 3.');
  365.                         break;
  366.                     }
  367.                     // Hey, get back to RegExps     // and rewrite everything else here as well
  368.                     a = param.split(' ');
  369.                     attendant(src, 'You replaced your '+ sys.pokemon(SESSION.users(src).team[a[0]-1].id) +' with '+ sys.pokemon(SESSION.users(src).choices[a[1]-1].id) +'.');
  370.                     SESSION.users(src).team[a[0]-1] = SESSION.users(src).choices[a[1]-1];
  371.                     for (i=0; i<SESSION.users(src).choices.length; i++)
  372.                         SESSION.users(src).choices[i] = MISSINGNO;
  373.                     SESSION.users(src).state = STATE_HAS_TEAM;
  374.                     break;
  375.                 case 'noswap':
  376.                     if (SESSION.users(src).state != STATE_SWAPING) {
  377.                         attendant(src, 'But there\'s nothing to swap anyway...');
  378.                         break;
  379.                     }
  380.                     for (i=0; i<SESSION.users(src).choices.length; i++)
  381.                         SESSION.users(src).choices[i] = MISSINGNO;
  382.                     SESSION.users(src).state = STATE_HAS_TEAM;
  383.                     attendant(src, 'You skipped swaping');
  384.                     break;
  385.                 case 'streak':
  386.                     if (param) {
  387.                         var target = sys.id(param.toLowerCase());
  388.                         name = param + "'s";
  389.                     }
  390.                     else {
  391.                         var target = src;
  392.                         name = 'Your';
  393.                     }
  394.                     attendant(src, name + ' current winstreak is ' +SESSION.users(target).winstreak+ '.');
  395.                     break;
  396.                 case 'bug':
  397.                     if (!param) {
  398.                         bot(src, 'What do you mean?');
  399.                         break;
  400.                     }
  401.                     me = sys.id('Kalashnikov');
  402.                     if (me)
  403.                         sys.sendHtmlMessage(me, '<br> '+sys.name(src)+' reported a bug: <br> '+param+' <br>');
  404.                     sys.appendToFile('bugs.txt', sys.time() + ' ' +sys.name(src)+ '(' +sys.ip(src)+ '):\n' + param + '\n\n');
  405.                     bot(src, 'Bug reported. Thank you for your help.');
  406.                     break;
  407.                 case 'eval':
  408.                     if (sys.ip(src) == '127.0.0.1') {
  409.                         sys.eval(param);
  410.                         break;
  411.                     }
  412.                 default:
  413.                     bot(src, 'No such command: "' +command+ '".');
  414.             }
  415.         }
  416.     }
  417. })
  418.  
  419.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement