Skip to content

Commit bfaf041

Browse files
committed
/v1/names の実装
1 parent c2b00cf commit bfaf041

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

cmd/GoBCDiceAPI/controllers/router.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ func setupV1(g *echo.Group) {
2020
version.Setup()
2121
systems := v1.NewSystemsController(g)
2222
systems.Setup()
23+
names := v1.NewNamesController(g)
24+
names.Setup()
2325
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package v1
2+
3+
import (
4+
"github.com/labstack/echo"
5+
"github.com/raa0121/GoBCDice/cmd/GoBCDiceAPI/helpers"
6+
"github.com/raa0121/GoBCDice/cmd/GoBCDiceAPI/models"
7+
)
8+
9+
type NamesController struct {
10+
Group *echo.Group
11+
}
12+
13+
func NewNamesController(g *echo.Group) *NamesController {
14+
return &NamesController{
15+
Group: g,
16+
}
17+
}
18+
19+
func (controller *NamesController) getNames(c echo.Context) error {
20+
names := models.NewNames()
21+
22+
return helpers.JSONResponseObject(c, 200, names)
23+
}
24+
25+
// Setup はコントローラの初期設定を行う。
26+
func (controller *NamesController) Setup() {
27+
controller.Group.Add("GET", "/names", controller.getNames)
28+
}
29+

cmd/GoBCDiceAPI/models/names.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package models
2+
3+
import (
4+
"github.com/raa0121/GoBCDice/cmd/GoBCDiceAPI/helpers"
5+
"github.com/raa0121/GoBCDice/pkg/dicebot/list"
6+
)
7+
8+
type Names struct {
9+
list.Names
10+
}
11+
12+
func NewNames() *Names {
13+
names := Names{
14+
Names: list.AvailableGameInfos(true),
15+
}
16+
return &names
17+
}
18+
19+
func (s *Names) ToResponseMap() helpers.ResponseMap {
20+
return helpers.ResponseMap{
21+
"names": s.Names.Name,
22+
}
23+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module github.com/raa0121/GoBCDice
22

3+
go 1.14
4+
35
require (
46
github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d
57
github.com/chzyer/logex v1.1.10 // indirect

pkg/dicebot/dicebot.go

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type DiceBot interface {
2121
GameName() string
2222
// Usage はダイスボットの使用法の説明を返す。
2323
Usage() string
24+
// SortKey は並べ替え順のよみがなを返す。
25+
SortKey() string
2426
// ExecuteCommand は指定されたコマンドを実行する。
2527
ExecuteCommand(command string, ev *evaluator.Evaluator) (*command.Result, error)
2628
}
@@ -33,6 +35,8 @@ type DiceBotBasicInfo struct {
3335
GameName string
3436
// Usage はダイスボットの使用法の説明。
3537
Usage string
38+
// SortKey は並べ替え順のよみがな。
39+
SortKey string
3640
}
3741

3842
// DiceBotImpl はダイスボットの実装のベースとなる構造体。
@@ -59,6 +63,10 @@ func (d *DiceBotImpl) Usage() string {
5963
return d.BasicInfo.Usage
6064
}
6165

66+
// SortKey は並べ替え順のよみがなを返す。
67+
func (d *DiceBotImpl)SortKey() string {
68+
return d.BasicInfo.SortKey
69+
}
6270
// ExecuteCommand は指定されたコマンドを実行する。
6371
//
6472
// 基本のダイスボットには特別なコマンドが存在しないため、必ずエラーを返す。

pkg/dicebot/gamesystem/basic/basic.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var basicInfo = dicebot.DiceBotBasicInfo{
2828
 S3d6 : 各コマンドの先頭に「S」を付けると他人結果の見えないシークレットロール
2929
 3d6/2 : ダイス出目を割り算(切り捨て)。切り上げは /2U、四捨五入は /2R。
3030
 D66 : D66ダイス。順序はゲームに依存。D66N:そのまま、D66S:昇順。`,
31+
SortKey: "*たいすほつと",
3132
}
3233

3334
// BasicInfo はダイスボットの基本情報を返す。

pkg/dicebot/list/list.go

+37
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ import (
1212
"sort"
1313
)
1414

15+
type Names struct {
16+
Name []SystemInfo
17+
}
18+
19+
type SystemInfo struct {
20+
System string `json:"system"`
21+
Name string `json:"name"`
22+
SortKey string `json:"sort_key"`
23+
}
24+
1525
// Find は指定された識別子を持つゲームシステムのダイスボットのコンストラクタを返す。
1626
// ゲームシステムが見つからなかった場合はエラーを返す。
1727
func Find(gameID string) (dicebot.DiceBotConstructor, error) {
@@ -48,5 +58,32 @@ func AvailableGameIDs(includeBasicDiceBot bool) []string {
4858
return gameIDsWithBasicDiceBot
4959
}
5060

61+
// AvailableGameInfos は利用可能なゲームシステムの情報のスライスを返す。
62+
func AvailableGameInfos(includeBasicDiceBot bool) Names {
63+
games := make([]SystemInfo, 0, len(gameIDToDiceBotConstructor))
64+
for _, k := range gameIDToDiceBotConstructor {
65+
info := SystemInfo{
66+
System: k().GameID(),
67+
Name: k().GameName(),
68+
SortKey: k().SortKey(),
69+
}
70+
games = append(games, info)
71+
}
72+
sort.Slice(games, func (i, j int) bool { return games[i].SortKey < games[j].SortKey })
73+
74+
if !includeBasicDiceBot {
75+
return Names{Name: games}
76+
}
77+
gamesWithBasicDiceBot := make([]SystemInfo, 1, len(games)+1)
78+
info := SystemInfo{
79+
System: basic.BasicInfo().GameID,
80+
Name: basic.BasicInfo().GameName,
81+
SortKey: basic.BasicInfo().SortKey,
82+
}
83+
gamesWithBasicDiceBot[0] = info
84+
gamesWithBasicDiceBot = append(gamesWithBasicDiceBot, games...)
85+
return Names{Name: gamesWithBasicDiceBot}
86+
}
87+
5188
// ゲーム識別子とダイスボットのコンストラクタとの対応
5289
var gameIDToDiceBotConstructor = map[string]dicebot.DiceBotConstructor{}

0 commit comments

Comments
 (0)