Web version/Data storage

This page describes the data storage implementations for the Ancient Empires Web version.

## Ancient Empires 1

The data for Ancient Empires 1 are saved entirely in localStorage.

To get the items, type the following in Chrome browser console:

```
let storage = window.localStorage;
let data = JSON.parse(storage.getItem('ancient-empires-ls-item'));

```

Then if the `ancient-empires-ls-item` key already has entry, then `items` will be a dictionary. Otherwise `items` will be `null`. This entry will be created when you run the game in the browser for the first time.

The dictionary contains the following content:

### Sample JSON

(After the game is finished)

```
{
 "isIE": false,
 "isAndroid": false,
 "isIOS": false,
 "os": "android",
 "squareSize": 62,
 "setting": {
 "endTurnConfirm": false,
 "music": false,
 "gameSpeed": "5",
 "lang": "en"
 },
 "openMissions": [
 "c01\_regroup",
 "c02\_friendsAndEnemies",
 "c03\_escort",
 "c04\_reinforcements",
 "c05\_wyvernRescue",
 "c06\_siege",
 "c07\_finalAssault"
 ]
}

```

The output above can be obtained using the JavaScript below:

```
let storage = window.localStorage;
let data = JSON.parse(storage.getItem('ancient-empires-ls-item'));
console.log(JSON.stringify(data, null, 2));

```

## Ancient Empires 2

The data storage for Ancient Empires 2 are divided into two components:

1. **`window.localStorage`**, for saving game settings.
2. **Web SQL storage**, for saving Companies mode and Skirmish mode game progress and user maps.

The sections below will describe each of them respectively.

### `window.localStorage`

To get the items, type the following in Chrome browser console:

```
let storage = window.localStorage;
let items = JSON.parse(storage.getItem('ae2-ls-item'));

```

Then if the `ae2-ls-item` key already has entry, then `items` will be a dictionary. Otherwise `items` will be `null`. This entry will be created when you run the game in the browser for the first time.

The dictionary contains the following content:

#### Sample JSON

```
{
 "language": "en",
 "autoSave": "on",
 "confirmTurn": "off",
 "confirmMove": "off",
 "confirmAttack": "off",
 "music": "off",
 "vibrate": "off",
 "help": "off",
 "fightAnimation": "off",
 "buildingSmoke": "off",
 "unitAnimation": "off",
 "font": "lucida",
 "difficult": "hard",
 "gameSpeed": "5",
 "mapPackVersion": 4,
 "squareSize": 48
}

```

The output above can be obtained using the JavaScript below:

```
let storage = window.localStorage;
let data = JSON.parse(storage.getItem('ae2-ls-item'));
console.log(JSON.stringify(data, null, 2));

```

### Web SQL storage

The Web SQL storage is used to save user progress in Companies mode and Skirmish mode. The database files are in **SQLite** format. To open the database files, you can download and use the **DB Browser for SQLite** program.

To access the database files:

1. Type in **`chrome://version/`** in Google Chrome address bar, to show up the version information.
2. Find the line "**Profile Path**". One example of this line may be "**Profile Path:** `<user home directory>/.config/google-chrome./Default`". *This directory is different for each user of Google Chrome on the local machine.*
3. Navigate to the directory **Profile Path** directory as seen above, and navigate into the **`databases`** subdirectory. This directory contains all the Web SQL databases created by various websites the user have visited and stored on the local machine. Colons (:) and slashes (/) are replaced with underscores (\_). Navigate into each directory to see the database.
	* The `**Databases.db**` file keeps track of all the databases created on the local machine under the current user profile. See the #Databases.db format section for more details.
	* Each database file is named by a number: 0, 1, 2, 3, …
	* **For localhost addresses, each `<ip>:<host>` pair has a separate database, and the data are not shared among each other, even if different addresses were used to serve the same web application.** So to save and load the game progress, you must use the same `<ip>:<host>` pair across different executions of the game.
4. Pick one of the localhost directories, e.g. `**http\_127.0.0.1\_8080**`, and navigate into it. You can see the database file, named by a number (e.g. 0). See the #AE2 SQLite format section for more details about its format.

#### AE2 SQLite format

In the tables `mission`, `skirmish` and `userMap`:

* `jsMapKey` corresponds to a unique name for each map.
	+ Companies mode: `mission_001_001` through `mission_001_008` (only the last digit is different for each level).
	+ Skirmish mode (built-in maps): `skirmish_001_###` (`###` is an order number starting from 001).
	+ User maps: `userMap_<map-name>`, where `<map-name>` is supplied by the user when saving maps.
* `info` provides some basic information about the missions and maps.
* `map` provides detailed information about the maps.

## Notes

1. ↑ 1.0 1.1 To pretty print the JSON data, use the `JSON.stringify()` method, and set the `space` parameter to 2.
Use `console.log()` to show the JSON object with newlines and indentation.