Skip to content

Commit

Permalink
rework wonderbox (#375)
Browse files Browse the repository at this point in the history
added random utils
  • Loading branch information
sylvainpolletvillard authored Feb 11, 2023
1 parent 7a5503d commit 1e2a2a3
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 109 deletions.
12 changes: 3 additions & 9 deletions app/core/attack-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Synergy } from "../types/enum/Synergy"
import { Ability, AbilityStrategy } from "../types/enum/Ability"
import PokemonFactory from "../models/pokemon-factory"
import { Pkm } from "../types/enum/Pokemon"
import{ pickRandomIn, shuffleArray } from "../utils/random"

export class AttackStrategy {
process(
Expand Down Expand Up @@ -3020,7 +3021,7 @@ export class TeleportStrategy extends AttackStrategy {
[7, 5],
[7, 0]
]
this.shuffleArray(potentialCells)
shuffleArray(potentialCells)

for (let i = 0; i < potentialCells.length; i++) {
const entity = board.getValue(potentialCells[i][0], potentialCells[i][1])
Expand All @@ -3037,13 +3038,6 @@ export class TeleportStrategy extends AttackStrategy {
}
}
}

shuffleArray(array: any[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
}
}

export class NastyPlotStrategy extends AttackStrategy {
Expand Down Expand Up @@ -3345,7 +3339,7 @@ export class MetronomeStrategy extends AttackStrategy {
EarthquakeStrategy,
SteamEruptionStrategy
]
const strategy = new skills[Math.floor(Math.random() * skills.length)]()
const strategy = new pickRandomIn(skills)
strategy.process(pokemon, state, board, target)
}
}
5 changes: 2 additions & 3 deletions app/core/pokemon-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IPokemonEntity, Transfer, FIGHTING_PHASE_DURATION } from "../types"
import { Synergy } from "../types/enum/Synergy"
import { Ability } from "../types/enum/Ability"
import { FlyingProtectThreshold } from "../types/Config"
import { pickRandomIn } from "../utils/random"

export default class PokemonState {
handleHeal(
Expand Down Expand Up @@ -664,9 +665,7 @@ export default class PokemonState {
}
})
if (candidatesCoordinates.length > 0) {
return candidatesCoordinates[
Math.floor(Math.random() * candidatesCoordinates.length)
]
return pickRandomIn(candidatesCoordinates)
} else {
return undefined
}
Expand Down
29 changes: 16 additions & 13 deletions app/core/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Schema, MapSchema, type } from "@colyseus/schema"
import PokemonEntity from "./pokemon-entity"
import PokemonFactory from "../models/pokemon-factory"
import { Pokemon } from "../models/colyseus-models/pokemon"
import { BasicItems, Item } from "../types/enum/Item"
import { Item } from "../types/enum/Item"
import { Effect } from "../types/enum/Effect"
import { Climate, PokemonActionState, Stat } from "../types/enum/Game"
import Dps from "./dps"
Expand All @@ -16,6 +16,7 @@ import { Pkm } from "../types/enum/Pokemon"
import { ItemStats } from "../types/Config"
import { getPath } from "../public/src/pages/utils/utils"
import GameRoom from "../rooms/game-room"
import { pickRandomIn } from "../utils/random"

export default class Simulation extends Schema implements ISimulation {
@type("string") climate: Climate = Climate.NEUTRAL
Expand Down Expand Up @@ -270,6 +271,17 @@ export default class Simulation extends Schema implements ISimulation {
}

applyItemsEffects(pokemon: PokemonEntity) {
// wonderbox should be applied first so that wonderbox items effects can be applied after
if (pokemon.items.has(Item.WONDER_BOX)) {
pokemon.items.delete(Item.WONDER_BOX)
const randomItems = ItemFactory.createWonderboxItems(pokemon.items)
randomItems.forEach(item => {
if (pokemon.items.size < 3) {
pokemon.items.add(item)
}
})
}

pokemon.items.forEach((item) => {
Object.entries(ItemStats[item]!).forEach(([stat, value]) =>
this.applyStat(pokemon, stat as Stat, value)
Expand All @@ -279,14 +291,7 @@ export default class Simulation extends Schema implements ISimulation {
if (pokemon.items.has(Item.SOUL_DEW)) {
pokemon.status.triggerSoulDew(2000)
}
if (pokemon.items.has(Item.WONDER_BOX)) {
pokemon.items.delete(Item.WONDER_BOX)
for (let i = 0; i < 2; i++) {
if (pokemon.items.size < 3) {
pokemon.items.add(ItemFactory.createRandomItem())
}
}
}

if (pokemon.items.has(Item.AQUA_EGG)) {
pokemon.setMana(pokemon.mana + pokemon.maxMana / 2)
}
Expand Down Expand Up @@ -320,8 +325,7 @@ export default class Simulation extends Schema implements ISimulation {
1
)
})
const blueIronDefensePkm =
blueIronDefense[Math.floor(Math.random() * blueIronDefense.length)]
const blueIronDefensePkm = pickRandomIn(blueIronDefense)
blueIronDefensePkm.addAttack(blueIronDefensePkm.atk)
blueIronDefensePkm.effects.push(Effect.IRON_DEFENSE)
}
Expand All @@ -336,8 +340,7 @@ export default class Simulation extends Schema implements ISimulation {
1
)
})
const redIronDefensePkm =
redIronDefense[Math.floor(Math.random() * redIronDefense.length)]
const redIronDefensePkm = pickRandomIn(redIronDefense)
redIronDefensePkm.addAttack(redIronDefensePkm.atk)
redIronDefensePkm.effects.push(Effect.IRON_DEFENSE)
}
Expand Down
5 changes: 3 additions & 2 deletions app/core/tileset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Mask,
TerrainType
} from "../types/Config"
import { pickRandomIn } from "../utils/random"

export type TilesetTiled = {
columns: number
Expand Down Expand Up @@ -111,7 +112,7 @@ export default class Tileset {
// console.log(items);
if (items && items.length > 0) {
if (Math.random() > 0.8) {
return items[Math.floor(Math.random() * items.length)]
return pickRandomIn(items)
} else {
return this.ground.get(maskId)![0]
}
Expand All @@ -126,7 +127,7 @@ export default class Tileset {
// console.log(items);
if (items && items.length > 0) {
if (Math.random() > 0.8) {
return items[Math.floor(Math.random() * items.length)]
return pickRandomIn(items)
} else {
return this.wall.get(maskId)![0]
}
Expand Down
44 changes: 19 additions & 25 deletions app/models/item-factory.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import { Item, BasicItems } from "../types/enum/Item"
import { Item, BasicItems, SynergyStones } from "../types/enum/Item"
import { SetSchema } from "@colyseus/schema"
import { pickRandomIn, pickNRandomIn } from "../utils/random"

export default class ItemFactory {
static createRandomItem() {
const keys = (Object.keys(Item) as Item[]).filter(
(i) => !BasicItems.includes(i)
)
return keys[Math.floor(Math.random() * keys.length)]
static createWonderboxItems(existingItems: SetSchema<Item>): Item[] {
const wonderboxItems: Item[] = [];
for(let n=0; n<2; n++){
const elligibleItems = (Object.keys(Item) as Item[]).filter(
(i) => !BasicItems.includes(i)
&& !SynergyStones.includes(i)
&& !wonderboxItems.includes(i)
&& !existingItems.has(i)
&& i !== Item.WONDER_BOX
)
wonderboxItems.push(pickRandomIn(elligibleItems))
}
return wonderboxItems
}

static createBasicRandomItem() {
return BasicItems[Math.floor(Math.random() * BasicItems.length)]
return pickRandomIn(BasicItems)
}

static createRandomItems() {
const b = BasicItems.slice()
ItemFactory.shuffleArray(b)
const items = new Array<Item>()
for (let i = 0; i < 3; i++) {
const p = b.pop()
if (p) {
items.push(p)
}
}
return items
return pickNRandomIn(BasicItems, 3)
}

static createSpecificItems(array: Item[]) {
return array[Math.floor(Math.random() * array.length)]
}

static shuffleArray(array: Item[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return pickRandomIn(array)
}
}
3 changes: 2 additions & 1 deletion app/models/pokemon-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ import PRECOMPUTED_TYPE_POKEMONS from "./precomputed/type-pokemons.json"
import { Synergy } from "../types/enum/Synergy"
import { Pkm, PkmFamily } from "../types/enum/Pokemon"
import { PkmCost } from "../types/Config"
import { pickRandomIn } from "../utils/random"

export default class PokemonFactory {
static getNeutralPokemonsByLevelStage(level: number): MapSchema<Pokemon> {
Expand Down Expand Up @@ -1591,7 +1592,7 @@ export default class PokemonFactory {
}
)
if (possibleFossils.length > 0) {
return possibleFossils[Math.floor(Math.random() * possibleFossils.length)]
return pickRandomIn(possibleFossils)
} else {
return Pkm.CARBINK
}
Expand Down
55 changes: 14 additions & 41 deletions app/models/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Player from "./colyseus-models/player"
import { IPokemon } from "../types"
import { Probability } from "../types/Config"
import { Rarity } from "../types/enum/Game"
import { pickRandomIn } from "../utils/random"

export default class Shop {
COMMON: Pkm[]
Expand Down Expand Up @@ -226,9 +227,7 @@ export default class Shop {
assignFirstMythicalShop(player: Player) {
const mythicalCopy = JSON.parse(JSON.stringify(this.MYTHICAL_1))
for (let i = 0; i < 6; i++) {
const pkm = PokemonFactory.createPokemonFromName(
mythicalCopy[Math.floor(Math.random() * mythicalCopy.length)]
).name
const pkm = PokemonFactory.createPokemonFromName(pickRandomIn(mythicalCopy)).name
mythicalCopy.splice(mythicalCopy.indexOf(pkm), 1)
player.shop[i] = pkm
}
Expand All @@ -237,9 +236,7 @@ export default class Shop {
assignSecondMythicalShop(player: Player) {
const mythicalCopy = JSON.parse(JSON.stringify(this.MYTHICAL_2))
for (let i = 0; i < 6; i++) {
const pkm = PokemonFactory.createPokemonFromName(
mythicalCopy[Math.floor(Math.random() * mythicalCopy.length)]
).name
const pkm = PokemonFactory.createPokemonFromName(pickRandomIn(mythicalCopy)).name
mythicalCopy.splice(mythicalCopy.indexOf(pkm), 1)
player.shop[i] = pkm
}
Expand All @@ -250,11 +247,6 @@ export default class Shop {
const seed = Math.random()
let pokemon = Pkm.MAGIKARP
let threshold = 0
const common = new Array<Pkm>()
const uncommon = new Array<Pkm>()
const rare = new Array<Pkm>()
const epic = new Array<Pkm>()
const legendary = new Array<Pkm>()
const finals = new Array<Pkm>()

player.board.forEach((pokemon: IPokemon) => {
Expand All @@ -263,49 +255,30 @@ export default class Shop {
}
})

this.COMMON.forEach((name: Pkm) => {
if (!finals.includes(name)) {
common.push(name)
}
})
this.UNCOMMON.forEach((name: Pkm) => {
if (!finals.includes(name)) {
uncommon.push(name)
}
})
this.RARE.forEach((name: Pkm) => {
if (!finals.includes(name)) {
rare.push(name)
}
})
this.EPIC.forEach((name: Pkm) => {
if (!finals.includes(name)) {
epic.push(name)
}
})
this.LEGENDARY.forEach((name: Pkm) => {
if (!finals.includes(name)) {
legendary.push(name)
}
})
const common = this.COMMON.filter((name: Pkm) => !finals.includes(name))
const uncommon = this.UNCOMMON.filter((name: Pkm) => !finals.includes(name))
const rare = this.RARE.filter((name: Pkm) => !finals.includes(name))
const epic = this.EPIC.filter((name: Pkm) => !finals.includes(name))
const legendary = this.LEGENDARY.filter((name: Pkm) => !finals.includes(name))

for (let i = 0; i < playerProbality.length; i++) {
threshold += playerProbality[i]
if (seed < threshold) {
switch (i) {
case 0:
pokemon = common[Math.floor(Math.random() * common.length)]
pokemon = pickRandomIn(common)
break
case 1:
pokemon = uncommon[Math.floor(Math.random() * uncommon.length)]
pokemon = pickRandomIn(uncommon)
break
case 2:
pokemon = rare[Math.floor(Math.random() * rare.length)]
pokemon = pickRandomIn(rare)
break
case 3:
pokemon = epic[Math.floor(Math.random() * epic.length)]
pokemon = pickRandomIn(epic)
break
case 4:
pokemon = legendary[Math.floor(Math.random() * legendary.length)]
pokemon = pickRandomIn(legendary)
break
default:
console.log(
Expand Down
4 changes: 2 additions & 2 deletions app/rooms/commands/preparation-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Client } from "colyseus"
import PreparationRoom from "../preparation-room"
import { Emotion, IMessage, Role, Transfer } from "../../types"
import { BotDifficulty } from "../../types/enum/Game"
import PreparationState from "../states/preparation-state"
import { pickRandomIn } from "../../utils/random"

export class OnJoinCommand extends Command<
PreparationRoom,
Expand Down Expand Up @@ -307,7 +307,7 @@ export class OnAddBotCommand extends Command<PreparationRoom, OnAddBotPayload> {
return
}

const bot = bots[Math.floor(Math.random() * bots.length)]
const bot = pickRandomIn(bots)

if (this.state.users.size >= 8) {
return
Expand Down
11 changes: 2 additions & 9 deletions app/rooms/game-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
IDragDropCombineMessage,
IDragDropItemMessage,
IDragDropMessage,
IPlayer,
IPokemon,
Transfer
} from "../types"
Expand All @@ -43,6 +42,7 @@ import { components } from "../api-v1/openapi"
import { Title, Role } from "../types"
import PRECOMPUTED_TYPE_POKEMONS from "../models/precomputed/type-pokemons.json"
import BannedUser from "../models/mongo-models/banned-user"
import { shuffleArray } from "../utils/random"

export default class GameRoom extends Room<GameState> {
dispatcher: Dispatcher<this>
Expand All @@ -55,13 +55,6 @@ export default class GameRoom extends Room<GameState> {
this.additionalPokemonsPool = new Array<Pkm>()
}

shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
}

// When room is initialized
onCreate(options: {
users: { [key: string]: IGameUser }
Expand All @@ -79,7 +72,7 @@ export default class GameRoom extends Room<GameState> {
}
})
})
this.shuffleArray(this.additionalPokemonsPool)
shuffleArray(this.additionalPokemonsPool)

this.maxClients = 8
for (const id in options.users) {
Expand Down
Loading

0 comments on commit 1e2a2a3

Please sign in to comment.