-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorebook_result.js
167 lines (160 loc) · 5.27 KB
/
scorebook_result.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: magic;
const DAY_IN_MICROSECONDS = 86400000;
let game = await gameAPI();
const number = Math.floor(Math.random() * game.length);
let widget = await createWidget(game[number]);
if (config.runsInWidget) {
// The script runs inside a widget, so we pass our instance of ListWidget to be shown inside the widget on the Home Screen.
Script.setWidget(widget);
} else {
// The script runs inside the app, so we preview the widget.
widget.presentMedium();
}
// Calling Script.complete() signals to Scriptable that the script have finished running.
// This can speed up the execution, in particular when running the script from Shortcuts or using Siri.
Script.complete();
async function createWidget(game) {
const gameDate =
new Date(game.game_date).getMonth() +
1 +
"/" +
new Date(game.game_date).getDate();
let title = `${gameDate} ${game.name}`;
let widget = new ListWidget();
// Add background gradient
let gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [new Color("141414"), new Color("13233F")];
widget.backgroundGradient = gradient;
// Show app icon and title
let titleStack = widget.addStack();
titleStack.addSpacer(4);
// let docsSymbol = SFSymbol.named("baseball");
// let docsElement = titleStack.addImage(docsSymbol.image);
// docsElement.imageSize = new Size(13, 13);
// docsElement.tintColor = Color.white();
// docsElement.imageOpacity = 0.5;
let titleElement = titleStack.addText(title);
titleElement.textColor = Color.white();
titleElement.textOpacity = 0.7;
titleElement.font = Font.mediumSystemFont(13);
widget.addSpacer(4);
titleStack.addSpacer(6);
const resultStack = widget.addStack();
// Show API
const teamNameStack = resultStack.addStack();
resultStack.addSpacer(10);
teamNameStack.layoutVertically();
const scoreStack = resultStack.addStack();
scoreStack.layoutVertically();
let firstTElement = teamNameStack.addText(game.first_team_name);
firstTElement.textColor = Color.white();
firstTElement.font = Font.mediumSystemFont(18);
firstTElement;
const firstRunElement = scoreStack.addText(String(game.first_run));
firstRunElement.textColor = game.first_run > game.last_run? Color.orange(): Color.white()
let lastTElement = teamNameStack.addText(game.last_team_name);
lastTElement.textColor = Color.white();
lastTElement.font = Font.mediumSystemFont(18);
const lastRunElement = scoreStack.addText(String(game.last_run));
lastRunElement.textColor = game.first_run < game.last_run? Color.orange(): Color.white()
widget.addSpacer(4)
const progressText = widget.addText(progress(game))
progressText.font = Font.mediumSystemFont(13);
progressText.textOpacity = 0.7
// UI presented in Siri ans Shortcuta is non-interactive, so we only show the footer when not running the script from Siri.
if (!config.runsWithSiri) {
widget.addSpacer(8);
// Add button to open documentation
let linkSymbol = SFSymbol.named("arrow.up.forward");
let footerStack = widget.addStack();
let linkStack = footerStack.addStack();
linkStack.centerAlignContent();
linkStack.url = `https://cap-scorebook.com/game/${game.game_id}`;
let linkElement = linkStack.addText("スコアブックを開く");
linkElement.font = Font.mediumSystemFont(13);
linkElement.textColor = Color.blue();
linkStack.addSpacer(3);
let linkSymbolElement = linkStack.addImage(linkSymbol.image);
linkSymbolElement.imageSize = new Size(11, 11);
linkSymbolElement.tintColor = Color.blue();
footerStack.addSpacer()
}
return widget;
}
async function gameAPI() {
const beginDate = new Date();
beginDate.setTime(beginDate.getTime() - 14 * DAY_IN_MICROSECONDS);
let games = (await loadGames()).filter((game) => {
return (
new Date(game.game_date) >= beginDate &&
!game.is_private &&
game.isCap &&
game.league_id !== 1
);
});
return games;
}
async function loadGames() {
let url = "https://jcbl-score.com/scoresheet/api/v1/game";
let req = new Request(url, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
const res = await req.loadJSON();
return res;
}
async function loadTeamIcon(id) {
let url = `https://s3.ap-northeast-1.wasabisys.com/capbaseball/teamIcons/${id}.jpg`;
let req = new Request(url);
try {
const res = req.loadImage();
return res;
} catch (error) {
console.error(error)
return null
}
}
function progress(game){
let title = "";
const inningArray = [
"_1st",
"_2nd",
"_3rd",
"_4th",
"_5th",
"_6th",
"_7th",
"_8th",
"_9th",
];
if (game.locked) {
title = "試合終了";
} else {
let top = 0;
let bottom = 0;
for (let i = 0; i < inningArray.length; i++) {
if (game["top" + inningArray[i]] !== null) {
top = i + 1;
}
if (game["bottom" + inningArray[i]] !== null) {
bottom = i + 1;
}
}
if (top > bottom) {
title = top+ "回途中";//表
} else if (top === bottom) {
if(top > 0){
title = bottom + "回途中"//裏
} else {
title = '試合前'
}
}
}
return title;
};