//"use strict" const { finished } = require("nodemailer/lib/xoauth2/index.js") const data = require("./data.js") console.log('hello') var game, view, states = {} const DEM = "Democrat" const COM = "Communist" const first_strategy_card = 1 const last_strategy_card = 110 /* to check! */ const hand_limit = 8 const spaces = [ {name: 'Schwerin', name_unique: 'Schwerin', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 111, y: 61, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Rostock', name_unique: 'Rostock', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 228, y: 41, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Berlin', name_unique: 'Berlin', socio: 2, stability: 3, battleground: 1, country: 1, box: {x: 251, y: 113, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'German Writers', name_unique: 'German Writers', socio: 5, stability: 2, battleground: 0, country: 1, box: {x: 58, y: 185, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Walter Ulbricht Academy', name_unique: 'Walter Ulbricht Academy', socio: 6, stability: 1, battleground: 0, country: 1, box: {x: 175, y: 187, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Lutherian Church', name_unique: 'Lutherian Church', socio: 7, stability: 5, battleground: 1, country: 1, box: {x: 300, y: 212, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Magdeburg', name_unique: 'Magdeburg', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 58, y: 275, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Halle', name_unique: 'Halle', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 175, y: 278, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Leipzig', name_unique: 'Leipzig', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 297, y: 297, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Erfurt', name_unique: 'Erfurt', socio: 3, stability: 4, battleground: 0, country: 1, box: {x: 23, y: 356, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Karl-Marx-Stadt', name_unique: 'Karl-Marx-Stadt', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 138, y: 385, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Dresden', name_unique: 'Dresden', socio: 1, stability: 4, battleground: 1, country: 1, box: {x: 262, y: 375, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Szczecin', name_unique: 'Szczecin', socio: 4, stability: 3, battleground: 0, country: 2, box: {x: 434, y: 204, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Gdansk', name_unique: 'Gdansk', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 699, y: 215, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Bydgoszcz', name_unique: 'Bydgoszcz', socio: 1, stability: 4, battleground: 0, country: 2, box: {x: 679, y: 304, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Poznan', name_unique: 'Poznan', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 521, y: 355, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Warszawa', name_unique: 'Warszawa', socio: 2, stability: 3, battleground: 1, country: 2, box: {x: 806, y: 383, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Bialystok', name_unique: 'Bialystok', socio: 3, stability: 4, battleground: 0, country: 2, box: {x: 940, y: 342, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Wroclaw', name_unique: 'Wroclaw', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 462, y: 443, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Catholic Church', name_unique: 'Catholic Church Poland', socio: 7, stability: 5, battleground: 0, country: 2, box: {x: 625, y: 437, h: 65, w: 105}, demInfl: 5, comInfl: 0, demCtrl: 1, comCtrl: 0}, {name: 'Lodz', name_unique: 'Lodz', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 749, y: 486, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Katowice', name_unique: 'Katowice', socio: 4, stability: 3, battleground: 0, country: 2, box: {x: 570, y: 569, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Krakow', name_unique: 'Krakow', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 711, y: 598, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Lublin', name_unique: 'Lublin', socio: 3, stability: 4, battleground: 0, country: 2, box: {x: 879, y: 603, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Jagiellian University', name_unique: 'Jagiellian University', socio: 6, stability: 1, battleground: 0, country: 2, box: {x: 679, y: 681, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Polish Writers', name_unique: 'Polish Writers', socio: 5, stability: 2, battleground: 0, country: 2, box: {x: 832, y: 694, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Plzen', name_unique: 'Plzen', socio: 1, stability: 4, battleground: 0, country: 3, box: {x: 159, y: 483, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Ceske Budejovice', name_unique: 'Ceske Budejovice', socio: 3, stability: 3, battleground: 0, country: 3, box: {x: 196, y: 601, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Praha', name_unique: 'Praha', socio: 2, stability: 3, battleground: 1, country: 3, box: {x: 317, y: 614, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Charles University', name_unique: 'Charles University', socio: 6, stability: 1, battleground: 1, country: 3, box: {x: 380, y: 532, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Czech Writers', name_unique: 'Czech Writers', socio: 5, stability: 2, battleground: 1, country: 3, box: {x: 444, y: 607, h: 65, w: 105}, demInfl: 2, comInfl: 0, demCtrl: 1, comCtrl: 0}, {name: 'Brno', name_unique: 'Brno', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 403, y: 711, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Ostrava', name_unique: 'Ostrava', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 523, y: 681, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Bratislava', name_unique: 'Bratislava', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 417, y: 797, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Catholic Church', name_unique: 'Catholic Church Czech', socio: 7, stability: 5, battleground: 0, country: 3, box: {x: 538, y: 794, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Presov', name_unique: 'Presov', socio: 3, stability: 4, battleground: 0, country: 3, box: {x: 658, y: 794, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Kosice', name_unique: 'Kosice', socio: 3, stability: 4, battleground: 0, country: 3, box: {x: 777, y: 815, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Catholic Church', name_unique: 'Catholic Church Hungary', socio: 7, stability: 5, battleground: 0, country: 4, box: {x: 314, y: 886, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Gyor', name_unique: 'Gyor', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 434, y: 887, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Tatabanya', name_unique: 'Tatabanya', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 549, y: 886, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Miskolc', name_unique: 'Miskolc', socio: 4, stability: 3, battleground: 1, country: 4, box: {x: 664, y: 901, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Debrecen', name_unique: 'Debrecen', socio: 4, stability: 3, battleground: 1, country: 4, box: {x: 781, y: 938, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Szombathely', name_unique: 'Szombathely', socio: 1, stability: 4, battleground: 0, country: 4, box: {x: 316, y: 963, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Szekesfehervar', name_unique: 'Szekesfehervar', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 442, y: 962, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Budapest', name_unique: 'Budapest', socio: 2, stability: 3, battleground: 1, country: 4, box: {x: 630, y: 983, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Hungarian Writers', name_unique: 'Hungarian Writers', socio: 5, stability: 2, battleground: 0, country: 4, box: {x: 348, y: 1040, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Eotvos Lorand University', name_unique: 'Eotvos Lorand University', socio: 6, stability: 1, battleground: 0, country: 4, box: {x: 512, y: 1034, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Szeged', name_unique: 'Szeged', socio: 3, stability: 4, battleground: 1, country: 4, box: {x: 632, y: 1073, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Pecs', name_unique: 'Pecs', socio: 3, stability: 4, battleground: 1, country: 4, box: {x: 486, y: 1105, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Timisoara', name_unique: 'Timisoara', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 597, y: 1204, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Cluj-Napoca', name_unique: 'Cluj-Napoca', socio: 1, stability: 4, battleground: 1, country: 5, box: {x: 756, y: 1125, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Targu Mures', name_unique: 'Targu Mures', socio: 3, stability: 4, battleground: 0, country: 5, box: {x: 915, y: 1136, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Iasi', name_unique: 'Iasi', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1072, y: 1097, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Babes-Bolyai University', name_unique: 'Babes-Bolyai University', socio: 6, stability: 1, battleground: 0, country: 5, box: {x: 746, y: 1203, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Romanian Writers', name_unique: 'Romanian Writers', socio: 5, stability: 2, battleground: 0, country: 5, box: {x: 739, y: 1278, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Hargita/Covasna', name_unique: 'Hargita/Covasna', socio: 8, stability: 4, battleground: 0, country: 5, box: {x: 928, y: 1227, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Brasov', name_unique: 'Brasov', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1049, y: 1225, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Orthodox Church', name_unique: 'Orthodox Church Romania', socio: 7, stability: 3, battleground: 0, country: 5, box: {x: 855, y: 1338, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Ploiesti', name_unique: 'Ploiesti', socio: 4, stability: 3, battleground: 0, country: 5, box: {x: 1061, y: 1316, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Craiova', name_unique: 'Craiova', socio: 3, stability: 4, battleground: 0, country: 5, box: {x: 763, y: 1411, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Bucuresti', name_unique: 'Bucuresti', socio: 2, stability: 3, battleground: 1, country: 5, box: {x: 929, y: 1445, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Galatii', name_unique: 'Galatii', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1104, y: 1399, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Constanta', name_unique: 'Constanta', socio: 4, stability: 3, battleground: 0, country: 5, box: {x: 1130, y: 1517, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Pleven', name_unique: 'Pleven', socio: 3, stability: 4, battleground: 0, country: 6, box: {x: 764, y: 1534, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Orthodox Church', name_unique: 'Orthodox Church Bulgaria', socio: 7, stability: 3, battleground: 0, country: 6, box: {x: 882, y: 1540, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Ruse', name_unique: 'Ruse', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 998, y: 1540, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Sofia University', name_unique: 'Sofia University', socio: 6, stability: 1, battleground: 0, country: 6, box: {x: 645, y: 1650, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Sofia', name_unique: 'Sofia', socio: 2, stability: 3, battleground: 1, country: 6, box: {x: 768, y: 1653, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0}, {name: 'Stara Zagora', name_unique: 'Stara Zagora', socio: 1, stability: 4, battleground: 0, country: 6, box: {x: 886, y: 1694, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0}, {name: 'Razgrad', name_unique: 'Razgrad', socio: 8, stability: 4, battleground: 0, country: 6, box: {x: 954, y: 1620, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Burgas', name_unique: 'Burgas', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 1004, y: 1695, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Varna', name_unique: 'Varna', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 1086, y: 1613, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Bulgarian Writers', name_unique: 'Bulgarian Writers', socio: 5, stability: 2, battleground: 0, country: 6, box: {x: 652, y: 1726, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Plovdiv', name_unique: 'Plovdiv', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 771, y: 1739, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, {name: 'Sliven', name_unique: 'Sliven', socio: 3, stability: 4, battleground: 0, country: 6, box: {x: 894, y: 1768, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0}, ] exports.scenarios = [ "Standard" ] exports.roles = [ DEM, COM ] // --- SET UP --- exports.setup = function (seed, scenario, options) { console.log('setup ran') game = { seed: seed, log: [], undo: [], active: null, state: "com_init_1", played_card: [], available_ops: 0, turn: 0, round: 0, pieces: [], strategy_deck: [], strategy_discard: [], democrat_hand: [], communist_hand: [], democrat_set_aside: [], } log_h1("1989 Dawn of Freedom") game.active = COM start_game() return game } function start_game() { //starting influence console.log('start_game called') spaces.forEach(space => { game.pieces.push({ name_unique: space.name_unique, demInfl: space.demInfl, comInfl: space.comInfl }); }); //Communist starts by placing 2 influence game.available_ops = 2 } exports.view = function(state, player) { game = state view = { log: game.log, active: game.active, prompt: null, actions: null, played_card: game.played_card, selected_space: game.selected_space, pieces: game.pieces, turn: game.turn, round: game.round, strategy_deck: game.strategy_deck.length, democrat_hand: game.democrat_hand.length, communist_hand: game.communist_hand.length, hand: [], set_aside: [], } if (player === game.active && game.vm && game.vm.draw) view.drawn = game.vm.draw if (player === DEM) { view.hand = game.democrat_hand view.set_aside = game.democrat_set_aside } else if (player === COM) { view.hand = game.communist_hand } if (game.state === "game_over") { view.prompt = game.victory } else if (player === "Observer" || (game.active !== player && game.active !== "Both")) { if (states[game.state]) { let inactive = states[game.state].inactive if (typeof inactive === "function") view.prompt = `Waiting for ${game.active} ${inactive()}` else view.prompt = `Waiting for ${game.active} to ${inactive}` } else { view.prompt = "Unknown state: " + game.state } } else { view.actions = {} if (states[game.state]) states[game.state].prompt(player) else view.prompt = "Unknown state: " + game.state if (view.actions.undo === undefined) { if (game.undo && game.undo.length > 0) view.actions.undo = 1 else view.actions.undo = 0 } } return view } // === ACTIONS function gen_action(action, argument) { //console.log('gen_action called with ', action, ' and ', argument) if (argument === undefined) { view.actions[action] = 1 } else { if (!(action in view.actions)) view.actions[action] = [] view.actions[action].push(argument) } //console.log('view.actions: ', view.actions, 'view.actions[action]: ', view.actions[action]) } function gen_action_infl(space){ console.log('gen_action_infl called') gen_action("infl", space) } exports.action = function (state, player, action, arg) { //console.log('exports.action called with state:' , state, 'player:', player, 'action: ', action, 'arg: ', arg) game = state if (states[game.state] && action in states[game.state]) { states[game.state][action](arg, player) } else { if (action === "undo" && game.undo && game.undo.length > 0) pop_undo() else throw new Error("Invalid action: " + action) } return game } function init_player_cards(first_card) { let c = first_card let deck = [] for (let n = 0; n < era_cards_count; ++n) deck.push(++c) shuffle(deck) return deck } states.com_init_1 = { inactive: 'place starting influence', prompt() { view.prompt = `Place ${game.available_ops} starting influence` let available_spaces = spaces.slice() for (let space of available_spaces) { gen_action_infl(space.name_unique); //console.log('state: com_init_1') //console.log('space: ', space) } }, infl(space) { push_undo() console.log('space clicked was: ', space) const clicked_space = find_space_index(space) console.log('clicked_space: ', clicked_space) console.log('game.pieces.space', game.pieces[clicked_space]) console.log('initial influence: ', game.pieces[clicked_space] ) game.pieces[clicked_space].comInfl++ game.available_ops-- console.log('new influence: ', game.pieces[clicked_space]) if (game.available_ops == 0) { game.state = "next_step" } } } states.next_step = { inactive: "place starting influence", prompt() { view.prompt = "Next step" } } function find_space_index(name_unique) { return game.pieces.findIndex(piece => piece && piece.name_unique === name_unique); } // === COMMON LIBRARY === function shuffle(list) { // Fisher-Yates shuffle for (let i = list.length - 1; i > 0; --i) { let j = random(i + 1) let tmp = list[j] list[j] = list[i] list[i] = tmp } return list } function log_h1(msg) { log_br() log(".h1 " + msg) log_br() } function log_br() { if (game.log.length > 0 && game.log[game.log.length - 1] !== "") game.log.push("") } function log(msg) { game.log.push(msg) } function push_undo() { let copy = {} for (let k in game) { let v = game[k] if (k === "undo") continue else if (k === "log") v = v.length else if (typeof v === "object" && v !== null) v = object_copy(v) copy[k] = v } game.undo.push(copy) } function pop_undo() { let save_log = game.log let save_undo = game.undo game = save_undo.pop() save_log.length = game.log game.log = save_log game.undo = save_undo } // Fast deep copy for objects without cycles function object_copy(original) { if (Array.isArray(original)) { let n = original.length let copy = new Array(n) for (let i = 0; i < n; ++i) { let v = original[i] if (typeof v === "object" && v !== null) copy[i] = object_copy(v) else copy[i] = v } return copy } else { let copy = {} for (let i in original) { let v = original[i] if (typeof v === "object" && v !== null) copy[i] = object_copy(v) else copy[i] = v } return copy } }