File size: 4,687 Bytes
4b876a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | "use strict";
/**
* TODO: Apparently I should replace MUH ITERATORS with higher-order functions
* TODO: Separate pickup logic into it's own repo? Remove team format and stuff
*/
class Pickup {
constructor (settings = {}) {
// Just constructor doing constructor things.
// settings is a dict of params.
this.game_on = false;
this.max_teams = settings.max_teams;
this.team_size = settings.team_size;
this.team_filler = settings.team_filler;
this.teams = this.reset_teams(this.max_teams, this.team_size, this.team_filler);
this.player_count = 0;
}
// Helper functions I guess? But not really?
isAdded(nick) {
/**
* Checks each element of each array of this.teams and checks
* if it is the same as the nickname given.
* Returns true or false.
*/
return this.teams.some(element => element.includes(nick));
}
_open_spot(team) {
/**
* Returns the index of the first open spot ("?") in a given
* team (this.teams[team] array) for adding purposes.
* Returns -1 if the team is full.
*/
return this.teams[team].indexOf("?");
}
_sort_arr(arr) {
// Does some magic to sort an array
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
let mapped = arr.map((element, index) => {
return { index: index, value: element}
});
mapped.sort((a, b) => {
return +(a.value < b.value) || +(a.value === b.value) - 1;
});
let result = mapped.map((element) => {
return arr[element.index];
});
return result;
}
// Big boy functions
start_game(max_teams = this.max_teams, team_size = this.team_size) {
/**
* Starts a game, duh!
* Can create pickups with varying number of teams and team sizes.
*/
this.game_on = true;
this.max_teams = max_teams;
this.team_size = team_size;
this.teams = this.reset_teams(this.max_teams, this.team_size, this.team_filler);
return true;
}
cancel_game() {
// Cancels a game and resets the teams arrays.
this.game_on = false;
this.teams = this.reset_teams(this.max_teams, this.team_size, this.team_filler); // Redundancy?
this.player_count = 0;
return true;
}
add(team, nick) {
/**
* Adds a player to a particular team (this.teams[team]).
* Returns false if already added, 0 if there is no space
* and true if added successfully.
* My returns are weird but they are exactly that, MY returns.
*/
if (!this.isAdded(nick)) {
if (this._open_spot(team) != -1) {
this.teams[team][this._open_spot(team)] = nick;
this.teams[team] = this._sort_arr(this.teams[team]);
this.player_count += 1;
return true;
} else
if (this._open_spot(team) == -1) {
return 0;
}
} else
if (this.isAdded(nick)) {
return false;
}
}
random_add(nick) {
/**
* This, uhhh... adds a player to a random team?
* Returns true or false.
* TODO: Rewrite sometime ¯\_(ツ)_/¯
*/
let arr = []
for (let i = 0; i < this.teams.length; i++) {
if (this._open_spot(i) > -1) {
arr.push(i);
}
}
if (arr[0] == undefined) {
return false;
}
let team = Math.round(Math.random() * (arr.length - 1))
if (team > -1) {
this.add(arr[team], nick);
return true;
} else {
// Redundant, I know.
console.log("All teams are full?");
return false;
}
}
remove(nick) {
// Removes a player from any team. Returns a new 2D array
if (isAdded(nick)) {
return arr.map((x) => {
return x.map((y) => {
return y === nick ? '?' : y
})
})
}
}
/**
* Returns a dynamically sized 2-D array (sized based on input)
* but makes each element equal to "?" to denote a fresh pickup.
*/
reset_teams(width, depth, filler) {
let teams = new Array(width).fill(undefined).map(() => new Array(depth).fill(filler));
console.log(teams)
return teams
}
get display_teams() {
/**
* Uses the array "this.teams" and returns a string of
* all players added to every team in the format:
* Team A: (Russ), (?), (?), (?), (?) Team B: (Gryph), (Zoid), (?), (?), (?) - etc
*/
let alphabet = ["A", "B", "C", "D", "E", "F", "G" ,"H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z"];
let fence = '\`\`\`'
let teams = "";
for (let i = 0; i < this.teams.length; i++) {
teams += `${fence}Team ${alphabet[i]}: (${this.teams[i].join("), (")})${fence}`
}
console.log(this.teams)
return teams;
}
};
module.exports = {
Pickup: Pickup
};
|