### Backend (Golang)
```go
package main
import (
"encoding/json"
"net/http"
"sync"
)
type GameState struct {
PlayerID string `json:"player_id"`
Coins int `json:"coins"`
Fuel int `json:"fuel"`
CurrentLevel int `json:"current_level"`
}
var gameStates = make(map[string]*GameState)
var mu sync.Mutex
func getGameState(w http.ResponseWriter, r *http.Request) {
playerID := r.URL.Query().Get("player_id")
mu.Lock()
state, exists := gameStates[playerID]
mu.Unlock()
if !exists {
http.Error(w, "Player not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(state)
}
func updateGameState(w http.ResponseWriter, r *http.Request) {
var state GameState
if err := json.NewDecoder(r.Body).Decode(&state); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
mu.Lock()
gameStates[state.PlayerID] = &state
mu.Unlock()
w.WriteHeader(http.StatusNoContent)
}
func main() {
http.HandleFunc("/gamestate", getGameState)
http.HandleFunc("/update", updateGameState)
http.ListenAndServe(":8080", nil)
}
```
### Frontend (Angular)
1. **Serviço de Jogo** (`game.service.ts`):
```typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface GameState {
playerId: string;
coins: number;
fuel: number;
currentLevel: number;
}
@Injectable({
providedIn: 'root'
})
export class GameService {
private apiUrl = 'http://localhost:8080';
constructor(private http: HttpClient) {}
getGameState(playerId: string): Observable<GameState> {
return this.http.get<GameState>(`${this.apiUrl}/gamestate?player_id=${playerId}`);
}
updateGameState(state: GameState): Observable<void> {
return this.http.post<void>(`${this.apiUrl}/update`, state);
}
}
```
2. **Componente de Jogo** (`game.component.ts`):
```typescript
import { Component, OnInit } from '@angular/core';
import { GameService, GameState } from './game.service';
@Component({
selector: 'app-game',
templateUrl: './game.component.html',
styleUrls: ['./game.component.css']
})
export class GameComponent implements OnInit {
gameState: GameState;
constructor(private gameService: GameService) {}
ngOnInit(): void {
const playerId = 'player1'; // Exemplo de ID do jogador
this.gameService.getGameState(playerId).subscribe(state => {
this.gameState = state;
});
}
updateGame() {
// Lógica para atualizar o estado do jogo
this.gameService.updateGameState(this.gameState).subscribe();
}
}
```