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/
indigo_plant.rb
遊戲中玩家可以透過建築階段,支付卡片的費用後,將卡片面朝上放置於面前,成為玩家擁有的建築
並且可分為:
定義一下玩家資料的儲存格式
目前的 game 回傳值
# app/views/api/v1/games/_game.json.jbuilder
json.id game.id
json.status game.status
if game.status_unknown?
json.message "game not set up yet"
else
json.game_config do
json.seed game.seed
end
json.game_data do
json.current_price game.current_price
json.supply_pile game.game_data["supply_pile"]
end
end
實際遊玩時,有些資料不能被玩家知道,例如 牌庫是什麼牌、貨物是什麼牌、其他玩家的手牌、棄牌堆有什麼牌,==目前先都顯示==
玩家自己棄掉的手牌,也許可以顯示出來,增加決策性?
在 key game_data
中加上 players[]
其中 player
要儲存
# app/views/api/v1/games/_game.json.jbuilder
json.ignore_nil! # 把 nil 的欄位拿掉
# ...
json.game_data do
json.current_price game.current_price
json.supply_pile game.game_data["supply_pile"]
json.players game.players do |player|
json.id player.id
json.hand player.hand
json.buildings player.buildings do |building|
json.id building.id
json.good_id building.good_id
json.card_ids building.card_ids
end
end
end
# ...
# spec/requests/api/v1/games_spec.rb
# ...
response '200', 'Game created' do
schema type: :object,
properties: {
id: { type: :integer },
status: { type: :string }
},
required: [ 'id', 'status' ]
run_test! do
json = JSON.parse(response.body)
expect(json['status']).to eq('playing')
expect(json['game_config']['seed']).to eq('1234567890abcdef')
expect(json['game_data']['current_price']).to match_array([ 1, 2, 2, 2, 3 ])
expect(json['game_data']['supply_pile'].size).to eq(110 - 4)
expect(json['game_data']['supply_pile'][8]).to eq("01")
expect(json['game_data']['players'].size).to eq(4)
expect(json['game_data']['players'][0]['buildings'].size).to eq(1)
expect(json['game_data']['players'][0]['buildings'][0]['id']).to eq("01")
end
end
# ...
染坊
發給每位玩家作為起始建築玩家
與 建築
model (use Struct)# app/models/game.rb
class Game < ApplicationRecord
# ...
Player = Struct.new(:id, :hand, :buildings) do
def to_json
{
id: id,
hand: hand,
buildings: buildings
}
end
end
Building = Struct.new(:id, :good_id, :card_ids)
# ...
end
generate_players
類別方法# app/models/game.rb
class Game < ApplicationRecord
# ...
class << self
# ...
private
def generate_players
human_player = Player.new(1, [], [])
bot_players = 3.times.map { |i| Player.new(i + 2, [], []) }
[ human_player ] + bot_players
end
# ...
end
# ...
end
players
實體方法,用來將 players JSON data => 物件# app/models/game.rb
class Game < ApplicationRecord
# ...
def players
return [] unless game_data["players"]
JSON.parse(game_data["players"]).map do |player|
Player.new(player["id"], player["hand"], player["buildings"].map { |building|
Building.new(building["id"], building["good_id"], building["card_ids"])
})
end
end
# ...
end
start_new_game
加入玩家資訊# app/models/game.rb
class Game < ApplicationRecord
# ...
class << self
def start_new_game(seed: nil)
game.seed = seed || SecureRandom.hex(16)
# 產生玩家
game.game_data[:players] = generate_players.to_json
game.save
end
# ...
end
# ...
end
染坊
作為起始建築# app/models/game.rb
class Game < ApplicationRecord
# ...
class << self
def start_new_game(seed: nil)
# ...
# 4. Give each player 1 indigo plant as their initial building
players = game.players
players.each do |player|
player.buildings += [ Building.new("01") ]
end
game.game_data[:players] = players.to_json
# 5. deal 4 cards to each player as their initial hand, hidden from other players
# 6. choose first player
game.save
game
end
# ...
end
# ...
end
rails s
# open another session
curl localhost:3000/api/v1/games -d '' | jq
{
"id": 3,
"status": "playing",
"game_config": {
"seed": "5b693ed803e9427d4caa738918373d74"
},
"game_data": {
"current_price": [
1,
2,
2,
2,
3
],
"supply_pile": [
// 略
],
"players": [
{
"id": 1,
"hand": [],
"buildings": [
{
"id": "01"
}
]
},
{
"id": 2,
"hand": [],
"buildings": [
{
"id": "01"
}
]
},
{
"id": 3,
"hand": [],
"buildings": [
{
"id": "01"
}
]
},
{
"id": 4,
"hand": [],
"buildings": [
{
"id": "01"
}
]
}
]
}
}
收工.
也可以直接看 diff 7a645a0
Sprint 2: 實作遊戲開始
以上不代表明天會做,如有雷同純屬巧合
SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。
歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!
Discord頻道連結:https://sideproj.tw/dc