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>
<html>
<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>
</head>
<body>
</body>
</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 @@
one of its static method, createSquares() is very skeleton of the game for its specifies the names and
values of squares and properties.*/
var allSquares = [];
export let allSquares = [];
class Square {
export default class Square {
constructor(sellable){
this.isSellable = sellable;
}
......@@ -52,23 +52,8 @@ class Square {
{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){
......@@ -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 @@
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.*/
let companyID = 0;
let companyArray = [];
import Pawn from "./Pawn.js"
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){
companyID++;
this.id = companyID;
......@@ -18,15 +23,15 @@ class Company {
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 tr = Basic.createTr(this.name+"CN", "companies");
var td = Basic.createTd(this.name+"CName", this.name, tr);
var td = Basic.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);
var td = Basic.createTd(this.name+"CPro", this.owned.length, tr);
var td = Basic.createTd(this.name+"CTre", this.treasury+"K", tr);
var td = Basic.createTd(this.name+"CTv", this.getTotalValue(), tr);
}
......@@ -34,7 +39,7 @@ class Company {
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");
Basic.message(this.name+" was forced to sell "+property.name+" to "+buyer.name+" for "+amount+"K", "negative");
this.lead.changeReputation(-25);
}
......@@ -48,12 +53,12 @@ class Company {
gain(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){
this.treasury -= amount;
message(this.name+" lost "+amount+"K due event", "positive");
Basic.message(this.name+" lost "+amount+"K due event", "positive");
this.seeBankruptcy();
}
......@@ -66,7 +71,7 @@ class Company {
}
}
if (this.treasury < 0){
message(this.name+" went bankrupt", "negative");
Basic.message(this.name+" went bankrupt", "negative");
companyArray.splice(companyArray.indexOf(this),1)
document.getElementById("entrepreneurs").removeChild(
document.getElementById(this.name+"LN"));
......@@ -85,7 +90,7 @@ class Company {
}
aunction(){
var target = choice(this.owned);
var target = Basic.choice(this.owned);
this.owned.splice(this.owned.indexOf(target),1);
target.owner = null;
......@@ -101,7 +106,7 @@ class Company {
}
if (buyers.length > 0){
var buyer = choice(buyers);
var buyer = Basic.choice(buyers);
this.sellPropertyTo(buyer, target, value);
}
......@@ -112,11 +117,11 @@ class Company {
}
holdTurn(){
var diceRoll = rollDice();
var diceRoll = Basic.rollDice();
this.pawn.moveSteps(diceRoll);
var target = this.pawn.location;
var m = this.lead.name+" ("+this.name+")"+" rolled "+diceRoll;
message(m, "regular");
Basic.message(m, "regular");
if (target.canBeBought()){
if (this.treasury > target.value && this.lead.willBuy(target)){
this.treasury -= target.value;
......
/*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
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 = [];
let cornerID = 0;
export let cornerArray = [];
export let cornerID = 0;
const positionCorner = [
[0, 567, 567, 0],
[0, 0, 563, 563]
];
class CornerProperty extends Square {
export default class CornerProperty extends Square {
constructor(){
super(false);
cornerID++;
......@@ -28,7 +30,7 @@ class CornerProperty extends Square {
document.getElementById("board").appendChild(newProperty);
}
enter(visitor) {
message(visitor.lead.getName()+" took a train", "regular");
Basic.message(visitor.lead.getName()+" took a train", "regular");
visitor.pawn.moveSteps(16)
}
}
......@@ -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
created or updated.*/
class Entrepreneur {
import Basic from "./Basic.js"
export default class Entrepreneur {
constructor(company){
this.name = choice(forenames)+" "+choice(surnames);
this.name = Basic.choice(forenames)+" "+Basic.choice(surnames);
this.company = company;
this.prudence = rollDice();
this.ambition = rollDice();
this.pride = rollDice();
this.prudence = Basic.rollDice();
this.ambition = Basic.rollDice();
this.pride = Basic.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 tr = Basic.createTr(company.name+"LN", "entrepreneurs");
var td = Basic.createTd(company.name+"LName", this.name, tr);
var td = Basic.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);
var td = Basic.createTd(company.name+"LPru", Basic.getDiceSymbol(this.prudence), tr);
var td = Basic.createTd(company.name+"LAmb", Basic.getDiceSymbol(this.ambition), tr);
var td = Basic.createTd(company.name+"LPri", Basic.getDiceSymbol(this.pride), tr);
var td = Basic.createTd(company.name+"LRep", this.reputation+"%", tr);
var td = Basic.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+"LPru").innerHTML = Basic.getDiceSymbol(this.prudence);
document.getElementById(company.name+"LAmb").innerHTML = Basic.getDiceSymbol(this.ambition);
document.getElementById(company.name+"LPri").innerHTML = Basic.getDiceSymbol(this.pride);
document.getElementById(company.name+"LRep").innerHTML = this.reputation+"%";
document.getElementById(company.name+"LTen").innerHTML = this.tenure;
}
......@@ -44,13 +47,13 @@ class Entrepreneur {
if (this.isConsideredNeeded()){
return true;
} 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 {
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 {
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 {
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");
Basic.message(this.getName()+" reputation was increased by "+amount+"%", "positive");
} else {
message(this.getName()+" reputation was decreased by "+amount+"%", "negative");
Basic.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");
Basic.message(this.name+" resigned due unpopularity and was replaced by "+this.company.lead.getName(), "negative");
}
return;
}
......
/*Event properties server to host a variation of the enter-method (which is called every time a pawn
moves into a new square), this enter method picks one of the 10 possible outcomes to happen, most
of which are treasury related.*/
import Square from "./Board.js"
import Property from "./Property.js"
import Basic from "./Basic.js"
let eventArray = [];
let eventID = 0;
export let eventArray = [];
export let eventID = 0;
const positionEvent = [
......@@ -11,7 +14,7 @@ const positionEvent = [
[0, 290, 563, 280]
];
class EventProperty extends Square{
export default class EventProperty extends Square{
constructor(display){
super(false);
eventID++;
......@@ -22,7 +25,7 @@ class EventProperty extends Square{
this.center = [positionEvent[0][eventID-1],positionEvent[1][eventID-1]];
}
enter(visitor){
var r = randint(10);
var r = Basic.randint(10);
switch(r){
case 0:
visitor.gain(100);
......@@ -45,7 +48,7 @@ class EventProperty extends Square{
case 6:
var oldLead = visitor.lead;
visitor.replaceLead();
message(oldLead.getName()+" was voted out and replaced by "+visitor.name, "negative");
Basic.message(oldLead.getName()+" was voted out and replaced by "+visitor.name, "negative");
break;
case 7:
visitor.gain(visitor.lead.ambition*10);
......
......@@ -4,11 +4,13 @@ that when multiple pawns are in the same square they won't overlap. It also has
indicate which particular square is located.
It only hosts the important moveSteps(steps)-method, which uses a switch to determine the ideal
coordinates for a graphical depiction of the pawn.*/
import {allSquares} from "./Board.js"
let pawnArray = [];
let pawnID = 0;
class Pawn{
export let pawnArray = [];
export let pawnID = 0;
export default class Pawn{
constructor(company){
pawnID++;
pawnArray.push(this);
......@@ -19,7 +21,7 @@ class Pawn{
newProperty.className = "pawn";
document.getElementById("board").appendChild(newProperty);
this.location = allSquares[0];
document.getElementById(this.name).style.top = 0;
document.getElementById(this.name).style.top = "0px";
this.moveSteps(0);
}
moveSteps(steps){
......@@ -51,20 +53,20 @@ class Pawn{
default:
switch(this.location.id){
case 1:
le = this.location.center[0]+this.offset;
to = this.location.center[1]+this.offset;
le = parseInt(this.location.center[0])+this.offset;
to = parseInt(this.location.center[1])+this.offset;
break;
case 2:
le = this.location.center[0]-this.offset;
to = this.location.center[1]+this.offset;
le = parseInt(this.location.center[0])-this.offset;
to = parseInt(this.location.center[1])+this.offset;
break;
case 3:
le = this.location.center[0]-this.offset;
to = this.location.center[1]-this.offset;
le = parseInt(this.location.center[0])-this.offset;
to = parseInt(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;
le = parseInt(this.location.center[0])+this.offset;
to = parseInt(this.location.center[1])-this.offset;
break;
}
}
......