////////////////////////////
// ボタン連打ゲーム《10秒》
//
// 更新履歴
// 2023.04.18 作成
// 2023.04.18 リトライ機能追加
// 2023.04.19 保存キー変更
// 2023.04.19 自己ベスト削除機能追加
//
// 追加したい
// ・重いときでも10秒で終わらせる
// → 凄い記録出るのも面白いので一旦様子見
////////////////////////////
// 定数
let OWNER_USERNAME = "M1zuu" // 作成者のユーザー名 (「https://misskey.io/@○○○」の○○○)
//let LIMIT_SECONDS = 10 // デフォルトの残り時間
let BESTSCORE_KEY = "{THIS_URL}_best_score" // 自己ベストの保管キー
let BESTSCORE_KEY_NEW = `{THIS_ID}_button_renda_game_best_score` // 自己ベストの保管キー(変更後)
let TITLE_INFOMATION = `ボタンを押したら即スタート`
// 変数
var bestScore = 0 // 保存されている自己ベスト
var score = 0 // 今回のスコア
var timeLeft = 10 // 残り時間
//var timeLeft = LIMIT_SECONDS // 残り時間
var flgUpdateBestScore = false // 自己ベスト更新したかどうかのフラグ
var extraInformation = `` // 追加情報(今のところは自己ベスト更新の時だけ設定する)
// 作成者にのみデバッグ文字列を表示する
@debug(text) {
if (USER_USERNAME == OWNER_USERNAME) {
Ui:get("debug").update({text: text})
}
}
// 初期化
@init() {
bestScore = 0
score = 0
timeLeft = 10
//timeLeft = LIMIT_SECONDS
flgUpdateBestScore = false
extraInformation = ``
loadBestScore()
// 何もない画面を表示(念のため)
//Ui:render([])
}
//自己ベスト読込
@loadBestScore() {
var loadData = Mk:load(BESTSCORE_KEY)
var loadData_new = Mk:load(BESTSCORE_KEY_NEW)
if (Core:type(loadData_new) == "num") {
bestScore = loadData_new
} elif (Core:type(loadData) == "num") {
bestScore = loadData
}
/*
if (Core:type(loadData) == "num") {bestScore = loadData}
*/
//bestScore = loadData
}
// 自己ベスト保存
@saveBestScore() {
debug("saveBestScore start")
Mk:save(BESTSCORE_KEY bestScore)
debug("saveBestScore finish")
}
// ヘッダー文字列生成
@genHeaderMfm() {
var timeValue = `{timeLeft}`
if (timeLeft < 10) {timeValue = `0{timeValue}`}
if (timeLeft > 3) {
return `残り時間:$[x2 {timeValue}] 秒 自己ベスト:$[x2 {bestScore}]`
} else {
return `残り時間:$[x2 $[fg.color=F00 {timeValue}]] 秒 自己ベスト:$[x2 {bestScore}]`
}
}
// タイトル画面用スコア文字列生成
@genTitleScoreMfm() {
return `$[x2 :blank:] $[x3 {score}] $[x2 :blank:]`
}
// スコア文字列生成
@genScoreMfm() {
var text = `$[x2 $[flip :blobtoofast:]] $[x3 {score}] $[x2 :blobtoofast:]`
if (flgUpdateBestScore) {
return `$[fg.color=FA0 {text}]`
} else {
return `{text}`
}
}
// 結果画面用スコア文字列生成
@genResultScoreMfm() {
return `$[x2 $[flip :blobhyperbounce:]] $[x3 {score}] $[x2 :blobhyperbounce:]`
}
// ボタン「スタート」を押したときに呼ぶ処理
@onClickBtnMainStart() {
debug("onClickBtnMainStart start")
updateInformation(``)
updateBtnMain("start")
updateScore()
updateBtnDeleteBestScore(true)
play()
debug("onClickBtnMainStart finish")
}
// ボタン「押せ!」を押したときに呼ぶ処理
@onClickBtnMainPush() {
debug("onClickBtnMainPush start")
score += 1
if ((flgUpdateBestScore == false) && (score > bestScore)) {
flgUpdateBestScore = true
}
updateScore()
if (flgUpdateBestScore) {
extraInformation = `$[sparkle 自己ベスト更新!!]`
updateInformation(extraInformation)
}
debug("onClickBtnMainPush finish")
}
// ボタン「自己ベスト削除」を押したときに呼ぶ処理
@onClickDeleteBestScore() {
bestScore = 0
saveBestScore()
updateHeader()
}
// ヘッダーUI更新
@updateHeader() {
debug("updateHeader start")
Ui:get("header").update({text: genHeaderMfm()})
debug("updateHeader finish")
}
// スコアUI更新
@updateScore() {
debug("updateScore start")
Ui:get("score").update({text: genScoreMfm()})
debug("updateScore finish")
}
// スコアUI更新(結果用)
@updateScoreResult() {
debug("updateScoreResult start")
Ui:get("score").update({text: genResultScoreMfm()})
debug("updateScoreResult finish")
}
// 情報UI更新
@updateInformation(text) {
debug("updateInformation start")
if (text == ``) {text = ` `}
Ui:get("information").update({text: text})
debug("updateInformation finish")
}
//メインボタンUI更新
@updateBtnMain(mode) {
debug("updateBtnMain start")
var textValue = ""
var primaryValue = false
var onClickValue = @(){}
if (mode == "start") {
textValue = "押せ!"
primaryValue = true
onClickValue = @(){onClickBtnMainPush()}
} elif (mode == "finish") {
textValue = "終了!"
} else {
textValue = "エラー"
}
Ui:get("btnMain").update({
text: textValue
primary: primaryValue
onClick: onClickValue
})
debug("updateBtnMain finish")
}
//リトライボタンUI更新
@updateBtnRetry() {
debug("updateBtnRetry start")
Ui:get("btnRetry").update({
text: "リトライ"
onClick: @(){retry()}
})
debug("updateBtnRetry finish")
}
// 投稿ボタンUI更新
@updatePostFormButton() {
//@createResultButtons() {
debug("updatePostFormButton start")
//debug("createResultButtons start")
Ui:get("postFormButton").update({
form: {
text: `$[x2 {score}]回連打しました:blobtoofast:{Str:lf}{extraInformation}{Str:lf}#ボタン連打ゲーム《10秒》{Str:lf}{THIS_URL}`
}
})
Ui:get("result").update({hidden: false})
/*
Ui:get("result").update({
children: [
Ui:C:postFormButton({
text: "投稿する"
primary: true
rounded: true
form: {
text: `$[x2 {score}]回連打しました:blobtoofast:{Str:lf}{extraInformation}{Str:lf}#ボタン連打ゲーム《10秒》{Str:lf}{THIS_URL}`
}
} "postFormButton")
]
})
*/
debug("updatePostFormButton finish")
//debug("createResultButtons finish")
}
// ベストスコア削除ボタンUI更新
@updateBtnDeleteBestScore(value) {
Ui:get("btnDeleteBestScore").update({disabled: value})
}
// タイトル画面を表示
@renderTitle() {
Ui:render([
Ui:C:container({
align: "center"
children: [
Ui:C:button({
text: " "
onClick: @(){}
} "btnRetry")
]
} "sub")
Ui:C:container({
align: "center"
children: [
Ui:C:mfm({text: ``} "header")
//Ui:C:mfm({text: genHeaderMfm()} "header")
Ui:C:mfm({text: ``} "blankLine1")
Ui:C:mfm({text: ``} "information")
//Ui:C:mfm({text: TITLE_INFOMATION} "information")
Ui:C:mfm({text: ``} "score")
//Ui:C:mfm({text: genTitleScoreMfm()} "score")
Ui:C:mfm({text: ``} "blankLine2")
Ui:C:button({
/*
text: "スタート"
primary: true
onClick: @(){onClickBtnMainStart()}
*/
} "btnMain")
Ui:C:mfm({text: ``} "debug")
]
} "main")
Ui:C:container({
align: "center"
children: [
Ui:C:postFormButton({
text: "投稿する"
primary: true
rounded: true
} "postFormButton")
]
} "result")
/*
Ui:C:container({
align: "center"
children: []
} "result")
*/
Ui:C:folder({
title: "オプション"
opened: false
children: [
Ui:C:button({
text: "自己ベスト削除"
onClick: onClickDeleteBestScore
} "btnDeleteBestScore")
]
})
])
titleInit()
}
// タイトルの表示データを初期化
@titleInit() {
updateHeader()
Ui:get("btnRetry").update({
text: " "
onClick: @(){}
})
Ui:get("information").update({text: TITLE_INFOMATION})
Ui:get("score").update({text: genTitleScoreMfm()})
Ui:get("btnMain").update({
text: "スタート"
primary: true
onClick: @(){onClickBtnMainStart()}
})
Ui:get("result").update({hidden: true})
}
// プレイ中に再帰実行される処理
@play() {
debug("play start")
Async:timeout(1000, @(){
timeLeft -= 1
updateHeader()
if (timeLeft > 0) {
play()
} else {
result()
}
})
debug(`play finish`)
}
// プレイが終了した時に実行される処理
@result() {
debug("result start")
updateBtnMain("finish")
updateScoreResult()
// 自己ベスト更新している場合
if (flgUpdateBestScore == true) {
bestScore = score
saveBestScore()
extraInformation = `:left_side_jagged_balloon_without_tail:{extraInformation}:right_side_jagged_balloon_without_tail:{Str:lf}`
}
updateHeader()
updateBtnRetry()
updatePostFormButton()
updateBtnDeleteBestScore(false)
debug("result finish")
}
// リトライ処理
@retry() {
debug("retry start")
init()
titleInit()
debug("retry finish")
}
// メイン処理
@main() {
init()
renderTitle()
}
main()