summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/components/global/ad.vue45
-rw-r--r--src/client/instance.ts8
-rw-r--r--src/client/pages/instance/ads.vue6
3 files changed, 42 insertions, 17 deletions
diff --git a/src/client/components/global/ad.vue b/src/client/components/global/ad.vue
index f88a1d2026..e340f846ee 100644
--- a/src/client/components/global/ad.vue
+++ b/src/client/components/global/ad.vue
@@ -19,7 +19,7 @@
<script lang="ts">
import { defineComponent, ref } from 'vue';
-import { instance } from '@client/instance';
+import { Instance, instance } from '@client/instance';
import { host } from '@client/config';
import MkButton from '@client/components/ui/button.vue';
@@ -45,32 +45,45 @@ export default defineComponent({
showMenu.value = !showMenu.value;
};
- let ad = null;
+ const choseAd = (): Instance['ads'][number] | null => {
+ if (props.specify) {
+ return props.specify as Instance['ads'][number];
+ }
- if (props.specify) {
- ad = props.specify;
- } else {
let ads = instance.ads.filter(ad => props.prefer.includes(ad.place));
if (ads.length === 0) {
ads = instance.ads.filter(ad => ad.place === 'square');
}
- const high = ads.filter(ad => ad.priority === 'high');
- const middle = ads.filter(ad => ad.priority === 'middle');
- const low = ads.filter(ad => ad.priority === 'low');
+ const lowPriorityAds = ads.filter(ad => ad.ratio === 0);
+ ads = ads.filter(ad => ad.ratio !== 0);
- if (high.length > 0) {
- ad = high[Math.floor(Math.random() * high.length)];
- } else if (middle.length > 0) {
- ad = middle[Math.floor(Math.random() * middle.length)];
- } else if (low.length > 0) {
- ad = low[Math.floor(Math.random() * low.length)];
+ if (ads.length === 0) {
+ if (lowPriorityAds.length !== 0) {
+ return lowPriorityAds[Math.floor(Math.random() * lowPriorityAds.length)];
+ } else {
+ return null;
+ }
}
- }
+
+ const totalFactor = ads.reduce((a, b) => a + b.ratio, 0);
+ const r = Math.random() * totalFactor;
+
+ let stackedFactor = 0;
+ for (const ad of ads) {
+ if (r >= stackedFactor && r <= stackedFactor + ad.ratio) {
+ return ad;
+ } else {
+ stackedFactor += ad.ratio;
+ }
+ }
+
+ return null;
+ };
return {
- ad,
+ ad: choseAd(),
showMenu,
toggleMenu,
host,
diff --git a/src/client/instance.ts b/src/client/instance.ts
index bd6b1bd571..ad9e1a95fd 100644
--- a/src/client/instance.ts
+++ b/src/client/instance.ts
@@ -3,10 +3,16 @@ import { api } from './os';
// TODO: 他のタブと永続化されたstateを同期
-type Instance = {
+export type Instance = {
emojis: {
category: string;
}[];
+ ads: {
+ ratio: number;
+ place: string;
+ url: string;
+ imageUrl: string;
+ }[];
};
const data = localStorage.getItem('instance');
diff --git a/src/client/pages/instance/ads.vue b/src/client/pages/instance/ads.vue
index 20747d6f9c..6b536793b7 100644
--- a/src/client/pages/instance/ads.vue
+++ b/src/client/pages/instance/ads.vue
@@ -15,12 +15,17 @@
<MkRadio v-model="ad.place" value="horizontal">horizontal</MkRadio>
<MkRadio v-model="ad.place" value="horizontal-big">horizontal-big</MkRadio>
</div>
+ <!--
<div style="margin: 32px 0;">
{{ $ts.priority }}
<MkRadio v-model="ad.priority" value="high">{{ $ts.high }}</MkRadio>
<MkRadio v-model="ad.priority" value="middle">{{ $ts.middle }}</MkRadio>
<MkRadio v-model="ad.priority" value="low">{{ $ts.low }}</MkRadio>
</div>
+ -->
+ <MkInput v-model:value="ad.ratio" type="number">
+ <span>{{ $ts.ratio }}</span>
+ </MkInput>
<MkInput v-model:value="ad.expiresAt" type="date">
<span>{{ $ts.expiration }}</span>
</MkInput>
@@ -82,6 +87,7 @@ export default defineComponent({
memo: '',
place: 'square',
priority: 'middle',
+ ratio: 1,
url: '',
imageUrl: null,
expiresAt: null,