Skip to content

Commit

Permalink
Merge pull request #15 from Longwater1234/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Longwater1234 authored Jul 25, 2024
2 parents a1e4d15 + abccd8d commit fef7818
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 230 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ server-space-checkers
checkers-backend
bin/
*.prof
.DS_Store
.DS_Store
*.zip
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Server for SpaceCheckers game. Written entirely using only Go's standard library
- PLAYER 1 is always RED 🔴. PLAYER 2 IS always BLACK ⚫️
- When a piece is on EVEN row of cells. The number denotes the cell_index _delta_ between two pieces.

isEven : true, deltaForward = 4 , deltaBehindEnemy =5
```
4 3
\ /
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [ ] Client needs to check if server version is not NEWER than its own SPECIFIED
- [ ] Extra endpoint for live server metrics
- [ ] Add timeout after every move. 20 seconds or 10 seconds

- [ ] try avoid deadlocks

## timer example

Expand Down
230 changes: 115 additions & 115 deletions game/base_payload.pb.go

Large diffs are not rendered by default.

52 changes: 45 additions & 7 deletions game/pieces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package game

import (
"checkers-backend/player"
"log"
"math"
)

Expand All @@ -18,14 +20,25 @@ const (
Piece_Black
)

func (t PieceType) String() string {
switch t {
case Piece_Black:
return "Piece_Black"
case Piece_Red:
return "Piece_Red"
default:
return "unknown"
}
}

type Piece struct {
Id int32 // unique piece id
IsKing bool // whether this piece is King
Pos Vec2 // current piece position
PieceColor PieceType // either red or black
}

// When a piece is moved diagonally to given `destPos`. Returns TRUE if move is successful, else FALSE
// MoveSimple actually moves this piece diagonally to given `destPos` by 1 cell. Returns TRUE if successful
func (p *Piece) MoveSimple(destPos *Vec2) bool {
var deltaX = float64(destPos.X - p.Pos.X)
var deltaY = float64(destPos.Y - p.Pos.Y)
Expand All @@ -49,8 +62,7 @@ func (p *Piece) MoveSimple(destPos *Vec2) bool {
return true
}

// When capturing opponent, Move this piece by 2 cells diagonally to the given `destPos`. Returns TRUE
// if success, else FALSE
// MoveCapture actually moves this piece by 2 cells diagonally to the given `destPos`. Returns TRUE if success
func (p *Piece) MoveCapture(destPos *Vec2) bool {
var deltaX = float64(destPos.X - p.Pos.X)
var deltaY = float64(destPos.Y - p.Pos.Y)
Expand All @@ -74,13 +86,39 @@ func (p *Piece) MoveCapture(destPos *Vec2) bool {
return true
}

// IsEvenCellRow determines wheter given cell_index is on even Row
// IsEvenCellRow determines whether given `cellIdx` is on even Row
func IsEvenCellRow(cellIdx int32) bool {
rowNumber := 9 - (cellIdx-1)/4
return (rowNumber%2 == 0)
return rowNumber%2 == 0
}

// AwayFromEdge returns TRUE if given position is NOT on any edge of board
func AwayFromEdge(pos *Vec2) bool {
// IsAwayFromEdge returns TRUE if given position is NOT on any edge of board
func IsAwayFromEdge(pos *Vec2) bool {
return pos.X > 0 && pos.X < 7*SIZE_CELL && pos.Y > 0 && pos.Y < 7*SIZE_CELL
}

// HasWinner determines if `p` has won the match, and notifies both players if TRUE.
func HasWinner(p *player.Player, opponent *player.Player) bool {
if len(opponent.Pieces) == 0 {
//`opponent` has lost, `p` has won! game over
p.SendMessage(&BasePayload{
Notice: "Congrats! You won! GAME OVER",
Inner: &BasePayload_WinlosePayload{
WinlosePayload: &WinLosePayload{
Winner: TeamColor_TEAM_UNSPECIFIED, //TODO fix me
},
},
})
opponent.SendMessage(&BasePayload{
Notice: "Sorry! You lost! GAME OVER",
Inner: &BasePayload_WinlosePayload{
WinlosePayload: &WinLosePayload{
Winner: TeamColor_TEAM_UNSPECIFIED, //TODO fix me
},
},
})
log.Println("🏆 We got a winner!", p.Name, " has won!")
return true
}
return false
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module checkers-backend

go 1.21
go 1.21

require (
// github.com/goccy/go-json v0.10.3
golang.org/x/net v0.25.0
google.golang.org/protobuf v1.34.1
golang.org/x/net v0.27.0
google.golang.org/protobuf v1.34.2
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"golang.org/x/net/websocket"
)

var SERVER_VERSION = "2024.6.0"
const SERVER_VERSION = "2024.7.0"

const maxRequestSize int = 2 << 10 //2KB
const maxRequestSize int = 1 << 10 //1KB

var numPlayers atomic.Uint32 // total number of LIVE players
var lobby = make(chan *player.Player, 2) // waiting room for players
Expand Down Expand Up @@ -104,7 +104,7 @@ func listenForJoins() {
//start the match in new goroutine
go func(p1, p2 *player.Player) {
//Sleep necessary for [p2] Client to process prev message
time.Sleep(100 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
gameOver := make(chan bool, 1)
room.RunMatch(p1, p2, gameOver)
<-gameOver //block until match ends
Expand Down
10 changes: 5 additions & 5 deletions proto_schema/base_payload.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ message StartPayload {
}

message MovePayload {
// Represents destination cell
message DestCell {
// Where will this player land on
message Detination {
int32 cell_index = 1;
float x = 2;
float y = 3;
Expand All @@ -51,7 +51,7 @@ message MovePayload {
// older cell index for this pieceId
int32 source_cell = 3;
// destination cell
DestCell dest_cell = 4;
Detination destination = 4;
}

// whenever any of the players exits, or server terminates Match
Expand All @@ -73,7 +73,7 @@ message CapturePayload {
}

// Destination of hunterPiece after capturing prey
message HunterDestCell {
message HunterDestination {
int32 cell_index = 1;
float x = 2;
float y = 3;
Expand All @@ -84,7 +84,7 @@ message CapturePayload {
// the attacking player's piece
int32 hunter_piece_id = 2;
TargetDetails details = 4;
HunterDestCell hunter_dest_cell = 5;
HunterDestination destination = 5;
}

/** When one of player wins. Also, this ends the match */
Expand Down
4 changes: 2 additions & 2 deletions room/capture_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func processCapturePiece(basePayload *game.BasePayload, gameMap map[int32]*game.

// validateCapture when player `p` attacks by opponent's piece. returns TRUE if valid, else FALSE
func validateCapture(captureReq *game.CapturePayload, gameMap map[int32]*game.Piece, opponent *player.Player) bool {
if captureReq.GetDetails() == nil || captureReq.GetHunterDestCell() == nil {
if captureReq.GetDetails() == nil || captureReq.GetDestination() == nil {
return false
}
hunterPieceId := captureReq.GetHunterPieceId()
Expand All @@ -56,7 +56,7 @@ func validateCapture(captureReq *game.CapturePayload, gameMap map[int32]*game.Pi
}

//check if destCell already has a Piece or not
destCell := captureReq.GetHunterDestCell()
destCell := captureReq.GetDestination()
_, hasValue := gameMap[destCell.GetCellIndex()]
if hasValue {
return false
Expand Down
Loading

0 comments on commit fef7818

Please sign in to comment.