Advertisement
Slowstone72

Math Opponent for Multiplayer Piano

Dec 1st, 2024 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 6.32 KB | Software | 0 0
  1. /* Callum Fisher - cf.fisher.bham@gmail.com
  2. "Math Opponent for Multiplayer Piano" - Made to work against Math Teacher for Multiplayer Piano
  3. 2018.07.25 - 2024.12.06
  4.  
  5. This is free and unencumbered software released into the public domain.
  6.  
  7. Anyone is free to copy, modify, publish, use, compile, sell, or
  8. distribute this software, either in source code form or as a compiled
  9. binary, for any purpose, commercial or non-commercial, and by any
  10. means.
  11.  
  12. In jurisdictions that recognize copyright laws, the author or authors
  13. of this software dedicate any and all copyright interest in the
  14. software to the public domain. We make this dedication for the benefit
  15. of the public at large and to the detriment of our heirs and
  16. successors. We intend this dedication to be an overt act of
  17. relinquishment in perpetuity of all present and future rights to this
  18. software under copyright law.
  19.  
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. OTHER DEALINGS IN THE SOFTWARE.
  27.  
  28. For more information, please refer to <https://unlicense.org> */
  29.  
  30. //Math Opponent:
  31.  
  32. //variables
  33. var a = true;               // on or off
  34. var time = 8000             // default minimum response time in ms
  35. var learn = false;          // learn
  36. var lvl = 0;                // bot level
  37. var learnaccuracy = 0       // learning mode level
  38. var impossiblemsg = true;   // message that displays when bot is op
  39. var gamerunning = false;    // whether or not a math game is active
  40. var bot_ID = "29f3d649cb0e8de2156d1508" //Math bot _id
  41. //functions:
  42. function defaultname(){
  43.     MPP.client.sendArray([{
  44.         m: "userset",
  45.             set: {
  46.                 "name": "MathBot [say \"mathbot\" for more]"
  47.                 }
  48.     }]);
  49. }
  50.  
  51. function lvlUp(){
  52.     lvl=lvl+0.5
  53.     console.log('lvl up: '+lvl);
  54.     MPP.client.sendArray([{
  55.         m: "userset",
  56.             set: {
  57.                 "name": "MathBot [LEVEL UP ^^]: "+lvl
  58.                 }
  59.     }]);
  60.     setTimeout(function(){
  61.         defaultname();
  62.     }, 2000);
  63. }
  64.  
  65. function lvlDown(){
  66.     lvl=lvl-0.5
  67.     console.log('lvl down: '+lvl);
  68.     MPP.client.sendArray([{
  69.         m: "userset",
  70.             set: {
  71.                 "name": "MathBot [LEVEL DOWN vv]: "+lvl
  72.                 }
  73.     }]);
  74.     setTimeout(function(){
  75.         defaultname();
  76.     }, 2000);
  77. }
  78.  
  79. function reset(){
  80.     time=8000;
  81.     lvl=0;
  82.     learnaccuracy=0;
  83.     impossiblemsg=true;
  84.     MPP.chat.send('My statistics were reset!');
  85. }
  86.  
  87. function toggle(){
  88.     if(a){
  89.         a=false;
  90.         MPP.chat.send('I won\'t answer any math questions now! (Disabled)');
  91.     } else {
  92.         a=true;
  93.         MPP.chat.send('I will play the /math game now! (Enabled)');
  94.     }
  95. }
  96.  
  97. function display_help(){
  98.     MPP.chat.send('MathBot commands 1:');
  99.     setTimeout(function(){
  100.         MPP.chat.send('Say "mathbot reset" to reset me.');
  101.         MPP.chat.send('Say "mathbot 2" for more.');
  102.     }, 2000);
  103. }
  104.  
  105. function display_help2(){
  106.     MPP.chat.send('MathBot commands 2:');
  107.     setTimeout(function(){
  108.         MPP.chat.send('Say "mathbot toggle" to turn me on or off.');
  109.         MPP.chat.send('Say "mathbot stats" to see my statistics.');
  110.     }, 2000);
  111. }
  112.  
  113. function display_statistics(){
  114.     MPP.chat.send('[🕒] Time to calculate: '+Math.floor((time/1000) % 60)+'s'+' || [-->-|] Accuracy Points: '+learnaccuracy+' || [^v] Level: '+lvl+'');
  115. }
  116.  
  117. function display_welcome(){
  118.     MPP.chat.send('Hi, I\'m MathBot, I\'m an opponent for the /math command. Beat my scores!');
  119.     MPP.chat.send('Say "mathbot" for more.');
  120. }
  121.  
  122. //setup:
  123. display_welcome();
  124. defaultname();
  125.  
  126. //Listen for messages:
  127. MPP.client.on("a",function(msg){
  128.     //reset command:
  129.     if(msg.a.toLowerCase().startsWith('mathbot reset')){
  130.         reset();
  131.     }
  132.     //toggle command:
  133.     if(msg.a.toLowerCase().startsWith('mathbot toggle')){
  134.         toggle();
  135.     }
  136.     //default response:
  137.     if(msg.a.toLowerCase()=='mathbot'){
  138.         display_help();
  139.     }
  140.     //default response2
  141.     if(msg.a.toLowerCase()=='mathbot 2'){
  142.         display_help2();
  143.     }
  144.     //stats command:
  145.     if(msg.a.toLowerCase()=='mathbot stats'){
  146.         display_statistics();
  147.     }
  148.    
  149.     //time up:
  150.     if(a){
  151.     if(msg.a.startsWith('It\'s been a minute! Math game is over, the anwser was')){
  152.         gamerunning=false;
  153.         setTimeout(function(){
  154.             //become more of a challenge
  155.             MPP.chat.send('Oh no. Level down. I won\'t be beaten now!');
  156.             time=3000;
  157.             if(learn){
  158.                 learnaccuracy--;
  159.                 console.log('got worse');
  160.             }
  161.             lvlDown();
  162.             setTimeout(function(){
  163.                 if(!gamerunning){
  164.                     MPP.chat.send('.math');
  165.                 }
  166.             }, 800);
  167.         }, 800);
  168.     }
  169.     }
  170.    
  171.         //only process the message if it's the bot sending it:
  172.         if(msg.p._id==bot_ID){
  173.             if(a){
  174.             //game start:
  175.             if(msg.a.startsWith("Alright, "+MPP.client.getOwnParticipant().name+" started the math game! Get ready.")){
  176.                 gamerunning=true;
  177.             }
  178.             //bot asks math question:
  179.             if(msg.a.startsWith('What is')){
  180.                     setTimeout(function(){
  181.                         //respond in time:
  182.                     console.log(eval(msg.a.split('?')[0].split('What is')[1].trim()))
  183.                     if(lvl!==5){
  184.                         MPP.chat.send(JSON.stringify(eval(msg.a.split('?')[0].split('What is')[1].trim())));
  185.                         setTimeout(function(){
  186.                             if(!gamerunning){
  187.                                 MPP.chat.send('/math');
  188.                             }
  189.                         }, 800);
  190.                     } else {
  191.                         MPP.chat.send(JSON.stringify(eval(msg.a.split('?')[0].split('What is')[1].trim())));
  192.                         setTimeout(function(){
  193.                             if(!gamerunning){
  194.                                 MPP.chat.send('/math');
  195.                             }
  196.                         }, 800);
  197.                     }
  198.                     }, time);
  199.                 //bot announces that the answer was correct by this bot
  200.             } else if(msg.a.toLowerCase().startsWith(MPP.client.getOwnParticipant().name.toLowerCase()+' found the answer,')){
  201.                 lvlUp(); //lvl up
  202.                 gamerunning = false;
  203.                 if(learn){
  204.                     //get better & faster:
  205.                     learnaccuracy++;
  206.                     time=time-500;
  207.                     console.log('got better');
  208.                     //when bot gets too hard to beat:
  209.                     if(time<2500){
  210.                         if(impossiblemsg){
  211.                             impossiblemsg=false;
  212.                             MPP.chat.send('I\'m practically impossible to beat!!');
  213.                         }
  214.                     }
  215.                 }
  216.                 //bot announces that somebody else got the answer before this bot
  217.                 } else if(msg.a.toLowerCase().includes('found the answer,')) {
  218.                     lvlDown(); //lvl down
  219.                     gamerunning = false;
  220.                     if(learn){
  221.                         //get worse & slower:
  222.                         learnaccuracy--;
  223.                         time=time+500;
  224.                         console.log('got worse');
  225.                     }
  226.                 }
  227.             }
  228.         }
  229. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement