/variants/NorthSeaWars/interactiveMap/interactiveMapDrawExtension.js

https://github.com/Sleepcap/vDiplomacy · JavaScript · 99 lines · 63 code · 13 blank · 23 comment · 18 complexity · accfb43aa8725413151f222330a0da74 MD5 · raw file

  1. /*
  2. Copyright (C) 2013 Tobias Florin
  3. This file is part of the InterActive-Map mod for webDiplomacy
  4. The InterActive-Map mod for webDiplomacy is free software: you can
  5. redistribute it and/or modify it under the terms of the GNU Affero General
  6. Public License as published by the Free Software Foundation, either version
  7. 3 of the License, or (at your option) any later version.
  8. The InterActive-Map mod for webDiplomacy is distributed in the hope
  9. that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with webDiplomacy. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var tradeSCs = new Array(
  16. 'grains', 'wood', 'iron','Central North Sea'
  17. );
  18. function extension(drawFunction, order, fromTerrID, toTerrID, terrID){
  19. switch (order){
  20. case 'destroy':
  21. // Add 2nd destroyed-icon for the underworld gateways:
  22. //if (in_array($this->territoryNames[$terrID].' (Underworld)' ,$this->territoryNames))
  23. //parent::drawDestroyedUnit(array_search($this->territoryNames[$terrID].' (Underworld)',$this->territoryNames));
  24. if(Territories.any(function(terr){return terr[1].name === Territories.get(fromTerrID).name+' (2)';}))
  25. drawFunction(Territories.find(function(terr){return terr[1].name === Territories.get(fromTerrID).name+' (2)';})[1].id);
  26. //parent::drawDestroyedUnit($terrID);
  27. return true;
  28. case 'move':
  29. case 'retreat':
  30. case 'supportHold':
  31. var adjusted = adjustArrows(fromTerrID, toTerrID);
  32. drawFunction(adjusted[0],adjusted[1],true);
  33. return false;
  34. case 'supportMove':
  35. var adjusted = adjustArrows(fromTerrID, toTerrID, terrID);
  36. drawFunction(terrID, adjusted[0],adjusted[1],true);
  37. return false;
  38. }
  39. return true;
  40. }
  41. //documentation in drawMap.php
  42. function adjustArrows(fromID, toID, terrID){
  43. terrID = (typeof terrID == 'undefined')?0:terrID;
  44. var fromName = Territories.get(fromID).name;
  45. var toName = Territories.get(toID).name;
  46. if(terrID > 0)
  47. var terrName = Territories.get(terrID).name;
  48. if(terrID != 0)
  49. {
  50. if(( in_array(terrName, tradeSCs) && !in_array(fromName, tradeSCs) && in_array(toName,tradeSCs)) ||
  51. (!in_array(terrName, tradeSCs) && in_array(fromName, tradeSCs) && in_array(toName,tradeSCs)))
  52. {
  53. if(in_array(terrName, tradeSCs))
  54. {
  55. toName = toName+' (2)';
  56. var toTerrPair = Territories.find(function(terr){return terr[1].name === toName;});
  57. if (toTerrPair != undefined)
  58. toID = toTerrPair[1].id;
  59. else
  60. return new Array(fromID, toID)
  61. }
  62. return new Array(toID, toID);
  63. }
  64. }
  65. if(in_array(fromName, tradeSCs) && in_array(toName, tradeSCs))
  66. {
  67. if(in_array(fromName+' (2)', Territories.pluck(1).pluck('name')))
  68. {
  69. fromName = fromName+' (2)';
  70. fromID = Territories.find(function(terr){return terr[1].name === fromName;})[1].id;
  71. }
  72. if(in_array(toName+' (2)', Territories.pluck(1).pluck('name')))
  73. {
  74. toName = toName+' (2)';
  75. toID = Territories.find(function(terr){return terr[1].name === toName;})[1].id;
  76. }
  77. }
  78. return new Array(fromID, toID);
  79. }
  80. function in_array(needle, haystack){
  81. return haystack.any(function(e){if(typeof e==='Array') e = e[1]; return e===needle;});
  82. }