1.0.7 • Published 3 years ago

@tom-konda/cty2json v1.0.7

Weekly downloads
1
License
GPL-3.0
Repository
github
Last release
3 years ago

Cty2JSON

About

This JavaScript library converts from .cty file (Micropolis format) to JSON.

Future planning (ver 2.0.x)

  • Support only ES Module format.
    • Abandon CommonJS format and ES5 format output.
  • Change testing framework from mocha to Jest.
  • Change file output option to be required on CLI.
    • Abandon text output with standard output feature.

Support Browsers

  • Firefox, Google Chrome
  • Safari works probably.

Support Node.js version

  • Latest LTS only

Usage

CLI

$ cty2json [options] <inputFile>

Options

    -h, --help                 output usage information
    -V, --version              output the version number
    -o, --output <outputFile>  Output JSON file

Library

Browser (Legacy Style)

<!DOCTYPE html>
<html>
  <head>
    <title>Demo</title>
    <script src="../../lib/cty2json.js"></script>
    <script>
      'use strict';
      let xhr = new win.XMLHttpRequest();

      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          let json = Cty2JSON.analyze(xhr.response);
          let cityData = JSON.parse(json);
        }
      }

      xhr.open('GET', dataURLSch);
      xhr.responseType = 'arraybuffer';
      xhr.send();
    </script>
  </head>
</html>

Browser (ES Modules)

<!DOCTYPE html>
<html>
<head>
  <script type="module">
    import cty2JSON from './cty2json.js';
    
    fetch('./cty2jsonTest.cty')
    .then(
      (result) => {
        return result.arrayBuffer();
      }
    ).then(
      (buffer) => {
        const cityData = cty2JSON.analyze(buffer);
      }
    );
  </script>
</head>
</html>

Web worker

importScripts('../../lib/cty2json.js');

self.addEventListener(
    'message',
    function(event){
        let cityData = event.data;
        self.postMessage(self.Cty2JSON.analyze(cityData));
        self.close();
    },
    false
);

Node.js

const Cty2JSON = require('../../cty2json');

let file = fs.readFileSync(`PATH_TO_CTYFILE/test.cty`);

// Convert from buffer to Uint8Array
let uint8arr = new Uint8Array(file);
let json = Cty2JSON.analyze(uint8arr.buffer);

Output Format

{
  fileSize : Integer,
  historyData : {
    residential: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
    commercial: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
    industrial: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
    crime: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
    pollution: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
    landValue: {
      "10years": [
        0-255, // 120 items
      ],
      "120years": [
        0-255, // 120 items
      ],
    },
  },
  miscData : {
    CPopulation : Integer,
    CValve : Integer,
    IPopulation : Integer,
    IValve : Integer,
    RPopulation : Integer,
    RValve : Integer,
    autoBudget : 0 or 1,
    autoBulldoze : 0 or 1,
    autoGoto : 0 or 1,
    budget : 0 or 1,
    cityClass : Integer,
    cityScore : Integer,
    cityTime : Integer,
    crimeAve : Integer,
    crimeRamp : Integer,
    fireCovered : 0-65536,
    gameLevel : 0-2,
    gameSpeed : 0-4,
    landValueAve : Integer,
    policeCovered : 0-65536,
    polluteRamp : Integer,
    pollutionAve : Integer,
    transportCovered : 0-65536,
    soundOn : 0 or 1,
    tax : 0-20
  },
  tileDatas : [
    [
      {
        building : 0-1023,
        animated : Boolean,
        bulldozable : Boolean,
        combustible : Boolean,
        conductive : Boolean,
        zoneCenter : Boolean,
      }, {
      }, // 120 items
    ], [
    ], // 100 items
  ], 
}

How to build

  1. Clone the repo from github git clone https://github.com/tom-konda/cty2json.git
  2. Change current directory cd cty2json
  3. Run npm install
  4. Run npm run build

License

Licensed under the GPLv3

Acknowledgement

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago