|
| 1 | + |
| 2 | +import { Reactive } from "@web/core/utils/reactive"; |
| 3 | +import { EventBus } from "@odoo/owl"; |
| 4 | +import { rewards } from "./click_rewards"; |
| 5 | +import { randomChoice } from "./utils"; |
| 6 | +import { CURRENT_VERSION } from "./clicker_migration"; |
| 7 | + |
| 8 | +export class ClickerModel extends Reactive { |
| 9 | + |
| 10 | + constructor() { |
| 11 | + super(); |
| 12 | + this.value = 0; |
| 13 | + this.level = 0; |
| 14 | + this.bus = new EventBus(); |
| 15 | + this.bots = { |
| 16 | + clickBots : { |
| 17 | + price: 1000, |
| 18 | + number: 0, |
| 19 | + level: 1, |
| 20 | + clicks: 10 |
| 21 | + }, |
| 22 | + bigBots : { |
| 23 | + price: 5000, |
| 24 | + number: 0, |
| 25 | + level: 2, |
| 26 | + clicks: 100 |
| 27 | + } |
| 28 | + } |
| 29 | + this.power = 1; |
| 30 | + this.fruits = { |
| 31 | + cherries: 0, |
| 32 | + pears: 0, |
| 33 | + peaches: 0 |
| 34 | + }; |
| 35 | + this.trees = { |
| 36 | + cherryTrees: { |
| 37 | + price: 1000000, |
| 38 | + number: 0, |
| 39 | + level: 4, |
| 40 | + fruit: "cherries" |
| 41 | + }, |
| 42 | + pearTrees: { |
| 43 | + price: 1000000, |
| 44 | + number: 0, |
| 45 | + level: 4, |
| 46 | + fruit: "pears" |
| 47 | + }, |
| 48 | + peachTree: { |
| 49 | + price: 1500000, |
| 50 | + level: 4, |
| 51 | + produce: "peaches", |
| 52 | + purchased: 0, |
| 53 | + } |
| 54 | + }; |
| 55 | + this.version_number = CURRENT_VERSION; |
| 56 | + } |
| 57 | + |
| 58 | + increment(inc) { |
| 59 | + this.value += inc; |
| 60 | + if (this.milestones[this.level] && this.value >= this.milestones[this.level].clicks) { |
| 61 | + this.bus.trigger("MILESTONE", this.milestones[this.level]); |
| 62 | + this.level++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + buyBot(botName) { |
| 67 | + const bot = this.bots[botName]; |
| 68 | + const clickBotPrice = bot.price; |
| 69 | + if (this.value < clickBotPrice) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + bot.number++; |
| 73 | + this.value-=clickBotPrice; |
| 74 | + }; |
| 75 | + |
| 76 | + clickBotsAction() { |
| 77 | + for(const bot in this.bots) { |
| 78 | + this.value += ( this.bots[bot].clicks * this.bots[bot].number ) * this.power; |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + buyPower() { |
| 83 | + const powerPrice = 50000; |
| 84 | + if (this.value < powerPrice) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + this.power++; |
| 88 | + this.value-=powerPrice; |
| 89 | + }; |
| 90 | + |
| 91 | + getReward() { |
| 92 | + const availableReward = []; |
| 93 | + for (const reward of rewards) { |
| 94 | + if (reward.minLevel <= this.level || !reward.minLevel) { |
| 95 | + if (reward.maxLevel >= this.level || !reward.maxLevel) { |
| 96 | + availableReward.push(reward); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + const reward = randomChoice(availableReward); |
| 101 | + this.bus.trigger("REWARD", reward); |
| 102 | + return reward; |
| 103 | + } |
| 104 | + |
| 105 | + buyTree(treeName) { |
| 106 | + const tree = this.trees[treeName]; |
| 107 | + const treePrice = tree.price; |
| 108 | + if (this.value < treePrice) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + tree.number++; |
| 112 | + this.value-=treePrice; |
| 113 | + }; |
| 114 | + |
| 115 | + treesAction() { |
| 116 | + for(const tree in this.trees) { |
| 117 | + this.fruits[this.trees[tree].fruit] += this.trees[tree].number; |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + toJSON() { |
| 122 | + const json = Object.assign({}, this); |
| 123 | + delete json["bus"]; |
| 124 | + return json; |
| 125 | + } |
| 126 | + |
| 127 | + static fromJSON(json) { |
| 128 | + const clicker = new ClickerModel(); |
| 129 | + const clickerInstance = Object.assign(clicker, json); |
| 130 | + return clickerInstance; |
| 131 | + } |
| 132 | + |
| 133 | + get milestones() { |
| 134 | + return [ |
| 135 | + { clicks: 1000, bot: "ClickBot" }, |
| 136 | + { clicks: 5000, bot: "BigBot" }, |
| 137 | + { clicks: 100000, bot: "Power" }, |
| 138 | + { clicks: 1000000, bot: "Trees and Fruits" } |
| 139 | + ] |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments