chat commands: add pets command

Co-authored-by: Illya Myshakov <imyshako@uwaterloo.ca>
This commit is contained in:
Adam
2021-07-16 16:43:17 -04:00
committed by Adam
parent 3115142853
commit 21a7bf4906
9 changed files with 611 additions and 2 deletions

View File

@@ -247,4 +247,22 @@ public class ChatController
return layout;
}
@PostMapping("/pets")
public void submitPetList(@RequestParam String name, @RequestBody int[] petList)
{
chatService.setPetList(name, petList);
}
@GetMapping("/pets")
public int[] getPetList(@RequestParam String name)
{
int[] petList = chatService.getPetList(name);
if (petList == null)
{
throw new NotFoundException();
}
return petList;
}
}

View File

@@ -28,11 +28,13 @@ import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.runelite.http.api.chat.Duels;
import net.runelite.http.api.chat.LayoutRoom;
import net.runelite.http.api.chat.Task;
import net.runelite.http.api.chat.Duels;
import net.runelite.http.service.util.redis.RedisPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -229,4 +231,33 @@ public class ChatService
jedis.setex("layout." + name, (int) EXPIRE.getSeconds(), Joiner.on(' ').join(rooms));
}
}
public int[] getPetList(String name)
{
Set<String> pets;
try (Jedis jedis = jedisPool.getResource())
{
pets = jedis.smembers("pets." + name);
}
if (pets.isEmpty())
{
return null;
}
return pets.stream()
.mapToInt(Integer::parseInt)
.toArray();
}
public void setPetList(String name, int[] petList)
{
String[] pets = Arrays.stream(petList).mapToObj(Integer::toString).toArray(String[]::new);
String key = "pets." + name;
try (Jedis jedis = jedisPool.getResource())
{
jedis.sadd(key, pets);
jedis.expire(key, (int) EXPIRE.getSeconds());
}
}
}