-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #653 from eesast/dev
chore: add player code
- Loading branch information
Showing
275 changed files
with
943,499 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# CAPI: go | ||
|
||
## 简介 | ||
|
||
Go 通信组件与选手接口 | ||
|
||
## 敬请期待 | ||
|
||
Go 选手接口目前还在实验当中,参见:[实验性选手 Go 接口](../../experimental/CAPI/go/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# experimental/CAPI | ||
|
||
## 简介 | ||
|
||
实验性选手接口 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
# Protocol Buffers generated code directory | ||
/API/proto/ | ||
|
||
# Executables | ||
/API/API | ||
/API/API.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
thuai6 "API/thuai6" | ||
) | ||
|
||
const Asynchronous = false | ||
|
||
func GetStudentType() []thuai6.StudentType { | ||
return []thuai6.StudentType{thuai6.Athlete, thuai6.Teacher, thuai6.StraightAStudent, thuai6.Sunshine} | ||
} | ||
|
||
func GetTrickerType() thuai6.TrickerType { | ||
return thuai6.Assassin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package core | ||
|
||
import ( | ||
thuai6 "API/thuai6" | ||
) | ||
|
||
type ILogic interface { | ||
Move(time int64, angle float64) bool | ||
TryConnection() bool | ||
GetStudentSelfInfo() *thuai6.Student | ||
GetTrickerSelfInfo() *thuai6.Tricker | ||
} | ||
|
||
type StudentAPI struct { | ||
logic ILogic | ||
} | ||
|
||
type TrickerAPI struct { | ||
logic ILogic | ||
} | ||
|
||
func NewStudentAPI(logic ILogic) *StudentAPI { | ||
return &StudentAPI{ | ||
logic: logic, | ||
} | ||
} | ||
|
||
func NewTrickerAPI(logic ILogic) *TrickerAPI { | ||
return &TrickerAPI{ | ||
logic: logic, | ||
} | ||
} | ||
|
||
func (api *StudentAPI) Move(timeInMilliseconds int64, angleInRadian float64) <-chan bool { | ||
res := make(chan bool, 1) | ||
go func() { | ||
res <- api.logic.Move(timeInMilliseconds, angleInRadian) | ||
}() | ||
return res | ||
} | ||
|
||
func (api *StudentAPI) GetSelfInfo() *thuai6.Student { | ||
return api.logic.GetStudentSelfInfo() | ||
} | ||
|
||
func (api *StudentAPI) StartTimer() { | ||
// Nothing | ||
} | ||
|
||
func (api *StudentAPI) EndTimer() { | ||
// Nothing | ||
} | ||
|
||
func (api *StudentAPI) Play(ai thuai6.IAI) { | ||
ai.StudentPlay(api) | ||
} | ||
|
||
func (api *TrickerAPI) Move(timeInMilliseconds int64, angleInRadian float64) <-chan bool { | ||
res := make(chan bool, 1) | ||
go func() { | ||
res <- api.logic.Move(timeInMilliseconds, angleInRadian) | ||
}() | ||
return res | ||
} | ||
|
||
func (api *TrickerAPI) GetSelfInfo() *thuai6.Tricker { | ||
return api.logic.GetTrickerSelfInfo() | ||
} | ||
|
||
func (api *TrickerAPI) StartTimer() { | ||
// Nothing | ||
} | ||
|
||
func (api *TrickerAPI) EndTimer() { | ||
// Nothing | ||
} | ||
|
||
func (api *TrickerAPI) Play(ai thuai6.IAI) { | ||
ai.TrickerPlay(api) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package core | ||
|
||
import ( | ||
pb "API/proto" | ||
thuai6 "API/thuai6" | ||
"context" | ||
"sync" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
type Communication struct { | ||
stub pb.AvailableServiceClient | ||
messageToClient *pb.MessageToClient | ||
haveNewMessage bool | ||
mtxMessage sync.Mutex | ||
cvMessage *sync.Cond | ||
} | ||
|
||
func NewCommunication(ip string, port string) (*Communication, error) { | ||
addr := ip + ":" + port | ||
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stub := pb.NewAvailableServiceClient(conn) | ||
comm := &Communication{stub: stub, messageToClient: nil, haveNewMessage: false, mtxMessage: sync.Mutex{}} | ||
comm.cvMessage = sync.NewCond(&comm.mtxMessage) | ||
return comm, nil | ||
} | ||
|
||
func (self *Communication) TryConnection(playerID int64) bool { | ||
res, err := self.stub.TryConnection(context.Background(), &pb.IDMsg{PlayerId: playerID}) | ||
if err != nil { | ||
// TODO: do loggings | ||
return false | ||
} | ||
return res.GetActSuccess() | ||
} | ||
|
||
func (self *Communication) AddPlayer(playerID int64, playerType thuai6.PlayerType, studentType thuai6.StudentType, trickerType thuai6.TrickerType) { | ||
var playerMsg pb.PlayerMsg | ||
playerMsg.PlayerId = playerID | ||
playerMsg.PlayerType = thuai6.PlayerTypeToProto[playerType] | ||
if playerType == thuai6.StudentPlayer { | ||
playerMsg.JobType = &pb.PlayerMsg_StudentType{StudentType: thuai6.StudentTypeToProto[studentType]} | ||
} else if playerType == thuai6.TrickerPlayer { | ||
playerMsg.JobType = &pb.PlayerMsg_TrickerType{TrickerType: thuai6.TrickerTypeToProto[trickerType]} | ||
} else { | ||
// TODO: Report ERROR | ||
} | ||
|
||
go func() { | ||
msgReader, err := self.stub.AddPlayer(context.Background(), &playerMsg) | ||
if err != nil { | ||
// TODO: Report ERROR | ||
} | ||
for { | ||
msg, err := msgReader.Recv() | ||
if err != nil { | ||
break | ||
} | ||
|
||
func() { | ||
self.mtxMessage.Lock() | ||
defer self.mtxMessage.Unlock() | ||
self.messageToClient = msg | ||
self.haveNewMessage = true | ||
self.cvMessage.Signal() | ||
}() | ||
} | ||
}() | ||
} | ||
|
||
func (self *Communication) Move(time int64, angle float64, playerID int64) bool { | ||
res, err := self.stub.Move(context.Background(), &pb.MoveMsg{ | ||
TimeInMilliseconds: time, | ||
Angle: angle, | ||
PlayerId: playerID, | ||
}) | ||
if err != nil { | ||
// TODO: do loggings | ||
return false | ||
} | ||
return res.GetActSuccess() | ||
} | ||
|
||
func (self *Communication) GetMessageToClient() pb.MessageToClient { | ||
self.mtxMessage.Lock() | ||
defer self.mtxMessage.Unlock() | ||
for !self.haveNewMessage { | ||
self.cvMessage.Wait() | ||
} | ||
self.haveNewMessage = false | ||
return *self.messageToClient | ||
} |
Oops, something went wrong.