Skip to content

Commit

Permalink
fix(utils): clashing server and browser logging utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Zielak committed Feb 20, 2024
1 parent ae8057a commit c62dd04
Show file tree
Hide file tree
Showing 27 changed files with 604 additions and 495 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
"no-extra-semi": "off",
"no-undef": "off",
"no-unused-vars": "off",
"no-fallthrough": "off",
},
overrides: [
{
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"dependencies": {
"@cardsgame/entity-traits": "^1.7.2",
"@cardsgame/types": "^1.7.1",
"@cardsgame/utils": "^1.7.2"
"@cardsgame/utils": "^1.7.2",
"chalk": "^4.1.2"
},
"peerDependencies": {
"@colyseus/core": "^0.14.36",
Expand Down
19 changes: 11 additions & 8 deletions packages/server/src/actions/drag/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { chalk, Logs } from "@cardsgame/utils"
import { ServerLogger } from "@cardsgame/utils"
import chalk from "chalk"

import { State } from "../../state/state.js"

import { DragActionDefinition, DragActionTemplate } from "./dragAction.js"

export const tapFallbackLog = new Logs("tapFallback", true, {
serverStyle: chalk.yellowBright,
})
export const tapFallbackLog = new ServerLogger(
"tapFallback",
true,
chalk.yellowBright,
)

/**
* @ignore
*/
export function isDragActionTemplate<S extends State = State>(
o: unknown
o: unknown,
): o is DragActionTemplate<S> {
if (typeof o !== "object") {
return false
Expand Down Expand Up @@ -44,7 +47,7 @@ export function isDragActionTemplate<S extends State = State>(
* @ignore
*/
export function isDragActionDefinition<S extends State>(
o: unknown
o: unknown,
): o is DragActionDefinition<S> {
if (typeof o !== "object" && !(o instanceof DragActionDefinition)) {
return false
Expand All @@ -57,7 +60,7 @@ export function isDragActionDefinition<S extends State>(

export function isStartEvent(
interaction: InteractionType,
isTapDragging: boolean
isTapDragging: boolean,
): boolean {
return (
interaction === "dragstart" || (interaction === "tap" && !isTapDragging)
Expand All @@ -66,7 +69,7 @@ export function isStartEvent(

export function isEndEvent(
interaction: InteractionType,
isTapDragging: boolean
isTapDragging: boolean,
): boolean {
return interaction === "dragend" || (interaction === "tap" && isTapDragging)
}
12 changes: 6 additions & 6 deletions packages/server/src/bots/pickNeuron/getInteractionEntities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chalk } from "@cardsgame/utils"
import chalk from "chalk"

import { isEntityActionDefinition } from "../../actions/entity/utils.js"
import { prepareConditionsContext } from "../../conditions/context/utils.js"
Expand All @@ -12,7 +12,7 @@ import { logs } from "./pickNeuron.js"
export const getInteractionEntities = <S extends State>(
state: S,
bot: Bot,
neuron: BotNeuron<S>
neuron: BotNeuron<S>,
): ChildTrait[] => {
const { action } = neuron

Expand All @@ -31,20 +31,20 @@ export const getInteractionEntities = <S extends State>(
if (queries === "*") {
logs.debug(
`Action "${chalk.bold(
action.name
)}" has a "catch-all" definition, ignoring (for now?)`
action.name,
)}" has a "catch-all" definition, ignoring (for now?)`,
)
return []
}

if (typeof queries === "string") {
throw new Error(
`Invalid use of interactions, got "${queries}" where only "*" is accepted as a string.`
`Invalid use of interactions, got "${queries}" where only "*" is accepted as a string.`,
)
}

logs.debug(
`Action "${chalk.bold(action.name)}" has ${queries.length} QuerableProps`
`Action "${chalk.bold(action.name)}" has ${queries.length} QuerableProps`,
)

// results = results.reduce((all, query) => all.push(query) && all, new Array<QuerableProps>())
Expand Down
17 changes: 9 additions & 8 deletions packages/server/src/bots/pickNeuron/pickNeuron.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { performance } from "perf_hooks"

import { chalk, decimal, Logs } from "@cardsgame/utils"
import { decimal, ServerLogger } from "@cardsgame/utils"
import chalk from "chalk"

import { ClientMessageContext } from "../../conditions/context/clientMessage.js"
import { ENTITY_INTERACTION } from "../../interaction/constants.js"
Expand All @@ -12,9 +13,7 @@ import { markDebugTime } from "../utils.js"
import { filterNeuronConditions } from "./filterConditions.js"
import { getPossibleEvents } from "./getPossibleEvents.js"

export const logs = new Logs("pickNeuron", true, {
serverStyle: chalk.bgGreen.white,
})
export const logs = new ServerLogger("pickNeuron", true, chalk.bgGreen.white)

export type ChosenBotNeuronResult<S extends State> = {
message: ClientPlayerMessage
Expand All @@ -28,7 +27,7 @@ export type ChosenBotNeuronResult<S extends State> = {
export const pickNeuron = <S extends State>(
rootNeuron: BotNeuron<S>,
state: S,
bot: Bot
bot: Bot,
): ChosenBotNeuronResult<S> => {
const _start = performance.now()

Expand All @@ -40,7 +39,7 @@ export const pickNeuron = <S extends State>(
}

logs.group(
chalk.white(`pickNeuron(${bot.clientID}/${bot.name}) | ${rootNeuron.name}`)
chalk.white(`pickNeuron(${bot.clientID}/${bot.name}) | ${rootNeuron.name}`),
)

// 1. Filter all current level neurons by their own conditions
Expand All @@ -56,7 +55,7 @@ export const pickNeuron = <S extends State>(
}
const _countByConditions = neurons.length
logs.debug(
`${rootNeuron.children.length} neurons => ${_countByConditions} neurons`
`${rootNeuron.children.length} neurons => ${_countByConditions} neurons`,
)

// 2. Sort by their values
Expand Down Expand Up @@ -86,7 +85,9 @@ export const pickNeuron = <S extends State>(

const _actionsCount = chalk.bold(`${results.length} actions`)
logs.groupEnd(
`${_countByConditions} neurons => ${_actionsCount} ${markDebugTime(_start)}`
`${_countByConditions} neurons => ${_actionsCount} ${markDebugTime(
_start,
)}`,
)

return results[0]
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/bots/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chalk, decimal } from "@cardsgame/utils"
import { decimal } from "@cardsgame/utils"
import chalk from "chalk"

export const markDebugTime = (last: number): string => {
const delta = decimal(performance.now() - last, 1)
Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/commands/changeParent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chalk, def, logs } from "@cardsgame/utils"
import { def, logs } from "@cardsgame/utils"
import chalk from "chalk"

import {
Command,
Expand All @@ -23,7 +24,7 @@ export class ChangeParent extends Command {
constructor(
entities: Targets<ChildTrait>,
target: Target<ParentTrait>,
options: ChangeParentOptions = {}
options: ChangeParentOptions = {},
) {
super()

Expand Down Expand Up @@ -54,7 +55,7 @@ export class ChangeParent extends Command {
"moving",
this.entities.get().map((e) => `${e.idx}:${hasLabel(e) ? e.name : ""}`),
"entities to",
chalk.yellow(this.target.get()["name"] || "ROOT")
chalk.yellow(this.target.get()["name"] || "ROOT"),
)

this.entities.get().forEach((entity, idx) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/commands/placeOnGrid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chalk, def, logs } from "@cardsgame/utils"
import { def, logs } from "@cardsgame/utils"
import chalk from "chalk"

import { Command, Target, TargetHolder } from "../command.js"
import { isGrid } from "../entities/index.js"
Expand All @@ -20,7 +21,7 @@ export class PlaceOnGrid extends Command {
entity: Target<ChildTrait>,
target: Target<ParentTrait>,
row: number,
column: number
column: number,
) {
super()

Expand Down Expand Up @@ -51,7 +52,7 @@ export class PlaceOnGrid extends Command {
"moving",
hasLabel(entity) ? `${entity.type}:${entity.name}` : "",
"to",
chalk.yellow(target["name"] || "ROOT")
chalk.yellow(target["name"] || "ROOT"),
)

this.lastIndex = entity.idx
Expand Down

0 comments on commit c62dd04

Please sign in to comment.