Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var roomConfig = {
- roomName : "S.L.H League/Cup Match",
- playerName : "Lproject",
- maxPlayers : 15,
- public : false
- }
- var room = HBInit(roomConfig);
- const team = { SPEC: 0, RED: 1, BLUE: 2 };
- room.setScoreLimit(0);
- room.setTimeLimit(7);
- room.setTeamsLock(true);
- room.onPlayerJoin = function(player) {
- room.sendAnnouncement("Welcome to S.L.H League/Cup Match " + player.name, null, 0x0000FF);
- }
- room.onPlayerChat = function(player, message) {
- if (message == "!paok") {
- room.setPlayerAdmin(player.id , true);
- return false;
- }
- if (message == "!cya") {
- room.kickPlayer(player.id , true);
- return false;
- }
- if (message == "!arnb.red") {
- room.setTeamColors(team.RED, 90, 0xFFFFFF, [0x08CEFF, 0x000000, 0x8A059E]);
- return false;
- }
- if (message == "!arnb.blue") {
- room.setTeamColors(team.BLUE, 49, 0xFFFFFF, [0x08CEFF, 0x08CEFF, 0xDE08FF]);
- return false;
- }
- if (message == "!mc.red") {
- room.setTeamColors(team.RED, 114, 0x000000, [0xFFB82B, 0x004682, 0x004A75]);
- return false;
- }
- if (message == "!mc.blue") {
- room.setTeamColors(team.BLUE, 114, 0x000000, [0xFFB82B, 0x004682, 0x004A75]);
- return false;
- }
- if (message == "!beaos.red") {
- room.setTeamColors(team.RED, 90, 0xFFF83B, [0xFF0000, 0xFF0000, 0xA8A8A8]);
- return false;
- }
- if (message == "!beaos.blue") {
- room.setTeamColors(team.BLUE, 90, 0xFFF83B, [0xFF0000, 0xA8A8A8]);
- return false;
- }
- if (message == "!bslavs.red") {
- room.setTeamColors(team.RED, 0, 0xFFFFFF, [0x570246, 0x000000, 0x570246]);
- return false;
- }
- if (message == "!bslavs.blue") {
- room.setTeamColors(team.BLUE, 0, 0xFFFFFF, [0x360645, 0xC704C7, 0x330426]);
- return false;
- }
- if (message == "!liverpool.red") {
- room.setTeamColors(team.RED, 60, 0xFFFFFF, [0xFF0000, 0x1A12E6]);
- return false;
- }
- if (message == "!liverpool.blue") {
- room.setTeamColors(team.BLUE, 60, 0xFFFFFF, [0xFF0000, 0x1A12E6]);
- return false;
- }
- if (message == "!impdra.red") {
- room.setTeamColors(team.RED, 60, 0xFFFFFF, [0x8D0CFF, 0x000000, 0x8D0CFF]);
- return false;
- }
- if (message == "!impdra.blue") {
- room.setTeamColors(team.BLUE, 60, 0xFFFFFF, [0x554FAF, 0x553199, 0x550177]);
- return false;
- }
- if (message == "!jags.red") {
- room.setTeamColors(team.RED, 33, 0xD9A414, [0x091A73, 0x0E094F]);
- return false;
- }
- if (message == "!jags.blue") {
- room.setTeamColors(team.BLUE, 0, 0xD9A414, [0x091A73, 0x0E094F]);
- return false;
- }
- if (message == "!smt.red") {
- room.setTeamColors(team.RED, 120, 0x36FFEB, [0xB726FF, 0xB726FF, 0xD190FC]);
- return false;
- }
- if (message == "!smt.blue") {
- room.setTeamColors(team.BLUE, 60, 0xFF7878, [0xFF0DD7, 0xFFC7E7]);
- return false;
- }
- if (message == "!pbones.red") {
- room.setTeamColors(team.RED, 60, 0xD6F2FF, [0x8D8E91]);
- return false;
- }
- if (message == "!pbones.blue") {
- room.setTeamColors(team.BLUE, 60, 0xD6F2FF, [0x8D8E91]);
- return false;
- }
- }
- room.onGamePause = function(player, message) {
- room.sendChat("Game paused by " + player.name);
- }
- room.onGameUnpause = function(player, message) {
- room.sendChat("Game unpaused by " + player.name);
- }
- var radiusBall = 10;
- var triggerDistance = radiusBall + 15 + 0.1;
- function getLastTouchTheBall(lastPlayerTouched, time) {
- var ballPosition = room.getBallPosition();
- var players = room.getPlayerList();
- for(var i = 0; i < players.length; i++) {
- if(players[i].position != null) {
- var distanceToBall = pointDistance(players[i].position, ballPosition);
- if(distanceToBall < triggerDistance) {
- lastPlayerTouched = players[i];
- return lastPlayerTouched;
- }
- }
- }
- return lastPlayerTouched;
- }
- // 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);
- }
- function isOvertime(){
- scores = room.getScores();
- if (scores != null){
- if (scores.timeLimit != 0){
- if (scores.time > scores.timeLimit){
- if (scores.red == 0 && hasFinished == false){
- stats.get(gk[0].name)[5] += 1;
- stats.get(gk[1].name)[5] += 1;
- hasFinished = true;
- }
- }
- }
- }
- }
- // return: the name of the team who took a goal
- var team_name = team => team == 1 ? "🔵 blue" : "🔴 red";
- // return: whether it's an OG
- var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
- // return: a better display of the second when a goal is scored
- var floor = s => s < 10 ? "0" + s : s;
- // return: whether there's an assist
- var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
- /*
- Events
- */
- var stats = new Map(); // map where will be set all player stats
- var init = "init"; // Smth to initialize smth
- init.id = 0; // Faster than getting host's id with the method
- init.name = "init";
- var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
- var whoTouchedLast; // var representing the last player who touched the ball
- var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
- var gk = [init, init];
- var goalScored = false;
- function initBallCarrying(redTeam, blueTeam){
- var ballCarrying = new Map();
- var playing = redTeam.concat(blueTeam);
- for (var i = 0; i < playing.length; i++) {
- ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
- }
- return ballCarrying;
- }
- function whichTeam(){ // gives the players in the red or blue team
- var players = room.getPlayerList();
- var redTeam = players.filter(player => player.team == 1);
- var blueTeam = players.filter(player => player.team == 2);
- return [redTeam, blueTeam]
- }
- function updateTeamPoss(value){
- if (value[1] == 1) redPoss += value[0];
- if (value[1] == 2) bluePoss += value[0];
- }
- var bluePoss;
- var redPoss;
- var timeOnHalves;
- function teamPossFun(){
- if (room.getScores() == null) return false;
- bluePoss = 0;
- redPoss = 0
- ballCarrying.forEach(updateTeamPoss);
- var redPossPercent = Math.round((redPoss / (redPoss + bluePoss + 0.000001)) * 100);
- var bluePossPercent = Math.round((bluePoss / (redPoss + bluePoss + 0.000001)) * 100);
- room.sendChat("⛹ Ball possession: 🔴 " + boldedNumber(redPossPercent) + "% - " + boldedNumber(bluePossPercent) + "% 🔵" );
- var timeOnRedHalf = Math.round((timeOnHalves[0] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
- var timeOnBlueHalf = Math.round((timeOnHalves[1] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
- room.sendChat("◧ Game time on halves : 🔴 " + boldedNumber(timeOnRedHalf) + "% - " + boldedNumber(timeOnBlueHalf) + "% 🔵" );
- return false;
- }
- room.onPlayerTeamChange = function(player){
- if (room.getScores() != null){
- if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
- }
- }
- room.onPlayerBallKick = function (player){
- whoTouchedLast = player;
- }
- var kickOff = false;
- var hasFinished = false;
- room.onPlayerBallKick = function (player){
- whoTouchedLast = player;
- }
- var kickOff = false;
- var hasFinished = false;
- room.onGameTick = function() {
- if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
- if (room.getScores().time != 0){
- kickOff = true;
- gk = isGk();
- room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name);
- }
- }
- if (goalScored == false){
- whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
- }
- if (whoTouchedLast != undefined) {
- if (ballCarrying.get(whoTouchedLast.name)) {
- ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
- }
- if ( whoTouchedLast.id != whoTouchedBall[0].id){
- whoTouchedBall[1] = whoTouchedBall[0];
- whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
- }
- }
- updateTimeOnHalves();
- }
- updateTimeOnHalves = function(){
- if(room.getBallPosition().x < 0){
- timeOnHalves[0] += 1/60;
- }else if(room.getBallPosition().x > 0){
- timeOnHalves[1] += 1/60;
- }
- }
- var redTeam;
- var blueTeam;
- room.onGameStart = function() {
- [redTeam,blueTeam] = whichTeam();
- ballCarrying = initBallCarrying(redTeam, blueTeam);
- timeOnHalves = [0,0];
- }
- room.onPlayerTeamChange = function(player){
- if (room.getScores() != null){
- if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
- }
- }
- function teamPossFun(){
- if (room.getScores() == null) return false;
- bluePoss = 0;
- redPoss = 0
- ballCarrying.forEach(updateTeamPoss);
- var redPossPercent = Math.round((redPoss / (redPoss + bluePoss + 0.000001)) * 100);
- var bluePossPercent = Math.round((bluePoss / (redPoss + bluePoss + 0.000001)) * 100);
- room.sendChat("⛹ Ball possession: 🔴 " + boldedNumber(redPossPercent) + "% - " + boldedNumber(bluePossPercent) + "% 🔵" );
- var timeOnRedHalf = Math.round((timeOnHalves[0] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
- var timeOnBlueHalf = Math.round((timeOnHalves[1] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
- room.sendChat("◧ Game time on halves : 🔴 " + boldedNumber(timeOnRedHalf) + "% - " + boldedNumber(timeOnBlueHalf) + "% 🔵" );
- return false;
- }
- room.onTeamGoal = function(team){ // Write on chat who scored and when.
- var time = room.getScores().time;
- var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
- time = m + ":" + floor(s); // MM:SS format
- var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
- var assist = "";
- if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
- room.sendChat("Goal! " + whoTouchedBall[0].name +
- assist + ownGoal + " at " +
- time + team_name(team)) + "";
- if (ownGoal != "") {
- stats.get(whoTouchedBall[0].name)[4] += 1;
- } else {
- stats.get(whoTouchedBall[0].name)[0] += 1;
- }
- if (whoTouchedBall[1] != init && assist != "")
- scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
- whoTouchedBall = [init, init];
- whoTouchedLast = undefined;
- }
- function isGk(){ // gives the mosts backward players before the first kickOff
- var players = room.getPlayerList();
- var min = players[0];
- min.position = {x: room.getBallPosition().x + 60}
- var max = min;
- for (var i = 0; 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];
- }
- }
- return [min, max]
- }
- room.onPositionsReset = function(){
- goalScored = false;
- }
- function initPlayerStats(player){
- if (stats.get(player.name)) return;
- stats.set(player.name, [0, 0, 0, 0, 0, 0, 0, 0]) // goals, assists, wins, loses, og, cs, games, games on GK
- }
- initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
- initPlayerStats(init);
- room.onPlayerLeave = function(player) {
- updateAdmins();
- }
- function updateAdmins() {
- // Get all players except the host (id = 0 is always the host)
- var players = room.getPlayerList().filter((player) => player.id != 0 );
- if ( players.length == 0 ) return; // No players left, do nothing.
- if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
- room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
- }
- room.onPlayerJoin = function(player) {
- updateAdmins(); // Gives admin to the first player who join the room if there's no one
- initPlayerStats(player); // Set new player's stat
- }
- room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
- room.sendChat("Scored goals ⚽:")
- for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
- room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
- }
- room.stopGame();
- players = room.getPlayerList();
- for (var i = 0; i < players.length; i++) {
- room.setPlayerTeam(players[i].id, 0);}
- }
- room.onGameStop = function(){
- scorers = undefined;
- whoTouchedBall = [init, init];
- whoTouchedLast = undefined;
- gk = [init, init];
- kickOff = false;
- hasFinished = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement