📜 ⬆️ ⬇️

We expand the functionality of Monopoly City Streets

Developers in MCS, unfortunately, do not provide for the possibility of building the street with a large number of buildings in a couple of mouse clicks. But the game is written in JS, so nothing prevents us from writing a small bookmarklet, which will allow us to add the required functionality.

All MCS code is in the monopoly.1255614067.js file. If you look at the firebug that happens during the building purchase process, we will see that 2 functions are used - MCS.BUILD.showLocations () [to display the markers of available places] and MCS.BUILD.buyBuilding () [for the actual purchase of the building ]. After thinking, I got the following code:
if ( typeof MCS == "undefined" ) {
alert ( 'Bookmarklet needs to be called only on the monopoly page' ) ;
throw '' ;
}
var buildingType = null ;
var player = MCS. getPlayerData ( ) ;
if ( player == null ) {
alert ( 'Before calling, you must log in' ) ;
throw '' ;
}
var streetId = MCS. STREET . getStreetData ( ) . id ;
var streetData = MCS. STREET . getStreetData ( ) . data ;
if ( streetId == null ) {
alert ( 'Before calling, select the street' ) ;
throw '' ;
} else if ( streetData. o ! = player. nickname ) {
alert ( 'Before calling, you must choose your street' ) ;
throw '' ;
}
var locations = new Array ( ) ;
var builtCnt = 0 ;
var buildLimit = 0 ;
function buy ( ) {
if ( builtCnt < buildLimit ) {
buyBuilding ( locations [ 0 ] ) ;
MCS. STATUSBAR . redraw ( ) ;
getLocations ( ) ;
setTimeout ( buy , 250 ) ;
} else {
done ( ) ;
}
}
function done ( )
{
MCS. STREET . show ( streetId , null , false , false ) ;
MCS. LOADING . hide ( ) ;
alert ( 'Built buildings:' + builtCnt + 'pcs.' ) ;
}
function getLocations ( )
{
locations = new Array ( ) ;
$. ajax ( {
url : "/ build / getlocations" ,
cache : false ,
data : {
id : streetId ,
type : buildingType
} ,
dataType : "json" ,
async : false ,
success : function ( data , status ) {
$. each ( data , function ( foo , point ) {
if ( point ) {
locations. push ( foo ) ;
}
} ) ;
}
} ) ;
}
function buyBuilding ( location )
{
$. ajax ( {
url : "/ negotiate / buybuilding" ,
type : "post" ,
data : {
nickname : player. nickname ,
hash : player. hash ,
id : streetId ,
type : buildingType ,
loc : location
} ,
dataType : "json" ,
async : false ,
success : function ( data , status ) {
player. balance - = MCS. buildings [ buildingType ] . price ;
builtCnt ++;
}
} ) ;
MCS. TRACK . view ( {
page : "build" ,
section : "build" ,
street : streetData. n ,
country : streetData. cy ,
region : streetData. re ,
building_type : buildingType ,
activity : "build property" ,
value : MCS. buildings [ buildingType ] . price
} ) ;
}
buildingType = prompt ( 'Enter building identifier. List of available types can be found by typing "?"' , '?' ) ;
if ( buildingType ! = null ) {
if ( buildingType == '?' ) {
var string = '' ;
for ( i in MCS. buildings ) {
if ( ! isNaN ( MCS. buildings [ i ] . effect ) ) {
string = string + "ID:" + i + ", name:" + MCS. Lang building [ "building" + i ] + ", cost:" + MCS. buildings [ i ] . price * 1000 + " \ n " ;
}
}
alert ( string ) ;
} else if ( typeof MCS. buildings [ buildingType ] == "undefined" || isNaN ( MCS. buildings [ buildingType ] . effect ) ) {
alert ( "An invalid identifier is specified" ) ;
} else {
var count = Math. floor ( player. balance / MCS. buildings [ buildingType ] . price ) ;
if ( count > 0 ) {
getLocations ( ) ;
var minCnt = Math. min ( count , locations. length ) ;
if ( minCnt == 0 ) {
alert ( "On the street there is no place for buildings of this type" ) ;
} else {
buildLimit = minCnt ;
if ( ! confirm ( 'Now we will try to build houses in the amount of' + minCnt + 'pcs. If you want to change the limit (only down), click "cancel"' ) ) {
buildLimit = prompt ( 'How many buildings to try to build?' , minCnt ) ;
if ( buildLimit == null ) {
throw '' ;
}
if ( isNaN ( buildLimit ) || buildLimit > minCnt || buildLimit < 0 ) {
alert ( 'Incorrect value' ) ;
throw '' ;
}
}
MCS. LOADING . show ( ) ;
setTimeout ( buy , 250 ) ;
}
} else {
alert ( 'You do not have enough money to build this building' ) ;
}
}
}
code highlighting : highlight.hohli.com

Before starting the script, it conducts several checks: the presence of the MCS class (otherwise we are definitely not on the game site), the authorization of the user, and the current selected street is also checked. Next, a request is issued to determine the type of building - a list of the latter is taken from the game code. Checks the availability of money from the player and availability on the street. Then we find out the number of houses to build and proceed to this exciting activity. So that the user does not get bored - do not forget to display the hourglass in the top panel. And, finally, the number of houses built is displayed.

To convert this script to 1 line - use javascriptcompressor.com , as a result, we almost have a bookmarklet - it remains only to add 'javascript:' to the beginning of the resulting line.
')
Ready text of a bookmarklet in pastybin: pastebin.mozilla.org/678433

PS As a bonus, a bookmarklet for displaying the cost of the current street without buildings: javascript:if(typeof MCS != "undefined"&&MCS.STREET.getStreetData().data)alert(MCS.STREET.getStreetData().data.p*1000);

UPD Bookmarklet for batch removal of buildings:
if ( typeof MCS == "undefined" ) {
alert ( 'Bookmarklet needs to be called only on the monopoly page' ) ;
throw '' ;
}
var buildingType = null ;
var player = MCS. getPlayerData ( ) ;
if ( player == null ) {
alert ( 'Before calling, you must log in' ) ;
throw '' ;
}
var streetId = MCS. STREET . getStreetData ( ) . id ;
var streetData = MCS. STREET . getStreetData ( ) . data ;
if ( streetId == null ) {
alert ( 'Before calling, select the street' ) ;
throw '' ;
} else if ( streetData. o ! = player. nickname ) {
alert ( 'Before calling, you must choose your street' ) ;
throw '' ;
}
function demolish ( ) {
if ( removedCnt < removeCnt && buildingList. length ) {
$. ajax ( {
url : "/ negotiate / demolishbuilding" ,
type : "post" ,
data : {
nickname : player. nickname ,
hash : player. hash ,
id : streetId ,
bid : buildingList. shift ( )
} ,
dataType : "json" ,
async : false ,
success : function ( ) {
MCS. getPlayerData ( ) . balance + = parseInt ( MCS. buildings [ removeMe ] . price / 2 ) ;
}
} ) ;
removedCnt ++;
setTimeout ( demolish , 500 ) ;
} else {
done ( ) ;
}
}
function done ( )
{
MCS. STREET . show ( streetId , null , false , false ) ;
MCS. LOADING . hide ( ) ;
}
var buildingTypes = new Array ( ) ;
var str = '' ;
var i ;
for ( i in streetData. b ) {
if ( ! isNaN ( MCS. buildings [ streetData. b [ i ] . t ] . effect ) ) {
if ( typeof buildingTypes [ streetData. b [ i ] . t ] == "undefined" ) {
buildingTypes [ streetData. b [ i ] . t ] = new Array ( ) ;
buildingTypes [ streetData. b [ i ] . t ] [ 'name' ] = MCS. Lang building [ "building" + streetData. b [ i ] . t ] ;
buildingTypes [ streetData. b [ i ] . t ] [ 'count' ] = 1 ;
} else {
buildingTypes [ streetData. b [ i ] . t ] [ 'count' ] ++;
}
}
}
if ( buildingTypes. length == 0 ) {
alert ( 'There are no buildings available for demolition on the selected street' ) ;
throw '' ;
}
for ( i in buildingTypes ) {
str = str + "ID:" + i + ", name:" + buildingTypes [ i ] [ 'name' ] + ", number:" + buildingTypes [ i ] [ 'count' ] + "pcs." + " \ n " ;
}
var removeMe = prompt ( "Enter the ID of the building to demolish. List of buildings on the street: \ n " + str ) ;
var buildingList = new Array ( ) ;
if ( removeMe ! = null ) {
if ( typeof buildingTypes [ removeMe ] == 'undefined' ) {
alert ( 'Invalid building type specified' ) ;
throw '' ;
}
var removeCnt = buildingTypes [ removeMe ] [ 'count' ] ;
var removedCnt = 0 ;
if ( ! confirm ( 'An attempt will be made to demolish buildings in the amount of' + removeCnt + 'pcs. If you want to change the quantity (only down), click "cancel"' ) ) {
removeCnt = prompt ( 'Enter the number of houses to demolish' ) ;
if ( removeCnt == null ) {
throw '' ;
}
if ( isNaN ( removeCnt ) || removeCnt > buildingTypes [ removeMe ] [ 'count' ] || removeCnt < 0 ) {
alert ( 'Incorrect value' ) ;
throw '' ;
}
}
MCS. LOADING . show ( ) ;
for ( i in streetData. b ) {
if ( streetData. b [ i ] . t == removeMe && removedCnt < removeCnt ) {
buildingList. push ( i ) ;
}
}
setTimeout ( demolish , 250 ) ;
}

pastebin.mozilla.org/678730

Source: https://habr.com/ru/post/73164/


All Articles