Upgrade to Pro — share decks privately, control downloads, hide ads and more …

趣味コーディングってやっぱ楽しいんだから

4geru sakisaka
November 23, 2024
88

 趣味コーディングってやっぱ楽しいんだから

「初心者・初登壇Welcome!LINEを使ったLT大会 #10」での登壇資料です。
https://linedevelopercommunity.connpass.com/event/333879/

4geru sakisaka

November 23, 2024
Tweet

Transcript

  1. ϑΝΠϧߏ੒ game.ts postbackEvent.ts postbackEventType.ts messages ├── ... ├── matchGameMessage │

    └── matchGameService.ts ├── matchGameMessage.ts ├── scoreBoardMessage │ ├── gameScoreElement.ts │ └── scoreInputElement.ts └── scoreBoardMessage.ts postBackEvents ├── GameEvent.ts ├── ScoreEvent.ts ├── ... └── base.ts w HBNFUTɿίΞϩδοΫ w QPTUCBDL&WFOUUTɿϙετόοΫͷॲཧ w QPTUCBDL&WFOUTYYɿ֤ϙετόοΫॲཧ w QPTUCBDL&WFOU5ZQFUTɿUZQFΛ؅ཧ w NTTBHFTYYɿ6*ͱϩδοΫΛ෼཭
  2. HBNFͷղઆ game.ts postbackEvent.ts postbackEventType.ts messages ├── ... ├── matchGameMessage │

    └── matchGameService.ts ├── matchGameMessage.ts ├── scoreBoardMessage │ ├── gameScoreElement.ts │ └── scoreInputElement.ts └── scoreBoardMessage.ts postBackEvents ├── GameEvent.ts ├── ScoreEvent.ts ├── ... └── base.ts w HBNFUTɿίΞϩδοΫ w QPTUCBDL&WFOUUTɿϙετόοΫͷॲཧ w QPTUCBDL&WFOUTYYɿ֤ϙετόοΫॲཧ w QPTUCBDL&WFOU5ZQFUTɿUZQFΛ؅ཧ w NTTBHFTYYɿ6*ͱϩδοΫΛ෼཭
  3. HBNFͷղઆ // const gameData = (await supabase.from('games') // .select() //

    .eq('id', user.current_game_id)).data[0] // const game = new Game(gameData) export class Game { constructor(gameData: any) { /* ... */ } scoreByA() { /* ... */ } scoreByB() { /* ... */ } undo() { /* ... */ } shot(ball) { /* ... */ } dead() { /* ... */ } switch() { /* ... */ } status() { /* ... */ } winner() { /* ... */ } } w 4VQBCBTFͷΦϒδΣΫτΛ 
 ͦͷ··౉͢ w ήʔϜͷ৚݅Λ؅ཧ͢Δ w ݱঢ়ɺ໭͢ɺަ୅ɺউར৚݅ w 6*ͱϩδοΫΛ෼͚ͯɺ 
 ؆୯ʹςετ͠΍͘͢
  4. QPTUBCBDLͷղઆ game.ts postbackEvent.ts postbackEventType.ts messages ├── ... ├── matchGameMessage │

    └── matchGameService.ts ├── matchGameMessage.ts ├── scoreBoardMessage │ ├── gameScoreElement.ts │ └── scoreInputElement.ts └── scoreBoardMessage.ts postBackEvents !"" GameEvent.ts !"" ScoreEvent.ts !"" ... #"" base.ts w HBNFUTɿίΞϩδοΫ w QPTUCBDL&WFOUUTɿϙετόοΫͷॲཧ w QPTUCBDL&WFOUTYYɿ֤ϙετόοΫॲཧ w QPTUCBDL&WFOU5ZQFUTɿUZQFΛ؅ཧ w NTTBHFTYYɿ6*ͱϩδοΫΛ෼཭
  5. QPTUBCBDLͷղઆ /* postbackEvent/base.ts */ export class PostbackEvent { static events:

    number[] = [] static isTarget(event: number) { return this.events.includes(event); } execute(...) { throw new Error('ະ࣮૷Ͱ͢'); } } w #BTFDMBTTͰ͸ɺ w JT5BSHFUɿର৅͔൱͔ w &YFDVUFɿ࣮ߦ͢Δίʔυ
  6. QPTUBCBDLͷղઆ /* postbackEvent/GameEvent.ts */ export class GameEvent extends PostbackEvent {

    static events = [ postbackEventType.shot, postbackEventType.shoted, postbackEventType.dead, postbackEventType.undo, postbackEventType.switch ] async execute(postback: any, supabase: any, user: any, lineBotClient: any) { let game = new Game(...) if( game.status() === gameStatus.Done && // ήʔϜ͕ऴ͍ྃͯ͠Δ৔߹͸มߋͰ͖ͳ͍ postback.t !== postbackEventType.undo // ͨͩɺ undo ͸Մೳ ) { return [{type: "text", text: "ήʔϜ͕ऴ͍ྃͯ͠·͢"}] } switch(postback.t) { case postbackEventType.shot: game.shot(postback.ball); break; case postbackEventType.dead: game.dead(); break; case postbackEventType.undo: game.undo(); break; case postbackEventType.switch: game.switch(); break; } await supabase.from('games').update({ score: game.scores, status: game.status() }).eq('id', user.current_game_id).select() ... return replyMessages } } w (BNF&WFOUͰঢ়ଶΛߋ৽ w JT5BSHFUͰར༻͢Δ 
 FWFOUTΛఆٛ w &YFDVUFͷதͰɺΠϕϯτ͝ͱʹ 
 ॲཧΛมߋ w ઌ΄Ͳͷ(BNFDMBTTͷॲཧΛ 
 ࣮ߦͯ͠σʔλΛߋ৽͢Δ
  7. QPTUBCBDLͷղઆ /* postbackEvent.ts */ export const postbackEvent = async (event:

    any, ...) => { const postback = JSON.parse(event.postback.data); const targetEvent = [GameEvent, ...] .find( (eventClass) => eventClass.isTarget(postback.t) ) || NotFoundEvent const targetEventInstance = new targetEvent() return await targetEventInstance.execute(...) } /* postbackEventType.ts */ export const postbackEventType = { score: 2, showScore: 12, shot: 3, shoted: 4, dead: 5, undo: 6, switch: 7, start: 8, } w QPTUCBDL&WFOUUT w QPTUCBDL&WFOUͷॲཧΛ͢Δ w JGFMTFͰ͸ͳ͘഑ྻͰ؅ཧ w QPTUCBDL&WFOU5ZQFUT w UZQFΛ؅ཧ͢Δ w QPTUCBDLBDUJPOͰɺ 
 จࣈ਺੍ݶ͕͋ΔͷͰɺ 
 ൪߸Ͱ؅ཧ
  8. NFTTBHFT game.ts postbackEvent.ts postbackEventType.ts messages !"" ... !"" matchGameMessage $

    #"" matchGameService.ts !"" matchGameMessage.ts !"" scoreBoardMessage $ !"" gameScoreElement.ts $ #"" scoreInputElement.ts #"" scoreBoardMessage.ts postBackEvents ├── GameEvent.ts ├── ScoreEvent.ts ├── ... └── base.ts w TDPSF#PBSE.FTTBHFUT w NFTTBHFͷ+40/Λ؅ཧ͢Δ w TDPSF#PBSE.FTTBHFYYYYUT w NFTTBHFͰར༻͢ΔϩδοΫΛهड़ w &MFNFOUࢠཁૉʹ෼͚Δ w 4FSWJDFඞཁͳϩδοΫΛ࢖͏