Current Sprint 2: 實作遊戲開始
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/
盤點了遊戲需要的 table, action
今天來定義一下 Sprint 2
註:提醒自己要製作 詞彙表 XD
用來決定每次遊戲起始狀態,並將隨機種子記錄下來,方便重現
(理論上這樣可以做到在給定時間點,系統知道某張牌應該是什麼)
現在的 games table schema
create_table "games", force: :cascade do |t|
t.integer "status", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
參照昨天設計,要增加
rails g migration add_game_details
# db/migrate/20240911153802_add_game_details.rb
class AddGameDetails < ActiveRecord::Migration[7.2]
def change
add_column :games, :seed, :string
add_column :games, :version, :string
add_column :games, :game_data, :json, default: {}
add_column :games, :result, :json, default: {}
end
end
rails db:migrate
# == 20240911153802 AddGameDetails: migrating ===================================
# -- add_column(:games, :seed, :string)
# -> 0.0054s
# -- add_column(:games, :version, :string)
# -> 0.0004s
# -- add_column(:games, :game_data, :json, {:default=>{}})
# -> 0.0017s
# -- add_column(:games, :result, :json, {:default=>{}})
# -> 0.0010s
# == 20240911153802 AddGameDetails: migrated (0.0086s) ==========================
class Api::V1::GamesController < ApplicationController
# ...
def create
@game = Game.new(status: "playing")
unless @game.save
return render json: { error: @game.errors.full_messages }
end
# generate a random seed
@game.seed = SecureRandom.hex(16)
unless @game.save
render json: { error: @game.errors.full_messages }
end
end
# ...
在 ... create a game 中,加上
expect(json['game_config']['seed']).to be_present
# Failures:
# 1) Api::V1::Games /api/v1/games post Game created returns a 200 response
# Failure/Error: expect(json['game_config']['seed']).to be_present
# NoMethodError:
# undefined method `[]' for nil
# # ./spec/requests/api/v1/games_spec.rb:56:in `block (5 levels) in <main>'
# app/views/api/v1/games/_game.json.jbuilder
json.id game.id
json.status game.status
json.game_config do
json.seed game.seed
end
rspec spec/requests/api/v1/games_spec.rb
# ...
# Finished in 0.13503 seconds (files took 0.53839 seconds to load)
# 3 examples, 0 failures
收工
完成了 Spring 2-1 產生並儲存 random seed
Sprint 2: 實作遊戲開始
提醒自己要製作 詞彙表 XD
以上不代表明天會做,如有雷同純屬巧合
SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。
歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!
Discord頻道連結:https://sideproj.tw/dc