Add task scheduler
This commit is contained in:
		
							
								
								
									
										3
									
								
								src/Action/Action.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/Action/Action.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| export default abstract class Action { | ||||
|     abstract async run(); | ||||
| } | ||||
							
								
								
									
										7
									
								
								src/Action/GoToMainAction.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/Action/GoToMainAction.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| import Action from './Action'; | ||||
|  | ||||
| export default class GoToMainAction extends Action { | ||||
|     async run(): Promise<any> { | ||||
|         return Promise.resolve(null); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										30
									
								
								src/ModeDetector.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/ModeDetector.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| import * as URLParse from 'url-parse'; | ||||
|  | ||||
| const SESSION_KEY = 'travian_automation_mode'; | ||||
| const SESSION_VALUE = 'command_execution'; | ||||
|  | ||||
| export default class ModeDetector { | ||||
|     isAuto(): boolean { | ||||
|         return this.isAutoByLocation() || this.isAutoBySession(); | ||||
|     } | ||||
|  | ||||
|     setAuto(): void { | ||||
|         sessionStorage.setItem(SESSION_KEY, SESSION_VALUE); | ||||
|     } | ||||
|  | ||||
|     private isAutoByLocation(): boolean { | ||||
|         const p = new URLParse(window.location.href, true); | ||||
|         console.log('PARSED LOCATION', p); | ||||
|         if (p.query['auto-management'] !== undefined) { | ||||
|             console.log('AUTO MANAGEMENT ON'); | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     private isAutoBySession(): boolean { | ||||
|         const k = sessionStorage.getItem(SESSION_KEY); | ||||
|         return k === SESSION_VALUE; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										47
									
								
								src/Queue.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/Queue.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| export class QueueItem { | ||||
|     readonly name: string; | ||||
|  | ||||
|     readonly args; | ||||
|  | ||||
|     constructor(name: string, args: { [name: string]: any }) { | ||||
|         this.name = name; | ||||
|         this.args = args; | ||||
|     } | ||||
| } | ||||
|  | ||||
| export class Queue { | ||||
|     private readonly name; | ||||
|  | ||||
|     constructor(name: string) { | ||||
|         this.name = name; | ||||
|     } | ||||
|  | ||||
|     pop() { | ||||
|         const serialized = localStorage.getItem(this.name); | ||||
|         if (serialized === null) { | ||||
|             return null; | ||||
|         } | ||||
|         const items = JSON.parse(serialized) as Array<QueueItem>; | ||||
|         if (items.length === 0) { | ||||
|             return null; | ||||
|         } | ||||
|         const first = items.shift(); | ||||
|  | ||||
|         localStorage.setItem(this.name, JSON.stringify(items)); | ||||
|  | ||||
|         if (first === undefined) { | ||||
|             return null; | ||||
|         } | ||||
|  | ||||
|         return new QueueItem(first.name || '', first.args || {}); | ||||
|     } | ||||
|  | ||||
|     push(item: QueueItem): void { | ||||
|         const serialized = localStorage.getItem(this.name); | ||||
|         const items = serialized | ||||
|             ? (JSON.parse(serialized) as Array<QueueItem>) | ||||
|             : []; | ||||
|         const first = items.push(item); | ||||
|         localStorage.setItem(this.name, JSON.stringify(items)); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										54
									
								
								src/Scheduler.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/Scheduler.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| import { sleep } from './utils'; | ||||
| import { Queue } from './Queue'; | ||||
| import GoToMainAction from './Action/GoToMainAction'; | ||||
|  | ||||
| const ACTION_QUEUE = 'action_queue'; | ||||
| const TASK_QUEUE = 'task_queue'; | ||||
|  | ||||
| export default class Scheduler { | ||||
|     taskQueue: Queue; | ||||
|     actionQueue: Queue; | ||||
|  | ||||
|     constructor() { | ||||
|         this.taskQueue = new Queue(TASK_QUEUE); | ||||
|         this.actionQueue = new Queue(ACTION_QUEUE); | ||||
|     } | ||||
|  | ||||
|     async run() { | ||||
|         while (true) { | ||||
|             const action = this.popAction(); | ||||
|             console.log('POP ACTION', action); | ||||
|             if (action !== null) { | ||||
|                 await action.run(); | ||||
|             } else { | ||||
|                 const task = this.popTask(); | ||||
|                 console.log('POP TASK', task); | ||||
|                 if (task !== null) { | ||||
|                     // do task | ||||
|                 } | ||||
|             } | ||||
|             const waitTime = Math.random() * 5000; | ||||
|             console.log('WAIT', waitTime); | ||||
|             await sleep(waitTime); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private popTask() { | ||||
|         const item = this.taskQueue.pop(); | ||||
|         if (item === null) { | ||||
|             return null; | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     private popAction() { | ||||
|         const item = this.actionQueue.pop(); | ||||
|         if (item === null) { | ||||
|             return null; | ||||
|         } | ||||
|         if (item.name === 'go_to_main') { | ||||
|             return new GoToMainAction(); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import ModeDetector from './ModeDetector'; | ||||
| import Scheduler from './Scheduler'; | ||||
|  | ||||
| const md = new ModeDetector(); | ||||
| if (md.isAuto()) { | ||||
|     md.setAuto(); | ||||
|     console.log('AUTO MANAGEMENT ON'); | ||||
|     const scheduler = new Scheduler(); | ||||
|     scheduler.run(); | ||||
| } else { | ||||
|     console.log('NORMAL MODE'); | ||||
| } | ||||
							
								
								
									
										5
									
								
								src/utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/utils.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| function sleep(ms: number) { | ||||
|     return new Promise(resolve => setTimeout(resolve, ms)); | ||||
| } | ||||
|  | ||||
| export { sleep }; | ||||
		Reference in New Issue
	
	Block a user