Feat: card counter
This commit is contained in:
		
							
								
								
									
										6
									
								
								components/cardcounter.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								components/cardcounter.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | {{define "cardcounter"}} | ||||||
|  | <div class="flex justify-center"> | ||||||
|  |     <p>Blue cards left: {{.BlueCounter}}</p> | ||||||
|  |     <p>Red cards left: {{.RedCounter}}</p> | ||||||
|  | </div> | ||||||
|  | {{end}} | ||||||
| @@ -19,6 +19,9 @@ | |||||||
|     </p> |     </p> | ||||||
|   </div> |   </div> | ||||||
|   <hr /> |   <hr /> | ||||||
|  |   {{if .Room.IsRunning}} | ||||||
|  |     {{template "cardcounter" .Room}} | ||||||
|  |   {{end}} | ||||||
|   <div class="flex justify-center"> |   <div class="flex justify-center"> | ||||||
|     <!-- Left Panel --> |     <!-- Left Panel --> | ||||||
|     {{template "teamlist" .Room.BlueTeam}} |     {{template "teamlist" .Room.BlueTeam}} | ||||||
|   | |||||||
| @@ -65,6 +65,17 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | |||||||
| 		Revealed: true, | 		Revealed: true, | ||||||
| 	} | 	} | ||||||
| 	fi.Room.RevealSpecificWord(word) | 	fi.Room.RevealSpecificWord(word) | ||||||
|  | 	fi.Room.UpdateCounter() | ||||||
|  | 	// if opened card is of color of opp team, change turn | ||||||
|  | 	switch color { | ||||||
|  | 	case "black": | ||||||
|  | 		// game over | ||||||
|  | 		fi.Room.IsRunning = false | ||||||
|  | 		fi.Room.IsOver = true | ||||||
|  | 	case "white", fi.Room.GetOppositeTeamColor(): | ||||||
|  | 		// end turn | ||||||
|  | 		fi.Room.TeamTurn = fi.Room.GetOppositeTeamColor() | ||||||
|  | 	} | ||||||
| 	if err := saveFullInfo(fi); err != nil { | 	if err := saveFullInfo(fi); err != nil { | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -196,6 +196,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	fi.Room.UpdateCounter() | ||||||
| 	// reveal all cards | 	// reveal all cards | ||||||
| 	if fi.State.Role == "mime" { | 	if fi.State.Role == "mime" { | ||||||
| 		fi.Room.RevealAllCards() | 		fi.Room.RevealAllCards() | ||||||
|   | |||||||
| @@ -70,6 +70,8 @@ func HandleHome(w http.ResponseWriter, r *http.Request) { | |||||||
| 	if fi != nil && fi.Room != nil && fi.State != nil { | 	if fi != nil && fi.Room != nil && fi.State != nil { | ||||||
| 		if fi.State.Role == "mime" { | 		if fi.State.Role == "mime" { | ||||||
| 			fi.Room.RevealAllCards() | 			fi.Room.RevealAllCards() | ||||||
|  | 		} else { | ||||||
|  | 			fi.Room.UpdateCounter() | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if fi != nil && fi.Room == nil { | 	if fi != nil && fi.Room == nil { | ||||||
|   | |||||||
| @@ -62,6 +62,34 @@ type Room struct { | |||||||
| 	IsOver bool | 	IsOver bool | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (r *Room) GetOppositeTeamColor() string { | ||||||
|  | 	switch r.TeamTurn { | ||||||
|  | 	case "red": | ||||||
|  | 		return "blue" | ||||||
|  | 	case "blue": | ||||||
|  | 		return "red" | ||||||
|  | 	} | ||||||
|  | 	return "" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (r *Room) UpdateCounter() { | ||||||
|  | 	redCounter := uint8(0) | ||||||
|  | 	blueCounter := uint8(0) | ||||||
|  | 	for _, card := range r.Cards { | ||||||
|  | 		if card.Revealed { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  | 		switch card.Color { | ||||||
|  | 		case WordColorRed: | ||||||
|  | 			redCounter++ | ||||||
|  | 		case WordColorBlue: | ||||||
|  | 			blueCounter++ | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	r.RedCounter = redCounter | ||||||
|  | 	r.BlueCounter = blueCounter | ||||||
|  | } | ||||||
|  |  | ||||||
| func (r *Room) LoadTestCards() { | func (r *Room) LoadTestCards() { | ||||||
| 	cards := []WordCard{ | 	cards := []WordCard{ | ||||||
| 		{Word: "hamster", Color: "blue"}, | 		{Word: "hamster", Color: "blue"}, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder