Skip to content

Commit

Permalink
random manjše izboljšave
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Oct 20, 2023
1 parent 1f8d3f8 commit bfdcf10
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 120 deletions.
34 changes: 23 additions & 11 deletions backend/internal/ws/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
s.logger.Warnw("cards haven't started", "cardId", id, "gameId", gameId, "userId", userId, "cardsStarted", game.CardsStarted)

// dumbo, pls wait for the game to actually start
s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}

if game.WaitingFor != userId {
s.logger.Warnw("invalid person in queue", "cardId", id, "gameId", gameId, "userId", userId, "cardsStarted", game.CardsStarted, "waitingFor", game.WaitingFor)
s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}

Expand All @@ -245,7 +245,7 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
if err != nil {
s.logger.Warnw("invalid card")

s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}

Expand Down Expand Up @@ -290,11 +290,11 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
}
if imaBarvo && placedCard.Type != card.Type {
s.logger.Debugw("returning the card to the user due to him having the correct colour", "card", card.Full, "type", placedCard.Type)
s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
} else if !imaBarvo && imaTarok && placedCard.Type != "taroki" {
s.logger.Debugw("returning the card to the user due to him having tarocks", "card", card.Full, "type", placedCard.Type)
s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}

Expand All @@ -305,7 +305,7 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
}
if trula == 2 && (imaPalcko && id != "/taroki/pagat") {
s.logger.Debugw("returning the card to the user due to him having pagat while škis and mond have fallen", "card", card.Full, "type", placedCard.Type)
s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}
}
Expand All @@ -316,7 +316,7 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
if err != nil {
s.logger.Warnw("invalid card")

s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}
maxValue := c
Expand All @@ -326,7 +326,7 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
if err != nil {
s.logger.Warnw("invalid card")

s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}
ccParsed := helpers.ParseCardID(v.id)
Expand All @@ -346,7 +346,7 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
if err != nil {
s.logger.Warnw("invalid card")

s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}

Expand All @@ -360,12 +360,24 @@ func (s *serverImpl) CardDrop(id string, gameId string, userId string, clientId
if imaVisjo && placedCardDef.WorthOver < maxValue.WorthOver {
s.logger.Warnw("user has a higher ranked card than current one", "placedCard", placedCardDef, "maxValue", maxValue)

s.returnCardToSender(id, gameId, userId, clientId)
//s.returnCardToSender(id, gameId, userId, clientId)
return
}
}
} else {
// TODO: preveri če ma uporabnik sploh karto
cards := game.Players[userId].GetCards()
found := false
for _, v := range cards {
if v.id != id {
continue
}
found = true
break
}
if !found {
s.logger.Warnw("user doesn't have the sent card", "id", id, "userId", userId)
return
}
}

s.logger.Debugw("ending timer cards")
Expand Down
10 changes: 10 additions & 0 deletions backend/internal/ws/king.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (s *serverImpl) KingCalling(gameId string) {
return
}

game.WaitingFor = "king"

broadcast := &messages.Message{PlayerId: playing, Data: &messages.Message_KingSelection{KingSelection: &messages.KingSelection{Type: &messages.KingSelection_Notification{Notification: &messages.Notification{}}}}}
s.Broadcast("", gameId, broadcast)

Expand Down Expand Up @@ -117,6 +119,14 @@ func (s *serverImpl) KingCalled(userId string, gameId string, cardId string) {
s.logger.Warnw("modified client detected", "userId", userId)
return
}
if !((game.GameMode >= 0 && game.GameMode < 3) && game.PlayersNeeded == 4) {
s.logger.Warnw("modified client detected", "userId", userId)
return
}
if game.WaitingFor != "king" {
s.logger.Warnw("modified client detected", "userId", userId)
return
}

game.PlayingIn = cardId

Expand Down
7 changes: 7 additions & 0 deletions backend/internal/ws/licitiranje.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (s *serverImpl) BotGoroutineLicitiranje(gameId string, playing string) {
return
}

game.WaitingFor = "licitiranje"

go func() {
s.logger.Debugw("gorutina bot licitiranje se poganja")

Expand Down Expand Up @@ -92,6 +94,11 @@ func (s *serverImpl) Licitiranje(tip int32, gameId string, userId string) {

s.logger.Debugw("licitiranje called", "gameId", gameId, "userId", userId)

if game.WaitingFor != "licitiranje" {
s.logger.Warnw("modified client detected", "userId", userId)
return
}

if len(game.Playing) == 0 || game.Playing[0] != userId {
s.logger.Warnw("modified client detected. you cannot licitate if you're not the person")
}
Expand Down
114 changes: 90 additions & 24 deletions backend/internal/ws/predictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ws

import (
"encoding/json"
"fmt"
"github.com/mytja/Tarok/backend/internal/helpers"
"github.com/mytja/Tarok/backend/internal/messages"
"math"
Expand Down Expand Up @@ -202,26 +203,32 @@ func (s *serverImpl) FirstPrediction(gameId string) {
}}})
}

// poševnica se tako ali tako ne generira v UUID-jih
game.WaitingFor = fmt.Sprintf("predictions/%s", starts)

s.BotGoroutinePredictions(gameId, starts)
}

// na trulo & kralje se (verjetno) ne daje kontre
// pri truli in kraljih velja pravilo kdor prvi pride prvi melje
// lahko ju napove kdorkoli
// ultime lahko napovesta samo igralca
// TODO: kaj se zgodi, če kdo drug ukrade kontro s hijackanjem Id parametra kontre
// TODO: kaj se zgodi, če kdo drug kot trenunti igralec pošlje to
func (s *serverImpl) Predictions(userId string, gameId string, predictions *messages.Predictions) {
game, exists := s.games[gameId]
if !exists {
return
}
playing := game.Playing[0]

if game.WaitingFor != fmt.Sprintf("predictions/%s", userId) {
s.logger.Warnw("modified client detected", "userId", userId)
return
}

s.logger.Debugw("predictions", "p", predictions)

// ne moreš na novo napovedati
if predictions.BarvniValat != nil && game.CurrentPredictions.BarvniValat != nil && predictions.BarvniValat.Id != game.CurrentPredictions.BarvniValat.Id {
if predictions.BarvniValat != nil && game.CurrentPredictions.BarvniValat != nil &&
predictions.BarvniValat.Id != game.CurrentPredictions.BarvniValat.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
Expand Down Expand Up @@ -295,7 +302,8 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
}

// ne moreš na novo napovedati
if predictions.PagatUltimo != nil && game.CurrentPredictions.PagatUltimo != nil && predictions.PagatUltimo.Id != game.CurrentPredictions.PagatUltimo.Id {
if predictions.PagatUltimo != nil && game.CurrentPredictions.PagatUltimo != nil &&
predictions.PagatUltimo.Id != game.CurrentPredictions.PagatUltimo.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
Expand All @@ -304,11 +312,6 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš napovedati, če nisi igralec
if predictions.PagatUltimo == nil && game.CurrentPredictions.PagatUltimo != nil && playing != game.CurrentPredictions.PagatUltimo.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš preklicati napovedi
if predictions.PagatUltimo == nil && game.CurrentPredictions.PagatUltimo != nil {
s.logger.Debugw("prediction rule wasn't satisfied")
Expand All @@ -321,17 +324,13 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
}

// ne moreš na novo napovedati
if predictions.KraljUltimo != nil && game.CurrentPredictions.KraljUltimo != nil && predictions.KraljUltimo.Id != game.CurrentPredictions.KraljUltimo.Id {
if predictions.KraljUltimo != nil && game.CurrentPredictions.KraljUltimo != nil &&
predictions.KraljUltimo.Id != game.CurrentPredictions.KraljUltimo.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš napovedati, če ne igraš specifične igre
if predictions.KraljUltimo != nil && !(game.GameMode >= 0 && game.GameMode <= 5) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš napovedati, če nisi igralec
if predictions.KraljUltimo == nil && game.CurrentPredictions.KraljUltimo != nil && playing != game.CurrentPredictions.KraljUltimo.Id {
if predictions.KraljUltimo != nil && !(game.GameMode >= 0 && game.GameMode <= 2 && game.PlayersNeeded == 4) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
Expand All @@ -341,7 +340,9 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
return
}
// ne moreš napovedati, če nimaš kralja
if predictions.KraljUltimo != nil && predictions.KraljUltimo.Id != "" && !(s.HasKing(gameId, predictions.KraljUltimo.Id) && helpers.Contains(game.Playing, predictions.KraljUltimo.Id)) {
if predictions.KraljUltimo != nil && predictions.KraljUltimo.Id != "" &&
!(s.HasKing(gameId, predictions.KraljUltimo.Id) &&
helpers.Contains(game.Playing, predictions.KraljUltimo.Id)) {
s.logger.Debugw("prediction rule wasn't satisfied", "king", game.PlayingIn)
return
}
Expand Down Expand Up @@ -372,6 +373,17 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
return
}

/*
KONTRE
*/

if predictions.KraljUltimoKontraDal != nil && predictions.KraljUltimoKontra == 0 {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}

if predictions.KraljUltimoKontra != 0 {
// ne moreš kontrirati, če nihče ni napovedal
if game.CurrentPredictions.KraljUltimo == nil {
Expand All @@ -384,17 +396,20 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
return
}
// ne moreš napovedati več konter naenkrat (>2)
if game.CurrentPredictions.KraljUltimoKontra+1 != predictions.KraljUltimoKontra && game.CurrentPredictions.KraljUltimoKontra < predictions.KraljUltimoKontra {
if game.CurrentPredictions.KraljUltimoKontra+1 != predictions.KraljUltimoKontra &&
game.CurrentPredictions.KraljUltimoKontra < predictions.KraljUltimoKontra {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// kontro in subkontro lahko napovesta samo nasprotnika
if predictions.KraljUltimoKontra%2 == 1 && (predictions.KraljUltimoKontraDal == nil || helpers.Contains(game.Playing, predictions.KraljUltimoKontraDal.Id)) {
if predictions.KraljUltimoKontra%2 == 1 && (predictions.KraljUltimoKontraDal == nil ||
helpers.Contains(game.Playing, predictions.KraljUltimoKontraDal.Id)) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// rekontro in mortkontro lahko napovesta samo igralca
if predictions.KraljUltimoKontra%2 == 0 && (predictions.KraljUltimoKontraDal == nil || !helpers.Contains(game.Playing, predictions.KraljUltimoKontraDal.Id)) {
if predictions.KraljUltimoKontra%2 == 0 && (predictions.KraljUltimoKontraDal == nil ||
!helpers.Contains(game.Playing, predictions.KraljUltimoKontraDal.Id)) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
Expand All @@ -403,6 +418,19 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš povoziti kontre
if game.CurrentPredictions.KraljUltimoKontra == predictions.KraljUltimoKontra &&
game.CurrentPredictions.KraljUltimoKontraDal != nil &&
predictions.KraljUltimoKontraDal != nil &&
game.CurrentPredictions.KraljUltimoKontraDal.Id != predictions.KraljUltimoKontraDal.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
}

if predictions.PagatUltimoKontraDal != nil && predictions.PagatUltimoKontra == 0 {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
if predictions.PagatUltimoKontra != 0 {
// ne moreš kontrirati, če nihče ni napovedal
Expand All @@ -416,17 +444,20 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
return
}
// ne moreš napovedati več konter naenkrat (>2)
if game.CurrentPredictions.PagatUltimoKontra+1 != predictions.PagatUltimoKontra && game.CurrentPredictions.PagatUltimoKontra < predictions.PagatUltimoKontra {
if game.CurrentPredictions.PagatUltimoKontra+1 != predictions.PagatUltimoKontra &&
game.CurrentPredictions.PagatUltimoKontra < predictions.PagatUltimoKontra {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// kontro in subkontro lahko napovesta samo nasprotnika
if predictions.PagatUltimoKontra%2 == 1 && (predictions.PagatUltimoKontraDal == nil || helpers.Contains(game.Playing, predictions.PagatUltimoKontraDal.Id)) {
if predictions.PagatUltimoKontra%2 == 1 && (predictions.PagatUltimoKontraDal == nil ||
helpers.Contains(game.Playing, predictions.PagatUltimoKontraDal.Id)) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// rekontro in mortkontro lahko napovesta samo igralca
if predictions.PagatUltimoKontra%2 == 0 && (predictions.PagatUltimoKontraDal == nil || !helpers.Contains(game.Playing, predictions.PagatUltimoKontraDal.Id)) {
if predictions.PagatUltimoKontra%2 == 0 && (predictions.PagatUltimoKontraDal == nil ||
!helpers.Contains(game.Playing, predictions.PagatUltimoKontraDal.Id)) {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
Expand All @@ -435,6 +466,19 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš povoziti kontre
if game.CurrentPredictions.PagatUltimoKontra == predictions.PagatUltimoKontra &&
game.CurrentPredictions.PagatUltimoKontraDal != nil &&
predictions.PagatUltimoKontraDal != nil &&
game.CurrentPredictions.PagatUltimoKontraDal.Id != predictions.PagatUltimoKontraDal.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
}

if predictions.MondfangKontraDal != nil && predictions.MondfangKontra == 0 {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
if predictions.MondfangKontra != 0 {
// ne moreš kontrirati, če nihče ni napovedal
Expand Down Expand Up @@ -467,8 +511,20 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš povoziti kontre
if game.CurrentPredictions.MondfangKontra == predictions.MondfangKontra &&
game.CurrentPredictions.MondfangKontraDal != nil &&
predictions.MondfangKontraDal != nil &&
game.CurrentPredictions.MondfangKontraDal.Id != predictions.MondfangKontraDal.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
}

if predictions.IgraKontraDal != nil && predictions.IgraKontra == 0 {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
if predictions.IgraKontra != 0 {
// ne moreš zmanjšati kontre
if predictions.IgraKontra < game.CurrentPredictions.IgraKontra {
Expand Down Expand Up @@ -500,6 +556,14 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
// ne moreš povoziti kontre
if game.CurrentPredictions.IgraKontra == predictions.IgraKontra &&
game.CurrentPredictions.IgraKontraDal != nil &&
predictions.IgraKontraDal != nil &&
game.CurrentPredictions.IgraKontraDal.Id != predictions.IgraKontraDal.Id {
s.logger.Debugw("prediction rule wasn't satisfied")
return
}
}

barvicNapovedan := game.CurrentPredictions.Gamemode >= 3 && game.CurrentPredictions.Gamemode <= 5 &&
Expand Down Expand Up @@ -642,5 +706,7 @@ func (s *serverImpl) Predictions(userId string, gameId string, predictions *mess
MondfangKontra: predictions.Mondfang != nil && mondfangKontra,
}}})

game.WaitingFor = fmt.Sprintf("predictions/%s", newId)

s.BotGoroutinePredictions(gameId, newId)
}
Loading

0 comments on commit bfdcf10

Please sign in to comment.