Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jshint esversion: 6 */
- /* jshint proto: true */
- // TODO: MOTM
- // TODO: ELO
- // TODO: kickonref
- var room = HBInit({ roomName: "Bods Pub ||24/7|| Friends Only",
- playerName: "🌍", maxPlayers: 20, public: false });
- room.setDefaultStadium("Classic");
- room.setScoreLimit(3);
- room.setTimeLimit(3);
- class Player {
- constructor(name) {
- this.name = name;
- /*************************************
- ************ Individual stats**********
- **************************************/
- this.goals = 0; /* Number of goals */
- this.assists = 0; /* Number of assists */
- this.cs = 0; /* Number of cleensheets */
- this.ownGoals = 0; /* Number of own goals */
- this.wins = 0; /* Number of wins */
- this.loses = 0; /* Number of loses */
- this.motm = 0; /* Number of time a player is the man of the match */
- this.playedGk = 0;
- this.badPasses = 0; /* Number of passes */
- this.goodPasses = 1; /* Number of passes that reached a team mate */
- this.passAcc = 0; /* Pass accuracy % */
- this.motmRatio = 0; /* Man of the match % */
- this.winsRatio = 0; /* Wins / Loses */
- this.csPG = 0; /* CS % per game */
- this.goalsPG = 0; /* Number of goals per game */
- this.assistsPG = 0; /* Number of assists per game */
- this.elo = 1000; /* ELO */
- this.secsPlayed = 0; /* Number of mins played */
- this.minsPlayed = 0; /* Number of mins played */
- this.currentStreak = 0; /* Number of current matches won in a row */
- this.bestStreak = 0; /* Number of maximum amount of wins in a row */
- this.statsString1 = "";
- this.statsString2 = "";
- this.statsString3 = "";
- /*************************************
- ************ Haxball manager**********
- **************************************/
- this.price = 10;
- this.money = 0;
- this.dreamTeam = {"gk": null, "dm": null, "am": null, "st": null};
- this.teamScore = 0;
- /*************************************
- ************ Revelant to the room*****
- **************************************/
- this.team = 0;
- this.gotKicked = 0; /* Number of time this player got kicked by another player */
- this.kickedSomeone = 0; /* Number of time this player kicked someone */
- this.id = 0; /* Current player's id on the room */
- this.pw = 0; /* Password for player's account */
- this.isTrustedAdmin = 0; /* Whether he's a trusted admin */
- this.logged = 1; /* 0 if the player is not in the room, 1 if he's, 2 if he's logged */
- this.isBanned = 0; /* Whether a player is banned */
- this.isPermaBanned = 0; /* Whether a player is banned permanently*/
- this.isRegistered = 0; /* Whether a player is registered (e.g. his stats will count) */
- this.auth = 0; /* Player's last auth */
- this.isMuted = 0; /* Whether a player is muted */
- this.isAFK = false;
- }
- updateGoals(){
- if (gameStats.scorers.hasOwnProperty(this.name))
- this.goals += gameStats.scorers[this.name];
- }
- updateAssists(){
- if (gameStats.assisters.hasOwnProperty(this.name))
- this.assists += gameStats.assisters[this.name];
- }
- updateCs(){
- let [team, idteam] = this.team === 1 ? ["blueScore", 1] : ["redScore", 2];
- this.cs += gameStats[team] === 0 &&
- this.name === gameStats.Gks[idteam - 1];
- }
- updateOG(){
- if (gameStats.ownScorers.hasOwnProperty(this.name))
- this.ownGoals += gameStats.ownScorers[this.name];
- }
- updatePlayedGk(){
- this.playedGk += gameStats.Gks.includes(this.name);
- }
- updateWins(winningTeam){
- this.wins += this.team === winningTeam;
- }
- updateLoses(losingTeam){
- this.loses += this.team === losingTeam;
- }
- updateWinRatio(){
- this.winsRatio = ((this.wins / (this.wins + this.loses)) * 100).toFixed(2) || 0;
- }
- updateGoalsPG(){
- this.goalsPG = (this.goals / (this.loses + this.wins)).toFixed(2) || 0;
- }
- updateAssistsPG(){
- this.assistsPG = (this.assists / (this.loses + this.wins)).toFixed(2) || 0;
- }
- updateCSPG(){
- this.csPG = ((this.cs / this.playedGk) * 100).toFixed(2) || 0;
- }
- updateCurrentStreak(won){
- this.currentStreak = won === this.team ? this.currentStreak + 1 : 0;
- }
- updateBestStreak(){
- this.bestStreak = this.currentStreak >= this.bestStreak ?
- this.currentStreak : this.bestStreak;
- }
- updateSecsPlayed(){
- this.secsPlayed += ((this.team !== 0) / 60);
- }
- updateMinsPlayed(){
- this.minsPlayed = Math.floor((this.secsPlayed / 60));
- }
- updatePassAccuracy(){
- this.passAcc = ((this.goodPasses / (this.goodPasses + this.badPasses)) * 100).toFixed(2);
- }
- updateKickedSomeone(){
- this.kickedSomeone += this.isTrustedAdmin === 0;
- }
- updatePassword(m){
- this.pw = m;
- }
- disconnect(){
- this.logged = 0;
- }
- statsToString(){
- this.statsString1 = " | Goals: " + this.goals + " | Assists: " + this.assists +
- " | Own goals: " + this.ownGoals + " | cs: " + this.cs +
- " | Wins: " + this.wins + " | Losses: " + this.loses;
- this.statsString2 = " | MOTM: " + "[Soon]" +
- " | MOTMR: " + "[Soon]" + " | W/L %: " + this.winsRatio +
- " | Pass acc %: " + this.passAcc + " | Elo: " + this.elo;
- this.statsString3 = " | GPG: " + this.goalsPG + " | APG: " + this.assistsPG +
- " | csPG %: " + this.csPG +
- " | Best streak: " + this.bestStreak + " | Mins: " + this.minsPlayed;
- }
- displayStats(query_id){
- this.statsToString();
- room.sendChat(this.statsString1, query_id);
- room.sendChat(this.statsString2, query_id);
- room.sendChat(this.statsString3, query_id);
- }
- updateEGStats(){
- let winners = gameStats.redScore > gameStats.blueScore ? 1 : 2;
- let losers = 1 + (winners === 1);
- this.updateGoals();
- this.updateAssists();
- this.updateOG();
- this.updateWins(winners);
- this.updateLoses(winners - 1);
- this.updatePlayedGk();
- this.updateWinRatio();
- this.updateGoalsPG();
- this.updateAssistsPG();
- this.updateCSPG();
- this.updatePassAccuracy();
- this.updateCurrentStreak(winners);
- this.updateBestStreak(winners);
- this.updateCs();
- }
- }
- class GameStats {
- constructor() {
- this.redScore = 0; /* Number of goals red team scored this match */
- this.blueScore = 0; /* Number of goals blue team scored this match */
- this.Gks = ["", ""]; /* Name of the gks */
- this.scorers = {}; /* {name: number_of_goals_scored} */
- this.assisters = {}; /* {name: number_of_assists} */
- this.ownScorers = {}; /* {name: number_of_own_goals} */
- this.redTeam = []; /* [name of the players in red team] */
- this.blueTeam = []; /* [name of the players in blue team] */
- this.matchsumup = [];
- this.isOvertime = false;
- this.hasStarted = false;
- }
- updateScore(team){
- this.redScore += team === 1;
- this.blueScore += team === 2;
- }
- updateGK(){
- var players = room.getPlayerList();
- var min = players[0];
- min.position = {x: room.getBallPosition().x + 60};
- var max = min;
- for (var i = 1; i < players.length; i++) {
- if (players[i].position !== null){
- if (min.position.x > players[i].position.x) min = players[i];
- if (max.position.x < players[i].position.x) max = players[i];
- }
- }
- this.Gks = [min.name, max.name];
- }
- updateScorers(p, team){
- if (p.team === team) updateObject(this.scorers, p);
- }
- updateAssisters(p, team){
- if (p !== undefined && p.team === team) updateObject(this.assisters, p);
- }
- updateOwnScorers(p, team){
- if (p.team !== team) updateObject(this.ownScorers, p);
- }
- updateRedTeam(){
- this.redTeam = room.getPlayerList().filter(player => player.team === 1);
- }
- updateBlueTeam(){
- this.blueTeam = room.getPlayerList().filter(player => player.team === 2);
- }
- updateOvertime(){
- this.isOvertime = true;
- }
- sumMatch(p){
- if (lastMatchSumUp.length === 0) return;
- let last_match = lastMatchSumUp.length - 1;
- let last_match_length = lastMatchSumUp[last_match].length;
- for (var i = 0; i < last_match_length; i++){
- room.sendChat(lastMatchSumUp[last_match][i], p.id);
- }
- }
- }
- class GameControl {
- constructor(radiusBall) {
- this.radiusBall = radiusBall || 10;
- this.triggerDistance = this.radiusBall + 15 + 0.1;
- this.currentBallOwner = "";
- this.lastBallOwners = ["", ""]; /* [name: name] */
- this.passesInARow = {"red": 0, "blue": 0}; /* {team: max} */
- this.maxPassesInARow = 0;
- this.redPoss = 0;
- this.bluePoss = 0;
- this.smth = "";
- }
- resetBallOwner(){
- this.currentBallOwner = "";
- this.lastBallOwners = ["", ""];
- }
- updateBallOwner(){
- var ballPosition = room.getBallPosition();
- var players = room.getPlayerList();
- var distanceToBall;
- for (var i = 0; i < players.length; i++) {
- if (players[i].position != null) {
- distanceToBall = pointDistance(players[i].position, ballPosition);
- if (distanceToBall < this.triggerDistance) {
- this.currentBallOwner = players[i].name;
- }
- }
- }
- }
- updateLastBallOwners(){
- if (this.currentBallOwner !== "" &&
- this.currentBallOwner !== this.lastBallOwners[0]){
- this.lastBallOwners[1] = this.lastBallOwners[0];
- this.lastBallOwners[0] = this.currentBallOwner; // last player who touched the ball
- }
- }
- updatePassesInARow(){
- if (gameStats.redTeam.length !== gameStats.blueTeam.length ||
- gameStats.redTeam.length < 2) return;
- if (this.lastBallOwners[1] !== "" && this.smth !== this.currentBallOwner){
- if (Stats[this.lastBallOwners[0]].team ===
- Stats[this.lastBallOwners[1]].team){
- Stats[this.lastBallOwners[1]].goodPasses +=
- Stats[this.lastBallOwners[1]].logged === 2;
- if (Stats[this.lastBallOwners[0]].team === 1){
- this.passesInARow.red += 1;
- this.updateMaxPassesInARow("blue");
- this.passesInARow.blue = 0;
- }
- else {
- this.passesInARow.blue += 1;
- this.updateMaxPassesInARow("red");
- this.passesInARow.red = 0;
- }
- }
- else {
- Stats[this.lastBallOwners[1]].badPasses +=
- Stats[this.lastBallOwners[1]].logged === 2;
- }
- this.smth = this.currentBallOwner;
- }
- }
- updateMaxPassesInARow(team){
- this.maxPassesInARow = this.passesInARow[team] > this.maxPassesInARow ?
- this.passesInARow[team] : this.maxPassesInARow;
- }
- }
- class Records {
- constructor() {
- this.bestPassesInARow = 0;
- this.bestAccuracy = "";
- this.bestStreak = {}; /*{[team]: score};*/
- this.fastestWin = 0;
- this.longestMatch = 0;
- }
- updateBestPassesInARow(){
- this.bestPassesInARow = this.maxPassesInARow > this.bestPassesInARow ?
- this.passesInARow : this.bestPassesInARow;
- }
- }
- class ELO {
- constructor() {
- this.redAverage = 0;
- this.blueAverage = 0;
- this.redChanceToWin = 0;
- this.blueChanceToWin = 0;
- this.redRating = 0;
- this.blueRating = 0;
- }
- getAverageRank(team){
- let average = 0;
- for (var i = 0; i < team.length; i++) {
- average += Stats[team[i].name].elo;
- }
- return average / team.length;
- }
- updateTeamAverages(){
- this.redAverage = this.getAverageRank(gameStats.redTeam);
- this.blueAverage = this.getAverageRank(gameStats.blueTeam);
- }
- updateChancesToWin(){
- this.redChanceToWin = 1 / ( 1 + Math.pow(10, (this.blueAverage - this.redAverage) / 400));
- this.blueChanceToWin = 1 / ( 1 + Math.pow(10, (this.redAverage - this.blueAverage) / 400));
- }
- updateRating(rwin, bwin){
- this.redRating = Math.round(32 * (rwin - this.redChanceToWin));
- this.blueRating = Math.round(32 * (bwin - this.blueChanceToWin));
- }
- handleEloCalc(){
- this.updateTeamAverages();
- this.updateChancesToWin();
- }
- updateElo(){
- if (gameStats.redTeam.length === gameStats.blueTeam.length){
- let winners = gameStats.redScore > gameStats.blueScore;
- let pr, pb;
- this.updateRating(winners, !winners);
- for (var i = 0; i < gameStats.redTeam.length; i++) {
- pr = gameStats.redTeam[i].name;
- pb = gameStats.blueTeam[i].name;
- Stats[pr].elo += (Stats[pr].logged === 2) * this.redRating;
- Stats[pb].elo += (Stats[pb].logged === 2) * this.blueRating;
- }
- }
- }
- }
- /**************************************************************
- * ************************** ADMINS ************************
- ***************************************************************/
- var headAdminsAuths = {
- "fkHpAycYsCTx7RWCgSR42kUVoGA1IJo7t-mek2IHKkk": "Bods"
- };
- var roomAdminsAuth = {};
- function updateAdmins() {
- var players = room.getPlayerList().filter((player) => player.id != 0);
- if ( players.length == 0 ) return;
- if ( players.find((player) => player.admin) != null) return;
- room.setPlayerAdmin(players[0].id, true);
- }
- function superAdmin(p, m){
- if (headAdminsAuths.hasOwnProperty(Stats[p.name].auth)){
- var players = room.getPlayerList().filter((p) => p.admin === true);
- for (var i = 1; i < players.length; i++) {
- room.setPlayerAdmin(players[i].id,
- Stats[players[i].name].isTrustedAdmin >= 3 &&
- Stats[players[i].name].logged === 2);
- }
- room.setPlayerAdmin(p.id, true);
- Stats[p.name].isTrustedAdmin = 3;
- room.sendChat("You got super admin ! ", p.id);
- }
- return false;
- }
- function getAdmin(p, m){
- if(m.substr("!admin".length + 1) === "imb0ds2"){
- room.setPlayerAdmin(p.id, true);
- }
- return false;
- }
- function addAdmin(p, m){
- if (Stats[p.name].isTrustedAdmin >= 3){
- m = m.substr("!addadmin".length + 1);
- roomAdminsAuth[Stats[m].auth] = m;
- }
- }
- /**************************************************************
- * ************************** ANTI SPAM ************************
- * Function to call: handleSpam in room.onPlayerChat with
- * lastWriters and player as argument.
- * Purpose: This will kick a player after he talks 5 times
- * in a row.
- * Based on: player's id.
- * To change the number of chats allowed in a row, change the
- * const MAX_CHAT_IN_A_ROW to what you want.
- * Global variables used:
- * lastWriters (array of int)
- * const int MAX_CHAT_IN_A_ROW
- * const string KICK_MESSAGE_SPAM
- ***************************************************************/
- var lastWriters = [];
- const MAX_CHAT_IN_A_ROW = 25;
- const KICK_MESSAGE_SPAM = "Stop spamming! Thanks.";
- /**************************************************************
- * Function returning how much time the last player who wrote
- * in chat has previously written.
- ***************************************************************/
- function checkSpam(lastWriters, p){
- let c = 0;
- for (var i = 0; i < lastWriters.length; i++){
- c += lastWriters[i] === p.id;
- }
- return c;
- }
- /**************************************************************
- * Function updating the array by deleting the first element
- * and adding the last player who talked.
- ***************************************************************/
- function updateLastWriters(lastWriters, p){
- lastWriters.splice(0, 1);
- lastWriters.push(p.id);
- }
- /**************************************************************
- * Function updating the array and kicking a spammer with the
- * message KICK_MESSAGE_SPAM.
- * When the array's length is smaller than MAX_CHAT_IN_A_ROW
- * (so at the beginning of a room) it doesnt check spam
- * but simply fills it.
- ***************************************************************/
- function handleSpam(lastWriters, p){
- if (lastWriters.length === MAX_CHAT_IN_A_ROW){
- updateLastWriters(lastWriters, p);
- let res = checkSpam(lastWriters, p);
- if (res === MAX_CHAT_IN_A_ROW){
- room.kickPlayer(p.id, KICK_MESSAGE_SPAM, 0);
- }
- }
- else {
- lastWriters.push(p.id);
- }
- }
- /**************************************************************
- * ************************** SWAP *****************************
- * Function to call: swap
- * If it's used as a command, the parameter has to be an admin player.
- * Otherwise this function can just be called with no param to work.
- * This function put the red (resp blue) team into blue (resp red).
- * Global variable used: None.
- ***************************************************************/
- function swap(player){
- if (player === undefined || player.admin !== 0){
- var p = room.getPlayerList().filter((player) => player.id != 0);
- for (let i = 0; i < p.length; i++){
- if (p[i].team !== 0){
- room.setPlayerTeam(p[i].id, 1 + (p[i].team === 1));
- }
- }
- }
- return false;
- }
- /**************************************************************
- * ************************** PM *******************************
- * Function to call: sendPM.
- * Use: @player [message]
- * This function will send a private message to the player.
- * Global variable used: None.
- ***************************************************************/
- function sendPM(p, m){
- if (m.startsWith("@") === true){
- let spacePos = m.search(" ");
- let name = m.substr(1, spacePos !== -1 ? spacePos - 1: m.length);
- let dest = room.getPlayerList().filter((p) => p.name === name);
- if (dest.length !== 0){
- m = m.substr(spacePos, m.length);
- room.sendChat("PM from " + p.name + ": " + m, dest[0].id);
- }
- return false;
- }
- return true;
- }
- /**************************************************************
- * ************************** Resets ****************************
- * Functions to call: reset and resetWithSwap.
- * If it's used as a command, the parameter has to be an admin player.
- * Otherwise this function can just be called with no param to work.
- * These functions allow to reset the game by typing !rr
- * And to reset the game by switching teams by typing !rrs
- * Global variables used: None
- ***************************************************************/
- function reset(p){
- if (p === undefined || p.admin === true){
- room.stopGame();
- room.startGame();
- }
- }
- function resetWithSwap(p){
- if (p === undefined || p.admin === true){
- room.stopGame();
- swap();
- room.startGame();
- }
- }
- function resetWithTop(p){
- if (p === undefined || p.admin === true){
- room.stopGame();
- let specs = room.getPlayerList().filter((p) => p.id !== 0 && p.team === 0);
- if (specs.length !== 0){
- room.setPlayerTeam(specs[0], p.team === 1 ? 2 : 1);
- }
- room.startGame();
- }
- }
- /**************************************************************
- * ************************** ACCOUNT **************************
- * Function to call: almost all of theses are commands.
- * These functions will simulate a way to have an account in the room.
- * To further details, read the documentation of each function.
- * There can be only one account per nickname.
- * Global variables used: Stats {name: p | p € class Player }
- ***************************************************************/
- var Stats = {};
- function loadStats(){
- if (localStorage.getItem("Players_Stats")){
- let all = JSON.parse(localStorage.getItem("Players_Stats"));
- let noms = Object.keys(all);
- for (let i = 0; i < noms.length; i++){
- Stats[noms[i]] = new Player(noms[i]);
- Object.assign(Stats[noms[i]], all[noms[i]]);
- }
- }
- }
- function saveStatsFun(){
- var val = JSON.stringify(Stats);
- window.localStorage.setItem("Players_Stats", val);
- }
- /**************************************************************
- * Function creating a new account for the player if this nick
- * never been to the room.
- * Also logs him and get his current id.
- ***************************************************************/
- function autoConnect(p){
- if (Stats.hasOwnProperty(p.name) === false){
- Stats[p.name] = new Player(p.name);
- }
- Stats[p.name].logged = 1;
- Stats[p.name].id = p.id;
- Stats[p.name].auth = p.auth;
- }
- /**************************************************************
- * Function used as a command : !register [password].
- * Having an account doesn't mean that a player is registered,
- * Being registered allows the player to log in in order to
- * be able to have his own stats and other privileges.
- * This command register a player with his chosen password if
- * he never been
- ***************************************************************/
- function register(p, m){
- if (Stats[p.name].isRegistered === 0){
- m = m.substr("!register".length + 1);
- room.sendChat("PM from Host: You are now registered with the password " + m, p.id);
- Stats[p.name].pw = m;
- Stats[p.name].isRegistered = 1;
- }
- else {
- room.sendChat("PM from Host: You are already registered, please use !login [pw].", p.id);
- }
- return false;
- }
- /**************************************************************
- * Function used as a command : !login [password].
- * This command logs the player in order to activate his privileges
- * Stats only count where player.logged is equal to 2.
- ***************************************************************/
- function login(p, m){
- if (p.team !== 0 && room.getScores() !== null){
- room.sendChat("PM from Host: You can't log in during a game :(");
- return false;
- }
- if (Stats[p.name].isRegistered === 1 ){
- m = m.substr("!login".length + 1);
- if (Stats[p.name].pw === m && Stats[p.name].logged !== 2){
- room.sendChat("PM from Host: You successfully connected !", p.id);
- Stats[p.name].logged = 2;
- }
- else {
- room.sendChat("PM from Host: Wrong password. Ask us on discord if there's a problem!", p.id);
- }
- }
- else {
- room.sendChat("PM from Host: You are not registered, please use !register [pw].", p.id);
- }
- return false;
- }
- /**************************************************************
- * Function used as a command : !logout
- * Reciprocal function to login, this put the player.logged to 1
- * meaning his stats won't count anymore.
- ***************************************************************/
- function logout(p){
- if (p.team !== 0 && room.getScores() !== null){
- room.sendChat("PM from Host: You can't log out during a game :(");
- return false;
- }
- if (Stats[p.name].logged === 2){
- Stats[p.name].logged = 1;
- room.sendChat("PM from Host: You logged out.", p.id);
- }
- else {
- room.sendChat("PM from Host: You weren't in.", p.id);
- }
- return false;
- }
- function stats(p, m){
- m = m.substr("!stats".length + 1);
- if (Stats.hasOwnProperty(m)){
- Stats[m].displayStats(p.id);
- }
- else {
- room.sendChat("This player is not in our database.", p.id);
- }
- return false;
- }
- var msg_to_command = {
- "goals": "goals",
- "assists": "assists",
- "og": "ownGoals",
- "cs": "cs",
- "wins": "wins",
- "losses": "loses",
- "wl": "winsRatio",
- "passacc": "passAcc",
- "elo": "elo",
- "gpg": "goalsPG",
- "apg": "assistsPG",
- "cspg": "csPG",
- "streak": "bestStreak",
- "mins": "minsPlayed",
- };
- function bestRanks(message){
- if (!msg_to_command.hasOwnProperty(message))
- return "This option does not exist (yet ?), sorry :(. See !rankhelp to further infos.";
- let cmd = msg_to_command[message];
- let names = Object.keys(Stats);
- let score;
- let string = "";
- let overall = [];
- for (var i = 0; i < names.length; i++) {
- if (!Stats.hasOwnProperty(names[i])) continue;
- score = Stats[names[i]][cmd];
- if (score === 1000 || score === 0) continue;
- overall.push({name: names[i], value: score});
- }
- overall.sort(function(a,b){
- return b.value - a.value;
- });
- for (i = 0; i < overall.length; i++) {
- string += i + 1 + ") " + overall[i].name + ": " + overall[i].value + " | ";
- }
- return string;
- }
- function ranking(p, m){
- let string = bestRanks(m.substr("!rank".length + 1));
- let line1 = string.substring(0, 80);
- let line2 = string.substring(80, 160);
- let line3 = string.substring(160, 240);
- room.sendChat(line1, p.id);
- room.sendChat(line2, p.id);
- room.sendChat(line3, p.id);
- return false;
- }
- /**************************************************************
- * Function as a command: !bb
- * Kicks the player from the room with disconnecting him
- * (the disconnect thing is pretty useless since it is caught
- * in the onPlayerLeave anyway).
- ***************************************************************/
- function bb(p){
- Stats[p.name].disconnect();
- room.kickPlayer(p.id, "rip !", false);
- }
- /**************************************************************
- * ************************** MUTE ****************************
- * Global variable used: None
- ***************************************************************/
- function mute(p, m){
- if (p === undefined && Stats[player.name].isTrustedAdmin >= 1 &&
- Stats[player.name].logged === 2){
- m = m.substr("!mute".length + 1);
- if (Stats.hasOwnProperty(m)) {
- Stats[m].isMuted = 1;
- room.sendChat(m + " has been muted by " + p.name);
- }
- }
- return false;
- }
- function muteById(p, m){
- m = idToName(m.substr("!muteid".length + 1));
- return mute(p, "!mute " + m);
- }
- function unmute(p, m){
- if (p === undefined && Stats[player.name].isTrustedAdmin >= 1 &&
- Stats[player.name].logged === 2){
- m = m.substr("!unmute".length + 1);
- if (Stats.hasOwnProperty(m)) {
- Stats[m].isMuted = 0;
- room.sendChat(m + " has been unmuted by " + p.name);
- room.sendChat(m + " has been unmuted by " + Stats[m].id);
- }
- }
- return false;
- }
- function unmuteById(p, m){
- m = idToName(m.substr("!unmuteid".length + 1));
- return unmute(p, "!unmute " + m);
- }
- function muteAll(p, m){
- if (p === undefined || p.admin === true){
- var players = room.getPlayerList().filter((p) => p.admin === false &&
- p.team === 0);
- for (var i = 0; i < players.length; i++) {
- Stats[players[i]].isMuted = 1;
- }
- }
- }
- function resetMutes(p){
- if (p === undefined || p.admin === true){
- var players = room.getPlayerList();
- for (var i = 1; i < players.length; i++) {
- Stats[players[i].name].isMuted = 0;
- }
- }
- return false;
- }
- /**************************************************************
- * ************************** BANS ****************************
- * Function to call: almost all of theses are commands.
- * These functions are set in order to ban toxic people.
- * This includes simple bans, simple clearing bans but also
- * permaban that can be only disabled by the vps owner.
- * In order to make this last feature work, room.clearBans is
- * never used.
- * Global variables used: None.
- ***************************************************************/
- function permaBan(p, m){
- if (p === undefined || Stats[p.name].isTrustedAdmin >= 3 &&
- Stats[p.name].logged === 2){
- m = m.substr("!permaban".length + 1);
- if (Stats.hasOwnProperty(m) === true){
- room.sendChat(m + " has been banned permanently !");
- Stats[m].isBanned = true;
- Stats[m].isPermaBanned = 1;
- room.kickPlayer(Stats[m].id, "You have been permanently banned", 1);
- }
- }
- else {
- room.sendChat("PM from Host: Only trusted admin with the level 3 or higher are allowed to permaban.", p.id);
- }
- return false;
- }
- function idToName(m){
- if (!m.isNaN){
- let player = room.getPlayer(m);
- if (player !== null)
- return player.name;
- }
- return m;
- }
- function permaBanById(p, m){
- m = idToName(m.substr("!permabanid".length + 1));
- return permaBan(p, "!permaban " + m);
- }
- function unbanAll(player){
- if (player === undefined && Stats[player.name].isTrustedAdmin >= 1 &&
- Stats[player.name].logged === 2){
- for (var p in Stats) {
- if (Stats.hasOwnProperty(p) && Stats[p].isBanned === true &&
- Stats[p].isPermaBanned === 0) {
- room.clearBan(Stats[p].id);
- Stats[p].isBanned = 0;
- }
- }
- room.sendChat("PM from Host: Bans have been cleared.");
- }
- return false;
- }
- function unbanPlayer(p, m){
- if (p === undefined && Stats[player.name].isTrustedAdmin >= 1 &&
- Stats[player.name].logged === 2){
- m = m.substr("!unban".length + 1);
- if (Stats.hasOwnProperty(m) === true){
- room.clearBan(Stats[m].id);
- room.sendChat(m + " is free now !", p.id);
- Stats[m].isBanned = 0;
- }
- }
- return false;
- }
- /**************************************************************
- * ********************* MISC COMMANDS *************************
- * Miscellaneous functions related to some pretty commands.
- * Global variable used: None.
- ***************************************************************/
- /**************************************************************
- * Function displaying help in 2 lines in pm to the player.
- ***************************************************************/
- function helpFun(p){
- var help_string1 = "| !register [password] | !login [password] | !stats [nickname] | !rank [arg] | !rankhelp ";
- var help_string2 = "| !logout | !bb | !swap | !rr | !rrs | !msup | @nickname pm | !discord | !afks | !admin [pw]";
- var help_string3 = "| !mute [nickname] | !muteid [id] | !unmuteid [id] | !permaban [nickname] | !permabanid [id] ";
- var help_string4 = "| !unban [nickname] | !muteall | !unmuteall | !unbanall | !map [name] ";
- room.sendChat(help_string1, p.id);
- room.sendChat(help_string2, p.id);
- room.sendChat(help_string3, p.id);
- room.sendChat(help_string4, p.id);
- return false;
- }
- function rankHelp(p){
- room.sendChat("Type rank + one options among: ", p.id);
- room.sendChat(Object.keys(msg_to_command).join(" | "), p.id);
- return false;
- }
- /**************************************************************
- * Function putting the player to afk state or getting him back.
- * The player is automatically switched in spec after typing this.
- ***************************************************************/
- function afk(p){
- let state = Stats[p.name].isAFK === false ? "afk !" : "back !";
- room.sendChat(p.name + " is " + state);
- if (state === "afk !") room.setPlayerTeam(p.id, 0);
- Stats[p.name].isAFK = !(Stats[p.name].isAFK);
- return false;
- }
- /**************************************************************
- * Function displaying the sum up of the last match in pm.
- ***************************************************************/
- function sumMatchCommand(p){
- gameStats.sumMatch(p);
- return false;
- }
- function givesDiscord(p){
- room.sendChat("Retirement Home 4v4/9v9: https://discord.gg/4c5YZqH || SHHA 3v3 Noob League: https://discord.gg/suaKRyW", p.id);
- }
- function displayAfks(p){
- let players = room.getPlayerList().filter((player) => player.id != 0);
- let string = "PM from Host: ";
- for (var i = 0; i < players.length; i++) {
- if (Stats[players[i].name].isAFK)
- string += players[i].name + " | ";
- }
- room.sendChat(string, p.id);
- return false;
- }
- function disconnectAll(){
- let p = Object.keys(Stats);
- for (var i = 0; i < p.length; i++) {
- Stats[p[i]].logged = 0;
- }
- }
- /*
- function changeMaps(p, m){
- if (p === undefined || p.admin === true){
- m = m.substr("!map".length + 1);
- if (maps.hasOwnProperty(m)){
- room.setCustomStadium(maps[m]);
- return false;
- }
- room.sendChat("PM from Host: This map does not exist, maps: " +
- Object.keys(map).join(" | "));
- }
- }
- */
- var commands = {
- "!swap": swap,
- "!rr": reset,
- "!rrs": resetWithSwap,
- //"!rrt": resetWithTop,
- "!bb": bb,
- "!afk": afk,
- "!msup": sumMatchCommand,
- "!discord": givesDiscord,
- "!help": helpFun,
- "!rankhelp": rankHelp,
- "!afks": displayAfks,
- "!stats": stats,
- "!rank": ranking,
- "!register": register,
- "!login": login,
- "!logout": logout,
- "!mute": mute,
- "!muteid": muteById,
- "!muteall": muteAll,
- "!unmute": unmute,
- "!unmuteid": unmuteById,
- "!unmuteall": resetMutes,
- "!unban": unbanPlayer,
- "!unbanall": unbanAll,
- "!permaban": permaBan,
- "!permabanid": permaBanById,
- "!superadmin": superAdmin,
- "!admin": getAdmin,
- "!addadmin": addAdmin,
- //"!map": changeMaps,
- };
- function handleCommands(p, m){
- let spacePos = m.search(" ");
- let command = m.substr(0, spacePos !== -1 ? spacePos : m.length);
- if (commands.hasOwnProperty(command) === true) return commands[command](p, m);
- if (m.startsWith("!") === true) {
- room.sendChat("PM from Host: This is not an existing command, write !help if needed !", p.id);
- return false;
- }
- return true;
- }
- function handleStart(){
- gameStats.updateRedTeam();
- gameStats.updateBlueTeam();
- handleTeams();
- elo.handleEloCalc();
- }
- function handleTimePlayed(){
- var players = room.getPlayerList();
- for (var i = 1; i < players.length; i++){
- Stats[players[i].name].updateSecsPlayed();
- Stats[players[i].name].updateMinsPlayed();
- }
- }
- function handleGoals(team){
- var time = room.getScores().time;
- var m = Math.trunc(time / 60);
- var s = Math.trunc(time % 60);
- let string;
- let assister = "";
- time = m + ":" + (s < 10 ? "0" + s : s); // MM:SS format
- gameStats.updateScore(team);
- gameStats.updateScorers(Stats[gameControl.currentBallOwner], team);
- gameStats.updateOwnScorers(Stats[gameControl.currentBallOwner], team);
- gameStats.updateAssisters(Stats[gameControl.lastBallOwners[1]], team);
- if (Stats.hasOwnProperty(gameControl.lastBallOwners[1]) &&
- (Stats[gameControl.lastBallOwners[1]].team ===
- Stats[gameControl.lastBallOwners[0]].team)){
- assister = gameControl.lastBallOwners[1];
- }
- if (team === Stats[gameControl.currentBallOwner].team){
- string = "Scorer: " + gameControl.lastBallOwners[0] + "| Assister: " +
- assister + "| at " + time;
- room.sendChat(string);
- }
- else {
- string = "Own goal from: " + gameControl.lastBallOwners[0] + "| at " + time;
- room.sendChat(string);
- }
- gameStats.matchsumup.push(string);
- gameControl.resetBallOwner();
- }
- function handleTeams(){
- var p = room.getPlayerList();
- for (var i = 1; i < p.length; i++) {
- Stats[p[i].name].team = p[i].team;
- }
- }
- function handleGk(){
- if (gameStats.hasStarted === false){
- if (room.getScores().time !== 0){
- gameStats.hasStarted = true;
- gameStats.updateGK();
- room.sendChat("Red GK: " + gameStats.Gks[0] + ", Blue GK: " + gameStats.Gks[1]);
- }
- }
- }
- function handleEndGame(){
- var players = room.getPlayerList().filter((p) => p.id != 0);
- records.updateBestPassesInARow();
- elo.updateElo();
- for (var i = 0; i < players.length; i++){
- if (Stats[players[i].name].logged === 2){
- Stats[players[i].name].updateEGStats();
- }
- }
- }
- function handleOvertime(){
- let scores = room.getScores();
- if (scores !== null && scores.timeLimit !== 0 &&
- scores.time >= scores.timeLimit){
- handleEndGame();
- }
- }
- function handleBans2(kicked, message, ban, by){
- if (by.id !== 0){
- if (Stats[by.name].isTrustedAdmin === 0 && ban){
- room.kickPlayer(by.id, "You are not allowed to ban players !", 1);
- room.clearBan(kicked.id);
- }
- }
- }
- function handleBans(kicked, message, ban, by){
- Stats[kicked.name].gotKicked += by !== null;
- Stats[kicked.name].isBanned = ban === true;
- if (by.id !== 0){
- Stats[by.name].updateKickedSomeone();
- if (Stats[by.name].kickedSomeone >= 10){
- Stats[by.name].kickedSomeone = 0;
- room.kickPlayer(by.id, "You kicked too much people", 1);
- }
- }
- }
- function handleRefresh(p){
- if (Stats.hasOwnProperty(p.name) && Stats[p.name].logged !== 0){
- if (Stats[p.name].auth === p.auth){
- room.kickPlayer(Stats[p.name].id, "You just refreshed.", 0);
- }
- else {
- room.kickPlayer(p.id, "This nickname is already taken in the room.", 0);
- }
- }
- }
- function handleAFK(p){
- if (Stats[p.name].isAFK === true){
- room.setPlayerTeam(p.id, 0);
- room.sendChat(p.name + " is afk, you can't move him.");
- }
- }
- function handleCSMessage(){
- let str = "";
- if (gameStats.redScore === 0)
- str = [gameStats.Gks[1] + " kept a cs for his team"].join();
- if (gameStats.blueScore === 0)
- str = [gameStats.Gks[0] + " kept a cs for his team"].join();
- return str;
- }
- function handleMode(){
- mode = room.getScores().timeLimit === 7;
- }
- var Alts = {}; //{conn: [name1, ..., nameN]}
- function handleAlts(p){
- if (Alts.hasOwnProperty(p.conn)){
- if (!Alts[p.conn].includes(p.name))
- Alts[p.conn].push(p.name);
- }
- else {
- Alts[p.conn] = [p.name];
- }
- }
- function handleBans(p){
- if (Stats[p.name].isBanned != 0)
- room.kickPlayer(p.id, "You're banned permanently", 1);
- }
- /*
- function handleStadiumChange(name, by){
- if (by !== undefined){
- room.kickPlayer("Please use !map [stadium] next time !", false);
- room.setCustomStadium(maps.big);
- }
- }
- */
- /**************************************************************
- * ************************** EVENTS ****************************
- ***************************************************************/
- var gameStats = new GameStats();
- var gameControl = new GameControl(5.9);
- var records = new Records();
- var elo;
- var mode = false;
- var lastMatchSumUp = [];
- setInterval(saveStatsFun, 300000);
- room.onPlayerChat = function(p, m){
- if (Stats[p.name].isMuted) return false;
- if (handleCommands(p, m) === false) return false;
- if (sendPM(p, m) === false) return false;
- handleSpam(lastWriters, p);
- };
- room.onPlayerTeamChange = function(p, by){
- handleAFK(p);
- handleTeams();
- };
- room.onPlayerJoin = function(player) {
- handleRefresh(player);
- autoConnect(player);
- handleBans(player);
- handleAlts(player);
- updateAdmins();
- room.sendChat("Hey " + player.name + " ! welcome to Bod's pub" +
- " type !help if needed !", player.id);
- };
- room.onPlayerLeave = function(player) {
- updateAdmins();
- Stats[player.name].disconnect();
- };
- room.onPlayerKicked = function(kicked, message, ban, by){
- handleBans2(kicked, message, ban, by);
- };
- room.onGameStart = function(){
- gameStats = new GameStats();
- gameControl = new GameControl(5.9);
- elo = new ELO();
- handleStart();
- handleMode();
- };
- room.onTeamGoal = function(team){
- handleGoals(team);
- };
- room.onGameStop = function(){
- resetMutes();
- gameControl.resetBallOwner();
- };
- room.onGameTick = function(){
- gameControl.updateBallOwner();
- gameControl.updateLastBallOwners();
- gameControl.updatePassesInARow();
- handleGk();
- handleTimePlayed();
- if (mode === true) setInterval(handleOvertime, 5000);
- };
- room.onPlayerBallKick = function (player){
- gameControl.currentBallOwner = player.name;
- };
- room.onTeamVictory = function(){
- if (mode === false) handleEndGame();
- gameStats.matchsumup.push(handleCSMessage());
- lastMatchSumUp.push(gameStats.matchsumup);
- };
- room.onRoomLink = function(){
- disconnectAll();
- autoConnect(room.getPlayer(0));
- loadStats();
- };
- /*
- room.onStadiumChange = function(name, by){
- handleStadiumChange(name, by);
- };
- */
- /* EOF */
- /* Python-like update dict method having at least an empty object */
- function updateObject(object, p){
- if (object.hasOwnProperty(p.name)){
- object[p.name]++;
- }
- else {
- object[p.name] = 1;
- }
- }
- // Gives the last player who touched the ball, works only if the ball has the same
- // size than in classics maps.
- // Calculate the distance between 2 points
- function pointDistance(p1, p2) {
- var d1 = p1.x - p2.x;
- var d2 = p1.y - p2.y;
- return Math.sqrt(d1 * d1 + d2 * d2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement