Advertisement
I_GRIN_I

main.js

Jan 4th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _;
  2. var CardLibrary;
  3. var DialogLibrary;
  4. var fc;
  5.  
  6. function OnUndo() {
  7.     fc.undo();
  8. }
  9.  
  10. function OnNewGame() {
  11.     var num = _.getRandomInt(1, 32000);
  12.     StartGame(num);
  13. }
  14.  
  15. function StartGame(num) {
  16.     Game.EmitSound("Card.cardOpenPackage2");
  17.     fc.deal(num);
  18.     $('#game-number-label').text = $.Localize("game_number_label") + num;
  19. }
  20.  
  21. function OnPlayAgain() {
  22.     var dialog = new DialogLibrary.Dialog({
  23.         parentPanel: DialogLibrary.contextPanel,
  24.         layoutfile: "file://{resources}/layout/custom_game/dialog/dialog.xml",
  25.         id: "dialog-container",
  26.         hittest: true,
  27.         children: [{
  28.             id: "contents-container",
  29.             cssClasses: ["contents-container"],
  30.             style: {
  31.                 width: 400
  32.             },
  33.             children: [{
  34.                 cssClasses: ["control", "horizontal-center"],
  35.                 id: "control-1",
  36.                 children: [{
  37.                     id: "label-1",
  38.                     panelType: "Label",
  39.                     text: $.Localize("victory_text")
  40.                 }]
  41.             }, {
  42.                 cssClasses: ["control", "horizontal-center"],
  43.                 id: "control-2",
  44.                 children: [{
  45.                     events: {
  46.                         OnActivate: function() {
  47.                             OnNewGame();
  48.                             this.root.close();
  49.                         },
  50.                         OnTabForward: function() {
  51.                             this.root.focusNextInput(this);
  52.                         }
  53.                     },
  54.                     panelType: "Button",
  55.                     init: function() {
  56.                         this.root.controls.push(this);
  57.                     },
  58.                     cssClasses: ["btn"],
  59.                     children: [{
  60.                         panelType: "Label",
  61.                         text: $.Localize("play_again"),
  62.                         skipBindHandlers: true
  63.                     }]
  64.                 }]
  65.             }]
  66.         }]
  67.     });
  68. }
  69.  
  70. function OnLose() {
  71.     var dialog = new DialogLibrary.Dialog({
  72.         parentPanel: DialogLibrary.contextPanel,
  73.         layoutfile: "file://{resources}/layout/custom_game/dialog/dialog.xml",
  74.         id: "dialog-container",
  75.         hittest: true,
  76.         children: [{
  77.             id: "contents-container",
  78.             cssClasses: ["contents-container"],
  79.             style: {
  80.                 width: 400
  81.             },
  82.             children: [{
  83.                 cssClasses: ["control", "horizontal-center"],
  84.                 id: "control-1",
  85.                 children: [{
  86.                     id: "label-1",
  87.                     panelType: "Label",
  88.                     text: $.Localize("defeat_text")
  89.                 }]
  90.             }, {
  91.                 cssClasses: ["control"],
  92.                 id: "control-2",
  93.                 children: [{
  94.                     events: {
  95.                         OnActivate: function() {
  96.                             OnUndo();
  97.                             this.root.close();
  98.                         },
  99.                         OnTabForward: function() {
  100.                             this.root.focusNextInput(this);
  101.                         }
  102.                     },
  103.                     panelType: "Button",
  104.                     init: function() {
  105.                         this.root.controls.push(this);
  106.                     },
  107.                     cssClasses: ["btn"],
  108.                     children: [{
  109.                         panelType: "Label",
  110.                         text: $.Localize("undo"),
  111.                         skipBindHandlers: true
  112.                     }]
  113.                 }]
  114.             }, {
  115.                 cssClasses: ["control", "horizontal-right"],
  116.                 id: "control-3",
  117.                 children: [{
  118.                     events: {
  119.                         OnActivate: function() {
  120.                             OnNewGame();
  121.                             this.root.close();
  122.                         },
  123.                         OnTabForward: function() {
  124.                             this.root.focusNextInput(this);
  125.                         }
  126.                     },
  127.                     panelType: "Button",
  128.                     init: function() {
  129.                         this.root.controls.push(this);
  130.                     },
  131.                     cssClasses: ["btn"],
  132.                     children: [{
  133.                         panelType: "Label",
  134.                         text: $.Localize("play_again"),
  135.                         skipBindHandlers: true
  136.                     }]
  137.                 }]
  138.             }]
  139.         }]
  140.     });
  141.  
  142. }
  143.  
  144. function OnSelectGame() {
  145.     var dialog = new DialogLibrary.Dialog({
  146.         parentPanel: DialogLibrary.contextPanel,
  147.         layoutfile: "file://{resources}/layout/custom_game/dialog/dialog.xml",
  148.         id: "dialog-container",
  149.         hittest: true,
  150.         children: [{
  151.             id: "contents-container",
  152.             cssClasses: ["contents-container"],
  153.             style: {
  154.                 width: 400
  155.             },
  156.             children: [{
  157.                 cssClasses: ["control"],
  158.                 id: "control-1",
  159.                 children: [{
  160.                     id: "label-1",
  161.                     panelType: "Label",
  162.                     text: $.Localize("select_game_label")
  163.                 }, {
  164.                     events: {
  165.                         OnInputSubmit: function() {
  166.                             if (this.panel.text >= 1 && this.panel.text <= 32000) {
  167.                                 StartGame(this.panel.text);
  168.                                 this.root.close();
  169.                             }
  170.                         },
  171.                         OnTabForward: function() {
  172.                             this.root.focusNextInput(this);
  173.                         }
  174.                     },
  175.                     id: "textInput",
  176.                     panelType: "TextEntry",
  177.                     init: function() {
  178.                         this.root.controls.push(this);
  179.                     }
  180.                 }]
  181.             }, {
  182.                 cssClasses: ["control"],
  183.                 id: "control-2",
  184.                 children: [{
  185.                     events: {
  186.                         OnActivate: function() {
  187.                             var num = this.root.children[0].children[0].children[1].panel.text;
  188.                             if (num >= 1 && num <= 32000) {
  189.                                 StartGame(num);
  190.                                 this.root.close();
  191.                             }
  192.                         },
  193.                         OnTabForward: function() {
  194.                             this.root.focusNextInput(this);
  195.                         }
  196.                     },
  197.                     panelType: "Button",
  198.                     init: function() {
  199.                         this.root.controls.push(this);
  200.                     },
  201.                     cssClasses: ["btn"],
  202.                     children: [{
  203.                         panelType: "Label",
  204.                         text: $.Localize("ok"),
  205.                         skipBindHandlers: true
  206.                     }]
  207.                 }]
  208.             }, {
  209.                 cssClasses: ["control"],
  210.                 id: "control-3",
  211.                 children: [{
  212.                     events: {
  213.                         OnActivate: function() {
  214.                             this.root.panel.DeleteAsync(0);
  215.                         },
  216.                         OnTabForward: function() {
  217.                             this.root.focusNextInput(this);
  218.                         }
  219.                     },
  220.                     panelType: "Button",
  221.                     init: function() {
  222.                         this.root.controls.push(this);
  223.                     },
  224.                     cssClasses: ["btn"],
  225.                     children: [{
  226.                         panelType: "Label",
  227.                         text: $.Localize("cancel"),
  228.                         skipBindHandlers: true
  229.                     }]
  230.                 }]
  231.             }]
  232.         }]
  233.     });
  234. }
  235.  
  236. (function() {
  237.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_TOP_TIMEOFDAY, false);
  238.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_TOP_HEROES, false);
  239.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_FLYOUT_SCOREBOARD, false);
  240.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_ACTION_PANEL, false);
  241.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_ACTION_MINIMAP, false);
  242.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_INVENTORY_PANEL, false);
  243.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_INVENTORY_SHOP, false);
  244.     GameUI.SetDefaultUIEnabled(DotaDefaultUIElement_t.DOTA_DEFAULT_UI_TOP_MENU_BUTTONS, false);
  245.  
  246.     _ = GameUI.CustomUIConfig().UtilLibrary;
  247.     DialogLibrary = GameUI.CustomUIConfig().DialogLibrary;
  248.     CardLibrary = GameUI.CustomUIConfig().CardLibrary;
  249.  
  250.     function FreeCell() {
  251.         this.seed = 0;
  252.         this.columns = [];
  253.         this.cells = [];
  254.         this.foundations = [];
  255.         this.history = [];
  256.         this.aspectRatios = {
  257.             AspectRatio16x10: {
  258.                 columns: [200, 160],
  259.                 cells: [100, 160],
  260.                 foundations: [940, 160]
  261.             },
  262.             AspectRatio16x9: {
  263.                 columns: [370, 160],
  264.                 cells: [180, 160],
  265.                 foundations: [1180, 160]
  266.             },
  267.             AspectRatio5x4: {
  268.                 columns: [60, 160],
  269.                 cells: [40, 140],
  270.                 foundations: [780, 140]
  271.             },
  272.             AspectRatio4x3: {
  273.                 columns: [100, 160],
  274.                 cells: [80, 140],
  275.                 foundations: [820, 140]
  276.             }
  277.         }
  278.         this.currentAspectRatio = this.getAspectRatioClass();
  279.     };
  280.     _.extend(FreeCell.prototype, {
  281.         init: function() {
  282.             this.deck = new CardLibrary.Deck({
  283.                 fc: this,
  284.                 ctor: FreeCellCard,
  285.                 visible: true,
  286.                 faceUp: true,
  287.                 draggable: true,
  288.                 droppable: false
  289.             });
  290.             var parentPanel = CardLibrary.contextPanel;
  291.             var aspectRatio
  292.             aspectRatio = this.getAspectRatioClass();
  293.             for (var i = 0; i < 8; i++) {
  294.                 this.columns.push(new Column({
  295.                     fc: this,
  296.                     type: "column",
  297.                     cardOffsetY: 30,
  298.                     slot: {
  299.                         parentPanel: parentPanel,
  300.                         style: {
  301.                             x: this.getAspectRatioValues(aspectRatio, "columns", i),
  302.                             y: 370,
  303.                             zIndex: 0
  304.                         },
  305.                         visible: true,
  306.                         draggable: false,
  307.                         droppable: true
  308.                     }
  309.                 }));
  310.             }
  311.             for (var i = 0; i < 4; i++) {
  312.                 aspectRatio = this.getAspectRatioClass();
  313.                 this.cells.push(new Column({
  314.                     fc: this,
  315.                     type: "cell",
  316.                     cardOffsetY: 30,
  317.                     slot: {
  318.                         parentPanel: parentPanel,
  319.                         style: {
  320.                             x: this.getAspectRatioValues(aspectRatio, "cells", i),
  321.                             y: 100,
  322.                             zIndex: 0
  323.                         },
  324.                         visible: true,
  325.                         draggable: false,
  326.                         droppable: true
  327.                     }
  328.                 }));
  329.                 aspectRatio = this.getAspectRatioClass();
  330.                 this.foundations.push(new Column({
  331.                     fc: this,
  332.                     type: "foundation",
  333.                     slot: {
  334.                         parentPanel: parentPanel,
  335.                         style: {
  336.                             x: this.getAspectRatioValues(aspectRatio, "foundations", i),
  337.                             y: 100,
  338.                             zIndex: 0
  339.                         },
  340.                         visible: true,
  341.                         draggable: false,
  342.                         droppable: true
  343.                     }
  344.                 }));
  345.             }
  346.             this.setAspectRatio();
  347.             $.Schedule(3, this.setAspectRatioSchedule.bind(this));
  348.         },
  349.         setAspectRatioSchedule: function() {
  350.             var aspectRatioClass = this.getAspectRatioClass();
  351.             if (this.currentAspectRatio != aspectRatioClass) {
  352.                 this.currentAspectRatio = aspectRatioClass;
  353.                 this.setAspectRatio();
  354.             }
  355.             $.Schedule(3, this.setAspectRatioSchedule.bind(this));
  356.         },
  357.         setAspectRatio: function() {
  358.             var self = this;
  359.             var aspectRatioClass = this.getAspectRatioClass();
  360.             ["columns", "cells", "foundations"].forEach(function(c) {
  361.                 self[c].forEach(function(column, i) {
  362.                     column.slot.style.x(self.getAspectRatioValues(aspectRatioClass, c, i));
  363.                     column.render();
  364.                 });
  365.             });
  366.         },
  367.         getAspectRatioClass: function() {
  368.             var aspectRatioClasses = Object.keys(this.aspectRatios);
  369.             for (var i = 0; i < aspectRatioClasses.length; i++) {
  370.                 var aspectRatioClass = aspectRatioClasses[i];
  371.                 if ($.GetContextPanel().GetParent().GetParent().GetParent().BHasClass(aspectRatioClass)) return aspectRatioClass;
  372.             }
  373.  
  374.         },
  375.         getAspectRatioValues: function(aspectRatioClass, column, index) {
  376.             var aspectRatio = this.aspectRatios[aspectRatioClass][column];
  377.             return aspectRatio[0] + aspectRatio[1] * index;
  378.         },
  379.         setSeed: function(seed) {
  380.             this.seed = seed;
  381.         },
  382.         getSeed: function() {
  383.             return this.seed;
  384.         },
  385.         rand: function() {
  386.             this.setSeed((this.getSeed() * 214013 + 2531011) & 0x7FFFFFFF);
  387.             return ((this.getSeed() >> 16) & 0x7fff);
  388.         },
  389.         maxRand: function(mymax) {
  390.             return this.rand() % mymax;
  391.         },
  392.         shuffle: function(deck) {
  393.             if (deck.length) {
  394.                 var i = deck.length;
  395.                 while (--i) {
  396.                     var j = this.maxRand(i + 1);
  397.                     var tmp = deck[i];
  398.                     deck[i] = deck[j];
  399.                     deck[j] = tmp;
  400.                 }
  401.             }
  402.             return deck;
  403.         },
  404.         deal: function(seed) {
  405.             var deck = [];
  406.             this.setSeed(seed);
  407.             this.columns.concat(this.cells).concat(this.foundations).forEach(function(column) {
  408.                 column.items.length(0);
  409.             });
  410.             this.history.length = 0;
  411.             for (var i = 0; i < 52; i++) {
  412.                 var rank = "a23456789tjqk".charAt(Math.floor(i / 4));
  413.                 var suit = "cdhs".charAt(i % 4);
  414.                 deck.push(rank + suit);
  415.             }
  416.             deck = this.shuffle(deck).reverse();
  417.             for (var i = 0; i < deck.length; i++) {
  418.                 var card = this.deck.cards[deck[i]];
  419.                 card.draggable(true);
  420.                 this.columns[i % 8].add(card);
  421.             }
  422.             this.columns.forEach(function(column) {
  423.                 column.render();
  424.             });
  425.         },
  426.         getMaxStackSize: function(toEmptyColumn) {
  427.             var emptyCellCount = 1;
  428.             var emptyColumnCount = 0;
  429.  
  430.             this.cells.forEach(function(cell) {
  431.                 if (cell.isEmpty()) emptyCellCount++;
  432.             });
  433.             this.columns.forEach(function(column) {
  434.                 if (column.isEmpty()) emptyColumnCount++;
  435.             });
  436.             if (toEmptyColumn) emptyColumnCount--;
  437.             return emptyCellCount * Math.pow(2, emptyColumnCount);
  438.         },
  439.         getRankValue: function(card) {
  440.             switch (card.rank()) {
  441.                 case 'a':
  442.                     return 1;
  443.                     break;
  444.                 case 't':
  445.                     return 10;
  446.                     break;
  447.                 case 'j':
  448.                     return 11;
  449.                     break;
  450.                 case 'q':
  451.                     return 12;
  452.                     break;
  453.                 case 'k':
  454.                     return 13;
  455.                     break;
  456.                 default:
  457.                     return parseInt(card.rank());
  458.                     break;
  459.             }
  460.         },
  461.         isOrdered: function(cards) {
  462.             for (var i = 0; i < cards.length - 1; i++) {
  463.                 var a = cards[i];
  464.                 var b = cards[i + 1];
  465.                 if (a.suitColor() == b.suitColor() || this.compareRank(b, a) != -1) {
  466.                     return false;
  467.                 }
  468.             }
  469.             return true;
  470.         },
  471.         undo: function() {
  472.             var action = this.history.pop();
  473.             if (action) {
  474.                 var card = action.card
  475.                 card.container.moveStack(card, action.column, true);
  476.                 action.column.render();
  477.                 if (action.toFoundation) {
  478.                     card.draggable(true);
  479.                     this.undo();
  480.                 }
  481.             }
  482.         },
  483.         defeatCheck: function() {
  484.             var bValidMoveRemaining = false;
  485.             var cellsandcolumns = this.columns.concat(this.cells);
  486.             // check if empty cell or column
  487.             for (var i = 0; i < cellsandcolumns.length; i++) {
  488.                 if (cellsandcolumns[i].isEmpty()) return true;
  489.             }
  490.             // check if valid move between stacks
  491.             for (var i = 0; i < this.columns.length; i++) {
  492.                 var column = this.columns[i];
  493.                 for (var j = 0; j < column.length(); j++) {
  494.                     var card = column.get(j);
  495.                     for (var k = 0; k < this.columns.length; k++) {
  496.                         if (k == i) continue;
  497.                         var destCard = this.columns[k].last();
  498.                         var stackSlice = column.getStack(card);
  499.                         if (!this.isOrdered(stackSlice)) break;
  500.                         if (card.isValidMove(card, destCard)) return true;
  501.                     }
  502.                 };
  503.             }
  504.             // check if valid move to cell to stack
  505.             for (var i = 0; i < this.cells.length; i++) {
  506.                 var card = this.cells[i].last();
  507.                 for (var j = 0; j < column.length(); j++) {
  508.                     var destCard = this.columns[j].last();
  509.                     if (card.isValidMove(card, destCard)) return true;
  510.                 };
  511.             }
  512.             // check if valid move to foundation
  513.             for (var i = 0; i < cellsandcolumns.length; i++) {
  514.                 var column = cellsandcolumns[i];
  515.                 var card = column.last();
  516.                 for (var j = 0; j < this.foundations.length; j++) {
  517.                     var foundationCard = this.foundations[j].last();
  518.                     if (foundationCard &&
  519.                         this.getRankValue(foundationCard) == this.getRankValue(card) - 1 &&
  520.                         foundationCard.suit() == card.suit()) {
  521.                         return true;
  522.                     }
  523.                 }
  524.             }
  525.             OnLose();
  526.         },
  527.         victoryCheck: function() {
  528.             for (var i = 0; i < this.foundations.length; i++) {
  529.                 var foundation = this.foundations[i];
  530.                 var foundationCard = foundation.last();
  531.                 if (!foundationCard || this.getRankValue(foundationCard) != 13) return false;
  532.             }
  533.             OnPlayAgain();
  534.         },
  535.         process: function() {
  536.             var self = this;
  537.             var repeat = false;
  538.             this.columns.concat(this.cells).forEach(function(column, j) {
  539.                 if (!column.isEmpty()) {
  540.                     var card = column.last();
  541.                     var rankValue = self.getRankValue(card);
  542.                     var destFoundation;
  543.                     var isReady = 0;
  544.                     for (var i = 0; i < self.foundations.length; i++) {
  545.                         var foundation = self.foundations[i];
  546.                         var foundationCard = foundation.last();
  547.                         if ((rankValue == 1 && !foundationCard) ||
  548.                             (rankValue == 2 && foundationCard && foundationCard.suit() == card.suit())) {
  549.                             destFoundation = foundation;
  550.                             isReady = 2;
  551.                             break;
  552.                         } else if (rankValue > 2 && foundationCard) {
  553.                             if (self.getRankValue(foundationCard) == rankValue - 1 &&
  554.                                 foundationCard.suit() == card.suit()) {
  555.                                 destFoundation = foundation;
  556.                             } else if (self.getRankValue(foundationCard) >= rankValue - 1 &&
  557.                                 foundationCard.suitColor() != card.suitColor()) {
  558.                                 isReady++;
  559.                             }
  560.                         }
  561.                     }
  562.  
  563.                     if (destFoundation && isReady == 2) {
  564.                         //$.Msg("process moving card ", card.value());
  565.                         column.moveStack(card, destFoundation);
  566.                         destFoundation.render();
  567.                         repeat = true;
  568.                     }
  569.                 }
  570.             });
  571.             if (repeat) {
  572.                 this.process();
  573.             } else {
  574.                 this.victoryCheck();
  575.                 this.defeatCheck();
  576.             }
  577.         },
  578.         compareRank: function(a, b) {
  579.             return this.getRankValue(a) - this.getRankValue(b);
  580.         },
  581.         getColumn: function(i) {
  582.             return this.columns[i];
  583.         },
  584.         getCell: function(i) {
  585.             return this.cells[i];
  586.         },
  587.         getFoundation: function(i) {
  588.             return this.foundations[i];
  589.         }
  590.     });
  591.  
  592.     function FreeCellCard(options) {
  593.         this.fc = options.fc;
  594.         CardLibrary.Card.call(this, options);
  595.     }
  596.     _.inherits(FreeCellCard, CardLibrary.Card);
  597.     _.extend(FreeCellCard.prototype, {
  598.         isValidMove: function(draggedCard, card) {
  599.             var column = card.container;
  600.             var columnDragged = draggedCard.container;
  601.             var stackSlice = columnDragged.getStack(draggedCard);
  602.             var maxStackSize = this.fc.getMaxStackSize();
  603.             var columnType = column.type();
  604.             if (columnType == "column" &&
  605.                 this.fc.isOrdered([card, draggedCard]) &&
  606.                 this.fc.isOrdered(stackSlice) &&
  607.                 stackSlice.length <= maxStackSize) {
  608.                 //$.Msg("valid move to column");
  609.                 return true;
  610.             } else if (columnType == "foundation" &&
  611.                 draggedCard.suit() == card.suit() &&
  612.                 this.fc.compareRank(draggedCard, card) == 1 &&
  613.                 stackSlice.length == 1) {
  614.                 //$.Msg("valid move to foundation");
  615.                 return true;
  616.             }
  617.             //$.Msg("invalid move");
  618.             return false;
  619.         },
  620.         OnDragEnter: function(a, draggedPanel, card) {
  621.             //$.Msg("FreeCellCard Card OnDragEnter", draggedPanel, " ", card.value());
  622.             var draggedCard = draggedPanel.card;
  623.             card.droppable(this.isValidMove(draggedCard, card));
  624.             CardLibrary.Card.prototype.OnDragEnter.apply(this, arguments);
  625.         },
  626.         OnDragDrop: function(panelId, draggedPanel, card) {
  627.             //$.Msg("FreeCellCard Card OnDragDrop", panelId, " ", draggedPanel, " ", card.value());
  628.             var column = card.container;
  629.             var draggedCard = draggedPanel.card;
  630.             var columnDragged = draggedCard.container;
  631.             //if (columnDragged.type() == "foundation") return;
  632.             if (this.isValidMove(draggedCard, card)) {
  633.                 columnDragged.moveStack(draggedCard, column);
  634.                 column.render();
  635.             }
  636.         },
  637.         //OnDragLeave: function (panelId, draggedPanel, card) {};
  638.         OnDragStart: function(panelId, dragCallbacks, card) {
  639.             //$.Msg("FreeCellCard OnDragStart", this.value());
  640.             var column = card.container;
  641.             var stackSlice = column.getStack(card);
  642.             if (this.fc.isOrdered(stackSlice)) {
  643.                 CardLibrary.Card.prototype.OnDragStart.apply(this, arguments);
  644.             }
  645.         },
  646.         OnDragEnd: function(panelId, draggedPanel, card) {
  647.             this.fc.process();
  648.             Game.EmitSound("Card.cardSlide1");
  649.             CardLibrary.Card.prototype.OnDragEnd.apply(this, arguments);
  650.         },
  651.         OnDblClick: function(card) {
  652.             //$.Msg("FreeCellCard Card OnDblClick ", card.value());
  653.             var column = card.container;
  654.             if (column.last() == card) {
  655.                 for (var i = 0; i < this.fc.cells.length; i++) {
  656.                     var cell = this.fc.cells[i];
  657.                     if (cell.isEmpty()) {
  658.                         column.moveStack(card, cell);
  659.                         cell.render()
  660.                             //$.Msg("move to cell", i);
  661.                         break;
  662.                     }
  663.                 }
  664.             }
  665.             this.fc.process();
  666.         },
  667.         OnMouseOver: function(card) {
  668.             if (card.container.type() == "foundation") return;
  669.             var column = card.container;
  670.             var stackSlice = column.getStack(card);
  671.             if (this.fc.isOrdered(stackSlice)) {
  672.                 CardLibrary.Card.prototype.OnMouseOver.apply(this, arguments);
  673.             }
  674.         },
  675.         OnMouseOut: function(card) {
  676.             //$.Msg("FreeCellCard OnMouseOut ", card.value());
  677.             if (this.fc.rightClickedCard) {
  678.                 this.fc.rightClickedCard.style.zIndex(this.fc.rightClickedCard.style.zIndex());
  679.                 this.fc.rightClickedCard = null;
  680.             }
  681.             CardLibrary.Card.prototype.OnMouseOut.apply(this, arguments);
  682.         },
  683.         OnContextMenu: function(card) {
  684.             //$.Msg("FreeCellCard OnContextMenu ", card.value());
  685.             if (this.fc.rightClickedCard) this.fc.rightClickedCard.style.zIndex(this.fc.rightClickedCard.style.zIndex());
  686.             this.fc.rightClickedCard = card;
  687.             card.panel.style.zIndex = 999;
  688.         }
  689.     });
  690.  
  691.     function Column(options) {
  692.         this.fc = options.fc;
  693.         this.type = _.observable(options.type);
  694.         CardLibrary.Pile.call(this, options);
  695.     }
  696.     _.inherits(Column, CardLibrary.Pile);
  697.     _.extend(Column.prototype, {
  698.         OnDragEnter: function(a, draggedPanel, card) {
  699.             //$.Msg("Column Column OnDragDrop");
  700.             var draggedCard = draggedPanel.card;
  701.             //$.Msg("dragenter ", draggedCard.value(), " onto empty column ", this.type());
  702.             this.slot.droppable(this.isValidMove(draggedCard));
  703.             CardLibrary.Pile.prototype.OnDragEnter.apply(this, arguments);
  704.         },
  705.         OnDragDrop: function(panelId, draggedPanel, card) {
  706.             //$.Msg("FreeCell Column OnDragDrop");
  707.             var draggedCard = draggedPanel.card;
  708.             //$.Msg("dropped ", draggedCard.value(), " onto empty column ", this.type());
  709.             if (this.isValidMove(draggedCard)) {
  710.                 draggedCard.container.moveStack(draggedCard, this);
  711.                 this.render();
  712.             }
  713.         },
  714.         OnMouseOver: function(card) {},
  715.         isValidMove: function(draggedCard) {
  716.             //$.Msg("isValidMove");
  717.             var column = this;
  718.             var columnDragged = draggedCard.container;
  719.             if (columnDragged.type() == "foundation") return false;
  720.             if (!column.isEmpty()) return false;
  721.             var stackSlice = columnDragged.getStack(draggedCard);
  722.             var maxStackSize = this.fc.getMaxStackSize(true);
  723.             var columnType = column.type();
  724.             if (columnType == "column" &&
  725.                 this.fc.isOrdered(stackSlice) &&
  726.                 stackSlice.length <= maxStackSize) {
  727.                 //$.Msg("valid move to empty column");
  728.                 return true;
  729.             } else if (columnType == "cell" &&
  730.                 columnDragged.type() != "cell" &&
  731.                 stackSlice.length == 1) {
  732.                 //$.Msg("valid move to empty cell");
  733.                 return true;
  734.             } else if (columnType == "foundation" &&
  735.                 this.fc.getRankValue(draggedCard) == 1 &&
  736.                 stackSlice.length == 1) {
  737.                 //$.Msg("valid move to empty foundation");
  738.                 return true;
  739.             }
  740.             //$.Msg("invalid move");
  741.             return false;
  742.         },
  743.         add: function(card) {
  744.             card.container = this;
  745.             this.items.push(card);
  746.         },
  747.         getStack: function(card, bSplice) {
  748.             var index = this.items.indexOf(card);
  749.             if (bSplice) {
  750.                 return this.items.splice(index, this.items.length() - index).items;
  751.             } else {
  752.                 return this.items.slice(index, this.items.length()).items;
  753.             }
  754.         },
  755.         moveStack: function(card, destColumn, bNoUndo) {
  756.             Game.EmitSound("Card.cardSlide1");
  757.             //$.Msg("Column moveStack");
  758.             if (!bNoUndo) {
  759.                 this.fc.history.push({
  760.                     card: card,
  761.                     column: card.container,
  762.                     toFoundation: destColumn.type() == "foundation"
  763.                 });
  764.             }
  765.             var stack = this.getStack(card, true);
  766.             stack.forEach(function(c) {
  767.                 destColumn.add(c);
  768.                 if (destColumn.type() == "foundation") c.draggable(false);
  769.             });
  770.         }
  771.     });
  772.  
  773.  
  774.     fc = new FreeCell();
  775.     fc.init();
  776.     $.Schedule(1, function() {
  777.         //fc.deal(2217);
  778.         OnNewGame();
  779.     });
  780.  
  781.     $.Msg("main.js");
  782. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement