Skip to main content

Functions

Before we start writing the bot script, we will create several functions that will implement the mechanics of the game.

Main functions of the game

Create responseCity.js file in the src folder. It will contain the game functions that we will call from the script:

function responseCity(parseTree, keys, cities) {
var city = parseTree._City.name
var letter = city[city.length - 1]
var response = 0

keys.forEach(function(elem) {
if (cities[elem].value.name[0].toLowerCase() == letter) {
response = elem
}
})

if (response == 0) {
letter = city[city.length - 2]
keys.forEach(function(elem) {
if (cities[elem].value.name[0].toLowerCase() == letter) {
response = elem
}
})
}
return response
}

function findByName(city, keys, cities) {
var response = 0
var i = 0

keys.forEach(function(elem) {
if (cities[elem].value.name == city) {
response = i
}
i++
})

return response
}

function checkCity(parseTree, keys, cities) {
var city = parseTree._City.name
var response = false

keys.forEach(function(elem) {
if (cities[elem].value.name == city) {
response = true
}
})
return response;
}

function checkLetter(playerCity, botCity) {
var response = false
if ((playerCity[0].toLowerCase() == botCity[botCity.length - 1])
|| (playerCity[0].toLowerCase() == botCity[botCity.length - 2])) {
response = true
}
return response
}

function chooseRandCityKey(keys) {
var i = 0
keys.forEach(function(elem) {
i++
})
return $jsapi.random(i);
}

function isAFullNameOfCity() {
return $jsapi.context().parseTree._City.name.toUpperCase() == $jsapi.context().parseTree.text.toUpperCase();
}

In the project, we will use city/cities-en.csv additional module that contains a list of more than 8500 city names. We will take a look at how this module is used in the Project files section.

Each city in the list has the following structure:

6614;New York City,New York, NY;
{
"name":"New York City",
"lat":40.71427,
"lon":-74.00597,
"country":"US",
"timezone":"America/New_York",
"population":8175133
}

We will only use id and name values.

Functions description

responseCity ()

The responseCity() function returns a city that starts from the last letter of the entered city. When the city list runs out of city names starting with a certain letter, then the function returns a city starting with the second to last letter.

function responseCity(parseTree, keys, cities) {
// save the entered city name
var city = parseTree._City.name
// take the last letter of the entered city
var letter = city[city.length - 1]
// initialize the key of the city
var response = 0

/* looking through the elements of the city list
until we find a city that starts with the last letter of the previous city*/
keys.forEach(function(elem) {
if (cities[elem].value.name[0].toLowerCase() == letter) {
response = elem
}
})

/* if the city list runs out of city names starting with a certain letter,
then use the second to last letter*/
if (response == 0) {
letter = city[city.length - 2]
keys.forEach(function(elem) {
if (cities[elem].value.name[0].toLowerCase() == letter) {
response = elem
}
})
}
return response
}

findByName ()

The findByName() function searches for the named city in the city/cities-en.csv file.

/* we will call this function from the script to remove the entered city from the list*/
function findByName(city, keys, cities) {
// initialize the key of the city
var response = 0
var i = 0

// looking through the elements of the list until we find the entered city
keys.forEach(function(elem) {
if (cities[elem].value.name == city) {
response = i
}
i++
})

return response
}

checkCity ()

The checkCity() function checks if the entered city is in the city/cities-en.csv list. Returns true or false.

function checkCity(parseTree, keys, cities) {
// save the entered city name
var city = parseTree._City.name
var response = false

// looking through the elements of the list until we find the entered city
keys.forEach(function(elem) {
if (cities[elem].value.name == city) {
response = true
}
})
return response;
}

checkLetter ()

The checkLetter() function checks if the first letter of the city the user has entered matches the last or the second to last letter of the city the bot has entered.

function checkLetter(playerCity, botCity) {
var response = false
/* true, if the first letter of the city the user has entered
matches the last letter of the city the bot has entered */
if ((playerCity[0].toLowerCase() == botCity[botCity.length - 1])
|| (playerCity[0].toLowerCase() == botCity[botCity.length - 2])) {
response = true
}
return response
}

chooseRandCityKey ()

The chooseRandCityKey() function returns a random key number of the city. The function is called from the script when the bot starts the game.

function chooseRandCityKey(keys) {
return $jsapi.random(keys.length);
}

isAFullNameOfCity ()

The isAFullNameOfCity() function checks if the user has entered a full city name. Returns true or false.

function isAFullNameOfCity() {
return $jsapi.context().parseTree._City.name.toUpperCase() == $jsapi.context().parseTree.text.toUpperCase();
}

Next, move on to creating the configuration file and the script.