Skip to content
Snippets Groups Projects
Commit a09eda56 authored by Maxime Tissot's avatar Maxime Tissot
Browse files

bug fixes and adaption

parent 11461bc1
No related branches found
No related tags found
No related merge requests found
package hsrt.VSundSOA.backendapi.gameInstance;
import hsrt.VSundSOA.backendapi.fieldCreation.FieldCreation;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
......@@ -15,13 +16,25 @@ public class GameController {
private Field field = new Field();
@PostConstruct
private void init(){
field.field();
@ResponseStatus(HttpStatus.OK)
@GetMapping("/game/newField")
private int[][] createField(){
try {
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
history.clear();
FieldCreation fieldCreation = new FieldCreation();
ArrayList<int[][]> list = fieldCreation.getNewField();
field.setStartField(list.get(0));
field.setSolutionField(list.get(1));
return field.getStartField();
}
@ResponseStatus(HttpStatus.OK)
@GetMapping("/game/fields")
public int[][] GetFields(){
@GetMapping("/game/field")
public int[][] getField(){
return field.getStartField();
}
......@@ -37,9 +50,8 @@ public class GameController {
@ResponseStatus(HttpStatus.OK)
@GetMapping("/game/win")
public Boolean VerifyWin(){
public Boolean verifyWin(){
if (field.getCellsToFill() <= history.size()){
history.clear();
return true;
}
return false;
......
......@@ -17,7 +17,7 @@ public class LoginController {
}
@ResponseStatus(HttpStatus.OK)
@GetMapping("/login/user/{username}")
@GetMapping("/login/{username}")
public User getUser(@PathVariable String username){
for (User user : userList) {
if (username.equals(user.getUsername())) {
......@@ -28,10 +28,10 @@ public class LoginController {
}
@ResponseStatus(HttpStatus.OK)
@PutMapping("/login/registry")
public User putUser(User user){
@PostMapping("/login/registry")
public User postUser(@RequestBody User user){
for (User userInList : userList) {
if (user.equals(userInList)) {
if (user.getUsername().equals(userInList.getUsername())) {
return userInList;
}
}
......@@ -41,7 +41,7 @@ public class LoginController {
@ResponseStatus(HttpStatus.OK)
@PatchMapping("/login/update/{username}")
public User patchUser(@PathVariable String username, User user){
public User patchUser(@PathVariable String username, @RequestBody User user){
User userToUpdate = getUser(username);
userToUpdate.setColor(user.getColor());
userToUpdate.setGamesPlayed(user.getGamesPlayed());
......
......@@ -61,12 +61,13 @@ h3{
.Game{
display: grid;
grid-template-columns: 10% 75% 15%;
align-content: center;
grid-template-columns: 15% 70% 15%;
justify-items: center;
}
.Sudoku{
width: 700px;
width: 100%;
height: 100%;
overflow: hidden;
background: #7918f2;
background: -webkit-linear-gradient(-68deg, #ac32e4 , #4801ff);
......@@ -100,6 +101,5 @@ h3{
}
.PadButton{
height: 80px;
font-size: 50px;
}
\ No newline at end of file
......@@ -10,7 +10,7 @@
<h1>Sudoku with Friends</h1>
<h2 id="usernameMessage">Welcome, please enter your username</h2>
<input type="text" id="username" class="input" placeholder="Username"/>
<button type="button" id="login" class="button" onclick="loginOnClick()">Connect</button>
<button type="button" id="login" class="button" onclick="loginOnClickAsync()">Connect</button>
</div>
<div class="Game" id="game">
<div class="NumberPad" id="number-pad">
......
......@@ -5,27 +5,27 @@ let activeNumber = 0;
let history = [];
let colors = ['gray', 'cyan', 'darkred', 'green', 'yellow', 'indianred', 'lime', 'orangered', 'lightblue', 'salmon', 'indigo', 'white', 'palegreen', 'teal', 'olive', 'deeppink'];
let users = {};
let reloadInterval;
async function initiateGame() {
users[username] = colors[Math.floor(Math.random() * colors.length)];
async function initiateGameAsync() {
users[username] = await postUserAsync(username, colors[Math.floor(Math.random() * colors.length)], 0);
createEmptySudokuGrid();
createUserList();
await fillSudokuGrid();
await fillSudokuGridAsync();
await reloadOnTimerAsync();
setInterval(reloadOnTimerAsync, 2 * 1000);
reloadInterval = setInterval(reloadOnTimerAsync, 2 * 1000);
}
async function loginOnClick() {
async function loginOnClickAsync() {
username = document.getElementById('username').value;
document.getElementById('login').remove();
document.getElementById('username').remove();
document.getElementById("usernameMessage").textContent = username;
createNumberPadButtons();
await initiateGame();
await initiateGameAsync();
}
function createNumberPadButtons() {
......@@ -74,15 +74,15 @@ function createUserList() {
if (occurrence == 0) {
const user = document.createElement(`h2`);
user.id = `user-${key}`
user.textContent = key;
user.style.color = users[key];
user.textContent = `${key} won: ${users[key].gamesPlayed}`;
user.style.color = users[key].color;
userBlock.appendChild(user);
}
}
}
async function fillSudokuGrid() {
async function fillSudokuGridAsync() {
const sudoku = await getFieldsAsync();
for (let row = 0; row < 9; row++) {
for (let cell = 0; cell < 9; cell++) {
......@@ -98,11 +98,16 @@ async function fillSudokuGrid() {
}
}
async function getFieldsAsync() {
const fields = await fetch(GameAPI + "/fields", {method: "GET"});
async function createFieldAsync(){
const fields = await fetch(GameAPI + "/game/newField", {method: "GET"});
return await fields.json();
}
async function getFieldsAsync() {
const field = await fetch(GameAPI + "/game/field", {method: "GET"});
return await field.json();
}
async function getHistoryAsync() {
const history = await fetch(GameAPI + "/game/history", {method: "GET"});
return await history.json();
......@@ -113,7 +118,45 @@ async function getWinVerificationAsync() {
return await win.json();
}
async function reloadCell(placement) {
async function getUserAsync(username){
const user = await fetch(GameAPI + `/login/${username}`, {method: 'GET'});
return await user.json();
}
async function getUserListAsync(){
const userList = await fetch(GameAPI + '/login/userList', {method: 'GET'});
return await userList.json();
}
async function postUserAsync(username, color, gamesPlayed){
const user = {
username: username,
color: color,
gamesPlayed: gamesPlayed
};
const newUser = await fetch(GameAPI + `/login/registry`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(user)
});
return await newUser.json();
}
async function patchUserAsync(username, color, gamesPlayed){
const user = {
username: username,
color: color,
gamesPlayed: gamesPlayed
};
const updatedUser = await fetch(GameAPI + `/login/update/${username}`, {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(user)
});
return await updatedUser.json();
}
async function reloadCellAsync(placement) {
history.push(placement);
let occurrence = 0;
for (const key of Object.keys(users)) {
......@@ -122,16 +165,22 @@ async function reloadCell(placement) {
}
}
if (occurrence == 0) {
users[placement.username] = colors[Math.floor(Math.random() * colors.length)];
users[placement.username] = await getUserAsync(placement.username);
}
const cellText = document.getElementById(`${placement.x}cell${placement.y}`).children;
cellText[0].innerText = placement.value;
cellText[0].style.color = users[`${placement.username}`];
cellText[0].style.color = users[placement.username].color;
if (await getWinVerificationAsync()) {
clearInterval(reloadInterval);
window.alert("you won!");
const user = await getUserAsync(username);
await patchUserAsync(username, user.color, ++user.gamesPlayed);
// Reset
if (placement.username === username){
await createFieldAsync();
}
history = [];
users = {};
document.getElementById('Sudoku').remove();
......@@ -145,7 +194,7 @@ async function reloadCell(placement) {
list.className = 'UserList';
document.getElementById('game').append(list);
await initiateGame();
await initiateGameAsync();
}
}
......@@ -165,9 +214,8 @@ async function reloadOnTimerAsync() {
continue;
}
reloadCell(element);
await reloadCellAsync(element);
createUserList();
}
}
......@@ -183,7 +231,7 @@ async function sudokuOnClickAsync(X, Y) {
};
const worked = await putPlacementAsync(placement);
if (worked) {
reloadCell(placement);
await reloadCellAsync(placement);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment