From fc569fa42d3dd1f8709103c7dae7e995597072ca Mon Sep 17 00:00:00 2001 From: cirakg Date: Mon, 2 Jan 2023 16:34:46 +0100 Subject: Po zavrsetu igre se dodaje u history u bazi. --- pokemon.sql | 120 ++++++++++++++++++++++++++--------- src/main/java/database/Database.java | 29 +++++++++ src/main/java/pokemon/IService.java | 1 + src/main/java/pokemon/Server.java | 2 +- src/main/java/pokemon/Service.java | 6 ++ 5 files changed, 127 insertions(+), 31 deletions(-) diff --git a/pokemon.sql b/pokemon.sql index f7972b7..63abc1c 100644 --- a/pokemon.sql +++ b/pokemon.sql @@ -1,11 +1,11 @@ -- phpMyAdmin SQL Dump --- version 5.0.2 +-- version 5.2.0 -- https://www.phpmyadmin.net/ -- --- Host: 127.0.0.1:3306 --- Generation Time: Dec 22, 2022 at 01:40 AM --- Server version: 5.7.31 --- PHP Version: 7.4.9 +-- Host: 127.0.0.1 +-- Generation Time: Jan 02, 2023 at 04:34 PM +-- Server version: 10.4.27-MariaDB +-- PHP Version: 8.1.12 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; @@ -29,16 +29,14 @@ USE `pokemon`; -- Table structure for table `ability` -- -DROP TABLE IF EXISTS `ability`; -CREATE TABLE IF NOT EXISTS `ability` ( - `id` int(11) NOT NULL AUTO_INCREMENT, +CREATE TABLE `ability` ( + `id` int(11) NOT NULL, `monsterId` int(11) NOT NULL, `name` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `type` int(11) NOT NULL, - `power` float NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; + `power` float NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; -- -- Dumping data for table `ability` @@ -64,15 +62,25 @@ INSERT INTO `ability` (`id`, `monsterId`, `name`, `description`, `type`, `power` -- Table structure for table `history` -- -DROP TABLE IF EXISTS `history`; -CREATE TABLE IF NOT EXISTS `history` ( - `id` int(11) NOT NULL AUTO_INCREMENT, +CREATE TABLE `history` ( + `id` int(11) NOT NULL, `player` int(11) NOT NULL, `pokemon` int(11) NOT NULL, `time` date NOT NULL, - `result` int(11) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; + `result` int(11) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; + +-- +-- Dumping data for table `history` +-- + +INSERT INTO `history` (`id`, `player`, `pokemon`, `time`, `result`) VALUES +(1, 5, 2, '2023-01-02', 1), +(2, 2, 1, '2023-01-02', 0), +(3, 5, 2, '2023-01-02', 0), +(4, 2, 1, '2023-01-02', 1), +(5, 2, 1, '2023-01-02', 1), +(6, 4, 3, '2023-01-02', 0); -- -------------------------------------------------------- @@ -80,15 +88,13 @@ CREATE TABLE IF NOT EXISTS `history` ( -- Table structure for table `monster` -- -DROP TABLE IF EXISTS `monster`; -CREATE TABLE IF NOT EXISTS `monster` ( - `id` int(11) NOT NULL AUTO_INCREMENT, +CREATE TABLE `monster` ( + `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `hp` int(11) NOT NULL, - `base64Image` varchar(15000) NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; + `base64Image` varchar(15000) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; -- -- Dumping data for table `monster` @@ -105,15 +111,13 @@ INSERT INTO `monster` (`id`, `name`, `description`, `hp`, `base64Image`) VALUES -- Table structure for table `user` -- -DROP TABLE IF EXISTS `user`; -CREATE TABLE IF NOT EXISTS `user` ( - `id` int(11) NOT NULL AUTO_INCREMENT, +CREATE TABLE `user` ( + `id` int(11) NOT NULL, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `admin` tinyint(1) NOT NULL, - `pokemonId` int(11) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; + `pokemonId` int(11) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; -- -- Dumping data for table `user` @@ -122,9 +126,65 @@ CREATE TABLE IF NOT EXISTS `user` ( INSERT INTO `user` (`id`, `username`, `password`, `admin`, `pokemonId`) VALUES (3, 'cirakg', '$2a$10$tElFyUJsnb9xWuV3PRWOW.HPZ8qwLebQEpfu61eqozUcxC3ydEv7.', 1, NULL), (2, 'cirakg1', '$2a$10$TJHJfYbgZO97r0CJi2eKpeEhOW8wKEO5BGXQBpR33js3R5rM98Y3W', 0, 1), -(4, 'cirakg2', '$2a$10$LQsWtrn7OqrNU8wSSfg/6uo47lzQqw27T3ubUDRqFQVZhI6F0uBli', 0, NULL), +(4, 'cirakg2', '$2a$10$LQsWtrn7OqrNU8wSSfg/6uo47lzQqw27T3ubUDRqFQVZhI6F0uBli', 0, 3), (5, 'cirakg5', '$2a$10$cO0oAPi5O4hoVQWTHuw0fOXXBYLqwJ2V5qrHy32ZjpmR9fTpNBGHy', 0, 2), (6, 'cirakg10', '$2a$10$IiK5l7wFvi/KPliiCpNMCOAuehXvJFofN6ARotiSN.5FuRpjke32G', 0, NULL); + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `ability` +-- +ALTER TABLE `ability` + ADD PRIMARY KEY (`id`); + +-- +-- Indexes for table `history` +-- +ALTER TABLE `history` + ADD PRIMARY KEY (`id`); + +-- +-- Indexes for table `monster` +-- +ALTER TABLE `monster` + ADD PRIMARY KEY (`id`); + +-- +-- Indexes for table `user` +-- +ALTER TABLE `user` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `ability` +-- +ALTER TABLE `ability` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13; + +-- +-- AUTO_INCREMENT for table `history` +-- +ALTER TABLE `history` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; + +-- +-- AUTO_INCREMENT for table `monster` +-- +ALTER TABLE `monster` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; + +-- +-- AUTO_INCREMENT for table `user` +-- +ALTER TABLE `user` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; diff --git a/src/main/java/database/Database.java b/src/main/java/database/Database.java index 80d195c..f6e18b3 100644 --- a/src/main/java/database/Database.java +++ b/src/main/java/database/Database.java @@ -1,19 +1,23 @@ package database; import java.sql.Connection; +import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Calendar; import java.util.LinkedList; import java.util.List; import models.Ability; import models.AbilityType; +import models.GameStatus; import models.Monster; import models.MonsterViewModel; import models.User; +import pokemon.Game; public class Database { private Connection conn; @@ -327,5 +331,30 @@ public class Database { return user; } + public int addHistory(Game game) throws Exception{ + Date date=new Date(Calendar.getInstance().getTimeInMillis()); + String sql="INSERT INTO history(player,pokemon,time,result) values(?,?,?,?),(?,?,?,?)"; + PreparedStatement ps=conn.prepareStatement(sql); + if(game.getStatus()==GameStatus.PLAYER1WIN) { + ps.setLong(1, game.player1Id); + ps.setLong(2, game.monster1.id); + ps.setDate(3, date); + ps.setInt(4, 1); + ps.setLong(5, game.player2Id); + ps.setLong(6, game.monster2.id); + ps.setDate(7, date); + ps.setInt(8, 0); + }else if(game.getStatus()==GameStatus.PLAYER2WIN) { + ps.setLong(1, game.player1Id); + ps.setLong(2, game.monster1.id); + ps.setDate(3, date); + ps.setInt(4, 0); + ps.setLong(5, game.player2Id); + ps.setLong(6, game.monster2.id); + ps.setDate(7, date); + ps.setInt(8, 1); + } + return ps.executeUpdate(); + } } diff --git a/src/main/java/pokemon/IService.java b/src/main/java/pokemon/IService.java index 81b4019..de4b6e5 100644 --- a/src/main/java/pokemon/IService.java +++ b/src/main/java/pokemon/IService.java @@ -24,5 +24,6 @@ public interface IService extends Remote { public boolean addPokemonWithAbilities(PokemonAddModel pokemon)throws RemoteException; public void deleteMonster(long id)throws RemoteException; public User getUserById(long id) throws RemoteException; + public int addHistory(Game game) throws Exception; } diff --git a/src/main/java/pokemon/Server.java b/src/main/java/pokemon/Server.java index e08d7bc..77b7fad 100644 --- a/src/main/java/pokemon/Server.java +++ b/src/main/java/pokemon/Server.java @@ -327,7 +327,7 @@ public class Server implements Runnable { games.remove(game); System.out.println("GAME FINISHED"+games.size()); sendAvailablePlayers(); - //add game to database + s.addHistory(game); } } diff --git a/src/main/java/pokemon/Service.java b/src/main/java/pokemon/Service.java index 77a398b..f9f76de 100644 --- a/src/main/java/pokemon/Service.java +++ b/src/main/java/pokemon/Service.java @@ -112,6 +112,12 @@ public class Service extends UnicastRemoteObject implements IService { Database db=Database.getInstance(); return db.getUserById(id); } + + @Override + public int addHistory(Game game) throws Exception { + Database db=Database.getInstance(); + return db.addHistory(game); + } } -- cgit v1.2.3