1 /******************************************************************************
  2  *
  3  *  Copyright 2017 Paphus Solutions Inc.
  4  *
  5  *  Licensed under the Eclipse Public License, Version 1.0 (the "License");
  6  *  you may not use this file except in compliance with the License.
  7  *  You may obtain a copy of the License at
  8  *
  9  *      http://www.eclipse.org/legal/epl-v10.html
 10  *
 11  *  Unless required by applicable law or agreed to in writing, software
 12  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  *  See the License for the specific language governing permissions and
 15  *  limitations under the License.
 16  *
 17  ******************************************************************************/
 18 
 19 /**
 20  * Bot Libre open gaming SDK.
 21  * This JavaScript SDK lets you integrate web games with your bot.
 22  * 
 23  * Version: 6.0.0-2017-10-16
 24  */
 25 
 26 /**
 27  * Example game implementation of Tic Tac Toe.
 28  * This Game interface can be used to create a new game class.
 29  * @class
 30  */
 31 function TicTacToe() {
 32 	this.listener;
 33 	
 34 	/**
 35 	 * Initialize the game with the WebChatbotListener.
 36 	 */
 37 	this.init = function(listener) {
 38 		this.listener = listener;
 39 	}
 40 	
 41 	this.moveSquare = function(cordinate) {
 42 	    this.listener.sendMessage(cordinate);
 43 	}
 44 
 45 	this.drawBoard = function(newBoard) {
 46 	    if (newBoard.length < 9) {
 47 	        return;
 48 	    }
 49 	    var div = document.getElementById(this.listener.prefix + "avatar-game-div");	    
 50 	    var html = "<table id='tictactoe'><tr>";
 51 	    for (var count = 0; count < 9; count++) {
 52 	        var character = newBoard.substring(count, count + 1);
 53 	        var cordinate = "A" + (count + 1);
 54 	        if (count > 2) {
 55 	            cordinate = "B" + (count - 2);
 56 	        }
 57 	        if (count > 5) {
 58 	            cordinate = "C" + (count - 5);
 59 	        }
 60 	        if (character == "X") {
 61 	            html = html + "<td class='X'>" + character + "</td>";
 62 	        }
 63 	        if (character == "O") {
 64 	            html = html + "<td class='O'>" + character + "</td>";
 65 	        }
 66 	        if (character == "_") {
 67 	        html = html + "<td class='blank'>" + character + "</td>";
 68 	        }
 69 	        if (count == 2 || count == 5) {
 70 	            html = html + "</tr><tr>";
 71 	        }
 72 	    }
 73 	    html = html + "</tr></table>";
 74 	    div.innerHTML = html;
 75     	var cells = div.getElementsByTagName("td");
 76     	var self = this;
 77 		for (var count = 0; count < cells.length; count++) {
 78 	        var coordinate = "A" + (count + 1);
 79 	        if (count > 2) {
 80 	        	coordinate = "B" + (count - 2);
 81 	        }
 82 	        if (count > 5) {
 83 	        	coordinate = "C" + (count - 5);
 84 	        }
 85 			var cell = cells[count];
 86 			var scope = function() {
 87 				var scopedCoordinate = coordinate;
 88 				cell.addEventListener("click", function(event) {
 89 					self.moveSquare(scopedCoordinate);
 90 					event.stopPropagation();
 91 					return true;
 92 				});
 93 			};
 94 			scope();
 95 		}
 96 	}
 97 
 98 	/**
 99 	 * Callback to let the game draw the new board from the bot's message command.
100 	 * The board is drawn on the "avatar-game-div" element.
101 	 */
102 	this.updateAvatar = function(responseMessage) {
103 		var command = JSON.parse(responseMessage.command);
104 	    console.log(command);
105 	    if (command == null || command.board == null) {
106 			var div = document.getElementById(this.listener.prefix + "avatar-game-div");
107 			if (div != null) {
108 				div.style.display = "none";
109 			}	    	
110 			return;
111 	    }
112 		this.drawBoard(command.board);
113 		var div = document.getElementById(this.listener.prefix + "avatar-video-div");
114 		if (div != null) {
115 			div.style.display = "none";
116 		}
117 		div = document.getElementById(this.listener.prefix + "avatar-canvas-div");
118 		if (div != null) {
119 			div.style.display = "none";
120 		}
121 		div = document.getElementById(this.listener.prefix + "avatar-image-div");
122 		if (div != null) {
123 			div.style.display = "none";
124 		}
125 	    var div = document.getElementById(this.listener.prefix + "avatar-game-div");
126 	    if (div != null) {
127 			div.style.display = "inline-block";
128 		}
129 	}
130 }
131