Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. Array.prototype.randomize = function () {
  2. let i = this.length;
  3. if (i === 0) return false;
  4. while (--i) {
  5. let j = Math.floor(Math.random() * (i + 1));
  6. let tempi = this[i];
  7. let tempj = this[j];
  8. this[i] = tempj;
  9. this[j] = tempi;
  10. }
  11. };
  12.  
  13. Array.prototype.toObject = function () {
  14. let o = {};
  15. for (let i = 0; i < this.length; i++) {
  16. o[this[i]] = '';
  17. }
  18. return o;
  19. };
  20.  
  21. function bindEvent(el, eventName, eventHandler) {
  22. if (el.addEventListener) {
  23. el.addEventListener(eventName, eventHandler, false);
  24. } else if (el.attachEvent) {
  25. el.attachEvent('on' + eventName, eventHandler);
  26. }
  27. }
  28.  
  29. function div(parent, className) {
  30. let r = document.createElement('div');
  31. r.className = className;
  32. parent.appendChild(r);
  33. return r;
  34. }
  35. let WordDisplay = (function () {
  36. let display = document.getElementById('display'),
  37. characters;
  38.  
  39. class WordDisplay {
  40. constructor(puzzle) {
  41. characters = [];
  42. while (display.hasChildNodes()) {
  43. display.removeChild(display.firstChild);
  44. }
  45. let word = div(display, 'word');
  46. for (let i = 0; i < puzzle.length; ++i) {
  47. if (puzzle[i] == ' ') {
  48. characters.push(div(display, 'space'));
  49. word = div(display, 'word');
  50. }
  51. else {
  52. characters.push(div(word, 'letter'));
  53. }
  54. //console.log(i, puzzle[i], word.children, characters.length);
  55. }
  56. div(display, 'clear');
  57. }
  58. showLetter(i, letter) {
  59. characters[i].innerHTML = letter;
  60. }
  61. }
  62. return WordDisplay;
  63. })();
  64. let Wheel = (function () {
  65. let wheel = document.getElementById('wheel'),
  66. wheelValues =
  67. [5000, 500, 900,
  68. 700, 300, 800,
  69. 550, 400, 500,
  70. 600, 350, 500,
  71. 900, 0, 650,
  72. 300, 700, 400,
  73. 800, 500, 450,
  74. 500, 300, 0],
  75. spinTimeout = false,
  76. spinModifier = function () {
  77. return Math.random() * 10 + 20;
  78. },
  79. modifier = spinModifier(),
  80. slowdownSpeed = 0.5,
  81. prefix = (function () {
  82. if (document.body.style.MozTransform !== undefined) {
  83. return "MozTransform";
  84. } else if (document.body.style.WebkitTransform !== undefined) {
  85. return "WebkitTransform";
  86. } else if (document.body.style.OTransform !== undefined) {
  87. return "OTransform";
  88. } else {
  89. return "";
  90. }
  91. }()),
  92. degreeToRadian = function (deg) {
  93. return deg / (Math.PI * 180);
  94. };
  95.  
  96. function Wheel() {}
  97.  
  98. Wheel.prototype.rotate = function (degrees) {
  99. let val = "rotate(-" + degrees + "deg)";
  100. if (wheel.style[prefix] !== undefined) wheel.style[prefix] = val;
  101. let rad = degreeToRadian(degrees % 360),
  102. filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + rad + ", M12=-" + rad + ", M21=" + rad + ", M22=" + rad + ")";
  103. if (wheel.style.filter !== undefined) wheel.style.filter = filter;
  104. wheel.setAttribute("data-rotation", degrees);
  105. };
  106.  
  107. Wheel.prototype.addEventListener = function (eventName, eventHandler) {
  108. wheel.addEventListener(eventName, eventHandler, false);
  109. }
  110.  
  111. Wheel.prototype.spin = function (callback, amount) {
  112. let _this = this;
  113. clearTimeout(spinTimeout);
  114. modifier -= slowdownSpeed;
  115. if (amount === undefined) {
  116. amount = parseInt(wheel.getAttribute('data-rotation'), 10);
  117. }
  118. this.rotate(amount);
  119. if (modifier > 0) {
  120. spinTimeout = setTimeout(function () {
  121. _this.spin(callback, amount + modifier);
  122. }, 1000 / 5);
  123. } else {
  124. let dataRotation = parseInt(wheel.getAttribute('data-rotation'), 10);
  125. modifier = spinModifier();
  126. let divider = 360 / wheelValues.length;
  127. let offset = divider / 2; //half division
  128. let wheelValue = wheelValues[Math.floor(Math.ceil((dataRotation + offset) % 360) / divider)];
  129. switch (wheelValue) {
  130. case 0:
  131. return callback(0);
  132. case -1:
  133. return callback("Dodatkowy obrót kołem ");
  134. case -2:
  135. return callback("Tracisz kolejkę");
  136. default:
  137. return callback(wheelValue);
  138. }
  139. }
  140. };
  141.  
  142. return Wheel;
  143. })();
  144.  
  145. let store;
  146. function show(text, inputType) {
  147. let showPrompt = document.getElementById('show');
  148. let div = document.getElementById('content');
  149.  
  150. let textInput = document.querySelector("#box-input");
  151. let button = document.querySelector("#box-input-btn");
  152.  
  153. button.addEventListener("click", () =>{
  154. store = textInput.value;
  155. disable();
  156. });
  157.  
  158. showPrompt.style.display = "block";
  159. div.innerHTML = text;
  160. textInput.style.display = inputType;
  161. }
  162. function disable() {
  163. let showPrompt = document.getElementById('show');
  164. showPrompt.style.display = "none";
  165. }
  166. let wheelFortune = (function () {
  167.  
  168. let wheel = new Wheel(),
  169. vowels = ['A', 'E', 'I', 'O', 'U' , 'Y'],
  170. wordDisplay,
  171. spinWheel = document.getElementById('spin'),
  172. buyVowel = document.getElementById('vowel'),
  173. newButton = document.getElementById('newpuzzle'),
  174. cash = document.getElementById('cash'),
  175. solve = document.getElementById('solve');
  176.  
  177. function wheelFortune(puzzles) {
  178. let _this = this;
  179. this.puzzles = puzzles;
  180. this.puzzles.randomize();
  181. this.currentCash = 0;
  182. this.puzzleSolved = false;
  183.  
  184. bindEvent(buyVowel, "click", function () {
  185. if (_this.currentCash > 200) {
  186. if (_this.createGuessPrompt("wpisz samogłoskę", true) !== false) {
  187. _this.currentCash -= 200;
  188. _this.updateCash();
  189. }
  190. } else {
  191. show('Potrzebujesz 150 zł aby kupić samogłoskę', 'none');
  192. }
  193. });
  194. bindEvent(newButton, "click", function () {
  195. _this.newRound();
  196. });
  197. let spinTheWheel = function () {
  198. wheel.spin(function (valueSpun) {
  199. if (isNaN(valueSpun)) {
  200. alert(valueSpun);
  201. } else {
  202. //is a valid number
  203. if (valueSpun === 0) {
  204. show('Bankrut', 'none');
  205. _this.currentCash = 0;
  206. } else {
  207. //spun greater than 0
  208. let amountFound = _this.createGuessPrompt(valueSpun);
  209. _this.currentCash += (valueSpun * amountFound);
  210. }
  211. _this.updateCash();
  212. }
  213. });
  214. };
  215. bindEvent(spinWheel, "click", spinTheWheel);
  216. bindEvent(wheel, "click", spinTheWheel);
  217.  
  218. function arrays_equal(a, b) {
  219. return !(a < b || b < a);
  220. }
  221. bindEvent(solve, "click", function () {
  222. if (!_this.puzzleSolved) {
  223. show("Podaj rozwiązanie", 'block');
  224.  
  225. //let solution = prompt('bla','');
  226. let solution = store;
  227.  
  228.  
  229. //console.log(solve)
  230. // console.log(solve2)
  231.  
  232. if (solution) {
  233. guess = solution.toUpperCase().split("");
  234. if (arrays_equal(guess, _this.currentPuzzleArray)) {
  235. for (let i = 0; i < guess.length; ++i) {
  236. _this.guessLetter(guess[i], false, true);
  237. }
  238. }
  239. if (!_this.puzzleSolved) {
  240. show('Złe hasło, spróbuj ponownie. Pamietaj o znakach diakrytycznych(ą, ę, ć itd.)', 'none');
  241. }
  242. }
  243. }
  244. });
  245. this.startRound(0); //start the 1st round
  246. }
  247.  
  248. wheelFortune.prototype.updateCash = function () {
  249. cash.innerHTML = this.currentCash;
  250. };
  251.  
  252. wheelFortune.prototype.guessLetter = function (guess, isVowel, solvingPuzzle) {
  253. let timesFound = 0;
  254. solvingPuzzle = solvingPuzzle === undefined ? false : true;
  255. //find it:
  256. if (guess.length && !this.puzzleSolved) {
  257. if (!solvingPuzzle && !isVowel && (guess in vowels.toObject())) {
  258. show('Nie możesz zgadywać teraz samogłosek', 'none');
  259. return false;
  260. }
  261. if (!solvingPuzzle && isVowel && !(guess in vowels.toObject())) {
  262. show('Nie możesz zgadywać teraz spółgłosek', 'none');
  263. return false;
  264. }
  265. if (guess in this.guessedArray) {
  266. return 0;
  267. }
  268. for (let i = 0; i < this.currentPuzzleArray.length; ++i) {
  269. if (guess == this.currentPuzzleArray[i]) {
  270. wordDisplay.showLetter(i, guess);
  271. ++timesFound;
  272. }
  273. }
  274. if (timesFound > 0) {
  275. this.guessedArray.push(guess);
  276. if (this.guessedArray.length == this.lettersInPuzzle.length) {
  277. show('BRAWOOOO odgadłeś hasło 🎉🎉🎉', 'none');
  278. this.puzzleSolved = true;
  279. }
  280. }
  281. return timesFound;
  282. }
  283. return false;
  284.  
  285. };
  286.  
  287. let guessTimes = 0;
  288. wheelFortune.prototype.createGuessPrompt = function (valueSpun, isVowel) {
  289. isVowel = isVowel === undefined ? false : true;
  290. if (!this.puzzleSolved) {
  291. let letter;
  292. if (isVowel) {
  293. letter = prompt("Wpisz samogłoskę", "");
  294. } else {
  295. letter = prompt("Wylosowałeś " + valueSpun + " wpisz spółgłoskę", "");
  296. }
  297. if (letter) {
  298. let guess = letter.toUpperCase().charAt(0);
  299. let timesFound = this.guessLetter(guess, isVowel);
  300. if (timesFound === false) {
  301. ++guessTimes;
  302. if (guessTimes < 5) {
  303. return this.createGuessPrompt(valueSpun, isVowel);
  304. }
  305. }
  306. guessTimes = 0;
  307. return timesFound;
  308. } else {
  309. ++guessTimes;
  310. if (guessTimes < 5) {
  311. return this.createGuessPrompt(valueSpun, isVowel);
  312. } else {
  313. // reset guessTimes
  314. guessTimes = 0;
  315. }
  316. }
  317. }
  318. return false;
  319. };
  320.  
  321. wheelFortune.prototype.newRound = function () {
  322. let round = ++this.round;
  323. if (round < this.puzzles.length) {
  324. this.startRound(round);
  325. } else {
  326. show('brak!', 'none');
  327. }
  328. };
  329.  
  330. wheelFortune.prototype.startRound = function (round) {
  331. this.round = round;
  332. this.lettersInPuzzle = [];
  333. this.guessedArray = [];
  334. this.puzzleSolved = false;
  335. this.currentPuzzle = this.puzzles[this.round].toUpperCase();
  336. this.currentPuzzleArray = this.currentPuzzle.split("");
  337. for (let i = 0; i < this.currentPuzzleArray.length; i++) {
  338. if (!(this.currentPuzzleArray[i] in this.lettersInPuzzle)) {
  339. this.lettersInPuzzle.push(this.currentPuzzleArray[i]);
  340. }
  341. }
  342. wordDisplay = new WordDisplay(this.currentPuzzleArray);
  343.  
  344. };
  345.  
  346. return wheelFortune;
  347. })();
  348.  
  349. let Game = new wheelFortune([
  350. "reforma walutowa","reforma monetarna","bank polski","katarzyna lewandowska","Borów","reforma systemu podatkowego","grabski",
  351. "halina brzezinska","książka idea polski","reforma rolna","Powązki Warszawskie","grabski","grabski"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement