Skip to content

Instantly share code, notes, and snippets.

@yerooleopard
Last active June 2, 2018 16:45
Show Gist options
  • Save yerooleopard/b8bccf9bf0d1f037eaf8a8efa9e0220b to your computer and use it in GitHub Desktop.
Save yerooleopard/b8bccf9bf0d1f037eaf8a8efa9e0220b to your computer and use it in GitHub Desktop.
Headless 6Man Strong Host: https://html5.haxball.com/headless. Installation Instruction: https://youtu.be/9u9FEB8Jimc?t=1m51s
/*
* Host Configuration
*/
var ROOM_NAME = "6MAN STRONG 2.0";
var HOST_NAME = "[HOST]";
var MAX_PLAYERS = 12;
var ROOM_PUBLIC = true;
var HOST_GEO = {"code": "PL", "lat" : 52, "lon" : 21};
var ADMIN_PASSWORD = 'YOUR_ADMIN_PASSWORD'; // type "!admin YOUR_ADMIN_PASSWORD" to get admin on server
/*
* Gameplay Configuration
*/
var touchDetection = true;
var chat = true;
var ignoreList = [];
var _ballRadius = 9.2; // required for correct touch detection. Do not change unless you know what you doing
/**
* Available commands
*
* !admin YOUR_ADMIN_PASSWORD - set you admin on a server
* !touch_on - enable touch detection - enabled by default (admin only)
* !touch_off - disable touch detection (admin only)
* !set_skins - set custom player skins (admin only)
* !reset_skins - reset skins to default (admin only)
* !p - pause the game (admin only)
* !g - unpause the game (admin only)
* !rr - restart game (admin only)
* !rrs - resart game and switch team sides (admin only)
* !s - switch team sides (admin only)
* !clear_bans - clear all bans (admin only)
* !clear_bans_s - clear all bans silently (without message) (admin only)
* !chat_on - enable chat
* !chat_off - disable chat
* !ignore [player name] - silence player by name
* !unignore [player name] - remove silenced player from ignore list
* !ignore_list - display ignore list on chat
* !ignore_clear - clear ignore list
* !players - display player list on chat
*/
/**
* Configuration end
*/
/**
* Map
*/
var customMap = `{
"name" : "6Man Strong",
"width" : 330,
"height" : 240,
"spawnDistance" : 75,
"bg" : { "type" : "hockey", "height" : 240, "width" : 330, "cornerRadius" : 10, "kickOffRadius" : 0 },
"vertexes" : [
/* 0 */ { "x" : 150, "y" : 240, "trait" : "ballArea" },
/* 1 */ { "x" : -150, "y" : 240, "trait" : "ballArea" },
/* 2 */ { "x" : 150, "y" : -240, "trait" : "ballArea" },
/* 3 */ { "x" : -150, "y" : -240, "trait" : "ballArea" },
/* 4 */ { "x" : 330, "y" : 0, "trait" : "ballArea" },
/* 5 */ { "x" : -330, "y" : 0, "trait" : "ballArea" },
/* 6 */ { "x" : 75, "y" : -240, "trait" : "goalNet", "curve" : 180, "color" : "FF00FF" },
/* 7 */ { "x" : -75, "y" : -240, "trait" : "goalNet", "curve" : 180, "color" : "FF00FF" },
/* 8 */ { "x" : 75, "y" : 240, "bCoef" : 2, "cMask" : ["wall" ], "cGroup" : ["red","blue","ball" ], "trait" : "ballArea", "color" : "00FFFF", "curve" : -90 },
/* 9 */ { "x" : -75, "y" : 240, "bCoef" : 2, "cMask" : ["wall" ], "cGroup" : ["red","blue","ball" ], "trait" : "ballArea", "color" : "00FFFF", "curve" : -180 },
/* 10 */ { "x" : 195, "y" : 180, "bCoef" : 0.1, "cMask" : ["ball" ], "cGroup" : ["red","blue","ball" ], "trait" : "goalNet", "color" : "808080", "curve" : -180 },
/* 11 */ { "x" : -195, "y" : 180, "trait" : "goalNet", "curve" : 180, "color" : "808080" },
/* 12 */ { "x" : 195, "y" : -180, "trait" : "goalNet", "curve" : 180, "color" : "FFFF00" },
/* 13 */ { "x" : -195, "y" : -180, "trait" : "goalNet", "curve" : -180, "color" : "0000FF" },
/* 14 */ { "x" : 285, "y" : 60, "bCoef" : 0.1, "cMask" : ["ball" ], "cGroup" : ["red","blue","ball" ], "trait" : "goalNet", "color" : "808080", "curve" : -180 },
/* 15 */ { "x" : -285, "y" : 60, "trait" : "goalNet", "curve" : 180, "color" : "808080" },
/* 16 */ { "x" : 285, "y" : -60, "trait" : "goalNet", "curve" : 180, "color" : "808080" },
/* 17 */ { "x" : -285, "y" : -60, "trait" : "goalNet", "curve" : -180, "color" : "808080" },
/* 18 */ { "x" : 195, "y" : 180, "bCoef" : 2, "cMask" : ["wall" ], "cGroup" : ["red","blue","ball" ], "trait" : "ballArea", "color" : "FF0000" },
/* 19 */ { "x" : -195, "y" : 180, "bCoef" : 1, "cMask" : ["ball" ], "trait" : "ballArea", "color" : "00FF00" },
/* 20 */ { "x" : 195, "y" : -180, "bCoef" : 1, "cMask" : ["ball" ], "trait" : "line", "color" : "808080" },
/* 21 */ { "x" : -195, "y" : -180, "bCoef" : 1, "cMask" : ["ball" ], "trait" : "line", "curve" : -180, "color" : "808080" },
/* 22 */ { "x" : 75, "y" : 240, "curve" : -180 },
/* 23 */ { "x" : -75, "y" : 240, "bCoef" : 0.1, "cMask" : ["ball" ], "cGroup" : ["red","blue","ball" ], "trait" : "goalNet", "color" : "808080", "curve" : -180 },
/* 24 */ { "x" : 75, "y" : 240, "bCoef" : 0.1, "cMask" : ["ball" ], "trait" : "goalNet", "curve" : -180, "color" : "808080" },
/* 25 */ { "x" : -75, "y" : 0, "bCoef" : 2, "cMask" : ["wall" ], "cGroup" : ["red","blue","ball" ], "trait" : "line", "color" : "808080", "curve" : 180 },
/* 26 */ { "x" : 75, "y" : 0, "trait" : "line", "curve" : 180, "color" : "808080" },
/* 27 */ { "x" : -75, "y" : 0, "bCoef" : 2, "cMask" : ["wall" ], "cGroup" : ["red","blue","ball" ], "trait" : "line", "color" : "808080", "curve" : 0 },
/* 28 */ { "x" : 75, "y" : 0, "trait" : "line", "curve" : -180, "color" : "808080" },
/* 29 */ { "x" : 75, "y" : 0, "trait" : "line", "curve" : 0 },
/* 30 */ { "x" : -1, "y" : -240, "trait" : "line", "curve" : 0, "color" : "686868" },
/* 31 */ { "x" : -1, "y" : 240, "trait" : "line", "curve" : 0, "color" : "686868" },
/* 32 */ { "x" : 1, "y" : -240, "trait" : "line", "curve" : 0, "color" : "717171" },
/* 33 */ { "x" : 0, "y" : 240, "trait" : "line", "curve" : 0, "color" : "717171" },
/* 34 */ { "x" : 1, "y" : -240, "trait" : "line", "curve" : 0, "color" : "717171" },
/* 35 */ { "x" : 0, "y" : 240, "trait" : "line", "curve" : 0, "color" : "717171" }
],
"discs" : [
{ "radius" : 8, "pos" : [196,180 ], "color" : "FF0000", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-195,180 ], "color" : "00FF00", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [195,-180 ], "color" : "FFFF00", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-195,-180 ], "color" : "0000FF", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [285,60 ], "color" : "FF0000", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-285,60 ], "color" : "00FF00", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [285,-60 ], "color" : "FFFF00", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-285,-60 ], "color" : "0000FF", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [75,240 ], "color" : "00FFFF", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-75,240 ], "color" : "00FFFF", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [75,-240 ], "color" : "FF00FF", "trait" : "goalPost" },
{ "radius" : 8, "pos" : [-75,-240 ], "color" : "FF00FF", "trait" : "goalPost" }
],
"segments" : [
{ "v0" : 30, "v1" : 31, "curve" : 0, "vis" : true, "color" : "686868", "trait" : "line" },
{ "v0" : 32, "v1" : 33, "curve" : 0, "vis" : true, "color" : "717171", "trait" : "line", "x" : 1 },
{ "v0" : 8, "v1" : 0, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 9, "v1" : 1, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 6, "v1" : 2, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 7, "v1" : 3, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 10, "v1" : 0, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 11, "v1" : 1, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 12, "v1" : 2, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 13, "v1" : 3, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 4, "v1" : 14, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 5, "v1" : 15, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 16, "v1" : 4, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 17, "v1" : 5, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 14, "v1" : 4, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 15, "v1" : 5, "color" : "FFFFFF", "trait" : "ballArea" },
{ "v0" : 7, "v1" : 6, "curve" : 180, "color" : "FF00FF", "trait" : "goalNet" },
{ "v0" : 10, "v1" : 14, "curve" : -180, "color" : "FF0000", "bCoef" : 0.1, "cMask" : ["ball" ], "trait" : "goalNet" },
{ "v0" : 11, "v1" : 15, "curve" : 180, "color" : "00FF00", "trait" : "goalNet" },
{ "v0" : 12, "v1" : 16, "curve" : 180, "color" : "FFFF00", "trait" : "goalNet" },
{ "v0" : 13, "v1" : 17, "curve" : -180, "color" : "0000FF", "trait" : "goalNet" },
{ "v0" : 14, "v1" : 18, "color" : "FF0000", "trait" : "kickOffBarrier" },
{ "v0" : 15, "v1" : 19, "color" : "00FF00", "trait" : "kickOffBarrier" },
{ "v0" : 16, "v1" : 20, "color" : "FFFF00", "trait" : "kickOffBarrier" },
{ "v0" : 17, "v1" : 21, "color" : "0000FF", "trait" : "kickOffBarrier" },
{ "v0" : 8, "v1" : 9, "color" : "00FFFF", "trait" : "kickOffBarrier" },
{ "v0" : 6, "v1" : 7, "color" : "FF00FF", "trait" : "kickOffBarrier" },
{ "v0" : 23, "v1" : 24, "curve" : -180, "vis" : true, "color" : "00FFFF", "bCoef" : 0.1, "cMask" : ["ball" ], "trait" : "goalNet" },
{ "v0" : 23, "v1" : 24, "curve" : 180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 15, "v1" : 11, "curve" : 180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 17, "v1" : 21, "curve" : -180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 7, "v1" : 6, "curve" : -180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 20, "v1" : 16, "curve" : -180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 14, "v1" : 10, "curve" : -180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 25, "v1" : 26, "curve" : 180, "vis" : true, "color" : "808080", "trait" : "line" },
{ "v0" : 27, "v1" : 28, "curve" : -180, "vis" : true, "color" : "808080", "trait" : "line" }
],
"goals" : [
{ "p0" : [75,240 ], "p1" : [-75,240 ], "team" : "red", "trait" : "line", "curve" : 180 },
{ "p0" : [-75,-240 ], "p1" : [75,-240 ], "team" : "blue" },
{ "p0" : [195,180 ], "p1" : [285,60 ], "team" : "blue" },
{ "p0" : [-195,180 ], "p1" : [-285,60 ], "team" : "red" },
{ "p0" : [195,-180 ], "p1" : [285,-60 ], "team" : "blue" },
{ "p0" : [-195,-180 ], "p1" : [-285,-60 ], "team" : "red" }
],
"planes" : [
],
"traits" : {
"ballArea" : { "vis" : true, "bCoef" : 1, "cMask" : ["ball" ] },
"goalPost" : { "radius" : 8, "invMass" : 0, "bCoef" : 0.5 },
"goalNet" : { "vis" : true, "bCoef" : 0.1, "cMask" : ["ball" ] },
"kickOffBarrier" : { "vis" : true, "bCoef" : 0.1, "cGroup" : ["redKO","blueKO" ], "cMask" : ["red","blue" ] },
"line" : { "vis" : true, "cMask" : [ ], "color" : "C7E6BD" }
},
"ballPhysics" : {
"radius" : 9.2,
"bCoef" : 1,
"damping" : 0.991,
"invMass" : 1,
"color" : "FFFF00"
},
"playerPhysics" : {
"kickStrength" : 6,
"invMass" : 1e+26,
"bCoef" : 0
}
}`;
var room = HBInit({
roomName: ROOM_NAME,
playerName: HOST_NAME,
maxPlayers: MAX_PLAYERS,
public: ROOM_PUBLIC,
// password: 'your_password',
geo : HOST_GEO
});
room.setCustomStadium(customMap);
room.setScoreLimit(0);
room.setTimeLimit(0);
room.setTeamsLock(true);
var playersThatTouchedTheBall = new Set();
function pointDistance(p1, p2) {
var d1 = p1.x - p2.x;
var d2 = p1.y - p2.y;
return Math.sqrt(d1 * d1 + d2 * d2);
}
var lastTouch = [];
function handleGameTick() {
var players = room.getPlayerList();
var ballPosition = room.getBallPosition();
var ballRadius = _ballRadius;
var playerRadius = 15;
var triggerDistance = ballRadius + playerRadius + 0.01;
room.onPlayerBallKick = function(player) {
lastTouch = player;
}
for (var i = 0; i < players.length; i++) { // Iterate over all the players
var player = players[i];
if ( player.position == null ) continue; // Skip players that don't have a position
var distanceToBall = pointDistance(player.position, ballPosition);
var hadTouchedTheBall = playersThatTouchedTheBall.has(player.id);
// This check is here so that the event is only notified the first game tick in which the player is touching the ball.
if ( !hadTouchedTheBall ) {
if ( distanceToBall < triggerDistance ) {
playersThatTouchedTheBall.add(player.id);
lastTouch = player;
}
}else{
// If a player that had touched the ball moves away from the ball remove him from the set to allow the event to be notified again.
if ( distanceToBall > triggerDistance + 4 ) {
playersThatTouchedTheBall.delete(player.id);
}
}
}
}
function handleGameStart() {
playersThatTouchedTheBall.clear(); // Reset the set of players that reached the goal
}
room.onGameTick = handleGameTick;
room.onGameStart = handleGameStart;
var displayPlayerList = function() {
var players = room.getPlayerList().filter((player) => player.id !== 0);
var playerNames = [];
for (var i = 0; i < players.length; i++) {
playerNames.push(players[i].name);
}
room.sendChat('Player List: ' + playerNames.join(' | '));
}
var switchSides = function(room) {
var players = room.getPlayerList();
for (var i = 0; i < players.length; i++) {
if (players[i].team != 0) {
room.setPlayerTeam(players[i].id, (players[i].team % 2) + 1);
}
}
}
lastTouchDisplay = function(player, team) {
var teamName = null,
display = player;
if (team == 1) {
teamName = 'Red'
} else if (team == 2) {
teamName = 'Blue';
}
if (teamName !== null) {
display = player + ' ['+ teamName +']';
}
room.sendChat("Last touch | Ostatnie dotknięcie: " + display );
}
room.onTeamGoal = function(team) {
if (touchDetection && (team == 1 || team == 2)) {
lastTouchDisplay(lastTouch.name, lastTouch.team);
}
}
room.onPlayerChat = function(player, message) {
var _admin_valid = message.match(/!(admin|adm|amdin|admi|admim|adnim|adin)/);
if (_admin_valid !== null) {
if (message == '!admin '+ ADMIN_PASSWORD) {
room.setPlayerAdmin(player.id, true);
}
return false;
}
// Chat Enable / disable
if (message == '!chat_on' && player.admin && chat === false) {
chat = true;
room.sendChat('Chat Enabled! | Czat włączony!');
return false;
}
if (message == '!chat_off' && player.admin && chat === true) {
chat = false;
room.sendChat('Chat Disabled! | Czat wyłączony!');
return false;
}
if (chat === false & !player.admin) {
return false;
}
// Ignore player by name
if (message && player.admin) {
var _get_ignore = message.match(/!ignore\s(.*)/);
if (_get_ignore !== null) {
var ignoreCheck = ignoreList.indexOf(_get_ignore[1]);
if (ignoreCheck === -1) {
ignoreList.push(_get_ignore[1]);
room.sendChat('Player '+ _get_ignore[1] +' added to ignore list!');
} else {
room.sendChat('Player '+ _get_ignore[1] +' is already on ignore list!');
}
return false;
}
var _get_unignore = message.match(/!unignore\s(.*)/);
if (_get_unignore !== null) {
var unignoreCheck = ignoreList.indexOf(_get_unignore[1]);
if (unignoreCheck !== -1) {
ignoreList.splice(_get_unignore[1], unignoreCheck);
room.sendChat('Player '+ _get_unignore[1] +' removed from ignore list!');
} else {
return false;
}
return false;
}
}
if (ignoreList.indexOf(player.name) !== -1) {
return false;
}
if (message == '!ignore_list' && player.admin) {
if (ignoreList.length > 0) {
room.sendChat('Ignore list: '+ ignoreList.join(' | '));
} else {
room.sendChat('Ignore List is empty!');
}
return false;
}
if (message == '!ignore_clear' && player.admin) {
ignoreList = [];
room.sendChat('Ignore List cleared!');
return false;
}
// Display player list
if (message == '!players' && player.admin) {
displayPlayerList();
return false;
}
// set skins
if (message == '!set_skins') {
room.setTeamColors(1, 90, 0xC24100, [0xC4BBBB]);
room.setTeamColors(2, 90, 0x0061C2, [0xC4BBBB]);
return false;
}
if (message == '!reset_skins') {
room.setTeamColors(1, 90, 0xFFFFFF, [0xE56E56]);
room.setTeamColors(2, 90, 0xFFFFFF, [0x5689E5]);
return false;
}
if (message == '!p' && player.admin) {
room.pauseGame(true);
}
if (message == '!g' && player.admin) {
room.pauseGame(false);
}
if (message == '!rr' && player.admin) {
room.stopGame();
room.startGame();
return false;
}
if (message == '!rrs' && player.admin) {
room.stopGame();
switchSides(room);
room.startGame();
return false;
}
if (message == "!s" && (player.admin || room.getScores() == null)) {
switchSides(room);
return false;
}
if (message == "!clear_bans" && player.admin) {
room.clearBans();
room.sendChat('All bans removed! | Wszystkie bany usunięte!');
return false;
}
if (message == "!clear_bans_s" && player.admin) {
room.clearBans();
return false;
}
if (message == '!touch_on' && player.admin && touchDetection === false) {
touchDetection = true;
room.sendChat('Touch detection enabled!');
return false;
}
if (message == '!touch_off' && player.admin && touchDetection === true) {
touchDetection = false;
room.sendChat('Touch detection disabled!');
return false;
}
}
@yerooleopard
Copy link
Author

yerooleopard commented May 27, 2018

Installation Instruction: https://youtu.be/9u9FEB8Jimc?t=1m51s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment