- Day 22 - 建立 Step model 用來儲存遊戲(每一步)紀錄
Current Sprint: 3. 遊戲基本流程完成
repo: https://github.com/side-project-at-SPT/ithome-ironman-2024-san-juan
swagger docs: https://side-project-at-spt.github.io/ithome-ironman-2024-san-juan/
為了計算下一位玩家是誰,定義以下方法
turn_over
告訴遊戲玩家行動結束phase_over
告訴遊戲 xx 階段結束round_over
告訴遊戲回合結束notify_turn_begins
通知玩家輪到你了phase
,用來描述目前是哪個職業階段礦工
階段行動step
model 用來儲存遊戲(每一步)紀錄議員
階段行動建築
階段行動生產
階段行動交易
階段行動step
model 用來儲存遊戲(每一步)紀錄step
modelrails g model step game:belongs_to game_data:json description steps:integer reason:integer
# db/migrate/20240923173105_create_game_steps.rb
class CreateGameSteps < ActiveRecord::Migration[7.2]
def change
create_table :game_steps do |t|
t.belongs_to :game, null: false, foreign_key: true
t.json :game_data, null: false, default: {}
t.string :description
t.integer :steps, null: false
t.integer :reason, null: false, default: 0
# 0: unknown, 1: game_init, 2: choose_role, 3: builder_action, 4: producer_action,
# 5: trader_action, 6: prospector_action, 7: councillor_action, 8: game_over
# 9: round_begin, 10: round_end,
# 11: phase_begin, 12: phase_end
# 13: player_turn_begin, 14: player_turn_end
t.timestamps
t.index [ :game_id, :steps ], unique: true
end
end
end
# app/models/game_step.rb
class GameStep < ApplicationRecord
belongs_to :game
enum :reason, {
unknown: 0,
game_init: 1,
choose_role: 2,
builder_action: 3,
producer_action: 4,
trader_action: 5,
prospector_action: 6,
councillor_action: 7,
game_over: 8,
round_begin: 9,
round_end: 10,
phase_begin: 11,
phase_end: 12,
player_turn_begin: 13,
player_turn_end: 14
}, prefix: true
end
generate_game_steps
用來建立 game_step
# app/models/game.rb
class Game < ApplicationRecord
# ...
has_many :game_steps
def generate_game_steps(reason: 0, description: "")
steps = game_steps.last&.steps || -1
game_steps.create(game_data: game_data, steps: steps + 1, reason: reason, description: description)
end
# ...
end
generate_game_steps
之後可以在方法內呼叫 websocket broadcast
達到前端播放動畫的效果
收工
以上不代表明天會做,如有雷同純屬巧合
SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。
歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!
Discord頻道連結: https://sideproj.tw/dc