Difference between revisions of "MediaWiki:TileSelectorUtils.js"

From Wikicarpedia
Jump to navigation Jump to search
m (Test)
m (Test)
Line 269: Line 269:
     }
     }
   }
   }
   return iTotalCount;
   return iTotalCount;
}
}
Line 275: Line 276:
{
{
   var iTotalCount = 0;
   var iTotalCount = 0;
  var iReferenceCount = 0;
   var strRef = strParentRef + " #" + oExpansion.id;
   var strRef = strParentRef + " #" + oExpansion.id;


Line 288: Line 290:
       iTotalCount += doUpdateTile( oTile, strRef );
       iTotalCount += doUpdateTile( oTile, strRef );
     }
     }
    iReferenceCount = oExpansion.totalCount;
   }
   }


Line 295: Line 298:
     oExpansion.currentCount = iTotalCount;
     oExpansion.currentCount = iTotalCount;


     var strCounterText = iTotalCount + " / " + oExpansion.totalCount;
     var strCounterText = iTotalCount + " / " + iReferenceCount; // oExpansion.totalCount;


     var strCounterId = strRef + " " + EXPANSION_COUNTER;
     var strCounterId = strRef + " " + EXPANSION_COUNTER;
Line 310: Line 313:
   }
   }
*/
*/
   return iTotalCount;
 
  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };
 
   return oCounterRes; // iTotalCount;
}
}


Line 316: Line 322:
{
{
   var iTotalCount = 0;
   var iTotalCount = 0;
  var iReferenceCount = 0;
   var strRef = strParentRef + " #" + oEdges.id;
   var strRef = strParentRef + " #" + oEdges.id;


Line 327: Line 334:
     {
     {
       var oExpansion = arrExpansions[ i ];
       var oExpansion = arrExpansions[ i ];
       iTotalCount += doUpdateExpansion( oExpansion, strRef );
       var oCounterRes = doUpdateExpansion( oExpansion, strRef );
      iTotalCount += oCounterRes.tileCount;
      iReferenceCount += oCounterRes.referenceCount;
     }
     }
   }
   }
Line 336: Line 345:
     oEdges.currentCount = iTotalCount;
     oEdges.currentCount = iTotalCount;


     var strRawCounterText = iTotalCount + " / " + oEdges.totalCount;
     var strRawCounterText = iTotalCount + " / " + iReferenceCount; // oEdges.totalCount;
     //var strHeaderCounterText = "(" + strRawCounterText + ")";
     //var strHeaderCounterText = "(" + strRawCounterText + ")";
     var strHeaderCounterText = strRawCounterText;
     var strHeaderCounterText = strRawCounterText;
Line 357: Line 366:
   }
   }


   return iTotalCount;
  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };
 
   return oCounterRes; // iTotalCount;
}
}


Line 363: Line 374:
{
{
   var iTotalCount = 0;
   var iTotalCount = 0;
  var iReferenceCount = 0;
   var strRef = strParentRef + " #" + oSection.id;
   var strRef = strParentRef + " #" + oSection.id;


Line 369: Line 381:
   {
   {
     var oEdges = arrEdges[ i ];
     var oEdges = arrEdges[ i ];
     iTotalCount += doUpdateEdges( oEdges, strRef );
     var oResCount = doUpdateEdges( oEdges, strRef );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
   }
   }


Line 378: Line 392:


     //var strCounterText = "(" + iTotalCount + "/" + oSection.totalCount + ")";
     //var strCounterText = "(" + iTotalCount + "/" + oSection.totalCount + ")";
     var strCounterText = iTotalCount + " / " + oSection.totalCount;
     var strCounterText = iTotalCount + " / " + iReferenceCount; // oSection.totalCount;


     var strCounterId = strRef + " " + SECTION_COUNTER;
     var strCounterId = strRef + " " + SECTION_COUNTER;
Line 393: Line 407:
   }
   }


   return iTotalCount;
  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };
 
   return oCounterRes; // iTotalCount;
}
}


Line 400: Line 416:
{
{
   var iTotalCount = 0;
   var iTotalCount = 0;
  var iReferenceCount = 0;


   var strRef = "#" + oEdition.id;
   var strRef = "#" + oEdition.id;
Line 407: Line 424:
   {
   {
     var oSection = arrSections[ i ];
     var oSection = arrSections[ i ];
     iTotalCount += doUpdateSection( oSection, strRef );
     var oResCount = doUpdateSection( oSection, strRef );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
   }
   }


Line 416: Line 435:


//    var strCounterText = "(" + iTotalCount + "/" + oEdition.totalCount + ")";
//    var strCounterText = "(" + iTotalCount + "/" + oEdition.totalCount + ")";
     var strCounterText = iTotalCount + " / " + oEdition.totalCount;
     var strCounterText = iTotalCount + " / " + iReferenceCount; // oEdition.totalCount;


     var strCounterId = strRef + " " + EDITION_COUNTER;
     var strCounterId = strRef + " " + EDITION_COUNTER;
Line 434: Line 453:
   }
   }


   return iTotalCount;
  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };
 
   return oCounterRes; // iTotalCount;
}
}


Line 440: Line 461:
{
{
   var iTotalCount = 0;
   var iTotalCount = 0;
  var iReferenceCount = 0;


   var arrEdition = PageInfoMap.editions;
   var arrEdition = PageInfoMap.editions;
Line 445: Line 467:
   {
   {
     var oEdition = arrEdition[ strEditionKey ];
     var oEdition = arrEdition[ strEditionKey ];
     iTotalCount += doUpdateEdition( oEdition );
     var oResCount = doUpdateEdition( oEdition );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
   }
   }
   PageInfoMap.totalCount = iTotalCount;
   PageInfoMap.totalCount = iTotalCount;
  PageInfoMap.referenceCount = iReferenceCount;
}
}

Revision as of 23:40, 27 October 2020

// ***** Updating tile counter *****


var EDITION_COUNTER = "#_Edition_Tile_Counter_";
var EDITION_SEPARATOR = "#_Tile_Separator_";
var SECTION_COUNTER = "#_Section_Tile_Counter_";
var EDGES_HEADER_COUNTER = "#_Edges_Header_Tile_Counter_";
var EDGES_TABLE_COUNTER = "#_Edges_Table_Tile_Counter_";
var EXPANSION_COUNTER = "#_Expansion_Tile_Counter_";
var TILE_COUNTER = "#_Tile_Counter_";
var TILE_DESCRIPTION = "#_Tile_Description_";
var TILE_DESCRIPTION_VISIBILITY = "#_Tile_Desc_Visible_";

var BaseGameFeaturesMap =
{
  garden: "acronG_Label",
  highwaymen: "acronH_Label",
  farmhouse: "acronF_Label",
  pigs: "acronP_Label",
  cows: "acronC_Label",
  donkeys: "acronD_Label",
  water_tower: "acronW_Label",
}

var FeatureMap =
{
  road: "showRoad",
  city: "showCity",
  monastery: "showMonastery",
  field: "showField",
  abbey: "showAbbey",
  shrine: "showShrine",
  german_castle: "showGermanCastle",
  german_cathedral: "showGermanCathedral",
  german_monastery: "showGermanMonastery",
  dutch_belgian_monastery: "showDutchAndBelgianMonastery",
  japanese_building: "showJapaneseBuilding",
  darmstadt_church: "showDarmstadtChurch",
  baba_yagas_Hut: "showBabaYagasHut",

  garden: "showGarden",
  highwaymen: "showHighwaymen",
  farmhouse: "showFarmhouse",
  pigs: "showPigsty",
  cows: "showCowShed",
  donkeys: "showDonkeyStable",
  water_tower: "showWaterTower",

  river_source: "showRiverSource",
  river_lake: "showRiverLake",
  river_fork: "showRiverFork",
  river_segment: "showRiverSegment",

  magic_portal: "showMagicPortal",
  volcano: "showVolcano",
  tower_foundation: "showTowerFoundation",
  fair: "showFair",
  bazaar: "showBazaar",
  circus_area: "showCircusArea",
  acrobat_area: "showAcrobatArea",
  flying_machine: "showFlyingMachine",
  crop_circle: "showCropCicle",
  fruit_tree: "showFruitBearingTree",
  solovei: "showSoloveiRazboyniksTree",
  vodyanoy: "showVodyanoysLake",
  
  farm_cottage: "showFieldCottage",
  farm_cattle: "showFieldCattle",
  small_bridge: "showSmallBridge",
  large_bridge: "showLargeBridge",
  city_overpass: "showCityOverpass",
  road_village : "showCrossroadWithVillage",
  road_trees: "showCrossroadsWithTrees",
  road_cottage: "showRoadCottage",
  bogatyr: "showBogatyrsStone",
  roundabout: "showRoundabout",
  
  inn: "showInn",
  cathedral: "showCathedral",
  pig_herd: "showPigHerd",
  vineyard: "showVineyard",
  printed_sheep: "showPrintedSheep",
  porxada: "showPorxada",
  tunnel: "showTunnel",
  darmstadtium: "showDarmstadtium",
  labyrinth: "showLabyrinth",
  ferryLake: "showFerryLake",
  siege: "showSiege",
  festival: "showFestival",
  watchtower: "showWatchtower",
  bathhouse: "showBathhouse",
  traveler_group: "showTravelerGroup",

  pennant: "showCoatOfArms",
  princess: "showPrincessSymbol",
  barrel: "showBarrelSymbol",
  cloth: "showClothSymbol",
  wheat: "showWheatSymbol",
  dragon: "showDragonSymbol",
  wheel_number: "showWheelWithNumber",
  gold: "showGoldSymbol",
  magic_symbol: "showMagicSymbol",
  robber_symbol: "showRobberSymbol",
  cathar: "showCatharSymbol",
  festival_symbol: "showFestivalSymbol",
  gingerbread_man: "showGingerbreadManSymbol",
  outbreak: "showOutbreakSymbol",
  wind_rose: "showWindRose",

  city_Carcassonne: "showCityOfCarcassonne",
  wheel_of_Fortune: "showWheelOfFortune",
  school: "showSchool",
  city_of_Leipzig: "showCityOfLeipzig",

/* 
// *** Not features anymore ***
  points_tile: "showPointsTile",
  goldmine_tile: "showGoldminesPointsTile",
  king_tile: "showKingTile",
  robber_tile: "showRobberBaronTile",
  message: "showMessageTile",
*/
}

function getTileInfo( oTile )
{
  var iTileCount = oTile.totalCount;
  var oPresentFeatures = {};
  var oRes = { tileCount: 0, tileDescription: "" };

  var arrFeatures = oTile.features;
  for ( var strFeature in arrFeatures )
  {
    var strFeatureClassId = FeatureMap[ strFeature ];
    
    if ( !strFeatureClassId ) continue;

    var bVisible = doEval( strFeatureClassId );

    // As soon as one feature in the tile is not active, returns notifying no tile is visible
    if( !bVisible ) return oRes;

    oPresentFeatures[ strFeature ] = arrFeatures[ strFeature ];
  }

  var arrSpecialCases = oTile.specialCases;
  for ( var i in arrSpecialCases )
  {
    var oSpecialCase = arrSpecialCases[ i ];
    var bRes = true;
    var iCount = oSpecialCase.totalCount;
    var arrFeatures = oSpecialCase.features;

    var arrOtherFeatures = {};

    for ( var strFeature in arrFeatures )
    {
      var strFeatureClassId = FeatureMap[ strFeature ];
      if ( !strFeatureClassId ) continue;

      var bVisible = doEval( strFeatureClassId );

      bRes = bRes && bVisible;

      if ( !bRes ) break;

      arrOtherFeatures[ strFeature ] = iCount; // tweak to get right count // arrFeatures[ strFeature ];
    }

    if ( !bRes )
    {
      iTileCount -= iCount;
    }
    else
    {
      // Append special features
      for ( var strFeature in arrOtherFeatures )
      {
      
        oPresentFeatures[ strFeature ] = arrOtherFeatures[ strFeature ];
      }
    }
    
  }

  if ( iTileCount > 0 )
  {
    var nCount = Object.keys( oPresentFeatures ).length;
    var bShortFormat = ( nCount > 3 );
    var iItems = 0;
    var strRes = ""; 

    for ( var strFeature in BaseGameFeaturesMap )
    {
      var iFeatureCount = oPresentFeatures[ strFeature ];

      // If no entry exits, skip feature
      if ( !iFeatureCount ) continue;

      var strLabelId = BaseGameFeaturesMap[ strFeature ];
      var strLabel = getLabel( strLabelId );

      if ( bShortFormat )
      {
        strRes += ( iItems > 0  ? ";" : "" ) + ( iFeatureCount > 1 ? iFeatureCount + "x" : "" ) + strLabel;
        iItems++;                  
      }
      else
      {
        for ( var i = 0; i < iFeatureCount; i++ )
        {
          strRes += ( iItems > 0  ? ";" : "" ) + strLabel;
          iItems++;          
        }
      }
    }

    if ( strRes.length > 0 )
    {
      oRes.tileDescription = "(" + strRes + ")";
    }
  }

  oRes.tileCount = iTileCount;

  return oRes;
}


function doUpdateTile( oTile, strParentRef )
{
  var oTileInfo = getTileInfo( oTile );
  var iTotalCount = oTileInfo.tileCount;

  var iPreviousCount = oTile.currentCount;
  if ( iPreviousCount != iTotalCount )
  {
    oTile.currentCount = iTotalCount;

    var strRef = strParentRef + " #" + oTile.id;
    if ( iTotalCount == 0 )
    {
      $( strRef ).hide();    
    }
    else
    {
      $( strRef ).show();    
    }

    var strTileCountId = strRef + " " + TILE_COUNTER;
    var strTileCount = "" + ( oTile.displayedCount || iTotalCount );
    $( strTileCountId ).html( strTileCount );

    var strTileDescriptionId = strRef + " " + TILE_DESCRIPTION;
    var strDescription = oTileInfo.tileDescription;
    $( strTileDescriptionId ).html( strDescription );

    var strTileDescriptionVisibilityId = strRef + " " + TILE_DESCRIPTION_VISIBILITY;
    var bDescriptionVisible = (strDescription.length > 0 );
    if ( bDescriptionVisible )
    {
      $( strTileDescriptionVisibilityId ).show();    
    }
    else
    {
      $( strTileDescriptionVisibilityId ).hide();    
    }
  }

  return iTotalCount;
}

function doUpdateExpansion( oExpansion, strParentRef )
{
  var iTotalCount = 0;
  var iReferenceCount = 0;
  var strRef = strParentRef + " #" + oExpansion.id;

  var strClassId = "show" + oExpansion.id;
  var bVisible = doEval( strClassId );

  if ( bVisible )
  {
    var arrTiles = oExpansion.tiles;
    for ( var i in arrTiles )
    {
      var oTile = arrTiles[ i ];
      iTotalCount += doUpdateTile( oTile, strRef );
    }
    iReferenceCount = oExpansion.totalCount;
  }

  var iPreviousCount = oExpansion.currentCount;
  if ( iPreviousCount != iTotalCount )
  {
    oExpansion.currentCount = iTotalCount;

    var strCounterText = iTotalCount + "&nbsp;/&nbsp;" + iReferenceCount; // oExpansion.totalCount;

    var strCounterId = strRef + " " + EXPANSION_COUNTER;
    $( strCounterId ).html( strCounterText );
  }
/*
  if ( iTotalCount == 0 )
  {
    $( strRef ).hide();
  }
  else
  {
    $( strRef ).show();
  }
*/

  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };

  return oCounterRes; // iTotalCount;
}

function doUpdateEdges( oEdges, strParentRef )
{
  var iTotalCount = 0;
  var iReferenceCount = 0;
  var strRef = strParentRef + " #" + oEdges.id;

  var strClassId = "show" + oEdges.id;
  var bVisible = doEval( strClassId );

  if ( bVisible )
  {
    var arrExpansions = oEdges.expansions;
    for ( var i in arrExpansions )
    {
      var oExpansion = arrExpansions[ i ];
      var oCounterRes = doUpdateExpansion( oExpansion, strRef );
      iTotalCount += oCounterRes.tileCount;
      iReferenceCount += oCounterRes.referenceCount;
    }
  }

  var iPreviousCount = oEdges.currentCount;
  if ( iPreviousCount != iTotalCount )
  {
    oEdges.currentCount = iTotalCount;

    var strRawCounterText = iTotalCount + "&nbsp;/&nbsp;" + iReferenceCount; // oEdges.totalCount;
    //var strHeaderCounterText = "(" + strRawCounterText + ")";
    var strHeaderCounterText = strRawCounterText;
    var strTableCounterText = strRawCounterText;

    var strHeaderCounterId = strRef + " " + EDGES_HEADER_COUNTER;
    $( strHeaderCounterId ).html( strHeaderCounterText );

    var strTableCounterId = strRef + " " + EDGES_TABLE_COUNTER;
    $( strTableCounterId ).html( strTableCounterText );
  }

  if ( iTotalCount == 0 )
  {
    $( strRef ).hide();
  }
  else
  {
    $( strRef ).show();
  }

  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };

  return oCounterRes; // iTotalCount;
}

function doUpdateSection( oSection, strParentRef )
{
  var iTotalCount = 0;
  var iReferenceCount = 0;
  var strRef = strParentRef + " #" + oSection.id;

  var arrEdges = oSection.edges;
  for ( var i in arrEdges )
  {
    var oEdges = arrEdges[ i ];
    var oResCount = doUpdateEdges( oEdges, strRef );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
  }

  var iPreviousCount = oSection.currentCount;
  if ( iPreviousCount != iTotalCount )
  {
    oSection.currentCount = iTotalCount;

    //var strCounterText = "(" + iTotalCount + "/" + oSection.totalCount + ")";
    var strCounterText = iTotalCount + "&nbsp;/&nbsp;" + iReferenceCount; // oSection.totalCount;

    var strCounterId = strRef + " " + SECTION_COUNTER;
    $( strCounterId ).html( strCounterText );
  }

  if ( iTotalCount == 0 )
  {
    $( strRef ).hide();
  }
  else
  {
    $( strRef ).show();
  }

  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };

  return oCounterRes; // iTotalCount;
}


function doUpdateEdition( oEdition )
{
  var iTotalCount = 0;
  var iReferenceCount = 0;

  var strRef = "#" + oEdition.id;

  var arrSections = oEdition.sections;
  for ( var i in arrSections )
  {
    var oSection = arrSections[ i ];
    var oResCount = doUpdateSection( oSection, strRef );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
  }

  var iPreviousCount = oEdition.currentCount;
  if ( iPreviousCount != iTotalCount )
  {
    oEdition.currentCount = iTotalCount;

//    var strCounterText = "(" + iTotalCount + "/" + oEdition.totalCount + ")";
    var strCounterText = iTotalCount + "&nbsp;/&nbsp;" + iReferenceCount; // oEdition.totalCount;

    var strCounterId = strRef + " " + EDITION_COUNTER;
    $( strCounterId ).html( strCounterText );
  }

  var strSeparator = strRef + " " + EDITION_SEPARATOR;
  if ( iTotalCount == 0 )
  {
    $( strSeparator ).hide();
    //$( strRef ).hide();
  }
  else
  {
    $( strSeparator ).show();
    //$( strRef ).show();
  }

  var oCounterRes = { tileCount: iTotalCount, referenceCount: iReferenceCount };

  return oCounterRes; // iTotalCount;
}

function doUpdateTileCounters()
{
  var iTotalCount = 0;
  var iReferenceCount = 0;

  var arrEdition = PageInfoMap.editions;
  for ( var strEditionKey in arrEdition )
  {
    var oEdition = arrEdition[ strEditionKey ];
    var oResCount = doUpdateEdition( oEdition );
    iTotalCount += oResCount.tileCount;
    iReferenceCount += oResCount.referenceCount;
  }
  PageInfoMap.totalCount = iTotalCount;
  PageInfoMap.referenceCount = iReferenceCount;
}