@wilderzone/vgs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/npm/index.d.ts +82 -0
- package/npm/index.js +1 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wilderzone
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# VGS
|
|
2
|
+
Voice Game System
|
|
3
|
+
|
|
4
|
+
```sh
|
|
5
|
+
npm install @wilderzone/vgs
|
|
6
|
+
|
|
7
|
+
# or
|
|
8
|
+
|
|
9
|
+
deno add @wilderzone/vgs
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Create and configure a VGS instance.
|
|
16
|
+
```ts
|
|
17
|
+
import { VGS, type VGSConfig, type VGSMatch } from '@wilderzone/vgs';
|
|
18
|
+
import { data } from './your/data';
|
|
19
|
+
|
|
20
|
+
// Configure VGS.
|
|
21
|
+
const config: VGSConfig = {
|
|
22
|
+
trigger: 'V',
|
|
23
|
+
timeout: 3000,
|
|
24
|
+
options: data
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Create a new VGS instance.
|
|
28
|
+
const vgs = new VGS(config);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Connect callbacks with `.on`.
|
|
32
|
+
```ts
|
|
33
|
+
import { onCancel, onOptions, onMatch } from './your/logic';
|
|
34
|
+
|
|
35
|
+
vgs.on('cancel', onCancel);
|
|
36
|
+
vgs.on('options', onOptions);
|
|
37
|
+
vgs.on('match', onMatch);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then send keypresses to the system with `.press`.
|
|
41
|
+
```ts
|
|
42
|
+
vgs.press('V');
|
|
43
|
+
vgs.press('G');
|
|
44
|
+
vgs.press('W');
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
When the VGS finds a match, it will call your `'match'` callback so you can play the appropriate voice line.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Configuration
|
|
51
|
+
|
|
52
|
+
| Option | Required | Default | Description |
|
|
53
|
+
| :--------- | :------- | :--------: | :------------------------------------------------------------------------------------------------------------- |
|
|
54
|
+
| `options` | * | | The voice lines to include in the VGS. |
|
|
55
|
+
| `timeout` | | `3000` | The duration (in ms) to wait for further keypresses before timing out. Set this to `0` to disable the timeout. |
|
|
56
|
+
| `trigger` | | `"V"` | The key used to trigger VGS. |
|
|
57
|
+
| `cancel` | | `"Escape"` | The key used to cancel the current VGS sequence. |
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
### Methods
|
|
61
|
+
|
|
62
|
+
| Method | Description |
|
|
63
|
+
| :--------- | :-------------------------------------------------------------------- |
|
|
64
|
+
| `on` | Listen to VGS [events](#events). |
|
|
65
|
+
| `press` | Send a keypress to the VGS. Keys are case insensitive. |
|
|
66
|
+
| `cancel` | Cancel the current VGS sequence. |
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
### Events
|
|
70
|
+
|
|
71
|
+
| Event | Payload | Description |
|
|
72
|
+
| :--------- | :----------- | :--------------------------------------------------------------------------------- |
|
|
73
|
+
| `trigger` | | Fired when a new VGS sequence is started (after a `trigger` keypress is detected). |
|
|
74
|
+
| `options` | `VGSMatch[]` | Fired when the VGS produces the next set of options in the current sequence. |
|
|
75
|
+
| `match` | `VGSMatch` | Fired when the sequence matches a single voice line. The sequence is then reset. |
|
|
76
|
+
| `cancel` | | Fired when the sequence is cancelled by the `cancel` key or `.cancel()` method. |
|
|
77
|
+
| `timeout` | | Fired when the sequence times out. |
|
package/npm/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
type VGSOptionType = 'category' | 'line';
|
|
2
|
+
export interface VGSOption {
|
|
3
|
+
type: VGSOptionType;
|
|
4
|
+
name?: string;
|
|
5
|
+
file?: string;
|
|
6
|
+
phrase?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface VGSMatch {
|
|
9
|
+
type: VGSOptionType;
|
|
10
|
+
command: string;
|
|
11
|
+
key: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
file?: string;
|
|
14
|
+
phrase?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* VGS configuration.
|
|
18
|
+
*/
|
|
19
|
+
export interface VGSConfig {
|
|
20
|
+
/**
|
|
21
|
+
* The VGS options.
|
|
22
|
+
*/
|
|
23
|
+
options: Record<string, VGSOption>;
|
|
24
|
+
/**
|
|
25
|
+
* The duration (in ms) to wait for further keypresses before timing out.\
|
|
26
|
+
* Set this to `0` to disable the timeout.
|
|
27
|
+
*
|
|
28
|
+
* _Optional, default: `3000`_
|
|
29
|
+
*/
|
|
30
|
+
timeout?: number;
|
|
31
|
+
/**
|
|
32
|
+
* The key used to trigger VGS.
|
|
33
|
+
*
|
|
34
|
+
* _Optional, default: `"V"`_
|
|
35
|
+
*/
|
|
36
|
+
trigger?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The key used to cancel the current VGS sequence.
|
|
39
|
+
*
|
|
40
|
+
* _Optional, default: `"Escape"`_
|
|
41
|
+
*/
|
|
42
|
+
cancel?: string;
|
|
43
|
+
}
|
|
44
|
+
type VGSEvent = 'cancel' | 'options' | 'match' | 'timeout' | 'trigger';
|
|
45
|
+
type VGSCallback<T extends VGSEvent> = T extends 'cancel' ? () => void : T extends 'options' ? (options: VGSMatch[]) => void : T extends 'match' ? (match: VGSMatch) => void : T extends 'timeout' ? () => void : T extends 'trigger' ? () => void : never;
|
|
46
|
+
/**
|
|
47
|
+
* A Voice Game System (VGS) command processor.
|
|
48
|
+
*
|
|
49
|
+
* @param config The VGS configuration.
|
|
50
|
+
*/
|
|
51
|
+
export declare class VGS {
|
|
52
|
+
private config;
|
|
53
|
+
private callbacks;
|
|
54
|
+
private options;
|
|
55
|
+
private sequence;
|
|
56
|
+
private timer;
|
|
57
|
+
constructor(config: VGSConfig);
|
|
58
|
+
private reset;
|
|
59
|
+
private timeout;
|
|
60
|
+
private processSequence;
|
|
61
|
+
/**
|
|
62
|
+
* Send a keypress to the VGS.
|
|
63
|
+
*
|
|
64
|
+
* @param key The key that was pressed. For example, `"V"`.
|
|
65
|
+
* @note This is case insensitive.
|
|
66
|
+
*/
|
|
67
|
+
press(key: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Cancel the current VGS sequence.
|
|
70
|
+
*
|
|
71
|
+
* @note This will fire the `cancel` event.
|
|
72
|
+
*/
|
|
73
|
+
cancel(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Register a function to be called when a particular event is fired.
|
|
76
|
+
*
|
|
77
|
+
* @param event The event to listen to.
|
|
78
|
+
* @param callback The function to call.
|
|
79
|
+
*/
|
|
80
|
+
on<T extends VGSEvent>(event: T, callback: VGSCallback<T>): void;
|
|
81
|
+
}
|
|
82
|
+
export {};
|
package/npm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var o=class{config;callbacks={cancel:[],options:[],match:[],timeout:[],trigger:[]};options;sequence=[];timer;constructor(t){this.config={options:t.options,timeout:t.timeout??3e3,trigger:t.trigger?.toUpperCase()||"V",cancel:t.cancel?.toUpperCase()||"ESCAPE"},this.options=Object.entries(this.config.options).map(([s,e])=>({command:s,option:e}))}reset(){this.sequence=[],globalThis.clearTimeout(this.timer)}timeout(){this.reset();for(let t of this.callbacks.timeout)t()}processSequence(){let t=this.sequence.join(""),s=this.options.find(c=>c.command===t),e=this.options.filter(c=>c.command.length===t.length+1).filter(c=>c.command.startsWith(t));if(s&&s.option.type==="line"){for(let c of this.callbacks.match)c({...s.option,key:s.command.at(-1),command:s.command});this.reset()}if(e.length)for(let c of this.callbacks.options)c(e.map(i=>({...i.option,key:i.command.at(-1),command:i.command})));!s&&!e.length&&this.cancel()}press(t){t=t.toUpperCase();let s=t===this.config.cancel,e=t.length>1,c=t===this.config.trigger;if(s||e){this.cancel();return}if(!(!this.sequence.length&&!c)){globalThis.clearTimeout(this.timer),this.config.timeout&&(this.timer=globalThis.setTimeout(()=>this.timeout(),this.config.timeout));for(let i of this.callbacks.trigger)i();this.sequence.push(t),this.processSequence()}}cancel(){this.reset();for(let t of this.callbacks.cancel)t()}on(t,s){this.callbacks[t].push(s)}};export{o as VGS};
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wilderzone/vgs",
|
|
3
|
+
"description": "",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": "Giga",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./npm/index.js",
|
|
8
|
+
"types": "./npm/index.d.ts",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/wilderzone/vgs.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/wilderzone/vgs/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/wilderzone/vgs#readme"
|
|
17
|
+
}
|