Skip to content

Commit

Permalink
Merge pull request #653 from eesast/dev
Browse files Browse the repository at this point in the history
chore: add player code
  • Loading branch information
TCL606 authored Jun 24, 2023
2 parents ddf0bf4 + 7eaad7d commit d67e7e7
Show file tree
Hide file tree
Showing 275 changed files with 943,499 additions and 392 deletions.
1 change: 1 addition & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
source: '.'
extensions: 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx,i,ixx,ipp,i++'
clangFormatVersion: 14
exclude: './players'
inplace: False

dotnet-format-checking:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/upload_COS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
- name: Pip Install paramiko
run: pip install paramiko

- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v3
with:
name: my-artifact
path: ./THUAI6
Expand Down
9 changes: 9 additions & 0 deletions CAPI/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# CAPI: go

## 简介

Go 通信组件与选手接口

## 敬请期待

Go 选手接口目前还在实验当中,参见:[实验性选手 Go 接口](../../experimental/CAPI/go/)
4 changes: 2 additions & 2 deletions dependency/proto/Protos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</ItemGroup>-->

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.1" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.23.1" />
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.23.3" />
<PackageReference Include="Grpc" Version="2.46.6" />
<PackageReference Include="Grpc.Core" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.54.0">
Expand Down
36 changes: 19 additions & 17 deletions dependency/shell/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ cd ../..

for i in {1..3}
do
find . -iname "*.c" \
-or -iname "*.h" \
-or -iname "*.C" \
-or -iname "*.H" \
-or -iname "*.cpp" \
-or -iname "*.hpp" \
-or -iname "*.cc" \
-or -iname "*.hh" \
-or -iname "*.c++" \
-or -iname "*.h++" \
-or -iname "*.cxx" \
-or -iname "*.hxx" \
-or -iname "*.i" \
-or -iname "*.ixx" \
-or -iname "*.ipp" \
-or -iname "*.i++" \
| xargs clang-format -i
find . \
-not -path "./players/*" -and \( \
-iname "*.c" \
-or -iname "*.h" \
-or -iname "*.C" \
-or -iname "*.H" \
-or -iname "*.cpp" \
-or -iname "*.hpp" \
-or -iname "*.cc" \
-or -iname "*.hh" \
-or -iname "*.c++" \
-or -iname "*.h++" \
-or -iname "*.cxx" \
-or -iname "*.hxx" \
-or -iname "*.i" \
-or -iname "*.ixx" \
-or -iname "*.ipp" \
-or -iname "*.i++" \
\) | xargs clang-format -i
done

pushd logic && dotnet format && popd
Expand Down
7 changes: 6 additions & 1 deletion docs/GameRules.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ $$
- 主动技能
0. 惩罚 Punish
- CD:45s
- “使用瞬间,在视野距离/3范围内(不是可视范围)的翻窗、开锁门、攻击前后摇、使用技能期间的捣蛋鬼会被眩晕(3070+$\frac{500×已受伤害}{基本伤害(1500000)×2^{开局老师数量-1}}$)ms”
- 使用瞬间,在视野距离/3范围内(不是可视范围)的翻窗、开锁门、攻击前后摇、使用技能期间的捣蛋鬼会被眩晕

$$
(3070+\frac{500×已受伤害}{基本伤害(1500000)×2^{开局老师数量-1}})ms
$$

- 其眩晕得分为正常眩晕得分/2^开局老师数量-1^
1. 喝茶 HaveTea
- CD:90s
Expand Down
5 changes: 5 additions & 0 deletions experimental/CAPI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# experimental/CAPI

## 简介

实验性选手接口
28 changes: 28 additions & 0 deletions experimental/CAPI/go/.gitignore
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
15 changes: 15 additions & 0 deletions experimental/CAPI/go/API/ai.go
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
}
80 changes: 80 additions & 0 deletions experimental/CAPI/go/API/core/api.go
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)
}
97 changes: 97 additions & 0 deletions experimental/CAPI/go/API/core/communication.go
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
}
Loading

0 comments on commit d67e7e7

Please sign in to comment.