Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ $ grpcurl -v -plaintext -import-path . -proto apiServer/src/main/protobuf/room.p
grpcurl -v -plaintext -import-path . -proto apiServer/src/main/protobuf/room.proto -d '{"RoomId":"dfa65e98a18340cbb77a4fb9738d9a16","AccountId":"parent","ghostRecord":[{"x":0.1,"y":0.1,"z":0.1,"date":0}],"isGameClear":true,"date":10}' ${SERVER_ENDPOINT} room.RoomService/SendResult
```

## ws
```bash
$ wscat -c ws://localhost:18080/health


$ wscat -c "ws://localhost:18080/create_room?accountId=parent&accountName=parentName"
> {"":""}
```

```bash
$ wscat -c "ws://localhost:18080/join_room?accountId=child1&accountName=child1Name"
> {"direction":{"direction":"Up"}, "strength":0.1}

$ wscat -c "ws://localhost:18080/join_room?accountId=child2&accountName=child2Name"
$ wscat -c "ws://localhost:18080/join_room?accountId=child3&accountName=child3Name"


```

## C# code gen
```bash
$ chmod +x ./grpc-code-gen/code-gen-c#.sh
$ ./grpc-code-gen/code-gen-c#.sh ./apiServer/src/main/protobuf ./pb
```

## ssh鍵作成

`$ ssh-keygen -t rsa -f my-ssh-key -C [任意のsshユーザーネーム]`
Expand Down
40 changes: 40 additions & 0 deletions apiServer/src/main/protobuf/model.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.github.CA21engineer.HouseHackathonUnityServer.grpc";

package model;

message Operation {
Direction direction = 1;
float strength = 2; // 0 ~ 1
}

message Coordinate {
float x = 1;
float y = 2;
float z = 3;
// dateはゲームスタートからの開始ミリ秒(1s = 1000ms)
int64 date = 4; // ゲームスタートからの経過時間
}


message Member {
string AccountName = 1;
Direction Direction = 2;
}

enum Direction {
UNKNOWN = 0;
Up = 1;
Down = 2;
Left = 3;
Right =4;
}

enum ErrorType {
UNKNOWN_MESSAGE_TYPE = 0;
LOST_CONNECTION_ERROR = 1; // 親or子の接続切れ
ROOM_NOT_FOUND_ERROR = 2; // ルームが見つからない, Privateな部屋で合言葉間違いもこれ
MALFORMED_MESSAGE_TYPE = 3; // 送信のデータ形式が間違っている
}
27 changes: 27 additions & 0 deletions apiServer/src/main/protobuf/request.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.github.CA21engineer.HouseHackathonUnityServer.grpc";

import "model.proto";

package request;


message CreateRoomRequest {
string accountId = 1;
string accountName = 2;
string roomKey = 3; // Optional
}

message JoinRoomRequest {
string accountId = 1;
string accountName = 2;
string roomKey = 3; // Optional
}

message SendResultRequest {
repeated model.Coordinate ghostRecord = 1;
bool isGameClear = 2; // ゲームクリアならtrue、ゲームオーバーならfalse
int64 elapsedTime = 3; // 終了時点でのゲームスタートからの経過時間
}
34 changes: 34 additions & 0 deletions apiServer/src/main/protobuf/response.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.github.CA21engineer.HouseHackathonUnityServer.grpc";

import "model.proto";

package room;

message CreateRoomResponse {
string roomId = 1;
}

message JoinRoomResponse {
string roomId = 1;
int32 vagrant = 2; // 空き人数
}

message ReadyResponse {
string roomId = 1;
repeated model.Coordinate ghostRecord = 2;
repeated model.Member member = 3;
model.Direction yourDirection = 4;
}

message SimpleGameResultResponse {
bool isGameClear = 1; // ゲームクリアならtrue、ゲームオーバーならfalse
int64 elapsedTime = 2; // 終了時点でのゲームスタートからの経過時間
}

message ErrorResponse {
model.ErrorType errorType = 1;
string message = 2;
}
111 changes: 0 additions & 111 deletions apiServer/src/main/protobuf/room.proto

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import akka.http.scaladsl.{Http, HttpConnectionContext}
import akka.http.scaladsl.UseHttp2.Always
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.stream.ActorMaterializer
import com.github.CA21engineer.HouseHackathonUnityServer.grpc.room.RoomServicePowerApiHandler
import com.github.CA21engineer.HouseHackathonUnityServer.service.RoomServicePowerApiImpl
import com.github.CA21engineer.HouseHackathonUnityServer.service.{RoomAggregates, RoomServiceImpl}
import com.typesafe.config.ConfigFactory

import scala.concurrent.{ExecutionContextExecutor, Future}

import scalikejdbc._

object Main extends App {
Expand All @@ -28,21 +26,18 @@ object Main extends App {
Class.forName("com.mysql.jdbc.Driver")
ConnectionPool.singleton("jdbc:mysql://mysql:3306/unity?rewriteBatchedStatements=true", "ca21engineer", "pass")

val roomService: PartialFunction[HttpRequest, Future[HttpResponse]] = RoomServicePowerApiHandler.partial(new RoomServicePowerApiImpl)
val roomAggregates = new RoomAggregates()

val serviceHandlers: HttpRequest => Future[HttpResponse] =
ServiceHandler.concatOrNotFound(roomService)
val restServiceHandlers = new RestServiceHandler(new RoomServiceImpl(roomAggregates))
val restBindingFuture = Http().bindAndHandle(
handler = restServiceHandlers.toRoutes,
interface = "0.0.0.0",
port = 18080
)

val bindingFuture =
Http().bindAndHandleAsync(
handler = serviceHandlers,
interface = "0.0.0.0",
port = 18080,
connectionContext = HttpConnectionContext(http2 = Always)
)

sys.addShutdownHook {
bindingFuture
restBindingFuture
.flatMap(_.unbind())
.onComplete(_ => system.terminate())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.CA21engineer.HouseHackathonUnityServer.apiServer

import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.ws.Message
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import akka.stream.{KillSwitches, Materializer}
import akka.stream.scaladsl.Flow
import ch.megard.akka.http.cors.scaladsl.CorsDirectives._
import com.github.CA21engineer.HouseHackathonUnityServer.model._
import com.github.CA21engineer.HouseHackathonUnityServer.service.RoomServiceImpl
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import org.slf4j.{Logger, LoggerFactory}

class RestServiceHandler(roomService: RoomServiceImpl)(implicit materializer: Materializer) extends FailFastCirceSupport {
val logger: Logger = LoggerFactory.getLogger(getClass)

type QueryP[Q] = Directive[Q] => Route

val killSwitch = KillSwitches.shared("health")
val flow = Flow[Message] via killSwitch.flow

def toRoutes: Route = cors() {
Router(
route(GET, "create_room", createRoom),
route(GET, "join_room", joinRoom),
route(GET, "health", health)
).create
}

def health: QueryP[Unit] = _ {
val newFlow = flow.watchTermination()((f, d) => {
d.foreach { _ =>
logger.info("RoomAggregates: watchTermination.watchParentSource")
killSwitch.shutdown()
}(materializer.executionContext)
f
})
handleWebSocketMessages(newFlow)
}

def createRoom: QueryP[Unit] = _ {
parameters('accountId.as[String], 'roomKey.as[String]?, 'accountName.as[String]) { (accountId, roomKey, accountName) =>
handleWebSocketMessages(roomService.parentFlow(CreateRoomRequest(accountId, roomKey, accountName)))
}
}

def joinRoom: QueryP[Unit] = _ {
parameters('accountId.as[String], 'roomKey.as[String]?, 'accountName.as[String]) { (accountId, roomKey, accountName) =>
handleWebSocketMessages(roomService.childFlow(JoinRoomRequest(accountId, roomKey, accountName)))
}
}





}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.CA21engineer.HouseHackathonUnityServer.model

case class Coordinate(
case class CoordinateRecord(
roomID: String,
x: Float,
y: Float,
z: Float,
pastMilliSecond: Int,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.CA21engineer.HouseHackathonUnityServer.model

// Up, Down, Left, Right
case class Direction(direction: String)

object Direction {
def all: List[Direction] = List(Direction("Up"), Direction("Down"), Direction("Left"), Direction("Right"))

def shuffle: List[Direction] = scala.util.Random.shuffle(all)
}
Loading