-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLog.vue
140 lines (128 loc) · 3.38 KB
/
Log.vue
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
137
138
139
140
<template>
<v-card class="log-area">
<v-card-text v-if="logs.length == 0" class="log">
<div class="meta">
ダイスログはまだありません
</div>
</v-card-text>
<template v-for="(item, i) in logs">
<v-card-text :key="i" class="log">
<div class="meta">
<span>{{ item.gameType }}</span
><span class="timestamp">{{ format(item.timestamp) }}</span>
<v-tooltip right transition="slide-x-transition">
<template v-slot:activator="{ on }">
<v-icon
v-ripple
v-on="on"
@click="dicerollByText(item.gameType, item.command)"
>mdi-replay</v-icon
>
</template>
<span>もう一度ダイスロール</span>
</v-tooltip>
</div>
<div class="subheading">{{ item.command }} {{ item.body }}</div>
</v-card-text>
</template>
<v-snackbar v-model="snackbar" :timeout="4000" color="error" top>
{{ errorMsg }}
<template v-slot:action="{ attrs }">
<v-btn drak text v-bind="attrs" @click="snackbar = false">
閉じる
</v-btn>
</template>
</v-snackbar>
</v-card>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { diceRoll, selectDiceResults } from "./dice";
import { Log as LogI } from "./interface";
interface Dice {
faces: number;
value: number;
}
const aDay = 1000 * 60 * 60 * 24;
const baseTime = Date.now();
@Component
export default class Log extends Vue {
data() {
return {
snackbar: false,
errorMsg: ""
};
}
get logs() {
return this.$store.state.logs;
}
format(dateOrg: string | Date): string {
const date = new Date(dateOrg);
const diff = baseTime - date.getTime();
const h = this.zeroPadding(date.getHours());
const m = this.zeroPadding(date.getMinutes());
if (diff >= aDay) {
const month = date.getMonth() + 1;
const day = date.getDate();
return `${month}月${day}日 ${h}:${m}`;
} else {
return `${h}:${m}`;
}
}
zeroPadding(x: number): string {
return ("0" + x).slice(-2);
}
dicerollByText(gameType: string, text: string) {
diceRoll(gameType, text)
.then(res => {
const log: LogI = {
gameType: gameType,
command: text,
body: res.text,
drawables: selectDiceResults(res.rands),
timestamp: new Date(),
success: res.success,
failure: res.failure
};
this.$store.commit("appendLogBuffer", log);
})
.catch(error => {
if (error.response) {
this.$data.errorMsg = "ダイスコマンドを実行できませんでした";
} else {
this.$data.errorMsg = "APIサーバーに接続できませんでした";
}
this.$data.snackbar = true;
});
}
}
</script>
<style lang="scss" scoped>
.log-area {
padding-top: 0.7rem;
padding-bottom: 0.7rem;
}
.log {
padding-top: 0.5rem;
padding-bottom: 0.5rem;
.meta {
line-height: 1.4;
font-size: 14px;
color: rgba(0, 0, 0, 0.54);
.v-icon {
font-size: 14px;
padding: 1px;
}
}
.timestamp::before {
content: "・";
}
.subheading {
color: rgba(0, 0, 0, 0.87);
word-wrap: break-word;
white-space: pre-wrap;
line-height: 1.2;
}
}
</style>