Skip to content

Commit

Permalink
fix item diseapperance case (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpolletvillard authored Apr 26, 2024
1 parent 14d111d commit 63abb92
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 80 deletions.
9 changes: 4 additions & 5 deletions app/models/colyseus-models/player.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-inferrable-types */
import { ArraySchema, MapSchema, Schema, type } from "@colyseus/schema"
import GameState from "../../rooms/states/game-state"
import { IPlayer, Role, Title } from "../../types"
import type GameState from "../../rooms/states/game-state"
import type { IPlayer, Role, Title } from "../../types"
import { SynergyTriggers, UniqueShop } from "../../types/Config"
import { DungeonPMDO } from "../../types/enum/Dungeon"
import { BattleResult, Rarity } from "../../types/enum/Game"
Expand All @@ -15,7 +14,7 @@ import {
Pkm,
PkmDuos,
PkmFamily,
PkmProposition
type PkmProposition
} from "../../types/enum/Pokemon"
import { SpecialGameRule } from "../../types/enum/SpecialGameRule"
import { Synergy } from "../../types/enum/Synergy"
Expand All @@ -25,7 +24,7 @@ import { getFirstAvailablePositionInBench } from "../../utils/board"
import { pickNRandomIn, pickRandomIn } from "../../utils/random"
import { resetArraySchema, values } from "../../utils/schemas"
import { Effects } from "../effects"
import { IPokemonConfig } from "../mongo-models/user-metadata"
import type { IPokemonConfig } from "../mongo-models/user-metadata"
import PokemonFactory, { isInRegion } from "../pokemon-factory"
import { getPokemonData, PRECOMPUTED_REGIONAL_MONS } from "../precomputed"
import ExperienceManager from "./experience-manager"
Expand Down
1 change: 0 additions & 1 deletion app/models/colyseus-models/synergies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function computeSynergies(board: IPokemon[]): Map<Synergy, number> {
if (!dragonDoubleTypes.has(family))
dragonDoubleTypes.set(family, new Set())
dragonDoubleTypes.get(family)!.add(values(pkm.types)[1])
console.log("types", values(pkm.types))
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion app/models/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class Shop {
}

addRegionalPokemon(pkm: Pkm, player: Player) {
console.log("adding regional pokemon", pkm)
//logger.debug("adding regional pokemon", pkm)
const { rarity, stages } = getPokemonData(pkm)
const pool = this.getRegionalPool(rarity, player)
const entityNumber = getPoolSize(rarity, stages)
Expand Down
133 changes: 61 additions & 72 deletions app/rooms/commands/game-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ export class OnDragDropItemCommand extends Command<
}
> {
execute({ client, detail }) {
const commands = new Array<Command>()
const playerId = client.auth.uid
const message = {
updateBoard: true,
Expand All @@ -391,16 +390,13 @@ export class OnDragDropItemCommand extends Command<
message.updateBoard = false
message.updateItems = true

const item = detail.id
const { x, y, id: item } = detail

if (!player.items.includes(item) && !detail.bypass) {
if (!player.items.includes(item)) {
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}

const x = parseInt(detail.x)
const y = parseInt(detail.y)

const pokemon = player.getPokemonAt(x, y)

if (item === Item.METEORITE) {
Expand All @@ -423,11 +419,16 @@ export class OnDragDropItemCommand extends Command<
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}

const isBasicItem = BasicItems.includes(item)
const existingBasicItemToCombine = values(pokemon.items).find((i) =>
BasicItems.includes(i)
)

// check if full items and nothing to combine
if (
pokemon.items.size >= 3 &&
(BasicItems.includes(item) === false ||
values(pokemon.items).some((i) => BasicItems.includes(i)) === false)
(!isBasicItem || !existingBasicItemToCombine)
) {
client.send(Transfer.DRAG_DROP_FAILED, message)
return
Expand All @@ -442,70 +443,52 @@ export class OnDragDropItemCommand extends Command<
return
}

if (BasicItems.includes(item)) {
const itemToCombine = values(pokemon.items).find((i) =>
BasicItems.includes(i)
if (!isBasicItem && pokemon.items.has(item)) {
// prevent adding twitce the same completed item
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}

if (isBasicItem && existingBasicItemToCombine) {
const recipe = Object.entries(ItemRecipe).find(
([_result, recipe]) =>
(recipe[0] === existingBasicItemToCombine && recipe[1] === item) ||
(recipe[0] === item && recipe[1] === existingBasicItemToCombine)
)
if (itemToCombine) {
const recipe = Object.entries(ItemRecipe).find(
([result, recipe]) =>
(recipe[0] == itemToCombine && recipe[1] == item) ||
(recipe[0] == item && recipe[1] == itemToCombine)
)
if (recipe) {
const itemCombined = recipe[0] as Item

if (
itemCombined in SynergyGivenByItem &&
pokemon.types.has(SynergyGivenByItem[itemCombined])
) {
// prevent combining into a synergy stone on a pokemon that already has this synergy
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}
if (!recipe) {
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}

pokemon.items.delete(itemToCombine)
removeInArray(player.items, item)
const itemCombined = recipe[0] as Item

if (pokemon.items.has(itemCombined)) {
// pokemon already has the combined item so the second one pops off and go to player inventory
player.items.push(itemCombined)
} else {
const detail: IDragDropItemMessage = {
id: itemCombined,
x: pokemon.positionX,
y: pokemon.positionY,
bypass: true
}
commands.push(
new OnDragDropItemCommand().setPayload({
client: client,
detail: detail
})
)
}
}
} else {
pokemon.items.add(item)
removeInArray(player.items, item)
}
} else {
if (pokemon.items.has(item)) {
if (
itemCombined in SynergyGivenByItem &&
pokemon.types.has(SynergyGivenByItem[itemCombined])
) {
// prevent combining into a synergy stone on a pokemon that already has this synergy
client.send(Transfer.DRAG_DROP_FAILED, message)
return
}

pokemon.items.delete(existingBasicItemToCombine)
removeInArray(player.items, item)

if (pokemon.items.has(itemCombined)) {
// pokemon already has the combined item so the second one pops off and go to player inventory
player.items.push(itemCombined)
} else {
pokemon.items.add(item)
removeInArray(player.items, item)
pokemon.items.add(itemCombined)
}
} else {
pokemon.items.add(item)
removeInArray(player.items, item)
}

this.room.checkEvolutionsAfterItemAcquired(playerId, pokemon)

player.updateSynergies()

if (commands.length > 0) {
return commands
}
}
}

Expand Down Expand Up @@ -1050,8 +1033,8 @@ export class OnUpdatePhaseCommand extends Command<GameRoom> {
this.state.stageLevel === AdditionalPicksStages[0]
? this.room.additionalUncommonPool
: this.state.stageLevel === AdditionalPicksStages[1]
? this.room.additionalRarePool
: this.room.additionalEpicPool
? this.room.additionalRarePool
: this.room.additionalEpicPool
let remainingAddPicks = 8
this.state.players.forEach((player: Player) => {
if (!player.isBot) {
Expand Down Expand Up @@ -1090,8 +1073,8 @@ export class OnUpdatePhaseCommand extends Command<GameRoom> {
const fishingLevel = player.effects.has(Effect.PRIMORDIAL_SEA)
? 3
: player.effects.has(Effect.DRIZZLE)
? 2
: 1
? 2
: 1
const pkm = this.state.shop.fishPokemon(player, fishingLevel)
const fish = PokemonFactory.createPokemonFromName(pkm, player)
const x = getFirstAvailablePositionInBench(player.board)
Expand Down Expand Up @@ -1410,9 +1393,12 @@ export class OnUpdatePhaseCommand extends Command<GameRoom> {

const UNOWN_ENCOUNTER_CHANCE = 0.037
if (chance(UNOWN_ENCOUNTER_CHANCE)) {
setTimeout(() => {
client.send(Transfer.UNOWN_WANDERING)
}, Math.round((5 + 15 * Math.random()) * 1000))
setTimeout(
() => {
client.send(Transfer.UNOWN_WANDERING)
},
Math.round((5 + 15 * Math.random()) * 1000)
)
}

if (
Expand All @@ -1421,12 +1407,15 @@ export class OnUpdatePhaseCommand extends Command<GameRoom> {
) {
const nbPokemonsToSpawn = Math.ceil(this.state.stageLevel / 2)
for (let i = 0; i < nbPokemonsToSpawn; i++) {
setTimeout(() => {
client.send(
Transfer.POKEMON_WANDERING,
this.state.shop.pickPokemon(player, this.state)
)
}, 4000 + i * 400)
setTimeout(
() => {
client.send(
Transfer.POKEMON_WANDERING,
this.state.shop.pickPokemon(player, this.state)
)
},
4000 + i * 400
)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion app/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export interface IDragDropItemMessage {
x: number
y: number
id: Item
bypass?: boolean
}

export interface IDragDropCombineMessage {
Expand Down

0 comments on commit 63abb92

Please sign in to comment.