From d83ffade9d82e77b82c1ab5fa61f3fac0d17ee2e Mon Sep 17 00:00:00 2001 From: Freddie Date: Sat, 16 Apr 2022 15:46:31 +0800 Subject: [PATCH] v1.50a example --- handlers/example.ts | 4 ++++ index.ts | 9 ++++++--- webui/custom_page.pug | 5 +++++ webui/js/custom_page.js | 8 +++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/handlers/example.ts b/handlers/example.ts index 1a0e2d1..821ff9a 100644 --- a/handlers/example.ts +++ b/handlers/example.ts @@ -53,3 +53,7 @@ export const example: EPR = async (info, data, send) => { export const changeName = async (data: any) => { await DB.Update(data.refid, { collection: 'profile' }, { $set: { name: data.name } }); }; + +export const randomNumber = async (data: any, send: WebUISend) => { + send.json({ number: Math.floor(Math.random() * 100) }); +}; diff --git a/index.ts b/index.ts index 6e4b653..2e3e0d9 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import { example, changeName } from './handlers/example'; +import { example, changeName, randomNumber } from './handlers/example'; export function register() { /* Register game code */ @@ -14,7 +14,7 @@ export function register() { default: 'EVENT_1', options: ['EVENT_1', 'EVENT_2'], }); - + /* Register user-provided datafile This will allow user to upload their own data to the root of your plugin @@ -44,9 +44,12 @@ export function register() { /* Register a event and increment the click counter */ R.WebUIEvent('change', changeName); + /* Register a event that respond with a random number */ + R.WebUIEvent('random', randomNumber); + /* Use --dev argument to enable console output. */ console.log('Plugin Registered'); - + /* You can check the version of CORE using CORE_VERSION_MAJOR and CORE_VERSION_MINOR Note: these value can be undefined, which means the CORE is version v1.18 and under diff --git a/webui/custom_page.pug b/webui/custom_page.pug index baa5d1f..2d17458 100644 --- a/webui/custom_page.pug +++ b/webui/custom_page.pug @@ -24,6 +24,11 @@ button.button.is-primary#plugin-click | Send "click" event and refresh + p#random-number RandomNumber: Click the button below to get a random number + p + button.button.is-primary#plugin-random + | Use emit() to get a random number + //- You can include custom javascripts and send data back to plugin using emit() see profile_name.pug for sending data using form script(src="static/js/custom_page.js") \ No newline at end of file diff --git a/webui/js/custom_page.js b/webui/js/custom_page.js index 86cc11a..88a392b 100644 --- a/webui/js/custom_page.js +++ b/webui/js/custom_page.js @@ -1,5 +1,11 @@ $('#plugin-click').on('click', () => { - emit('click', {}).then(() => { + emit('click').then(() => { location.reload(); }); }); + +$('#plugin-random').on('click', () => { + emit('random').then(result => { + $('#random-number').text(result.data.number); + }); +});