Commit fe139ce4 authored by Totmak's avatar Totmak
Browse files

modularize

parent 99f5f5e5
Pipeline #41213 passed with stages
in 8 minutes and 39 seconds
node_modules
.git
monopoly/style.css
mononpoly/style.css
npm-debug.log
FROM node:latest
WORKDIR /test
COPY package.json /test/package.json
RUN npm install && npm i jasmine
COPY . /test
CMD [ "npm", "test"]
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="monopoly/style.css">
</head>
<body>
<main>
<div id="container">
<div id="board"></div>
<span id="info">
<span id="infoCoa"></span>
<span id="infoNameText"><p id="infoName"></p></span>
<span id="infoOtherS"><p><b>Value:</b><p id="infoValue"></p></p></span>
<span id="infoOther"> <p><b>Dignity:</b><p id="infoDignity"></p></p> </span>
<span id="infoShareS">
<p><b>Shares:</b><p id="infoShares"></p></p>
</span>
<span id="infoOwnerText"><p><b>Owner:</b><p id="infoOwner"></p></p></span>
<button id="buyShare" onclick="Player.buyShare()">BUY SHARE</button>
</span>
<span id="console">
</span>
</main><selection>
<table id="entrepreneurs">
<caption><h3>ENTREPRENEURS</h3></caption>
<tr><th width='50%'>Name</th><th>Company</th><th title="Prudence">PRU</th><th title="Ambition">AMB</th><th title="Pride">PRI</th><th>Reputation</th><th>Tenure</th></tr>
</table>
</selection>
<selection>
<table id="companies">
<caption><h3>COMPANIES</h3></caption>
<tr><th width="31.95%">Name</th><th>Icon</th><th>Properties</th><th>Treasury</th><th>Total value</th></tr>
</table>
</selection>
<aside>
<table id="shares">
<caption><h3>SHARES</h3></caption>
<tr><th>Location</th><th>Company</th><th title="Current Value">C. value</th><th title="Original value">O. value</th><th>Options</th></tr>
</table>
</aside>
<aside>
<fieldset>
<legend><h3>PLAYER</h3></legend>
<table id="player">
<tr><th>Treasury:</th><td id="playerTreasury">a</td></tr>
<tr><th>Shares:</th><td id="playerShares"></td></tr>
<tr><th>Shares value:</th><td id="playerSharesValue"></td></tr>
<tr><th>Turn:</th><td id="playerTurn"></td></tr>
<tr><td colspan="2"><button id="turnButton" onclick="turn()">TURN</button></td></tr>
<tr><td colspan="2"> <button id="cashOutButton" onclick="Player.cashOut()">CASH OUT</button></td></tr>
<tr><td colspan="2"> <button id="reloadButton" onclick="reloadGame()">RELOAD</button></td></tr>
</table>
</fieldset
</aside>
</body>
</head>
<body>
</body>
</html> </html>
<script src="monopoly/code/Player.js"></script>
<script src="monopoly/code/Share.js"></script>
<script src="monopoly/code/Entrepreneur.js"></script>
<script src="monopoly/code/Pawn.js"></script>
<script src="monopoly/code/Company.js"></script>
<script src="monopoly/code/Board.js"></script>
<script src="monopoly/code/Property.js"></script>
<script src="monopoly/code/EventProperty.js"></script>
<script src="monopoly/code/CornerProperty.js"></script>
<script src="monopoly/code/UnitTest.js"></script>
<script src="monopoly/code/index.js"></script>
/*The file is essentially a container for micro-methods who couldn't afford their own class, it also
triggers several import methods from other files. More important it defines the const player, which
most player-interactions are related to.*/
import Square from "./monopoly/code/Board.js"
import Player from "./monopoly/code/Player.js"
import Share from "./monopoly/code/Share.js"
import Entrepreneur from "./monopoly/code/Entrepreneur.js"
import Pawn from "./monopoly/code/Pawn.js"
import Company from "./monopoly/code/Company.js"
import Property, {propertyArray} from "./monopoly/code/Property.js"
import EventProperty, {eventArray} from "./monopoly/code/EventProperty.js"
import CornerProperty, {cornerArray} from "./monopoly/code/CornerProperty.js"
import Basic from "./monopoly/code/Basic.js"
Square.createSquares();
Square.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)]);
}
}
Company.createCompanies();
Player.createPlayer();
document.getElementById("turnButton").onclick = function() { Basic.turn(); };
document.getElementById("buyShare").onclick = function() { Player.buyShare();};
document.getElementById("cashOutButton").onclick = function(){Player.cashOut();}
document.getElementById("reloadButton").onclick = function(){Basic.reloadGame();}
//UnitTest.runUnitTest();
import {player} from "./Player.js"
import {companyArray} from "./Company.js"
const diceSymbol = ['', '', '', '', '', '', ''];
export let turnNumber = 0;
export default class Basic {
static message(m, c){
var p = document.createElement('p');
p.className = c;
p.innerHTML = "#"+turnNumber+": "+m;
document.getElementById("console").prepend(p);
}
static reloadGame(){
location.reload();
return false;
}
static turn(){
console.log("0");
turnNumber++;
for(var x of companyArray){
x.holdTurn();
}
player.update(false);
}
static getDiceSymbol(n){
return "<span class='dice'>"+diceSymbol[n]+"</span>";
}
static randint(i){
return Math.floor((Math.random() * i));
}
static flipCoin(){
return this.Basic.randint(2) == 0;
}
static choice(l){
return l[Basic.randint(l.length)];
}
static rollDice(){
return 1+Basic.randint(6);
}
static roll(n){
return Basic.randint(n) == 0;
}
static typecast(clas, obj) {
return Object.assign(new clas(),obj);
}
static createTr(id, table){
var tr = document.createElement("tr");
tr.id = id;
document.getElementById(table).appendChild(tr);
return tr;
}
static createTd(id, txt, tr){
var td = document.createElement("td");
td.id = id;
td.innerHTML = txt;
tr.appendChild(td);
return td;
}
static addToTable(tr, table){
document.getElementById(table).appendChild(tr);
}
}
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
one of its static method, createSquares() is very skeleton of the game for its specifies the names and one of its static method, createSquares() is very skeleton of the game for its specifies the names and
values of squares and properties.*/ values of squares and properties.*/
var allSquares = []; export let allSquares = [];
class Square { export default class Square {
constructor(sellable){ constructor(sellable){
this.isSellable = sellable; this.isSellable = sellable;
} }
...@@ -52,23 +52,8 @@ class Square { ...@@ -52,23 +52,8 @@ class Square {
{name: "Helsinki", value: 560, 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){ static listSquare(square){
...@@ -77,17 +62,3 @@ class Square { ...@@ -77,17 +62,3 @@ class Square {
} }
} }
function reloadGame(){
location.reload();
return false;
}
function turn(){
turnNumber++;
for(var x of companyArray){
x.holdTurn();
}
player.update(false);
}
...@@ -2,10 +2,15 @@ ...@@ -2,10 +2,15 @@
the array for all the company's bought properties in owned-attribute. It also contains several basic the array for all the company's bought properties in owned-attribute. It also contains several basic
interaction methods, of which many alter the CSS.*/ interaction methods, of which many alter the CSS.*/
let companyID = 0; import Pawn from "./Pawn.js"
let companyArray = []; import Basic from "./Basic.js"
import Entrepreneur from "./Entrepreneur.js"
class Company {
export let companyID = 0;
export let companyArray = [];
export default class Company {
constructor(note){ constructor(note){
companyID++; companyID++;
this.id = companyID; this.id = companyID;
...@@ -18,15 +23,15 @@ class Company { ...@@ -18,15 +23,15 @@ class Company {
this.owned = []; this.owned = [];
this.isActive = true; this.isActive = true;
var tr = createTr(this.name+"CN", "companies"); var tr = Basic.createTr(this.name+"CN", "companies");
var td = createTd(this.name+"CName", this.name, tr); var td = Basic.createTd(this.name+"CName", this.name, tr);
var td = createTd(this.name+"CIcon", "", tr); var td = Basic.createTd(this.name+"CIcon", "", tr);
var img = document.createElement("img"); var img = document.createElement("img");
img.src = this.icon; img.src = this.icon;
td.appendChild(img); td.appendChild(img);
var td = createTd(this.name+"CPro", this.owned.length, tr); var td = Basic.createTd(this.name+"CPro", this.owned.length, tr);
var td = createTd(this.name+"CTre", this.treasury+"K", tr); var td = Basic.createTd(this.name+"CTre", this.treasury+"K", tr);
var td = createTd(this.name+"CTv", this.getTotalValue(), tr); var td = Basic.createTd(this.name+"CTv", this.getTotalValue(), tr);
} }
...@@ -34,7 +39,7 @@ class Company { ...@@ -34,7 +39,7 @@ class Company {
buyer.treasury -= amount; buyer.treasury -= amount;
this.treasury += amount; this.treasury += amount;
property.directPurchase(buyer); property.directPurchase(buyer);
message(this.name+" was forced to sell "+property.name+" to "+buyer.name+" for "+amount+"K", "negative"); Basic.message(this.name+" was forced to sell "+property.name+" to "+buyer.name+" for "+amount+"K", "negative");
this.lead.changeReputation(-25); this.lead.changeReputation(-25);
} }
...@@ -48,12 +53,12 @@ class Company { ...@@ -48,12 +53,12 @@ class Company {
gain(amount){ gain(amount){
this.treasury += amount; this.treasury += amount;
message(this.name+" gained "+amount+"K due event", "positive"); Basic.message(this.name+" gained "+amount+"K due event", "positive");
} }
lose(amount){ lose(amount){
this.treasury -= amount; this.treasury -= amount;
message(this.name+" lost "+amount+"K due event", "positive"); Basic.message(this.name+" lost "+amount+"K due event", "positive");
this.seeBankruptcy(); this.seeBankruptcy();
} }
...@@ -66,7 +71,7 @@ class Company { ...@@ -66,7 +71,7 @@ class Company {
} }
} }
if (this.treasury < 0){ if (this.treasury < 0){
message(this.name+" went bankrupt", "negative"); Basic.message(this.name+" went bankrupt", "negative");
companyArray.splice(companyArray.indexOf(this),1) companyArray.splice(companyArray.indexOf(this),1)
document.getElementById("entrepreneurs").removeChild( document.getElementById("entrepreneurs").removeChild(
document.getElementById(this.name+"LN")); document.getElementById(this.name+"LN"));
...@@ -85,7 +90,7 @@ class Company { ...@@ -85,7 +90,7 @@ class Company {
} }
aunction(){ aunction(){
var target = choice(this.owned); var target = Basic.choice(this.owned);
this.owned.splice(this.owned.indexOf(target),1); this.owned.splice(this.owned.indexOf(target),1);
target.owner = null; target.owner = null;
...@@ -101,7 +106,7 @@ class Company { ...@@ -101,7 +106,7 @@ class Company {
} }
if (buyers.length > 0){ if (buyers.length > 0){
var buyer = choice(buyers); var buyer = Basic.choice(buyers);
this.sellPropertyTo(buyer, target, value); this.sellPropertyTo(buyer, target, value);
} }
...@@ -112,11 +117,11 @@ class Company { ...@@ -112,11 +117,11 @@ class Company {
} }
holdTurn(){ holdTurn(){
var diceRoll = rollDice(); var diceRoll = Basic.rollDice();
this.pawn.moveSteps(diceRoll); this.pawn.moveSteps(diceRoll);
var target = this.pawn.location; var target = this.pawn.location;
var m = this.lead.name+" ("+this.name+")"+" rolled "+diceRoll; var m = this.lead.name+" ("+this.name+")"+" rolled "+diceRoll;
message(m, "regular"); Basic.message(m, "regular");
if (target.canBeBought()){ if (target.canBeBought()){
if (this.treasury > target.value && this.lead.willBuy(target)){ if (this.treasury > target.value && this.lead.willBuy(target)){
this.treasury -= target.value; this.treasury -= target.value;
......
/*Corner property-objects are basically only created because of their unique center attribute—for /*Corner property-objects are basically only created because of their unique center attribute—for
corners require a very specific position for all the pawns. Initially, corner objects were meant to corners require a very specific position for all the pawns. Initially, corner objects were meant to
have a bunch of unique features, but due to time constraints, the stable version is devoid of them.*/ have a bunch of unique features, but due to time constraints, the stable version is devoid of them.*/
import Square from "./Board.js"
import Basic from "./Basic.js"
let cornerArray = []; export let cornerArray = [];
let cornerID = 0; export let cornerID = 0;
const positionCorner = [ const positionCorner = [
[0, 567, 567, 0], [0, 567, 567, 0],
[0, 0, 563, 563] [0, 0, 563, 563]
]; ];
class CornerProperty extends Square { export default class CornerProperty extends Square {
constructor(){ constructor(){
super(false); super(false);
cornerID++; cornerID++;
...@@ -28,7 +30,7 @@ class CornerProperty extends Square { ...@@ -28,7 +30,7 @@ class CornerProperty extends Square {
document.getElementById("board").appendChild(newProperty); document.getElementById("board").appendChild(newProperty);
} }
enter(visitor) { enter(visitor) {
message(visitor.lead.getName()+" took a train", "regular"); Basic.message(visitor.lead.getName()+" took a train", "regular");
visitor.pawn.moveSteps(16) visitor.pawn.moveSteps(16)
} }
} }
...@@ -6,33 +6,36 @@ Because entrepreneurs are often replaced and new entrepreneur takes their place, ...@@ -6,33 +6,36 @@ Because entrepreneurs are often replaced and new entrepreneur takes their place,
there is already pre-existing entrepreneur the row in which the entrepreneur is defined is either there is already pre-existing entrepreneur the row in which the entrepreneur is defined is either
created or updated.*/ created or updated.*/
class Entrepreneur { import Basic from "./Basic.js"
export default class Entrepreneur {
constructor(company){ constructor(company){
this.name = choice(forenames)+" "+choice(surnames); this.name = Basic.choice(forenames)+" "+Basic.choice(surnames);
this.company = company; this.company = company;
this.prudence = rollDice(); this.prudence = Basic.rollDice();
this.ambition = rollDice(); this.ambition = Basic.rollDice();
this.pride = rollDice(); this.pride = Basic.rollDice();
this.tenure = 0; this.tenure = 0;
this.reputation = 25; this.reputation = 25;
if (company.lead == null){ if (company.lead == null){
var tr = createTr(company.name+"LN", "entrepreneurs"); var tr = Basic.createTr(company.name+"LN", "entrepreneurs");
var td = createTd(company.name+"LName", this.name, tr); var td = Basic.createTd(company.name+"LName", this.name, tr);
var td = createTd(company.name+"LCName", "", tr); var td = Basic.createTd(company.name+"LCName", "", tr);
var img = document.createElement("img"); var img = document.createElement("img");
img.src = company.icon; img.src = company.icon;
td.appendChild(img); td.appendChild(img);
var td = createTd(company.name+"LPru", getDiceSymbol(this.prudence), tr); var td = Basic.createTd(company.name+"LPru", Basic.getDiceSymbol(this.prudence), tr);
var td = createTd(company.name+"LAmb", getDiceSymbol(this.ambition), tr); var td = Basic.createTd(company.name+"LAmb", Basic.getDiceSymbol(this.ambition), tr);
var td = createTd(company.name+"LPri", getDiceSymbol(this.pride), tr); var td = Basic.createTd(company.name+"LPri", Basic.getDiceSymbol(this.pride), tr);
var td = createTd(company.name+"LRep", this.reputation+"%", tr); var td = Basic.createTd(company.name+"LRep", this.reputation+"%", tr);
var td = createTd(company.name+"LTen", this.tenure, tr); var td = Basic.createTd(company.name+"LTen", this.tenure, tr);
} else { } else {
document.getElementById(company.name+"LName").innerHTML = this.name; document.getElementById(company.name+"LName").innerHTML = this.name;
document.getElementById(company.name+"LPru").innerHTML = getDiceSymbol(this.prudence); document.getElementById(company.name+"LPru").innerHTML = Basic.getDiceSymbol(this.prudence);
document.getElementById(company.name+"LAmb").innerHTML = getDiceSymbol(this.ambition); document.getElementById(company.name+"LAmb").innerHTML = Basic.getDiceSymbol(this.ambition);
document.getElementById(company.name+"LPri").innerHTML = getDiceSymbol(this.pride); document.getElementById(company.name+"LPri").innerHTML = Basic.getDiceSymbol(this.pride);
document.getElementById(company.name+"LRep").innerHTML = this.reputation+"%"; document.getElementById(company.name+"LRep").innerHTML = this.reputation+"%";
document.getElementById(company.name+"LTen").innerHTML = this.tenure; document.getElementById(company.name+"LTen").innerHTML = this.tenure;
} }
...@@ -44,13 +47,13 @@ class Entrepreneur { ...@@ -44,13 +47,13 @@ class Entrepreneur {
if (this.isConsideredNeeded()){ if (this.isConsideredNeeded()){
return true; return true;
} else { } else {
message(this.getName()+" didn't buy "+loc.name+" due to lack of ambition", "rejection"); Basic.message(this.getName()+" didn't buy "+loc.name+" due to lack of ambition", "rejection");
} }
} else { } else {
message(this.getName()+" didn't buy "+loc.name+" due to low worth", "rejection"); Basic.message(this.getName()+" didn't buy "+loc.name+" due to low worth", "rejection");
} }
} else { } else {
message(this.getName()+" didn't buy "+loc.name+" due to the cost", "rejection"); Basic.message(this.getName()+" didn't buy "+loc.name+" due to the cost", "rejection");
} }
} }
...@@ -80,12 +83,12 @@ class Entrepreneur { ...@@ -80,12 +83,12 @@ class Entrepreneur {
this.reputation = Math.max(Math.min(nr, 100), 0); this.reputation = Math.max(Math.min(nr, 100), 0);
if (change != this.reputation){ if (change != this.reputation){
if (amount > 0){ if (amount > 0){
message(this.getName()+" reputation was increased by "+amount+"%", "positive"); Basic.message(this.getName()+" reputation was increased by "+amount+"%", "positive");
} else { } else {
message(this.getName()+" reputation was decreased by "+amount+"%", "negative"); Basic.message(this.getName()+" reputation was decreased by "+amount+"%", "negative");
if (this.reputation == 0){ if (this.reputation == 0){
this.company.replaceLead(); this.company.replaceLead();
message(this.name+" resigned due unpopularity and was replaced by "+this.company.lead.getName(), "negative"); Basic.message(this.name+" resigned due unpopularity and was replaced by "+this.company.lead.getName(), "negative");
} }
return; return;