haxball / haxball-issues

115 stars 43 forks source link

This function doesn't work properly #1685

Open guguxh opened 2 years ago

guguxh commented 2 years ago

function randomizePlayers(){ let thisPlayers = room.getPlayerList(); let names = []; for (const player of thisPlayers) { names.push(player.name); } let random = Math.floor(Math.random() * names.length); playerList[names[random - 1]].isGirl = true; // Here I get error when game start, cant define properties of 'undefined'. room.sendAnnouncement(names[random - 1] + " é a garota da vez!!", 0xff0000, "bold"); let ran2; do{ ran2 = Math.floor(Math.random() * names.length); }while(ran2 == ids[random]); playerList[names[ran2 - 1]].isYourTurn = true; room.sendAnnouncement(names[ran2 - 1] + " começará dando a cantada!", 0xff0000, "bold"); }

hbenormous commented 2 years ago

function randomizePlayers(){ let thisPlayers = room.getPlayerList(); let names = []; for (const player of thisPlayers) { names.push(player.name); } let random = Math.floor(Math.random() * names.length); playerList[names[random - 1]].isGirl = true; // Here I get error when game start, cant define properties of 'undefined'. room.sendAnnouncement(names[random - 1] + " é a garota da vez!!", 0xff0000, "bold"); let ran2; do{ ran2 = Math.floor(Math.random() * names.length); }while(ran2 == ids[random]); playerList[names[ran2 - 1]].isYourTurn = true; room.sendAnnouncement(names[ran2 - 1] + " começará dando a cantada!", 0xff0000, "bold"); }

room.sendAnnouncement(message, forWhom, color, style, sound); https://github.com/haxball/haxball-issues/wiki/Headless-Host#sendannouncement

guguxh commented 2 years ago

function randomizePlayers(){ let thisPlayers = room.getPlayerList(); let names = []; for (const player of thisPlayers) { names.push(player.name); } let random = Math.floor(Math.random() * names.length); playerList[names[random - 1]].isGirl = true; // Here I get error when game start, cant define properties of 'undefined'. room.sendAnnouncement(names[random - 1] + " é a garota da vez!!", 0xff0000, "bold"); let ran2; do{ ran2 = Math.floor(Math.random() * names.length); }while(ran2 == ids[random]); playerList[names[ran2 - 1]].isYourTurn = true; room.sendAnnouncement(names[ran2 - 1] + " começará dando a cantada!", 0xff0000, "bold"); }

room.sendAnnouncement(message, forWhom, color, style, sound); https://github.com/haxball/haxball-issues/wiki/Headless-Host#sendannouncement

Isn't the problem, the problem is when I try to assign true bool to the isGirl key in playerList object.

Skarmunds commented 2 years ago

I renamed playerList to thisPlayers in function. Also headless getPlayerList() is an array of objects inside. Maybe your playerList is different. You can't substract -1 from random because for one player it gives -1 index. You can't avoid any arguments between Headless API calls. It chooses your color value in targetID which is player ID. Here working it sends a message. I don't know where in your code is ids or playerList defined.

function randomizePlayers() { 
    let thisPlayers = room.getPlayerList();
    let names = []; 
    for (const player of thisPlayers) { 
        names.push(player.name); 
    } 
    let random = Math.floor(Math.random() * names.length);
    // Renamed to thisPlayers, also it uses just random
    thisPlayers[random].isGirl = true;
    // Here we set 2nd argument to null or undefined, the rest follows correct order.
    // https://github.com/haxball/haxball-issues/wiki/Headless-Host#sendannouncement
    // sendAnnouncement(msg:String, targetId?:Int, color?:Int, style?:String, sound?:Int) 
    room.sendAnnouncement(names[random] + " é a garota da vez!!",null,0xff0000, "bold");
}

// Simple command test
room.onPlayerChat = (player, message) => {
    if (message == "!girl") {
        randomizePlayers();
    }
}
guguxh commented 2 years ago

I renamed playerList to thisPlayers in function. Also headless getPlayerList() is an array of objects inside. Maybe your playerList is different. You can't substract -1 from random because for one player it gives -1 index. You can't avoid any arguments between Headless API calls. It chooses your color value in targetID which is player ID. Here working it sends a message. I don't know where in your code is ids or playerList defined.

function randomizePlayers() { 
  let thisPlayers = room.getPlayerList();
  let names = []; 
  for (const player of thisPlayers) { 
      names.push(player.name); 
  } 
  let random = Math.floor(Math.random() * names.length);
  // Renamed to thisPlayers, also it uses just random
  thisPlayers[random].isGirl = true;
  // Here we set 2nd argument to null or undefined, the rest follows correct order.
  // https://github.com/haxball/haxball-issues/wiki/Headless-Host#sendannouncement
  // sendAnnouncement(msg:String, targetId?:Int, color?:Int, style?:String, sound?:Int) 
  room.sendAnnouncement(names[random] + " é a garota da vez!!",null,0xff0000, "bold");
}

// Simple command test
room.onPlayerChat = (player, message) => {
  if (message == "!girl") {
      randomizePlayers();
  }
}

I get it about the sendAnnounce, thanks, but I think the playerList isn't the real problem, because this is an object who, when player enter on room, this store informations about he like auth, conn, and isGirl property.

Skarmunds commented 2 years ago

Ok, then could be your random - 1. Because for one player in test it's playerList[names[-1]], names[-1].