Skip to content

Commit

Permalink
fixes + add workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaniog committed Apr 17, 2024
1 parent 1940fcf commit e87706c
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 23 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 'stable'

- name: Build
run: |
go mod tidy
go build -v ./...
- name: Lint
uses: golangci/golangci-lint-action@v4
with:
version: latest

- name: Test
run: |
go mod tidy
go test -v -race -coverpkg=./... ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
build
.env
2 changes: 0 additions & 2 deletions front/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
<canvas id="canvas"></canvas>
<button id="playBtn" class="btn btn-primary mt-5">GO</button>
</div>
<div id="logs"></div>
</body>

<script src="js/logs.js"></script>
<script src="js/sockets.js"></script>
<script src="js/game/loading.js"></script>
<script src="js/game/field.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion front/static/js/game/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Field {
this.colors = {
default: '#ffffff' // Default color is white
};
this.cellSize = 15; // Adjust as needed
this.cellSize = 13; // Adjust as needed
}

visualize(data) {
Expand Down
8 changes: 2 additions & 6 deletions front/static/js/game/game.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
let socket

const HTTP = "http"
const WS = "ws"

function connectSocket(url) {
function runGame(url) {
// TODO убрать
try {
socket.close()
Expand Down Expand Up @@ -53,11 +50,10 @@ document.addEventListener('keydown', function (event) {
});

function createGame() {
let id = 0
fetch(`${HTTP}://${window.location.host}/find-hub/`)
.then(r => r.json())
.then(data => {
connectSocket(`ws/play/${data.id}`)
runGame(`ws/play/${data.id}`)
})
}

Expand Down
39 changes: 39 additions & 0 deletions front/static/js/game/lobby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Unused now

class Options {
FPS
FrameDuration
SnakeLen
SnakesAmount
Duration
Field
}

class OptionsManager {
constructor() {
this.options = new Options()
this.optionsDiv = document.getElementById("options")
}

setOptions(options) {
this.options = options
this.optionsDiv.innerText = JSON.stringify(this.options)
}
}

manager = new OptionsManager()

const HTTP = "http"
const WS = "ws"

class Lobby {
run(id) {
this.socket = new WebSocket(`${WS}://${window.location.host}/ws/play/${id}`)
this.socket.onmessage = function (event) {
const e = JSON.parse(event.data)
if (e.event === "update_options") {
manager.setOptions(e.options)
}
}
}
}
1 change: 0 additions & 1 deletion internal/game/food.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package game

type Food struct {
Point Point `json:"point"`
game *Game
}

func (f Food) HandleCollision(s *Snake) {
Expand Down
62 changes: 62 additions & 0 deletions internal/play/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package play

import (
"context"
"github.com/Vaniog/Snaker/internal/game"
"github.com/Vaniog/Snaker/internal/play/event"
"time"
)

type Bot struct {
*Player
game *Game
snake *game.Snake
}

func (b *Bot) Run(ctx context.Context) {
ticker := time.NewTicker(b.game.Opts.FrameDuration / 10)
go b.fakeReadPump(ctx)
for {
select {
case <-ticker.C:
b.Update()
case <-ctx.Done():
return
}
}
}

func (b *Bot) Update() {
if len(b.snake.Body) < 3 {
return
}
goodDrc := b.snake.Drc
ds := map[game.Direction]int{
game.Up: 0,
game.Right: 0,
game.Down: 0,
game.Left: 0,
}

for drc := range ds {
goodDrc = drc
var newHead = b.game.Field.ToBounds(b.snake.Head().Move(drc))
good := true
for _, s := range b.game.Snakes {
handleLen := len(s.Body)
if s == b.snake {
handleLen--
}
for _, p := range s.Body[0:handleLen] {
if p == newHead {
good = false
}
}
}
if good {
break
}
}

b.Player.Input <- event.Bytes(rotate{Type: typeRotate, Drc: goodDrc})
}
19 changes: 8 additions & 11 deletions internal/play/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,37 @@ type PlayerEvent struct {
}

type Game struct {
*game.Game
players map[*Player]*game.Snake
game *game.Game

events chan PlayerEvent
}

func newGame(lobby *Lobby) *Game {
g := &Game{
Game: game.NewGame(lobby.opts),
events: lobby.events,
players: make(map[*Player]*game.Snake, len(lobby.players)),
game: game.NewGame(lobby.opts),
}
for _, p := range lobby.players {
g.players[p] = g.game.RegisterSnake()
g.players[p] = g.RegisterSnake()
}
return g
}

func (g *Game) Run(ctx context.Context) {
gameTicker := time.NewTicker(g.game.Opts.FrameDuration)
gameTicker := time.NewTicker(g.Opts.FrameDuration)
defer gameTicker.Stop()
g.game.Start()
g.Start()

// TODO move to better place
for p := range g.players {
go p.inputPump(ctx)
}
g.broadcast(event.Bytes(event.Event{Type: typeGameStart}))

for {
for {
select {
case <-gameTicker.C:
g.game.Update()
data := g.game.JSON()
g.Update()
data := g.JSON()
g.broadcast(data)
case ep := <-g.events:
p := ep.player
Expand Down
5 changes: 3 additions & 2 deletions internal/play/lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const typeUpdateOptions event.Type = "update_options"
const typeGameStart event.Type = "game_start"

type updateOptions struct {
opts game.Options
Type event.Type `json:"event"`
Opts game.Options `json:"options"`
}

type Lobby struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func (lb *Lobby) Run(ctx context.Context) {
switch event.ParseType(data) {
case typeUpdateOptions:
if eUpdateOpts, ok := event.Parse[updateOptions](data); ok {
lb.opts = eUpdateOpts.opts
lb.opts = eUpdateOpts.Opts
}
case typeGameStart:
g := newGame(lb)
Expand Down
2 changes: 2 additions & 0 deletions internal/server/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (h *Hub) Run(ctx context.Context) {
c.player = h.lobby.RegisterPlayer(ctx)
go c.readPump(ctx)
go c.writePump(ctx)
case <-ctx.Done():
return
}
}
}

0 comments on commit e87706c

Please sign in to comment.