Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add image output support #9

Merged
merged 12 commits into from
Oct 3, 2023
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
],
"peerDependencies": {
"koishi": "^4.11.0"
},
"devDependencies": {
"koishi-plugin-puppeteer": "^3.5.0"
MaikoTan marked this conversation as resolved.
Show resolved Hide resolved
}
}
137 changes: 113 additions & 24 deletions packages/core/src/index.ts → packages/core/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Argv, Command, Context, Plugin, Session } from 'koishi'
import { Argv, Command, Context, Element, Plugin, Session } from 'koishi'
import {} from 'koishi-plugin-puppeteer'

export interface WordleVariation<WordType extends any[] = string[], MoreUnitResult = WordType[number]>
extends Omit<Plugin.Object, 'apply'> {
Expand Down Expand Up @@ -100,7 +101,7 @@ export function defineVariation<WordType extends any[] = string[], MoreUnitResul
static reusable = variation.reusable
static reactive = variation.reactive

constructor(ctx: Context) {
constructor(public ctx: Context) {
// define locales
ctx.i18n.define('zh-CN', require('./locales/zh-CN'))
ctx.i18n.define('zh', require('./locales/zh-CN'))
MaikoTan marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -133,7 +134,7 @@ export function defineVariation<WordType extends any[] = string[], MoreUnitResul
if (state?.state === Wordle.GameState.Active) {
if (word) {
const result = await handleInput(word, state, session, ctx)
let text = ''
let text: string | Element = ''
switch (result.type) {
case 'bad-length':
text = session?.text('wordle.messages.bad-length')
Expand All @@ -145,7 +146,7 @@ export function defineVariation<WordType extends any[] = string[], MoreUnitResul
text = session?.text('wordle.messages.correct')
break
case 'incorrect':
text = this.formatTable(result.unitResults, state.guessedWords ?? [], session)
text = await this.formatTable(result.unitResults, state.guessedWords ?? [], session)
break
}

Expand Down Expand Up @@ -185,29 +186,117 @@ export function defineVariation<WordType extends any[] = string[], MoreUnitResul
return variation.getCurrentWord(argv, ctx)
}

formatTable(word: Wordle.UnitResult<any>[], guessedWords: Wordle.VerificatedResult[], session: Session): string {
async formatTable(
word: Wordle.UnitResult<any>[],
guessedWords: Wordle.VerificatedResult[],
session: Session,
): Promise<string | Element> {
const lines: string[] = []
;[...guessedWords.map((item) => item.unitResults), word].forEach((result) => {
let line: string = ''
result.forEach((unit) => {
switch (unit.type) {
case 'correct':
line += `[${unit.char}]`
break
case 'bad-position':
line += `(${unit.char})`
break
case 'incorrect':
line += ` ${unit.char} `
break
}
})

lines.push(line)
})
lines.push(session.text('wordle.messages.text-hint'))
if (this.ctx.puppeteer) {
return (
<html>
<div>
<style>{`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 400px;
padding: 20px;
font-family:
-apple-system, "Microsoft Yahei", "PingFang SC", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
font-size: '24px';
font-weight: 'bold';
}
`}</style>
<h1 style={{ textAlign: 'center', fontSize: '68px', textTransform: 'capitalize' }}>{command.name}</h1>
<div style={{ width: '100%' }}>
{[...guessedWords.map((item) => item.unitResults), word].map((items) => (
<div
style={{
marginTop: '5px',
width: '100%',
display: 'grid',
gridTemplateColumns: 'repeat(5, 1fr)',
gridGap: '5px',
}}
>
{items.map((item) => (
<span
style={{
fontSize: '40px',
fontWeight: 'bold',
textTransform: 'uppercase',
width: '100%',
aspectRatio: '1/1',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: 'white',
backgroundColor:
item.type === 'correct' ? '#6aaa64' : item.type === 'bad-position' ? '#c9b458' : '#787c7e',
}}
>
{item.char}
</span>
))}
</div>
))}

{Array.from({ length: (this._variation.guessCount ?? 6) - guessedWords.length - 1 }).map(() => (
<div
style={{
marginTop: '5px',
width: '100%',
display: 'grid',
gridTemplateColumns: 'repeat(5, 1fr)',
gridGap: '5px',
}}
>
{Array.from({ length: word.length }).map(() => (
<span
style={{
display: 'flex',
width: '100%',
aspectRatio: '1/1',
border: '2px solid #3a3a3c',
}}
>
&nbsp;
</span>
))}
</div>
))}
</div>
</div>
</html>
)
} else {
;[...guessedWords.map((item) => item.unitResults), word].forEach((result) => {
let line: string = ''
result.forEach((unit) => {
switch (unit.type) {
case 'correct':
line += `[${unit.char}]`
break
case 'bad-position':
line += `(${unit.char})`
break
case 'incorrect':
line += ` ${unit.char} `
break
}
})

return lines.join('\n')
lines.push(line)
})
lines.push(session.text('wordle.messages.text-hint'))

return lines.join('\n')
}
}
}
}