Commit 53d3ffdc authored by Totmak's avatar Totmak
Browse files

rework

parent 00fefcbe
Pipeline #40130 passed with stages
in 19 seconds
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*
var allSquares = [];
class Square {
constructor(sellable){
this.isSellable = sellable;
}
canBeBought(){
if (this.isSellable){
if (this.owner == null){
return true;
}
}
return false;
}
enter(){
}
static createSquares(){
this.state = {
propertySquares:
[
{name: "Lohja", value: 100, loc: "north", dignity: 1},
{name: "Hyvinkää", value: 120, loc: "north", dignity: 1},
{name: "Kokkola", value: 140, loc: "north", dignity: 1},
{name: "Porvoo", value: 160, loc: "north", dignity: 2},
{name: "Salo", value: 180, loc: "north", dignity: 2},
{name: "Kotka", value: 200, loc: "north", dignity: 2},
{name: "Mikkeli", value: 220, loc: "east", dignity: 3},
{name: "Rovaniemi", value: 240, loc: "east", dignity: 3},
{name: "Seinäjoki", value: 260, loc: "east", dignity: 3},
{name: "Vaasa", value: 280, loc: "east", dignity: 4},
{name: "Hämeenlinna", value: 300, loc: "east", dignity: 4},
{name: "Lappeenranta", value: 320, loc: "east", dignity: 4},
{name: "Joensuu", value: 340, loc: "south", dignity: 5},
{name: "Kouvola", value: 360, loc: "south", dignity: 5},
{name: "Pori", value: 380, loc: "south", dignity: 5},
{name: "Kuopio", value: 400, loc: "south", dignity: 6},
{name: "Lahti", value: 420, loc: "south", dignity: 6},
{name: "Jyväskylä", value: 440, loc: "south", dignity: 6},
{name: "Oulu", value: 480, loc: "west", dignity: 7},
{name: "Turku", value: 460, loc: "west", dignity: 7},
{name: "Vantaa", value: 500, loc: "west", dignity: 7},
{name: "Tampere", value: 520, loc: "west", dignity: 8},
{name: "Espoo", value: 540, loc: "west", dignity: 8},
{name: "Helsinki", value: 560, loc: "west", dignity: 8}
]
}
this.state.propertySquares.map(note => new Property(note));
for(var x = 0; x < 4; x++){
new EventProperty(x%2);
new CornerProperty();
};
for(var x = 0; x < 4; x++){
Square.listSquare(cornerArray[x]);
for(var y = 0; y < 3; y++){
Square.listSquare(propertyArray[y+(x*6)]);
}
Square.listSquare(eventArray[x]);
for(var y = 3; y < 6; y++){
Square.listSquare(propertyArray[y+(x*6)]);
}
}
}
static listSquare(square){
square.squareID = allSquares.length;
allSquares.push(square);
}
}
function reloadGame(){
location.reload();
return false;
}
function turn(){
turnNumber++;
for(var x of companyArray){
x.holdTurn();
}
player.update(false);
}
let companyID = 0;
let companyArray = [];
class Company {
constructor(note){
companyID++;
this.id = companyID;
this.name = note.name+" Oyj.";
this.icon = "./icons/"+companyID+".svg";
companyArray.push(this);
this.pawn = new Pawn(this, companyID-1);
this.lead = new Entrepreneur(this);
this.treasury = 2000;
this.owned = [];
this.isActive = true;
var tr = createTr(this.name+"CN", "companies");
var td = createTd(this.name+"CName", this.name, tr);
var td = createTd(this.name+"CIcon", "", tr);
var img = document.createElement("img");
img.src = this.icon;
td.appendChild(img);
var td = createTd(this.name+"CPro", this.owned.length, tr);
var td = createTd(this.name+"CTre", this.treasury+"K", tr);
var td = createTd(this.name+"CTv", this.getTotalValue(), tr);
}
sellPropertyTo(buyer, property, amount){
buyer.treasury -= amount;
this.treasury += amount;
property.directPurchase(buyer);
message(this.name+" was forced to sell "+property.name+" to "+buyer.name+" for "+amount+"K", "negative");
this.lead.changeReputation(-25);
}
getRentFrom(payer, amount){
payer.treasury -= amount;
this.treasury += amount;
this.lead.changeReputation(5);
payer.lead.changeReputation(-10);
payer.seeBankruptcy();
}
gain(amount){
this.treasury += amount;
message(this.name+" gained "+amount+"K due event", "positive");
}
lose(amount){
this.treasury -= amount;
message(this.name+" lost "+amount+"K due event", "positive");
this.seeBankruptcy();
}
seeBankruptcy(){
if (this.treasury < 0){
if (this.owned.length > 0){
while(this.treasury < 0 && this.owned.length > 0){
this.aunction();
}
}
}
if (this.treasury < 0){
message(this.name+" went bankrupt", "negative");
companyArray.splice(companyArray.indexOf(this),1)
document.getElementById("entrepreneurs").removeChild(
document.getElementById(this.name+"LN"));
document.getElementById("companies").removeChild(
document.getElementById(this.name+"CN"));
document.getElementById("board").removeChild(
document.getElementById("pawn"+this.id));
player.update(true);
this.isActive = false;
if (companyArray.length == 1){
alert("A monopoly has emerged, you have lost!");
location.reload();
return false;
}
}
}
aunction(){
var target = choice(this.owned);
this.owned.splice(this.owned.indexOf(target),1);
target.owner = null;
var buyers = [];
var value = target.value;
while(buyers.length == 0 && value > 0){
for(var x of companyArray){
if (x.treasury > value){
buyers.push(x);
}
}
value -= 10;
}
if (buyers.length > 0){
var buyer = choice(buyers);
this.sellPropertyTo(buyer, target, value);
}
}
replaceLead(){
this.lead = new Entrepreneur(this);
}
holdTurn(){
var diceRoll = rollDice();
this.pawn.moveSteps(diceRoll);
var target = this.pawn.location;
var m = this.lead.name+" ("+this.name+")"+" rolled "+diceRoll;
message(m, "regular");
if (target.canBeBought()){
if (this.treasury > target.value && this.lead.willBuy(target)){
this.treasury -= target.value;
target.beAcquiredBy(this);
this.lead.changeReputation(10);
}
} else {
target.enter(this);
}
if (this.isActive){
this.updateGUI();
}
this.lead.tenureUp();
}
updateGUI(){
document.getElementById(this.name+"CPro").innerHTML = this.owned.length;
document.getElementById(this.name+"CTre").innerHTML = Math.floor(this.treasury)+"K";
document.getElementById(this.name+"CTv").innerHTML = Math.floor(this.getTotalValue())+"K";
}
getTotalValue(){
var v = this.treasury;
for(var x of this.owned){
v += x.value;
}
return v;
}
static createCompanies(){
this.state = {
companies:
[
{name: "Seiväs"},
{name: "Nokka"},
{name: "P.A"},
{name: "Kaakko"},
]
}
this.state.companies.map(note => new Company(note));
}
}
let cornerArray = [];
let cornerID = 0;
const positionCorner = [
[0, 567, 567, 0],
[0, 0, 563, 563]
];
class CornerProperty extends Square {
constructor(){
super(false);
cornerID++;
this.id = cornerID
this.center = [positionCorner[0][cornerID-1],positionCorner[1][cornerID-1]];
this.displayType = 2;
cornerArray.push(this);
this.makeHTML();
}
makeHTML(){
var newProperty = document.createElement("span");
newProperty.id = "corner"+this.id;
newProperty.className = "cornerProperty";
document.getElementById("board").appendChild(newProperty);
}
}
class Entrepreneur {
constructor(company){
this.name = choice(forenames)+" "+choice(surnames);
this.company = company;
this.prudence = rollDice();
this.ambition = rollDice();
this.pride = rollDice();
this.tenure = 0;
this.reputation = 25;
if (company.lead == null){
var tr = createTr(company.name+"LN", "entrepreneurs");
var td = createTd(company.name+"LName", this.name, tr);
var td = createTd(company.name+"LCName", "", tr);
var img = document.createElement("img");
img.src = company.icon;
td.appendChild(img);
var td = createTd(company.name+"LPru", getDiceSymbol(this.prudence), tr);
var td = createTd(company.name+"LAmb", getDiceSymbol(this.ambition), tr);
var td = createTd(company.name+"LPri", getDiceSymbol(this.pride), tr);
var td = createTd(company.name+"LRep", this.reputation+"%", tr);
var td = createTd(company.name+"LTen", this.tenure, tr);
} else {
document.getElementById(company.name+"LName").innerHTML = this.name;
document.getElementById(company.name+"LPru").innerHTML = getDiceSymbol(this.prudence);
document.getElementById(company.name+"LAmb").innerHTML = getDiceSymbol(this.ambition);
document.getElementById(company.name+"LPri").innerHTML = getDiceSymbol(this.pride);
document.getElementById(company.name+"LRep").innerHTML = this.reputation+"%";
document.getElementById(company.name+"LTen").innerHTML = this.tenure;
}
}
willBuy(loc){
if (this.isConsideredSafe()){
if (this.isConsideredWorthy(loc)){
if (this.isConsideredNeeded()){
return true;
} else {
message(this.getName()+" didn't buy "+loc.name+" due to lack of ambition", "rejection");
}
} else {
message(this.getName()+" didn't buy "+loc.name+" due to low worth", "rejection");
}
} else {
message(this.getName()+" didn't buy "+loc.name+" due to the cost", "rejection");
}
}
isConsideredSafe(){
var safety = 25*this.prudence;
return this.company.treasury >= safety;
}
isConsideredWorthy(value){
return value.dignity >= this.pride;
}
isConsideredNeeded(){
return this.company.owned.length <= (Math.floor(this.tenure)+(this.ambition*4));
}
tenureUp(){
this.tenure += 0.25
if (this.company.isActive){
document.getElementById(this.company.name+"LTen").innerHTML = this.tenure;
}
}
changeReputation(amount){
var nr = this.reputation + amount;
var change = this.reputation;
this.reputation = Math.max(Math.min(nr, 100), 0);
if (change != this.reputation){
if (amount > 0){
message(this.getName()+" reputation was increased by "+amount+"%", "positive");
} else {
message(this.getName()+" reputation was decreased by "+amount+"%", "negative");
if (this.reputation == 0){
this.company.replaceLead();
message(this.name+" resigned due unpopularity and was replaced by "+this.company.lead.getName(), "negative");
}
return;
}
document.getElementById(this.company.name+"LRep").innerHTML = this.reputation+"%";
}
}
getName(){
return this.name+" ("+this.company.name+")";
}
}
const forenames = ["Lauri", "Kalle", "Pekka", "Petri", "Tommi", "Oskari", "Antti", "Juho", "Ville", "Mikko"];
const surnames = ["Alatalo", "Haapaniemi", "Hautamäki", "Järvenpää", "Kankaanpää", "Keskitalo", "Kivimäki", "Kiviniemi", "Koivuniemi", "Kujanpää", "Lähteenmäki", "Lehtimäki", "Mäenpää", "Myllymäki", "Palomäki", "Peltoniemi", "Riihimäki", "Sillanpää", "Uusitalo", "Välimäki", "Ylitalo"];
let eventArray = [];
let eventID = 0;
const positionEvent = [
[290, 570, 290, 0,],
[0, 290, 563, 280]
];
class EventProperty extends Square{
constructor(display){
super(false);
eventID++;
this.id = eventID
eventArray.push(this);
this.makeHTML();
this.displayType = display;
this.center = [positionEvent[0][eventID-1],positionEvent[1][eventID-1]];
}
enter(visitor){
var r = randint(10);
switch(r){
case 0:
visitor.gain(100);
break;
case 1:
visitor.gain(50);
break;
case 2:
visitor.lose(125);
break;
case 3:
visitor.lose(75);
break;
case 4:
visitor.lead.changeReputation(25);
break;
case 5:
visitor.lead.changeReputation(-30);
break;
case 6:
var oldLead = visitor.lead;
visitor.replaceLead();
message(oldLead.getName()+" was voted out and replaced by "+visitor.name, "negative");
break;
case 7:
visitor.gain(visitor.lead.ambition*10);
break;
case 8:
visitor.gain(visitor.lead.prudence*10);
break;
case 9:
visitor.gain(visitor.lead.pride*10);
break;
}
}
makeHTML(){
var newProperty = document.createElement("span");
newProperty.id = "event"+this.id;
newProperty.className = "eventProperty";
document.getElementById("board").appendChild(newProperty);
}
}
let pawnArray = [];
class Pawn{
constructor(company){
pawnArray.push(this);
this.name = "pawn"+company.id;
this.offset = (company.id-1)*20;
var newProperty = document.createElement("span");
newProperty.id = this.name;
newProperty.className = "pawn";
document.getElementById("board").appendChild(newProperty);
this.location = allSquares[0];
document.getElementById(this.name).style.top = 0;
this.moveSteps(0);
}
moveSteps(steps){
this.location = allSquares[
((this.location.squareID+steps)%32)
];
var cs = document.getElementById(this.name);
var le;
var to
switch(this.location.displayType){
case 0:
le = this.location.center[0];
if (this.location.squareID < 16){
to = this.offset+this.location.center[1];
} else {
to = this.location.center[1]-this.offset;
}
break;
case 1:
to = this.location.center[1];
if (this.location.squareID < 16){
le = this.location.center[0]-this.offset;
} else {
le = this.location.center[0]+this.offset;
}
break;
default:
switch(this.location.id){
case 1:
le = this.location.center[0]+this.offset;
to = this.location.center[1]+this.offset;
break;
case 2:
le = this.location.center[0]-this.offset;
to = this.location.center[1]+this.offset;
break;
case 3:
le = this.location.center[0]-this.offset;
to = this.location.center[1]-this.offset;
break;
case 4:
le = this.location.center[0]+this.offset;
document.getElementById(this.name).style.top = this.location.center[1]-this.offset;
break;
}
}
cs.style.left = le+"px";
cs.style.top = to+"px";
}
}
class Player {
constructor(){
this.owned = [];
this.treasury = 100;
this.update();
}
update(reloadIcons){
for(var x of this.owned){
x.updateGUI(reloadIcons);
}
document.getElementById("playerShares").innerHTML = this.owned.length;
document.getElementById("playerTreasury").innerHTML = Math.floor(this.treasury)+"K";
document.getElementById("playerSharesValue").innerHTML = Math.floor(this.getShareValue())+"K";
document.getElementById("playerTurn").innerHTML = turnNumber;
}
getShareValue(){
var v = 0;
for(var x of this.owned){
v += x.getValue();
}
return v;
}
static cashOut(){
var rem = [];
for(var x of player.owned){ rem.push(x); }
for(var x of rem){ x.sell(); }
}
static buyShare(){
if (squareSelection != null){
var ss = squareSelection;
var sh = ss.getShares();
if (sh.length > 0){
var s = sh[0];
var value = s.getValue();
if (player.treasury >= value){
s.isOwned = true;
player.treasury -= value;
ss.owner.treasury += value;
player.owned.push(s);
var tr = createTr("share"+s.id, "shares");
var td = createTd("share"+s.id+"0", s.property.name, tr);
var td = createTd("share"+s.id+"1", "", tr);
var img = document.createElement("img");
img.src = s.property.owner.icon;
img.id = "share"+s.id+"1I";
td.appendChild(img);
var td = createTd("share"+s.id+"2", Math.floor(value)+"K", tr);
var td = createTd("share"+s.id+"3", "", tr);
var button = document.createElement("button");
button.innerHTML = "SELL";
button.addEventListener("click", function() {
s.sell();