-
Notifications
You must be signed in to change notification settings - Fork 0
/
ink.js
136 lines (121 loc) · 4.85 KB
/
ink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { Story } = require('inkjs')
const { t } = require('koishi-core')
const extendMysql = require('./mysql')
const Templates = {
'description': 'ink功能',
'example': '查看当前剧情 / 选项',
'example-choice': '选择第一个选项',
'skip': '跳至下一个选项',
'hard-reset': '重置(请谨慎使用)',
'hard-unlock': '强制解除进程锁(需要 2 级权限,请谨慎使用)',
'is-locking': ' 正处于剧情中,请等待其剧情结束。',
'is-locking-self': '当前处于剧情中,请等待剧情结束。',
'hard-reset-confirm': '这将重置你的所有进度与数据,且不可挽回。请于5秒内回复 是 或 y(es) 以确认。',
'hard-reset-completed': '已重置。',
'hard-reset-failed': '已取消重置。',
'hard-unlock-unavail': '未处于剧情中,不需要解除进程锁。',
'hard-unlock-completed': '已强制解除进程锁。',
'choices': '选项:',
'skip-to-choices': '已跳转至选项:',
'the-end': '=== 故事结束 ===',
'error': '出现了一点错误,请尝试重新开始剧情。'
}
let StoryLock = []
module.exports = (ctx, config) => {
let command = config.command
extendMysql(config.subcommand)
t.set(command, Templates)
const storyJson = require(config.filePath)
ctx.command(command + ' <choice>', t(`${command}.description`))
.example(config.subcommand + ' ' + t(`${command}.example`))
.example(config.subcommand + ' 1 ' + t(`${command}.example-choice`))
.option('hard-reset', '-R ' + t(`${command}.hard-reset`))
.option('hard-unlock', '-U ' + t(`${command}.hard-unlock`), { authority: 2 })
.option('skip', '-s ' + t(`${command}.skip`))
.userFields(['id'])
.action(async ({ session, options }, choice) => {
try {
let db = session.database
let bot = session.bot
let ch = StoryLock.find(o => o.channel == session.channelId)
if (options['hard-unlock']) {
if (!ch || !ch.lock) {
return t(`${command}.hard-unlock-unavail`)
} else {
ch.lock = false
return t(`${command}.hard-unlock-completed`)
}
}
if (!ch) {
StoryLock.push({
channel: session.channelId,
lock: true,
id: session.userId,
uid: session.user.id
})
ch = StoryLock[StoryLock.length - 1]
} else if (ch.lock && ch.uid != session.user.id) {
let currentUser = await bot.getGroupMember(session.channelId, ch.id)
let name = currentUser.nickname || currentUser.username
return name + t(`${command}.is-locking`)
} else if (ch.lock && ch.uid == session.user.id) {
return t(`${command}.is-locking-self`)
} else {
ch.lock = true
ch.id = session.userId
ch.uid = session.user.id
}
let story = new Story(storyJson)
if (options['hard-reset']) {
let replyMessage
session.send(t(`${command}.hard-reset-confirm`))
let ans = await session.prompt(5 * 1000)
if (['是', 'y', 'yes'].indexOf(ans) != -1) {
story.ResetState()
db.saveGameData(config.subcommand, session.user.id, story.state.toJson())
replyMessage = t(`${command}.hard-reset-completed`)
} else {
replyMessage = t(`${command}.hard-reset-failed`)
}
ch.lock = false
return replyMessage
}
let save = await db.loadGameData(config.subcommand, session.user.id)
if (!save) story.ResetState()
else story.state.LoadJson(save)
if (story.currentChoices.length > 0 && choice) {
let userChoice = parseInt(options.choose)
if (isNaN(userChoice)) userChoice = 0
else userChoice -= 1
story.ChooseChoiceIndex(userChoice)
}
let speed = config.messageSpeed ?? session.app.options.delay.message
while (story.canContinue) {
if (options.skip) story.Continue()
else {
session.send(story.Continue())
let skip = await session.prompt(speed)
if (['-s', '--skip', 'skip', '跳过'].indexOf(skip) != -1) {
options.skip = true
}
}
}
if (story.currentChoices.length > 0) {
let choices = options.skip
? t(`${command}.skip-to-choices`)
: t(`${command}.choices`)
for (let i = 0; i < story.currentChoices.length; i++) {
choices += `\n${(i + 1)}. ${story.currentChoices[i].text}`
}
await session.sendQueued(choices)
} else {
await session.sendQueued(t(`${command}.the-end`))
}
ch.lock = false
db.saveGameData(config.subcommand, session.user.id, story.state.toJson())
} catch (err) {
console.log(err)
return t(`${command}.error`)
}
})
}