diff --git a/angular-frontend/src/app/app.component.html b/angular-frontend/src/app/app.component.html index d9ed0ae..e134ed0 100644 --- a/angular-frontend/src/app/app.component.html +++ b/angular-frontend/src/app/app.component.html @@ -23,7 +23,9 @@
- +
+ +
diff --git a/angular-frontend/src/app/app.component.scss b/angular-frontend/src/app/app.component.scss index e69de29..132fea6 100644 --- a/angular-frontend/src/app/app.component.scss +++ b/angular-frontend/src/app/app.component.scss @@ -0,0 +1,9 @@ +.inventories-container { + display:flex; + align-content: space-evenly; +} + +.inventory-container { + display:block; + width:270px; +} \ No newline at end of file diff --git a/angular-frontend/src/app/app.component.ts b/angular-frontend/src/app/app.component.ts index 0baccc5..1993671 100644 --- a/angular-frontend/src/app/app.component.ts +++ b/angular-frontend/src/app/app.component.ts @@ -1,8 +1,19 @@ -import { Component, OnDestroy, HostListener } from '@angular/core'; +import {Component, OnDestroy, HostListener} from '@angular/core'; import { Client } from '@stomp/stompjs'; -import {Button, Credentials, Cursor, Stock, User, Inventory, Character, Item} from "../model"; +import { + Credentials, + Cursor, + User, + Inventory, + Character, + Item, + ItemBase, + ItemLockMessage, + ItemTransferMessage +} from "../model"; import { HttpService } from 'src/http.service'; import {CreateItemEvent} from "../events"; +import store from "../store"; @Component({ selector: 'app-root', @@ -14,10 +25,17 @@ export class AppComponent implements OnDestroy { client: Client; httpService: HttpService; users: User[] = []; - currentUser = ""; cursors: Cursor[] = []; inventories?: Inventory[]; + get currentUser(): string { + return store.currentUser; + } + + set currentUser(currentUser: string) { + store.currentUser = currentUser; + } + constructor() { this.httpService = new HttpService(); this.client = new Client(); @@ -29,7 +47,7 @@ export class AppComponent implements OnDestroy { openWebSocketConnection(credentials : Credentials) { this.client.configure({ - debug: (msg) => console.log(msg), + debug: (msg) => console.debug(msg), webSocketFactory: () => this.httpService.getWebSocket(credentials) }) @@ -39,6 +57,8 @@ export class AppComponent implements OnDestroy { this.client.subscribe("/topic/cursor", (payload => this.updateCursors(JSON.parse(payload.body)))); this.client.subscribe("/app/inventory", (payload => this.updateInventory(JSON.parse(payload.body)))); this.client.subscribe("/topic/inventory", (payload => this.updateInventory(JSON.parse(payload.body)))); + this.client.subscribe("/topic/itembases", (payload => this.updateItemBases(JSON.parse(payload.body)))) + this.client.subscribe("/app/itembases", (payload => this.updateItemBases(JSON.parse(payload.body)))) }; this.client.onWebSocketError = (error) => { @@ -118,10 +138,10 @@ export class AppComponent implements OnDestroy { return; } - var newExceptEx = newItems.filter(x => !exInventory.items!.find(y => x.id === y.id)) - var exExceptNew = exInventory.items.filter(x => !newItems!.find(y => x.id === y.id)) - var toUpdate = exInventory.items - .map(exItem => ({ exItem, newItem: newItems!.find(x => exInventory.id === x.id) })) + const newExceptEx = newItems.filter(x => !exInventory.items!.find(y => x.id === y.id)) + const exExceptNew = exInventory.items.filter(x => !newItems!.find(y => x.id === y.id)) + const toUpdate = exInventory.items + .map(exItem => ({ exItem, newItem: newItems!.find(x => exItem.id === x.id) })) .filter(x => !!x.newItem) for (const newItem of newExceptEx) { @@ -132,6 +152,7 @@ export class AppComponent implements OnDestroy { } for (const items of toUpdate) { items.exItem.name = items.newItem!.name + items.exItem.userLock = items.newItem!.userLock; } } @@ -142,4 +163,37 @@ export class AppComponent implements OnDestroy { }); } + private updateItemBases(itemBase: ItemBase[]) { + store.itemBases = itemBase; + } + + onHoverOverItem(item: Item) { + if (!item.userLock) { + this.client?.publish({ + destination: "/app/itemlock", body: JSON.stringify({ + id: item.id, + lock: true + } as ItemLockMessage) + }); + console.log("lock pls") + } + } + onExitItem(item: Item) { + if (item.userLock === this.currentUser) { + this.client?.publish({ + destination: "/app/itemlock", body: JSON.stringify({ + id: item.id, + lock: false + } as ItemLockMessage) + }); + console.log("unlock pls") + } + } + + onItemMoved(event: ItemTransferMessage) { + console.log("moved"); + this.client?.publish({ + destination: "/app/itemtransfer", body: JSON.stringify(event) + }); + } } diff --git a/angular-frontend/src/app/app.module.ts b/angular-frontend/src/app/app.module.ts index 95865cb..1629556 100644 --- a/angular-frontend/src/app/app.module.ts +++ b/angular-frontend/src/app/app.module.ts @@ -11,9 +11,10 @@ import { StockComponent } from './components/stock/stock.component'; import { UserBarComponent } from './components/user-bar/user-bar.component'; import { GameComponent } from './components/game/game.component'; import { balIconAccount, balIconSend, balIconStarShape, balIconStarFull, balIconWeb, balIconX} from '@baloise/design-system-icons' +import { ItemBaseSelectionComponent } from './components/item-base-selection/item-base-selection.component' @NgModule({ - declarations: [AppComponent, CheckInComponent, InventoryComponent, InventoryItemComponent, ChatComponent, GameComponent, StockComponent, UserBarComponent], + declarations: [AppComponent, CheckInComponent, InventoryComponent, InventoryItemComponent, ChatComponent, GameComponent, StockComponent, UserBarComponent, ItemBaseSelectionComponent], imports: [ BrowserModule, // Provide all components and value accessors to the app module. diff --git a/angular-frontend/src/app/components/inventory-item/inventory-item.component.html b/angular-frontend/src/app/components/inventory-item/inventory-item.component.html index 8e4d533..7f351fc 100644 --- a/angular-frontend/src/app/components/inventory-item/inventory-item.component.html +++ b/angular-frontend/src/app/components/inventory-item/inventory-item.component.html @@ -1,3 +1,3 @@ -
- {{item.name}} -
+
+ +
\ No newline at end of file diff --git a/angular-frontend/src/app/components/inventory-item/inventory-item.component.scss b/angular-frontend/src/app/components/inventory-item/inventory-item.component.scss index 5be07bc..0f40069 100644 --- a/angular-frontend/src/app/components/inventory-item/inventory-item.component.scss +++ b/angular-frontend/src/app/components/inventory-item/inventory-item.component.scss @@ -1,7 +1,12 @@ .inventory-item { - width:90px; - height:90px; - border-radius:3px; - background-color: limegreen; - margin:5px; + width:50px; + height:50px; + border-radius:9px; + background-color: white; + margin:3px; + box-shadow: 0 0 5px lightgray; +} + +.locked-item { + background-color:lightgray; } \ No newline at end of file diff --git a/angular-frontend/src/app/components/inventory-item/inventory-item.component.ts b/angular-frontend/src/app/components/inventory-item/inventory-item.component.ts index f6abbaa..fb1ddc5 100644 --- a/angular-frontend/src/app/components/inventory-item/inventory-item.component.ts +++ b/angular-frontend/src/app/components/inventory-item/inventory-item.component.ts @@ -1,19 +1,25 @@ -import {Component, Input} from '@angular/core'; +import {Component, Input, OnInit} from '@angular/core'; import {Item} from "../../../model"; +import store from "../../../store"; @Component({ selector: 'app-inventory-item', templateUrl: './inventory-item.component.html', styleUrls: ['./inventory-item.component.scss'] }) -export class InventoryItemComponent { +export class InventoryItemComponent implements OnInit { @Input() - item!: Item; + item!: Item + get currentUser(): string { + return store.currentUser; + } onDragStart(event: DragEvent) { event.dataTransfer?.clearData() - event.dataTransfer?.setData("application/json", JSON.stringify( { - target: this.item - })) + event.dataTransfer?.setData("application/json", JSON.stringify(this.item)) + } + + ngOnInit(): void { + console.log("hey?") } } diff --git a/angular-frontend/src/app/components/inventory/inventory.component.html b/angular-frontend/src/app/components/inventory/inventory.component.html index 0cfdd8b..338e40f 100644 --- a/angular-frontend/src/app/components/inventory/inventory.component.html +++ b/angular-frontend/src/app/components/inventory/inventory.component.html @@ -4,8 +4,10 @@
- - + +
+ +
\ No newline at end of file diff --git a/angular-frontend/src/app/components/inventory/inventory.component.scss b/angular-frontend/src/app/components/inventory/inventory.component.scss index f0f50d4..cff6546 100644 --- a/angular-frontend/src/app/components/inventory/inventory.component.scss +++ b/angular-frontend/src/app/components/inventory/inventory.component.scss @@ -3,5 +3,15 @@ flex-wrap: wrap; } .inventory { - width:348px; +} +.inventory-add-button { + width:50px; + height:50px; + border-radius:9px; + background-color: white; + margin:3px; + box-shadow: 0 0 5px lightgray; + display:flex; + justify-content: center; + align-items: center; } \ No newline at end of file diff --git a/angular-frontend/src/app/components/inventory/inventory.component.ts b/angular-frontend/src/app/components/inventory/inventory.component.ts index 565daab..2e1d7ff 100644 --- a/angular-frontend/src/app/components/inventory/inventory.component.ts +++ b/angular-frontend/src/app/components/inventory/inventory.component.ts @@ -1,6 +1,8 @@ import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; -import {Inventory} from "../../../model"; +import {Inventory, Item, ItemBase, ItemTransferMessage} from "../../../model"; import {CreateItemEvent} from "../../../events"; +import {BalModalService} from "@baloise/design-system-components-angular"; +import {ItemBaseSelectionComponent} from "../item-base-selection/item-base-selection.component"; @Component({ selector: 'app-inventory', @@ -13,22 +15,48 @@ export class InventoryComponent implements OnInit { inventory!: Inventory @Output() - moveRequested = new EventEmitter + itemCreated = new EventEmitter @Output() - itemCreated = new EventEmitter + hoverOverItem = new EventEmitter; + + @Output() + exitItem = new EventEmitter; + + @Output() + moveItem = new EventEmitter + + constructor(private modalService: BalModalService) {} ngOnInit(): void { console.log(this.inventory) } - //todo: do propurle - temporaryCreate() { - this.itemCreated.emit({ name: 'gold' }) + async onCreateNew() { + const modal = await this.modalService.create({ + component: ItemBaseSelectionComponent, + componentProps: { + onSelected: (base: ItemBase) => { + modal.dismiss(); + this.createItemFromSelectedItemBase(base) + } + } + }) + await modal.present(); + } + + createItemFromSelectedItemBase(base: ItemBase) { + this.itemCreated.emit({ name: base.name }) } onDrop(event: DragEvent) { - console.log("drop", event.dataTransfer?.getData("application/json")); + if (event.dataTransfer) { + const data = JSON.parse(event.dataTransfer.getData("application/json")) as Item + this.moveItem.emit({ + id: data.id, + targetInventoryId: this.inventory.id + }) + } } onDragOver(event: DragEvent) { @@ -40,4 +68,12 @@ export class InventoryComponent implements OnInit { event.preventDefault(); console.log("enter"); } + + onHoverOverItem(item: Item) { + this.hoverOverItem.emit(item); + } + + onExitItem(item: Item) { + this.exitItem.emit(item) + } } diff --git a/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.html b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.html new file mode 100644 index 0000000..328eaff --- /dev/null +++ b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.html @@ -0,0 +1,8 @@ +
+
+
+ +
+
{{base.label}}
+
+
\ No newline at end of file diff --git a/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.scss b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.scss new file mode 100644 index 0000000..78a29cf --- /dev/null +++ b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.scss @@ -0,0 +1,30 @@ +.item-base { + width:50px; + height:50px; + border-radius:9px; + background-color: white; + margin:3px; + box-shadow: 0 0 5px lightgray; +} + +.item-base-container { + display: flex; + justify-content: space-evenly; + flex-wrap:wrap; +} + +.item-base-group { + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + width: 150px; + padding:10px; + margin-bottom: 10px; + border-radius:9px; + cursor: grab; +} + +.item-base-group:hover { + background-color:lightgray; +} \ No newline at end of file diff --git a/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.ts b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.ts new file mode 100644 index 0000000..c7785a5 --- /dev/null +++ b/angular-frontend/src/app/components/item-base-selection/item-base-selection.component.ts @@ -0,0 +1,22 @@ +import {Component, Input} from '@angular/core'; +import store from "../../../store"; +import {ItemBase} from "../../../model"; + +@Component({ + selector: 'app-item-base-selection', + templateUrl: './item-base-selection.component.html', + styleUrls: ['./item-base-selection.component.scss'] +}) +export class ItemBaseSelectionComponent { + + @Input() + onSelected?: (base: ItemBase) => void; + + get itemBases() { + return store.itemBases; + } + + onItemClicked(base: ItemBase) { + this.onSelected && this.onSelected(base); + } +} diff --git a/angular-frontend/src/assets/items/diamond.png b/angular-frontend/src/assets/items/diamond.png new file mode 100644 index 0000000..71f83d1 Binary files /dev/null and b/angular-frontend/src/assets/items/diamond.png differ diff --git a/angular-frontend/src/assets/items/gold_bar.png b/angular-frontend/src/assets/items/gold_bar.png new file mode 100644 index 0000000..76f0cd0 Binary files /dev/null and b/angular-frontend/src/assets/items/gold_bar.png differ diff --git a/angular-frontend/src/assets/items/gold_coin.png b/angular-frontend/src/assets/items/gold_coin.png new file mode 100644 index 0000000..4a9cb71 Binary files /dev/null and b/angular-frontend/src/assets/items/gold_coin.png differ diff --git a/angular-frontend/src/assets/items/gold_ore.png b/angular-frontend/src/assets/items/gold_ore.png new file mode 100644 index 0000000..6423a4b Binary files /dev/null and b/angular-frontend/src/assets/items/gold_ore.png differ diff --git a/angular-frontend/src/assets/items/health_potion.png b/angular-frontend/src/assets/items/health_potion.png new file mode 100644 index 0000000..e35c1d5 Binary files /dev/null and b/angular-frontend/src/assets/items/health_potion.png differ diff --git a/angular-frontend/src/assets/items/items.afphoto b/angular-frontend/src/assets/items/items.afphoto new file mode 100644 index 0000000..200bdd4 Binary files /dev/null and b/angular-frontend/src/assets/items/items.afphoto differ diff --git a/angular-frontend/src/assets/items/longsword.png b/angular-frontend/src/assets/items/longsword.png new file mode 100644 index 0000000..8ecd0e6 Binary files /dev/null and b/angular-frontend/src/assets/items/longsword.png differ diff --git a/angular-frontend/src/assets/items/shield.png b/angular-frontend/src/assets/items/shield.png new file mode 100644 index 0000000..09b065e Binary files /dev/null and b/angular-frontend/src/assets/items/shield.png differ diff --git a/angular-frontend/src/assets/items/shortsword.png b/angular-frontend/src/assets/items/shortsword.png new file mode 100644 index 0000000..36a7083 Binary files /dev/null and b/angular-frontend/src/assets/items/shortsword.png differ diff --git a/angular-frontend/src/model/index.ts b/angular-frontend/src/model/index.ts index ad792c8..843b5e0 100644 --- a/angular-frontend/src/model/index.ts +++ b/angular-frontend/src/model/index.ts @@ -45,12 +45,13 @@ export interface Pickup { } export interface Item { - id: string; + userLock: string; + id: number; name: string; } export interface Inventory { - id: string; + id: number; owner?: string; items: Item[]; } @@ -59,4 +60,19 @@ export interface ChatMessage { user: User, text: string, timestamp: Date +} + +export interface ItemBase { + name: string; + label: string; +} + +export interface ItemLockMessage { + id: number, + lock: boolean +} + +export interface ItemTransferMessage { + id: number, + targetInventoryId: number } \ No newline at end of file diff --git a/angular-frontend/src/store/index.ts b/angular-frontend/src/store/index.ts new file mode 100644 index 0000000..081b035 --- /dev/null +++ b/angular-frontend/src/store/index.ts @@ -0,0 +1,24 @@ +import {ItemBase} from "../model"; + +//me very lazy +export class MyStore { + private _itemBases: ItemBase[] = [] + private _currentUser?: string; + public get itemBases(): ItemBase[] { + return this._itemBases; + } + public set itemBases(itemBases: ItemBase[]) { + this._itemBases = itemBases; + } + + public get currentUser(): string { + return this._currentUser!; + } + + public set currentUser(currentUser: string) { + this._currentUser = currentUser; + } +} + +const store = new MyStore() +export default store \ No newline at end of file diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/InventoriesWebSocketHandler.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/InventoriesWebSocketHandler.java index 93ba855..ac990d6 100644 --- a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/InventoriesWebSocketHandler.java +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/InventoriesWebSocketHandler.java @@ -40,7 +40,7 @@ public List fetchAllInventories() { inventory.getOwner(), inventory.getItems().stream().map(item -> { return new ItemDto( - item.getName(), + item.getItemBase().getName(), item.getId(), item.getUserLock() ); diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseDto.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseDto.java new file mode 100644 index 0000000..4fe2f61 --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseDto.java @@ -0,0 +1,6 @@ +package com.baloise.collab.springbackend.items; + +public record ItemBaseDto( + String name, + String label +){} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseEntity.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseEntity.java new file mode 100644 index 0000000..d7e897b --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBaseEntity.java @@ -0,0 +1,18 @@ +package com.baloise.collab.springbackend.items; + +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Setter +@Entity +@NoArgsConstructor +@With +@AllArgsConstructor +@Table(name="item_base") +public class ItemBaseEntity { + @Id + private String name; + + private String label; +} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesRepository.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesRepository.java new file mode 100644 index 0000000..2d0e75a --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesRepository.java @@ -0,0 +1,9 @@ +package com.baloise.collab.springbackend.items; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ItemBasesRepository extends CrudRepository { + +} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesWebSocketHandler.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesWebSocketHandler.java new file mode 100644 index 0000000..3cfe53f --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemBasesWebSocketHandler.java @@ -0,0 +1,29 @@ +package com.baloise.collab.springbackend.items; + +import lombok.RequiredArgsConstructor; +import lombok.extern.java.Log; +import org.springframework.messaging.simp.annotation.SubscribeMapping; +import org.springframework.stereotype.Controller; + +import java.util.ArrayList; +import java.util.List; + +@RequiredArgsConstructor +@Log +@Controller +public class ItemBasesWebSocketHandler { + + private final ItemBasesRepository itemBasesRepository; + + public List fetchAllItemBases() { + var itemBases = new ArrayList(); + itemBasesRepository.findAll() + .forEach(base -> itemBases.add(new ItemBaseDto(base.getName(), base.getLabel()))); + return itemBases; + } + + @SubscribeMapping("/itembases") + public List initItemBases() { + return fetchAllItemBases(); + } +} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemEntity.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemEntity.java index 987f44c..6fd29ba 100644 --- a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemEntity.java +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemEntity.java @@ -16,11 +16,14 @@ public class ItemEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; + @Column(nullable = true) private String userLock; - private String name; - @ManyToOne @JoinColumn(name = "inventoryId") private InventoryEntity inventory; + + @ManyToOne + @JoinColumn(name = "name") + private ItemBaseEntity itemBase; } diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemLockDto.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemLockDto.java new file mode 100644 index 0000000..531c518 --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemLockDto.java @@ -0,0 +1,6 @@ +package com.baloise.collab.springbackend.items; + +public record ItemLockDto( + long id, + boolean lock +) {} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemTransferDto.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemTransferDto.java new file mode 100644 index 0000000..988cba8 --- /dev/null +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemTransferDto.java @@ -0,0 +1,6 @@ +package com.baloise.collab.springbackend.items; + +public record ItemTransferDto( + long id, + long targetInventoryId +) {} diff --git a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java index e94fe48..4aadbf4 100644 --- a/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java +++ b/spring-backend/src/main/java/com/baloise/collab/springbackend/items/ItemsWebSocketHandler.java @@ -1,14 +1,20 @@ package com.baloise.collab.springbackend.items; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.java.Log; import org.springframework.messaging.Message; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.security.authentication.AbstractAuthenticationToken; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.stereotype.Controller; import java.util.List; +import java.util.Objects; +import java.util.Optional; @RequiredArgsConstructor @Log @@ -18,18 +24,61 @@ public class ItemsWebSocketHandler { private final ObjectMapper objectMapper; private final InventoriesWebSocketHandler inventoriesWebSocketHandler; private final InventoriesRepository inventoriesRepository; + private final ItemBasesRepository itemBasesRepository; private final ItemsRepository itemsRepository; + private final SimpMessagingTemplate messageSender; @MessageMapping("/itemcreation") @SendTo("/topic/inventory") public List handleItemCreation(Message message) throws Exception { var newItem = objectMapper.readValue(message.getPayload(), CreateItemDto.class); var inventory = inventoriesRepository.findById(newItem.targetInventoryId()).orElseThrow(); + var itemBase = itemBasesRepository.findById(newItem.name()).orElseThrow(); var itemEntity = new ItemEntity() - .withName(newItem.name()) + .withItemBase(itemBase) .withInventory(inventory); - //.withInventoryId(newItem.targetInventoryId()); itemsRepository.save(itemEntity); return inventoriesWebSocketHandler.fetchAllInventories(); } + + @MessageMapping("/itemlock") + public void handleItemLock(Message message) throws JsonProcessingException { + var dto = objectMapper.readValue(message.getPayload(), ItemLockDto.class); + var item = itemsRepository.findById(dto.id()).orElseThrow(); + var optionalToken = Optional.ofNullable((UsernamePasswordAuthenticationToken) message.getHeaders().get("simpUser")); + String userName = optionalToken.map(AbstractAuthenticationToken::getName).orElse("dummyUser"); + if (dto.lock()) { + if (item.getUserLock() == null) { + item.setUserLock(userName); + itemsRepository.save(item); + notifyInventoryChange(); + } + } else { + if (Objects.equals(item.getUserLock(), userName)) { + item.setUserLock(null); + itemsRepository.save(item); + notifyInventoryChange(); + } + } + } + + @MessageMapping("/itemtransfer") + public void handleItemTransfer(Message message) throws JsonProcessingException { + var dto = objectMapper.readValue(message.getPayload(), ItemTransferDto.class); + var item = itemsRepository.findById(dto.id()).orElseThrow(); + var optionalToken = Optional.ofNullable((UsernamePasswordAuthenticationToken) message.getHeaders().get("simpUser")); + String userName = optionalToken.map(AbstractAuthenticationToken::getName).orElse("dummyUser"); + + if (Objects.equals(item.getUserLock(), userName)) { + var targetInventory = inventoriesRepository.findById(dto.targetInventoryId()).orElseThrow(); + item.setInventory(targetInventory); + item.setUserLock(null); + itemsRepository.save(item); + notifyInventoryChange(); + } + } + + private void notifyInventoryChange() { + messageSender.convertAndSend("/topic/inventory", inventoriesWebSocketHandler.fetchAllInventories()); + } } diff --git a/spring-backend/src/main/resources/data.sql b/spring-backend/src/main/resources/data.sql index f93a8e4..17383fa 100644 --- a/spring-backend/src/main/resources/data.sql +++ b/spring-backend/src/main/resources/data.sql @@ -1 +1,7 @@ -INSERT INTO button (username) VALUES ('Testing'); \ No newline at end of file +INSERT INTO item_base (name, label) VALUES ('gold_ore', 'Golderz'); +INSERT INTO item_base (name, label) VALUES ('gold_coin', 'Goldmünze'); +INSERT INTO item_base (name, label) VALUES ('shortsword', 'Kurzschwert'); +INSERT INTO item_base (name, label) VALUES ('longsword', 'Langschwert'); +INSERT INTO item_base (name, label) VALUES ('shield', 'Schild'); +INSERT INTO item_base (name, label) VALUES ('diamond', 'Diamant'); +INSERT INTO item_base (name, label) VALUES ('health_potion', 'Lebenstrank'); \ No newline at end of file