[feat] chiperchat

This commit is contained in:
kwaroran
2023-10-30 08:52:14 +09:00
parent 111f9a847e
commit c0a619abad
3 changed files with 87 additions and 4 deletions

View File

@@ -0,0 +1,70 @@
import type { OpenAIChat } from ".";
let lastShift = 0
export function cipherChat(chat: OpenAIChat[]): OpenAIChat[] {
//Caesar Chiper
const caesarCipher = (text: string, shift: number) => {
return text
.split('')
.map(char => {
const code = char.charCodeAt(0)
if ((code >= 65) && (code <= 90)) {
return String.fromCharCode(((code - 65 + shift) % 26) + 65)
} else if ((code >= 97) && (code <= 122)) {
return String.fromCharCode(((code - 97 + shift) % 26) + 97)
} else {
return char
}
})
.join('')
}
const shift = Math.floor(Math.random() * 26) + 1
lastShift = shift
for(let i = 0; i < chat.length; i++){
chat[i].content = caesarCipher(chat[i].content, shift)
}
chat.unshift({
content: ` 'You are an expert on The Caesar Cipher. We will communicate in Caesar Cipher. Do not be a translator. We are using shift ${shift}`,
role: 'system'
})
return chat
}
export function decipherChat(chat: string): string {
//Caesar Chiper
const caesarCipher = (text: string, shift: number) => {
return text
.split('')
.map(char => {
const code = char.charCodeAt(0)
if ((code >= 65) && (code <= 90)) {
const shifted = (code - 65 - shift)
if(shifted < 0){
return String.fromCharCode(((code - 65 - shift + 26) % 26) + 65)
}
return String.fromCharCode(((code - 65 - shift) % 26) + 65)
} else if ((code >= 97) && (code <= 122)) {
const shifted = (code - 97 - shift)
if(shifted < 0){
return String.fromCharCode(((code - 97 - shift + 26) % 26) + 97)
}
return String.fromCharCode(((code - 97 - shift) % 26) + 97)
} else {
return char
}
})
.join('')
}
return caesarCipher(chat, lastShift)
}